Browse Source
Conflicts: .travis.yml build/controllers/PhpDocController.php docs/guide-es/caching-data.md docs/guide-es/concept-events.md docs/guide-fr/concept-events.md docs/guide-ja/caching-data.md docs/guide-ja/concept-events.md docs/guide-pt-BR/caching-data.md docs/guide-pt-BR/concept-events.md docs/guide-ru/caching-data.md docs/guide-ru/concept-events.md docs/guide-zh-CN/caching-data.md docs/guide-zh-CN/concept-events.md docs/guide/caching-data.md docs/guide/concept-events.md docs/guide/runtime-logging.md framework/BaseYii.php framework/base/BaseObject.php framework/base/Security.php framework/caching/DbCache.php framework/caching/XCache.php framework/classes.php framework/db/ActiveQueryTrait.php framework/db/ActiveRelationTrait.php framework/db/BaseActiveRecord.php framework/db/Query.php framework/db/QueryBuilder.php framework/db/TableSchema.php framework/db/mssql/QueryBuilder.php framework/db/mysql/QueryBuilder.php framework/db/oci/QueryBuilder.php framework/db/pgsql/QueryBuilder.php framework/db/sqlite/QueryBuilder.php framework/filters/PageCache.php framework/grid/DataColumn.php framework/helpers/BaseFileHelper.php framework/helpers/BaseJson.php framework/helpers/BaseMarkdown.php framework/helpers/BaseUrl.php framework/i18n/Formatter.php framework/log/DbTarget.php framework/log/SyslogTarget.php framework/messages/sk/yii.php framework/mutex/PgsqlMutex.php framework/rbac/BaseManager.php framework/rbac/DbManager.php framework/rbac/PhpManager.php framework/web/Request.php framework/web/Response.php framework/web/Session.php framework/web/View.php framework/widgets/ActiveForm.php framework/widgets/FragmentCache.php tests/framework/behaviors/TimestampBehaviorTest.php tests/framework/db/CommandTest.php tests/framework/db/mysql/connection/DeadLockTest.php tests/framework/db/pgsql/CommandTest.php tests/framework/filters/auth/BasicAuthTest.php tests/framework/log/FileTargetTest.php tests/framework/log/SyslogTargetTest.php tests/framework/mail/BaseMailerTest.php tests/framework/rbac/ManagerTestCase.phptags/3.0.0-alpha1
Klimov Paul
7 years ago
262 changed files with 9882 additions and 1760 deletions
@ -0,0 +1,40 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\base; |
||||
|
||||
/** |
||||
* DynamicContentAwareInterface is the interface that should be implemented by classes |
||||
* which support a [[View]] dynamic content feature. |
||||
* |
||||
* @author Sergey Makinen <sergey@makinen.ru> |
||||
* @since 2.0.14 |
||||
*/ |
||||
interface DynamicContentAwareInterface |
||||
{ |
||||
/** |
||||
* Returns a list of placeholders for dynamic content. This method |
||||
* is used internally to implement the content caching feature. |
||||
* @return array a list of placeholders. |
||||
*/ |
||||
public function getDynamicPlaceholders(); |
||||
|
||||
/** |
||||
* Sets a list of placeholders for dynamic content. This method |
||||
* is used internally to implement the content caching feature. |
||||
* @param array $placeholders a list of placeholders. |
||||
*/ |
||||
public function setDynamicPlaceholders($placeholders); |
||||
|
||||
/** |
||||
* Adds a placeholder for dynamic content. |
||||
* This method is used internally to implement the content caching feature. |
||||
* @param string $name the placeholder name. |
||||
* @param string $statements the PHP statements for generating the dynamic content. |
||||
*/ |
||||
public function addDynamicPlaceholder($name, $statements); |
||||
} |
@ -0,0 +1,82 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\base; |
||||
|
||||
/** |
||||
* DynamicContentAwareTrait implements common methods for classes |
||||
* which support a [[View]] dynamic content feature. |
||||
* |
||||
* @author Sergey Makinen <sergey@makinen.ru> |
||||
* @since 2.0.14 |
||||
*/ |
||||
trait DynamicContentAwareTrait |
||||
{ |
||||
/** |
||||
* @var string[] a list of placeholders for dynamic content |
||||
*/ |
||||
private $_dynamicPlaceholders; |
||||
|
||||
/** |
||||
* Returns the view object that can be used to render views or view files using dynamic contents. |
||||
* @return View the view object that can be used to render views or view files. |
||||
*/ |
||||
abstract protected function getView(); |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getDynamicPlaceholders() |
||||
{ |
||||
return $this->_dynamicPlaceholders; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function setDynamicPlaceholders($placeholders) |
||||
{ |
||||
$this->_dynamicPlaceholders = $placeholders; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function addDynamicPlaceholder($name, $statements) |
||||
{ |
||||
$this->_dynamicPlaceholders[$name] = $statements; |
||||
} |
||||
|
||||
/** |
||||
* Replaces placeholders in $content with results of evaluated dynamic statements. |
||||
* @param string $content content to be parsed. |
||||
* @param string[] $placeholders placeholders and their values. |
||||
* @param bool $isRestoredFromCache whether content is going to be restored from cache. |
||||
* @return string final content. |
||||
*/ |
||||
protected function updateDynamicContent($content, $placeholders, $isRestoredFromCache = false) |
||||
{ |
||||
if (empty($placeholders) || !is_array($placeholders)) { |
||||
return $content; |
||||
} |
||||
|
||||
if (count($this->getView()->getDynamicContents()) === 0) { |
||||
// outermost cache: replace placeholder with dynamic content |
||||
foreach ($placeholders as $name => $statements) { |
||||
$placeholders[$name] = $this->getView()->evaluateDynamicContent($statements); |
||||
} |
||||
$content = strtr($content, $placeholders); |
||||
} |
||||
if ($isRestoredFromCache) { |
||||
foreach ($placeholders as $name => $statements) { |
||||
$this->getView()->addDynamicPlaceholder($name, $statements); |
||||
} |
||||
} |
||||
|
||||
return $content; |
||||
} |
||||
} |
@ -0,0 +1,199 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\behaviors; |
||||
|
||||
use yii\base\Behavior; |
||||
use yii\base\InvalidConfigException; |
||||
use yii\base\Widget; |
||||
use yii\base\WidgetEvent; |
||||
use yii\caching\CacheInterface; |
||||
use yii\caching\Dependency; |
||||
use yii\di\Instance; |
||||
|
||||
/** |
||||
* Cacheable widget behavior automatically caches widget contents according to duration and dependencies specified. |
||||
* |
||||
* The behavior may be used without any configuration if an application has `cache` component configured. |
||||
* By default the widget will be cached for one minute. |
||||
* |
||||
* The following example will cache the posts widget for an indefinite duration until any post is modified. |
||||
* |
||||
* ```php |
||||
* use yii\behaviors\CacheableWidgetBehavior; |
||||
* |
||||
* public function behaviors() |
||||
* { |
||||
* return [ |
||||
* [ |
||||
* 'class' => CacheableWidgetBehavior::className(), |
||||
* 'cacheDuration' => 0, |
||||
* 'cacheDependency' => [ |
||||
* 'class' => 'yii\caching\DbDependency', |
||||
* 'sql' => 'SELECT MAX(updated_at) FROM posts', |
||||
* ], |
||||
* ], |
||||
* ]; |
||||
* } |
||||
* ``` |
||||
* |
||||
* @property Widget $owner |
||||
* |
||||
* @author Nikolay Oleynikov <oleynikovny@mail.ru> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class CacheableWidgetBehavior extends Behavior |
||||
{ |
||||
/** |
||||
* @var CacheInterface|string|array a cache object or a cache component ID |
||||
* or a configuration array for creating a cache object. |
||||
* Defaults to the `cache` application component. |
||||
*/ |
||||
public $cache = 'cache'; |
||||
/** |
||||
* @var int cache duration in seconds. |
||||
* Set to `0` to indicate that the cached data will never expire. |
||||
* Defaults to 60 seconds or 1 minute. |
||||
*/ |
||||
public $cacheDuration = 60; |
||||
/** |
||||
* @var Dependency|array|null a cache dependency or a configuration array |
||||
* for creating a cache dependency or `null` meaning no cache dependency. |
||||
* |
||||
* For example, |
||||
* |
||||
* ```php |
||||
* [ |
||||
* 'class' => 'yii\caching\DbDependency', |
||||
* 'sql' => 'SELECT MAX(updated_at) FROM posts', |
||||
* ] |
||||
* ``` |
||||
* |
||||
* would make the widget cache depend on the last modified time of all posts. |
||||
* If any post has its modification time changed, the cached content would be invalidated. |
||||
*/ |
||||
public $cacheDependency; |
||||
/** |
||||
* @var string[]|string an array of strings or a single string which would cause |
||||
* the variation of the content being cached (e.g. an application language, a GET parameter). |
||||
* |
||||
* The following variation setting will cause the content to be cached in different versions |
||||
* according to the current application language: |
||||
* |
||||
* ```php |
||||
* [ |
||||
* Yii::$app->language, |
||||
* ] |
||||
* ``` |
||||
*/ |
||||
public $cacheKeyVariations = []; |
||||
/** |
||||
* @var bool whether to enable caching or not. Allows to turn the widget caching |
||||
* on and off according to specific conditions. |
||||
* The following configuration will disable caching when a special GET parameter is passed: |
||||
* |
||||
* ```php |
||||
* empty(Yii::$app->request->get('disable-caching')) |
||||
* ``` |
||||
*/ |
||||
public $cacheEnabled = true; |
||||
|
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function attach($owner) |
||||
{ |
||||
parent::attach($owner); |
||||
|
||||
$this->initializeEventHandlers(); |
||||
} |
||||
|
||||
/** |
||||
* Begins fragment caching. Prevents owner widget from execution |
||||
* if its contents can be retrieved from the cache. |
||||
* |
||||
* @param WidgetEvent $event `Widget::EVENT_BEFORE_RUN` event. |
||||
*/ |
||||
public function beforeRun($event) |
||||
{ |
||||
$cacheKey = $this->getCacheKey(); |
||||
$fragmentCacheConfiguration = $this->getFragmentCacheConfiguration(); |
||||
|
||||
if (!$this->owner->view->beginCache($cacheKey, $fragmentCacheConfiguration)) { |
||||
$event->isValid = false; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Outputs widget contents and ends fragment caching. |
||||
* |
||||
* @param WidgetEvent $event `Widget::EVENT_AFTER_RUN` event. |
||||
*/ |
||||
public function afterRun($event) |
||||
{ |
||||
echo $event->result; |
||||
$event->result = null; |
||||
|
||||
$this->owner->view->endCache(); |
||||
} |
||||
|
||||
/** |
||||
* Initializes widget event handlers. |
||||
*/ |
||||
private function initializeEventHandlers() |
||||
{ |
||||
$this->owner->on(Widget::EVENT_BEFORE_RUN, [$this, 'beforeRun']); |
||||
$this->owner->on(Widget::EVENT_AFTER_RUN, [$this, 'afterRun']); |
||||
} |
||||
|
||||
/** |
||||
* Returns the cache instance. |
||||
* |
||||
* @return CacheInterface cache instance. |
||||
* @throws InvalidConfigException if cache instance instantiation fails. |
||||
*/ |
||||
private function getCacheInstance() |
||||
{ |
||||
$cacheInterface = 'yii\caching\CacheInterface'; |
||||
return Instance::ensure($this->cache, $cacheInterface); |
||||
} |
||||
|
||||
/** |
||||
* Returns the widget cache key. |
||||
* |
||||
* @return string[] an array of strings representing the cache key. |
||||
*/ |
||||
private function getCacheKey() |
||||
{ |
||||
// `$cacheKeyVariations` may be a `string` and needs to be cast to an `array`. |
||||
$cacheKey = array_merge( |
||||
(array)get_class($this->owner), |
||||
(array)$this->cacheKeyVariations |
||||
); |
||||
|
||||
return $cacheKey; |
||||
} |
||||
|
||||
/** |
||||
* Returns a fragment cache widget configuration array. |
||||
* |
||||
* @return array a fragment cache widget configuration array. |
||||
*/ |
||||
private function getFragmentCacheConfiguration() |
||||
{ |
||||
$cache = $this->getCacheInstance(); |
||||
$fragmentCacheConfiguration = [ |
||||
'cache' => $cache, |
||||
'duration' => $this->cacheDuration, |
||||
'dependency' => $this->cacheDependency, |
||||
'enabled' => $this->cacheEnabled, |
||||
]; |
||||
|
||||
return $fragmentCacheConfiguration; |
||||
} |
||||
} |
@ -0,0 +1,170 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Class ArrayExpression represents an array SQL expression. |
||||
* |
||||
* Expressions of this type can be used in conditions as well: |
||||
* |
||||
* ```php |
||||
* $query->andWhere(['@>', 'items', new ArrayExpression([1, 2, 3], 'integer')]) |
||||
* ``` |
||||
* |
||||
* which, depending on DBMS, will result in a well-prepared condition. For example, in |
||||
* PostgreSQL it will be compiled to `WHERE "items" @> ARRAY[1, 2, 3]::integer[]`. |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable |
||||
{ |
||||
/** |
||||
* @var null|string the type of the array elements. Defaults to `null` which means the type is |
||||
* not explicitly specified. |
||||
* |
||||
* Note that in case when type is not specified explicitly and DBMS can not guess it from the context, |
||||
* SQL error will be raised. |
||||
*/ |
||||
private $type; |
||||
/** |
||||
* @var array|QueryInterface|mixed the array content. Either represented as an array of values or a [[Query]] that |
||||
* returns these values. A single value will be considered as an array containing one element. |
||||
*/ |
||||
private $value; |
||||
/** |
||||
* @var int the number of indices needed to select an element |
||||
*/ |
||||
private $dimension; |
||||
|
||||
|
||||
/** |
||||
* ArrayExpression constructor. |
||||
* |
||||
* @param array|QueryInterface|mixed $value the array content. Either represented as an array of values or a Query that |
||||
* returns these values. A single value will be considered as an array containing one element. |
||||
* @param string|null $type the type of the array elements. Defaults to `null` which means the type is |
||||
* not explicitly specified. In case when type is not specified explicitly and DBMS can not guess it from the context, |
||||
* SQL error will be raised. |
||||
* @param int $dimension the number of indices needed to select an element |
||||
*/ |
||||
public function __construct($value, $type = null, $dimension = 1) |
||||
{ |
||||
$this->value = $value; |
||||
$this->type = $type; |
||||
$this->dimension = $dimension; |
||||
} |
||||
|
||||
/** |
||||
* @return null|string |
||||
* @see type |
||||
*/ |
||||
public function getType() |
||||
{ |
||||
return $this->type; |
||||
} |
||||
|
||||
/** |
||||
* @return array|mixed|QueryInterface |
||||
* @see value |
||||
*/ |
||||
public function getValue() |
||||
{ |
||||
return $this->value; |
||||
} |
||||
|
||||
/** |
||||
* @return int the number of indices needed to select an element |
||||
* @see dimensions |
||||
*/ |
||||
public function getDimension() |
||||
{ |
||||
return $this->dimension; |
||||
} |
||||
|
||||
/** |
||||
* Whether a offset exists |
||||
* |
||||
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||||
* @param mixed $offset <p> |
||||
* An offset to check for. |
||||
* </p> |
||||
* @return bool true on success or false on failure. |
||||
* </p> |
||||
* <p> |
||||
* The return value will be casted to boolean if non-boolean was returned. |
||||
* @since 5.0.0 |
||||
*/ |
||||
public function offsetExists($offset) |
||||
{ |
||||
return isset($this->value[$offset]); |
||||
} |
||||
|
||||
/** |
||||
* Offset to retrieve |
||||
* |
||||
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
||||
* @param mixed $offset <p> |
||||
* The offset to retrieve. |
||||
* </p> |
||||
* @return mixed Can return all value types. |
||||
* @since 5.0.0 |
||||
*/ |
||||
public function offsetGet($offset) |
||||
{ |
||||
return $this->value[$offset]; |
||||
} |
||||
|
||||
/** |
||||
* Offset to set |
||||
* |
||||
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
||||
* @param mixed $offset <p> |
||||
* The offset to assign the value to. |
||||
* </p> |
||||
* @param mixed $value <p> |
||||
* The value to set. |
||||
* </p> |
||||
* @return void |
||||
* @since 5.0.0 |
||||
*/ |
||||
public function offsetSet($offset, $value) |
||||
{ |
||||
$this->value[$offset] = $value; |
||||
} |
||||
|
||||
/** |
||||
* Offset to unset |
||||
* |
||||
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||||
* @param mixed $offset <p> |
||||
* The offset to unset. |
||||
* </p> |
||||
* @return void |
||||
* @since 5.0.0 |
||||
*/ |
||||
public function offsetUnset($offset) |
||||
{ |
||||
unset($this->value[$offset]); |
||||
} |
||||
|
||||
/** |
||||
* Count elements of an object |
||||
* |
||||
* @link http://php.net/manual/en/countable.count.php |
||||
* @return int The custom count as an integer. |
||||
* </p> |
||||
* <p> |
||||
* The return value is cast to an integer. |
||||
* @since 5.1.0 |
||||
*/ |
||||
public function count() |
||||
{ |
||||
return count($this->value); |
||||
} |
||||
} |
@ -0,0 +1,125 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* ConstraintFinderInterface defines methods for getting a table constraint information. |
||||
* |
||||
* @author Sergey Makinen <sergey@makinen.ru> |
||||
* @since 2.0.14 |
||||
*/ |
||||
interface ConstraintFinderInterface |
||||
{ |
||||
/** |
||||
* Obtains the primary key for the named table. |
||||
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||||
* @param bool $refresh whether to reload the information even if it is found in the cache. |
||||
* @return Constraint|null table primary key, `null` if the table has no primary key. |
||||
*/ |
||||
public function getTablePrimaryKey($name, $refresh = false); |
||||
|
||||
/** |
||||
* Returns primary keys for all tables in the database. |
||||
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||||
* @param bool $refresh whether to fetch the latest available table schemas. If this is `false`, |
||||
* cached data may be returned if available. |
||||
* @return Constraint[] primary keys for all tables in the database. |
||||
* Each array element is an instance of [[Constraint]] or its child class. |
||||
*/ |
||||
public function getSchemaPrimaryKeys($schema = '', $refresh = false); |
||||
|
||||
/** |
||||
* Obtains the foreign keys information for the named table. |
||||
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||||
* @param bool $refresh whether to reload the information even if it is found in the cache. |
||||
* @return ForeignKeyConstraint[] table foreign keys. |
||||
*/ |
||||
public function getTableForeignKeys($name, $refresh = false); |
||||
|
||||
/** |
||||
* Returns foreign keys for all tables in the database. |
||||
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||||
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
||||
* cached data may be returned if available. |
||||
* @return ForeignKeyConstraint[][] foreign keys for all tables in the database. |
||||
* Each array element is an array of [[ForeignKeyConstraint]] or its child classes. |
||||
*/ |
||||
public function getSchemaForeignKeys($schema = '', $refresh = false); |
||||
|
||||
/** |
||||
* Obtains the indexes information for the named table. |
||||
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||||
* @param bool $refresh whether to reload the information even if it is found in the cache. |
||||
* @return IndexConstraint[] table indexes. |
||||
*/ |
||||
public function getTableIndexes($name, $refresh = false); |
||||
|
||||
/** |
||||
* Returns indexes for all tables in the database. |
||||
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||||
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
||||
* cached data may be returned if available. |
||||
* @return IndexConstraint[][] indexes for all tables in the database. |
||||
* Each array element is an array of [[IndexConstraint]] or its child classes. |
||||
*/ |
||||
public function getSchemaIndexes($schema = '', $refresh = false); |
||||
|
||||
/** |
||||
* Obtains the unique constraints information for the named table. |
||||
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||||
* @param bool $refresh whether to reload the information even if it is found in the cache. |
||||
* @return Constraint[] table unique constraints. |
||||
*/ |
||||
public function getTableUniques($name, $refresh = false); |
||||
|
||||
/** |
||||
* Returns unique constraints for all tables in the database. |
||||
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||||
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
||||
* cached data may be returned if available. |
||||
* @return Constraint[][] unique constraints for all tables in the database. |
||||
* Each array element is an array of [[Constraint]] or its child classes. |
||||
*/ |
||||
public function getSchemaUniques($schema = '', $refresh = false); |
||||
|
||||
/** |
||||
* Obtains the check constraints information for the named table. |
||||
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||||
* @param bool $refresh whether to reload the information even if it is found in the cache. |
||||
* @return CheckConstraint[] table check constraints. |
||||
*/ |
||||
public function getTableChecks($name, $refresh = false); |
||||
|
||||
/** |
||||
* Returns check constraints for all tables in the database. |
||||
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||||
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
||||
* cached data may be returned if available. |
||||
* @return CheckConstraint[][] check constraints for all tables in the database. |
||||
* Each array element is an array of [[CheckConstraint]] or its child classes. |
||||
*/ |
||||
public function getSchemaChecks($schema = '', $refresh = false); |
||||
|
||||
/** |
||||
* Obtains the default value constraints information for the named table. |
||||
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
||||
* @param bool $refresh whether to reload the information even if it is found in the cache. |
||||
* @return DefaultValueConstraint[] table default value constraints. |
||||
*/ |
||||
public function getTableDefaultValues($name, $refresh = false); |
||||
|
||||
/** |
||||
* Returns default value constraints for all tables in the database. |
||||
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
||||
* @param bool $refresh whether to fetch the latest available table schemas. If this is false, |
||||
* cached data may be returned if available. |
||||
* @return DefaultValueConstraint[] default value constraints for all tables in the database. |
||||
* Each array element is an array of [[DefaultValueConstraint]] or its child classes. |
||||
*/ |
||||
public function getSchemaDefaultValues($schema = '', $refresh = false); |
||||
} |
@ -0,0 +1,30 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Class ExpressionBuilder builds objects of [[yii\db\Expression]] class. |
||||
* |
||||
* @author Dmitry Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class ExpressionBuilder implements ExpressionBuilderInterface |
||||
{ |
||||
use ExpressionBuilderTrait; |
||||
|
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
* @param Expression|ExpressionInterface $expression the expression to be built |
||||
*/ |
||||
public function build(ExpressionInterface $expression, array &$params = []) |
||||
{ |
||||
$params = array_merge($params, $expression->params); |
||||
return $expression->__toString(); |
||||
} |
||||
} |
@ -0,0 +1,28 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Interface ExpressionBuilderInterface is designed to build raw SQL from specific expression |
||||
* objects that implement [[ExpressionInterface]]. |
||||
* |
||||
* @author Dmitry Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
interface ExpressionBuilderInterface |
||||
{ |
||||
/** |
||||
* Method builds the raw SQL from the $expression that will not be additionally |
||||
* escaped or quoted. |
||||
* |
||||
* @param ExpressionInterface $expression the expression to be built. |
||||
* @param array $params the binding parameters. |
||||
* @return string the raw SQL that will not be additionally escaped or quoted. |
||||
*/ |
||||
public function build(ExpressionInterface $expression, array &$params = []); |
||||
} |
@ -0,0 +1,33 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Trait ExpressionBuilderTrait provides common constructor for classes that |
||||
* should implement [[ExpressionBuilderInterface]] |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
trait ExpressionBuilderTrait |
||||
{ |
||||
/** |
||||
* @var QueryBuilder |
||||
*/ |
||||
protected $queryBuilder; |
||||
|
||||
/** |
||||
* ExpressionBuilderTrait constructor. |
||||
* |
||||
* @param QueryBuilder $queryBuilder |
||||
*/ |
||||
public function __construct(QueryBuilder $queryBuilder) |
||||
{ |
||||
$this->queryBuilder = $queryBuilder; |
||||
} |
||||
} |
@ -0,0 +1,24 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Interface ExpressionInterface should be used to mark classes, that should be built |
||||
* in a special way. |
||||
* |
||||
* The database abstraction layer of Yii framework supports objects that implement this |
||||
* interface and will use [[ExpressionBuilderInterface]] to build them. |
||||
* |
||||
* The default implementation is a class [[Expression]]. |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
interface ExpressionInterface |
||||
{ |
||||
} |
@ -0,0 +1,73 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Class JsonExpression represents data that should be encoded to JSON. |
||||
* |
||||
* For example: |
||||
* |
||||
* ```php |
||||
* new JsonExpression(['a' => 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}' |
||||
* ``` |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class JsonExpression implements ExpressionInterface |
||||
{ |
||||
const TYPE_JSON = 'json'; |
||||
const TYPE_JSONB = 'jsonb'; |
||||
|
||||
/** |
||||
* @var mixed the value to be encoded to JSON. |
||||
* The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] input requirements. |
||||
*/ |
||||
protected $value; |
||||
/** |
||||
* @var string|null Type of JSON, expression should be casted to. Defaults to `null`, meaning |
||||
* no explicit casting will be performed. |
||||
* This property will be encountered only for DBMSs that support different types of JSON. |
||||
* For example, PostgreSQL has `json` and `jsonb` types. |
||||
*/ |
||||
protected $type; |
||||
|
||||
|
||||
/** |
||||
* JsonExpression constructor. |
||||
* |
||||
* @param mixed $value the value to be encoded to JSON. |
||||
* The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] requirements. |
||||
* @param string|null $type the type of the JSON. See [[JsonExpression::type]] |
||||
* |
||||
* @see type |
||||
*/ |
||||
public function __construct($value, $type = null) |
||||
{ |
||||
$this->value = $value; |
||||
$this->type = $type; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
* @see value |
||||
*/ |
||||
public function getValue() |
||||
{ |
||||
return $this->value; |
||||
} |
||||
|
||||
/** |
||||
* @return null|string the type of JSON |
||||
* @see type |
||||
*/ |
||||
public function getType() |
||||
{ |
||||
return $this->type; |
||||
} |
||||
} |
@ -0,0 +1,65 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Class PdoValue represents a $value that should be bound to PDO with exact $type. |
||||
* |
||||
* For example, it will be useful when you need to bind binary data to BLOB column in DBMS: |
||||
* |
||||
* ```php |
||||
* [':name' => 'John', ':profile' => new PdoValue($profile, \PDO::PARAM_LOB)]`. |
||||
* ``` |
||||
* |
||||
* To see possible types, check [PDO::PARAM_* constants](http://php.net/manual/en/pdo.constants.php). |
||||
* |
||||
* @see http://php.net/manual/en/pdostatement.bindparam.php |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
final class PdoValue implements ExpressionInterface |
||||
{ |
||||
/** |
||||
* @var mixed |
||||
*/ |
||||
private $value; |
||||
/** |
||||
* @var int One of PDO_PARAM_* constants |
||||
* @see http://php.net/manual/en/pdo.constants.php |
||||
*/ |
||||
private $type; |
||||
|
||||
|
||||
/** |
||||
* PdoValue constructor. |
||||
* |
||||
* @param $value |
||||
* @param $type |
||||
*/ |
||||
public function __construct($value, $type) |
||||
{ |
||||
$this->value = $value; |
||||
$this->type = $type; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getValue() |
||||
{ |
||||
return $this->value; |
||||
} |
||||
|
||||
/** |
||||
* @return int |
||||
*/ |
||||
public function getType() |
||||
{ |
||||
return $this->type; |
||||
} |
||||
} |
@ -0,0 +1,31 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Class PdoValueBuilder builds object of the [[PdoValue]] expression class. |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class PdoValueBuilder implements ExpressionBuilderInterface |
||||
{ |
||||
const PARAM_PREFIX = ':pv'; |
||||
|
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function build(ExpressionInterface $expression, array &$params = []) |
||||
{ |
||||
$placeholder = static::PARAM_PREFIX . count($params); |
||||
$params[$placeholder] = $expression; |
||||
|
||||
return $placeholder; |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* Class QueryExpressionBuilder is used internally to build [[Query]] object |
||||
* using unified [[QueryBuilder]] expression building interface. |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class QueryExpressionBuilder implements ExpressionBuilderInterface |
||||
{ |
||||
use ExpressionBuilderTrait; |
||||
|
||||
|
||||
/** |
||||
* Method builds the raw SQL from the $expression that will not be additionally |
||||
* escaped or quoted. |
||||
* |
||||
* @param ExpressionInterface|Query $expression the expression to be built. |
||||
* @param array $params the binding parameters. |
||||
* @return string the raw SQL that will not be additionally escaped or quoted. |
||||
*/ |
||||
public function build(ExpressionInterface $expression, array &$params = []) |
||||
{ |
||||
list($sql, $params) = $this->queryBuilder->build($expression, $params); |
||||
|
||||
return "($sql)"; |
||||
} |
||||
} |
@ -0,0 +1,27 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db\conditions; |
||||
|
||||
/** |
||||
* Condition that connects two or more SQL expressions with the `AND` operator. |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class AndCondition extends ConjunctionCondition |
||||
{ |
||||
/** |
||||
* Returns the operator that is represented by this condition class, e.g. `AND`, `OR`. |
||||
* |
||||
* @return string |
||||
*/ |
||||
public function getOperator() |
||||
{ |
||||
return 'AND'; |
||||
} |
||||
} |
@ -0,0 +1,121 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db\conditions; |
||||
|
||||
use yii\base\InvalidArgumentException; |
||||
use yii\db\ExpressionInterface; |
||||
use yii\db\Query; |
||||
|
||||
/** |
||||
* Class BetweenColumnCondition represents a `BETWEEN` condition where |
||||
* values is between two columns. For example: |
||||
* |
||||
* ```php |
||||
* new BetweenColumnsCondition(42, 'BETWEEN', 'min_value', 'max_value') |
||||
* // Will be build to: |
||||
* // 42 BETWEEN min_value AND max_value |
||||
* ``` |
||||
* |
||||
* And a more complex example: |
||||
* |
||||
* ```php |
||||
* new BetweenColumnsCondition( |
||||
* new Expression('NOW()'), |
||||
* 'NOT BETWEEN', |
||||
* (new Query)->select('time')->from('log')->orderBy('id ASC')->limit(1), |
||||
* 'update_time' |
||||
* ); |
||||
* |
||||
* // Will be built to: |
||||
* // NOW() BETWEEN (SELECT time FROM log ORDER BY id ASC LIMIT 1) AND update_time |
||||
* ``` |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class BetweenColumnsCondition implements ConditionInterface |
||||
{ |
||||
/** |
||||
* @var string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||||
*/ |
||||
private $operator; |
||||
/** |
||||
* @var mixed the value to compare against |
||||
*/ |
||||
private $value; |
||||
/** |
||||
* @var string|ExpressionInterface|Query the column name or expression that is a beginning of the interval |
||||
*/ |
||||
private $intervalStartColumn; |
||||
/** |
||||
* @var string|ExpressionInterface|Query the column name or expression that is an end of the interval |
||||
*/ |
||||
private $intervalEndColumn; |
||||
|
||||
|
||||
/** |
||||
* Creates a condition with the `BETWEEN` operator. |
||||
* |
||||
* @param mixed the value to compare against |
||||
* @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||||
* @param string|ExpressionInterface $intervalStartColumn the column name or expression that is a beginning of the interval |
||||
* @param string|ExpressionInterface $intervalEndColumn the column name or expression that is an end of the interval |
||||
*/ |
||||
public function __construct($value, $operator, $intervalStartColumn, $intervalEndColumn) |
||||
{ |
||||
$this->value = $value; |
||||
$this->operator = $operator; |
||||
$this->intervalStartColumn = $intervalStartColumn; |
||||
$this->intervalEndColumn = $intervalEndColumn; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getOperator() |
||||
{ |
||||
return $this->operator; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getValue() |
||||
{ |
||||
return $this->value; |
||||
} |
||||
|
||||
/** |
||||
* @return string|ExpressionInterface|Query |
||||
*/ |
||||
public function getIntervalStartColumn() |
||||
{ |
||||
return $this->intervalStartColumn; |
||||
} |
||||
|
||||
/** |
||||
* @return string|ExpressionInterface|Query |
||||
*/ |
||||
public function getIntervalEndColumn() |
||||
{ |
||||
return $this->intervalEndColumn; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
* @throws InvalidArgumentException if wrong number of operands have been given. |
||||
*/ |
||||
public static function fromArrayDefinition($operator, $operands) |
||||
{ |
||||
if (!isset($operands[0], $operands[1], $operands[2])) { |
||||
throw new InvalidArgumentException("Operator '$operator' requires three operands."); |
||||
} |
||||
|
||||
return new static($operands[0], $operator, $operands[1], $operands[2]); |
||||
} |
||||
} |
@ -0,0 +1,81 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db\conditions; |
||||
|
||||
use yii\db\ExpressionBuilderInterface; |
||||
use yii\db\ExpressionBuilderTrait; |
||||
use yii\db\ExpressionInterface; |
||||
use yii\db\Query; |
||||
|
||||
/** |
||||
* Class BetweenColumnsConditionBuilder builds objects of [[BetweenColumnsCondition]] |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class BetweenColumnsConditionBuilder implements ExpressionBuilderInterface |
||||
{ |
||||
use ExpressionBuilderTrait; |
||||
|
||||
|
||||
/** |
||||
* Method builds the raw SQL from the $expression that will not be additionally |
||||
* escaped or quoted. |
||||
* |
||||
* @param ExpressionInterface|BetweenColumnsCondition $expression the expression to be built. |
||||
* @param array $params the binding parameters. |
||||
* @return string the raw SQL that will not be additionally escaped or quoted. |
||||
*/ |
||||
public function build(ExpressionInterface $expression, array &$params = []) |
||||
{ |
||||
$operator = $expression->getOperator(); |
||||
|
||||
$startColumn = $this->escapeColumnName($expression->getIntervalStartColumn(), $params); |
||||
$endColumn = $this->escapeColumnName($expression->getIntervalEndColumn(), $params); |
||||
$value = $this->createPlaceholder($expression->getValue(), $params); |
||||
|
||||
return "$value $operator $startColumn AND $endColumn"; |
||||
} |
||||
|
||||
/** |
||||
* Prepares column name to be used in SQL statement. |
||||
* |
||||
* @param Query|ExpressionInterface|string $columnName |
||||
* @param array $params the binding parameters. |
||||
* @return string |
||||
*/ |
||||
protected function escapeColumnName($columnName, &$params = []) |
||||
{ |
||||
if ($columnName instanceof Query) { |
||||
list($sql, $params) = $this->queryBuilder->build($columnName, $params); |
||||
return "($sql)"; |
||||
} elseif ($columnName instanceof ExpressionInterface) { |
||||
return $this->queryBuilder->buildExpression($columnName, $params); |
||||
} elseif (strpos($columnName, '(') === false) { |
||||
return $this->queryBuilder->db->quoteColumnName($columnName); |
||||
} |
||||
|
||||
return $columnName; |
||||
} |
||||
|
||||
/** |
||||
* Attaches $value to $params array and returns placeholder. |
||||
* |
||||
* @param mixed $value |
||||
* @param array $params passed by reference |
||||
* @return string |
||||
*/ |
||||
protected function createPlaceholder($value, &$params) |
||||
{ |
||||
if ($value instanceof ExpressionInterface) { |
||||
return $this->queryBuilder->buildExpression($value, $params); |
||||
} |
||||
|
||||
return $this->queryBuilder->bindParam($value, $params); |
||||
} |
||||
} |
@ -0,0 +1,98 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\db\conditions; |
||||
|
||||
use yii\base\InvalidArgumentException; |
||||
|
||||
/** |
||||
* Class BetweenCondition represents a `BETWEEN` condition. |
||||
* |
||||
* @author Dmytro Naumenko <d.naumenko.a@gmail.com> |
||||
* @since 2.0.14 |
||||
*/ |
||||
class BetweenCondition implements ConditionInterface |
||||
{ |
||||
/** |
||||
* @var string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||||
*/ |
||||
private $operator; |
||||
/** |
||||
* @var mixed the column name to the left of [[operator]] |
||||
*/ |
||||
private $column; |
||||
/** |
||||
* @var mixed beginning of the interval |
||||
*/ |
||||
private $intervalStart; |
||||
/** |
||||
* @var mixed end of the interval |
||||
*/ |
||||
private $intervalEnd; |
||||
|
||||
|
||||
/** |
||||
* Creates a condition with the `BETWEEN` operator. |
||||
* |
||||
* @param mixed $column the literal to the left of $operator |
||||
* @param string $operator the operator to use (e.g. `BETWEEN` or `NOT BETWEEN`) |
||||
* @param mixed $intervalStart beginning of the interval |
||||
* @param mixed $intervalEnd end of the interval |
||||
*/ |
||||
public function __construct($column, $operator, $intervalStart, $intervalEnd) |
||||
{ |
||||
$this->column = $column; |
||||
$this->operator = $operator; |
||||
$this->intervalStart = $intervalStart; |
||||
$this->intervalEnd = $intervalEnd; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getOperator() |
||||
{ |
||||
return $this->operator; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getColumn() |
||||
{ |
||||
return $this->column; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getIntervalStart() |
||||
{ |
||||
return $this->intervalStart; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getIntervalEnd() |
||||
{ |
||||
return $this->intervalEnd; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
* @throws InvalidArgumentException if wrong number of operands have been given. |
||||
*/ |
||||
public static function fromArrayDefinition($operator, $operands) |
||||
{ |
||||
if (!isset($operands[0], $operands[1], $operands[2])) { |
||||
throw new InvalidArgumentException("Operator '$operator' requires three operands."); |
||||
} |
||||
|
||||
return new static($operands[0], $operator, $operands[1], $operands[2]); |
||||
} |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue