Commit 37c874f1 by Qiang Xue

...

parent a1973f2c
...@@ -16,7 +16,7 @@ use yii\base\ApplicationComponent; ...@@ -16,7 +16,7 @@ use yii\base\ApplicationComponent;
* *
* A data item can be stored in cache by calling [[set()]] and be retrieved back * A data item can be stored in cache by calling [[set()]] and be retrieved back
* later (in the same or different request) by [[get()]]. In both operations, * later (in the same or different request) by [[get()]]. In both operations,
* a key identifying the data item is required. An expiration time and/or a [[CacheDependency|dependency]] * a key identifying the data item is required. An expiration time and/or a [[Dependency|dependency]]
* can also be specified when calling [[set()]]. If the data item expires or the dependency * can also be specified when calling [[set()]]. If the data item expires or the dependency
* changes at the time of calling [[get()]], the cache will return no data. * changes at the time of calling [[get()]], the cache will return no data.
* *
...@@ -60,7 +60,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess ...@@ -60,7 +60,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
* a two-element array. The first element specifies the serialization function, and the second the deserialization * a two-element array. The first element specifies the serialization function, and the second the deserialization
* function. If this property is set false, data will be directly sent to and retrieved from the underlying * function. If this property is set false, data will be directly sent to and retrieved from the underlying
* cache component without any serialization or deserialization. You should not turn off serialization if * cache component without any serialization or deserialization. You should not turn off serialization if
* you are using [[CacheDependency|cache dependency]], because it relies on data serialization. * you are using [[Dependency|cache dependency]], because it relies on data serialization.
*/ */
public $serializer; public $serializer;
...@@ -102,7 +102,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess ...@@ -102,7 +102,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
} else { } else {
$value = call_user_func($this->serializer[1], $value); $value = call_user_func($this->serializer[1], $value);
} }
if (is_array($value) && ($value[1] instanceof CacheDependency) || !$value[1]->getHasChanged()) { if (is_array($value) && ($value[1] instanceof Dependency) || !$value[1]->getHasChanged()) {
return $value[0]; return $value[0];
} else { } else {
return false; return false;
...@@ -136,7 +136,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess ...@@ -136,7 +136,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
$results[$id] = false; $results[$id] = false;
if (isset($values[$uid])) { if (isset($values[$uid])) {
$value = $this->serializer === null ? unserialize($values[$uid]) : call_user_func($this->serializer[1], $values[$uid]); $value = $this->serializer === null ? unserialize($values[$uid]) : call_user_func($this->serializer[1], $values[$uid]);
if (is_array($value) && (!($value[1] instanceof CacheDependency) || !$value[1]->getHasChanged())) { if (is_array($value) && (!($value[1] instanceof Dependency) || !$value[1]->getHasChanged())) {
$results[$id] = $value[0]; $results[$id] = $value[0];
} }
} }
...@@ -153,7 +153,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess ...@@ -153,7 +153,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
* @param string $id the key identifying the value to be cached * @param string $id the key identifying the value to be cached
* @param mixed $value the value to be cached * @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param CacheDependency $dependency dependency of the cached item. If the dependency changes, * @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. * the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* @return boolean whether the value is successfully stored into cache * @return boolean whether the value is successfully stored into cache
*/ */
...@@ -176,7 +176,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess ...@@ -176,7 +176,7 @@ abstract class Cache extends ApplicationComponent implements \ArrayAccess
* @param string $id the key identifying the value to be cached * @param string $id the key identifying the value to be cached
* @param mixed $value the value to be cached * @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param CacheDependency $dependency dependency of the cached item. If the dependency changes, * @param Dependency $dependency dependency of the cached item. If the dependency changes,
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. * the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
* @return boolean whether the value is successfully stored into cache * @return boolean whether the value is successfully stored into cache
*/ */
......
...@@ -10,16 +10,12 @@ ...@@ -10,16 +10,12 @@
namespace yii\caching; namespace yii\caching;
/** /**
* ChainedDependency represents a list of cache dependencies. * ChainedDependency represents a dependency which is composed of a list of other dependencies.
* *
* If any of the dependencies reports a dependency change, ChainedDependency * When [[dependOnAll]] is true, if any of the dependencies has changed, this dependency is
* will return true for the checking. * considered changed; When [[dependOnAll]] is false, if one of the dependencies has NOT changed,
* this dependency is considered NOT changed.
* *
* To add dependencies to ChainedDependency, use {@link getDependencies Dependencies}
* which gives a {@link CTypedList} instance and can be used like an array
* (see {@link CList} for more details}).
*
* @property CTypedList $dependencies List of dependency objects.
* @property boolean $hasChanged Whether the dependency is changed or not. * @property boolean $hasChanged Whether the dependency is changed or not.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
...@@ -27,55 +23,52 @@ namespace yii\caching; ...@@ -27,55 +23,52 @@ namespace yii\caching;
*/ */
class ChainedDependency extends Dependency class ChainedDependency extends Dependency
{ {
private $_dependencies=null;
/** /**
* Constructor. * @var array list of dependencies that this dependency is composed of.
* @param array $dependencies the dependencies to be added to this chain. * Each array element should be a dependency object or a configuration array
* @since 1.1.4 * that can be used to create a dependency object via [[\Yii::createObject()]].
*/ */
public function __construct($dependencies=array()) public $dependencies = array();
{ /**
if(!empty($dependencies)) * @var boolean whether this dependency is depending on every dependency in [[dependencies]].
$this->setDependencies($dependencies); * Defaults to true, meaning if any of the dependencies has changed, this dependency is considered changed.
} * When it is set false, it means if one of the dependencies has NOT changed, this dependency
* is considered NOT changed.
*/
public $dependOnAll = true;
/** /**
* @return CTypedList list of dependency objects * Constructor.
* @param array $dependencies list of dependencies that this dependency is composed of.
* Each array element should be a dependency object or a configuration array
* that can be used to create a dependency object via [[\Yii::createObject()]].
*/ */
public function getDependencies() public function __construct($dependencies = array())
{ {
if($this->_dependencies===null) $this->dependencies = $dependencies;
$this->_dependencies=new CTypedList('ICacheDependency');
return $this->_dependencies;
} }
/** /**
* @param array $values list of dependency objects or configurations to be added to this chain. * Evaluates the dependency by generating and saving the data related with dependency.
* If a depedency is specified as a configuration, it must be an array that can be recognized
* by {@link YiiBase::createComponent}.
*/ */
public function setDependencies($values) public function evaluateDependency()
{
$dependencies=$this->getDependencies();
foreach($values as $value)
{ {
if(is_array($value)) foreach ($this->dependencies as $dependency) {
$value=Yii::createComponent($value); if (!$dependency instanceof Dependency) {
$dependencies->add($value); $dependency = \Yii::createObject($dependency);
}
$dependency->evalulateDependency();
} }
} }
/** /**
* Evaluates the dependency by generating and saving the data related with dependency. * Generates the data needed to determine if dependency has been changed.
* This method does nothing in this class.
* @return mixed the data needed to determine if dependency has been changed.
*/ */
public function evaluateDependency() protected function generateDependencyData()
{ {
if($this->_dependencies!==null) return null;
{
foreach($this->_dependencies as $dependency)
$dependency->evaluateDependency();
}
} }
/** /**
...@@ -86,12 +79,16 @@ class ChainedDependency extends Dependency ...@@ -86,12 +79,16 @@ class ChainedDependency extends Dependency
*/ */
public function getHasChanged() public function getHasChanged()
{ {
if($this->_dependencies!==null) foreach ($this->dependencies as $dependency) {
{ if (!$dependency instanceof Dependency) {
foreach($this->_dependencies as $dependency) $dependency = \Yii::createObject($dependency);
if($dependency->getHasChanged())
return true;
} }
if ($this->dependOnAll && $dependency->getHasChanged()) {
return true;
} elseif (!$this->dependOnAll && !$dependency->getHasChanged()) {
return false; return false;
} }
}
return !$this->dependOnAll;
}
} }
...@@ -47,8 +47,7 @@ use yii\db\dao\Query; ...@@ -47,8 +47,7 @@ use yii\db\dao\Query;
class DbCache extends Cache class DbCache extends Cache
{ {
/** /**
* @var string the ID of the [[Connection|DB connection]] application component. * @var string the ID of the [[Connection|DB connection]] application component. Defaults to 'db'.
* Defaults to 'db'.
*/ */
public $connectionID = 'db'; public $connectionID = 'db';
/** /**
...@@ -108,6 +107,7 @@ class DbCache extends Cache ...@@ -108,6 +107,7 @@ class DbCache extends Cache
->where('id = :id AND (expire = 0 OR expire > :time)', array(':id' => $key, ':time' => time())); ->where('id = :id AND (expire = 0 OR expire > :time)', array(':id' => $key, ':time' => time()));
$db = $this->getDbConnection(); $db = $this->getDbConnection();
if ($db->queryCachingDuration >= 0) { if ($db->queryCachingDuration >= 0) {
// temporarily disable and re-enable query caching
$duration = $db->queryCachingDuration; $duration = $db->queryCachingDuration;
$db->queryCachingDuration = -1; $db->queryCachingDuration = -1;
$result = $query->createCommand($db)->queryScalar(); $result = $query->createCommand($db)->queryScalar();
......
...@@ -9,44 +9,42 @@ ...@@ -9,44 +9,42 @@
namespace yii\caching; namespace yii\caching;
use yii\base\Exception;
use yii\db\dao\Connection;
use yii\db\dao\Query;
/** /**
* DbDependency represents a dependency based on the query result of a SQL statement. * DbDependency represents a dependency based on the query result of a SQL statement.
* *
* If the query result (a scalar) changes, the dependency is considered as changed. * If the query result changes, the dependency is considered as changed.
* To specify the SQL statement, set {@link sql} property. * The query is specified via the [[query]] property.
* The {@link connectionID} property specifies the ID of a {@link CDbConnection} application
* component. It is this DB connection that is used to perform the query.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class DbDependency extends CacheDependency class DbDependency extends Dependency
{ {
/** /**
* @var string the ID of a {@link CDbConnection} application component. Defaults to 'db'. * @var string the ID of the [[Connection|DB connection]] application component. Defaults to 'db'.
*/ */
public $connectionID = 'db'; public $connectionID = 'db';
/** /**
* @var string the SQL statement whose result is used to determine if the dependency has been changed. * @var Query the SQL query whose result is used to determine if the dependency has been changed.
* Note, the SQL statement should return back a single value. * Only the first row of the query result will be used.
*/ */
public $sql; public $query;
/** /**
* @var array parameters (name=>value) to be bound to the SQL statement specified by {@link sql}. * @var Connection the DB connection instance
* @since 1.1.4
*/ */
public $params;
private $_db; private $_db;
/** /**
* Constructor. * Constructor.
* @param string $sql the SQL statement whose result is used to determine if the dependency has been changed. * @param Query $query the SQL query whose result is used to determine if the dependency has been changed.
*/ */
public function __construct($sql = null) public function __construct($query = null)
{ {
$this->sql = $sql; $this->query = $query;
} }
/** /**
...@@ -67,44 +65,44 @@ class DbDependency extends CacheDependency ...@@ -67,44 +65,44 @@ class DbDependency extends CacheDependency
*/ */
protected function generateDependencyData() protected function generateDependencyData()
{ {
if ($this->sql !== null) {
$db = $this->getDbConnection(); $db = $this->getDbConnection();
$command = $db->createCommand($this->sql); $command = $this->query->createCommand($db);
if (is_array($this->params)) { if ($db->queryCachingDuration >= 0) {
foreach ($this->params as $name => $value) {
$command->bindValue($name, $value);
}
}
if ($db->queryCachingDuration > 0) {
// temporarily disable and re-enable query caching // temporarily disable and re-enable query caching
$duration = $db->queryCachingDuration; $duration = $db->queryCachingDuration;
$db->queryCachingDuration = 0; $db->queryCachingDuration = -1;
$result = $command->queryRow(); $result = $command->queryRow();
$db->queryCachingDuration = $duration; $db->queryCachingDuration = $duration;
} else { } else {
$result = $command->queryRow(); $result = $command->queryRow();
} }
return $result; return $result;
} else {
throw new CException(Yii::t('yii', 'DbDependency.sql cannot be empty.'));
}
} }
/** /**
* @return CDbConnection the DB connection instance * Returns the DB connection instance used for caching purpose.
* @throws CException if {@link connectionID} does not point to a valid application component. * @return Connection the DB connection instance
* @throws Exception if [[connectionID]] does not point to a valid application component.
*/ */
protected function getDbConnection() public function getDbConnection()
{ {
if ($this->_db !== null) { if ($this->_db === null) {
return $this->_db; $db = \Yii::$application->getComponent($this->connectionID);
} else { if ($db instanceof Connection) {
if (($this->_db = \Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection) { $this->_db = $db;
return $this->_db;
} else { } else {
throw new CException(Yii::t('yii', 'DbDependency.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.', throw new Exception("DbDependency.connectionID must refer to the ID of a DB connection application component.");
array('{id}' => $this->connectionID))); }
} }
return $this->_db;
} }
/**
* Sets the DB connection used by the cache component.
* @param Connection $value the DB connection instance
*/
public function setDbConnection($value)
{
$this->_db = $value;
} }
} }
<?php <?php
/** /**
* CExpressionDependency class file. * ExpressionDependency class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
...@@ -10,9 +10,9 @@ ...@@ -10,9 +10,9 @@
namespace yii\caching; namespace yii\caching;
/** /**
* CExpressionDependency represents a dependency based on the result of a PHP expression. * ExpressionDependency represents a dependency based on the result of a PHP expression.
* *
* CExpressionDependency performs dependency checking based on the * ExpressionDependency performs dependency checking based on the
* result of a PHP {@link expression}. * result of a PHP {@link expression}.
* The dependency is reported as unchanged if and only if the result is * The dependency is reported as unchanged if and only if the result is
* the same as the one evaluated when storing the data to cache. * the same as the one evaluated when storing the data to cache.
...@@ -20,7 +20,7 @@ namespace yii\caching; ...@@ -20,7 +20,7 @@ namespace yii\caching;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class CExpressionDependency extends CCacheDependency class ExpressionDependency extends Dependency
{ {
/** /**
* @var string the PHP expression whose result is used to determine the dependency. * @var string the PHP expression whose result is used to determine the dependency.
...@@ -35,9 +35,9 @@ class CExpressionDependency extends CCacheDependency ...@@ -35,9 +35,9 @@ class CExpressionDependency extends CCacheDependency
* Constructor. * Constructor.
* @param string $expression the PHP expression whose result is used to determine the dependency. * @param string $expression the PHP expression whose result is used to determine the dependency.
*/ */
public function __construct($expression='true') public function __construct($expression = 'true')
{ {
$this->expression=$expression; $this->expression = $expression;
} }
/** /**
......
...@@ -42,6 +42,6 @@ class FileDependency extends Dependency ...@@ -42,6 +42,6 @@ class FileDependency extends Dependency
*/ */
protected function generateDependencyData() protected function generateDependencyData()
{ {
return $this->fileName !== null ? @filemtime($this->fileName) : 0; return @filemtime($this->fileName);
} }
} }
...@@ -129,7 +129,7 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA ...@@ -129,7 +129,7 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA
* It changes the query caching parameter of the {@link dbConnection} instance. * It changes the query caching parameter of the {@link dbConnection} instance.
* @param integer $duration the number of seconds that query results may remain valid in cache. * @param integer $duration the number of seconds that query results may remain valid in cache.
* If this is 0, the caching will be disabled. * If this is 0, the caching will be disabled.
* @param CCacheDependency $dependency the dependency that will be used when saving the query results into cache. * @param \yii\caching\Dependency $dependency the dependency that will be used when saving the query results into cache.
* @param integer $queryCount number of SQL queries that need to be cached after calling this method. Defaults to 1, * @param integer $queryCount number of SQL queries that need to be cached after calling this method. Defaults to 1,
* meaning that the next SQL query will be cached. * meaning that the next SQL query will be cached.
* @return ActiveRecord the active record instance itself. * @return ActiveRecord the active record instance itself.
......
...@@ -78,16 +78,16 @@ class BaseQuery extends \yii\base\Component ...@@ -78,16 +78,16 @@ class BaseQuery extends \yii\base\Component
*/ */
public $having; public $having;
/** /**
* @var array list of query parameter values indexed by parameter placeholders.
* For example, `array(':name'=>'Dan', ':age'=>31)`.
*/
public $params;
/**
* @var string|BaseQuery[] the UNION clause(s) in a SQL statement. This can be either a string * @var string|BaseQuery[] the UNION clause(s) in a SQL statement. This can be either a string
* representing a single UNION clause or an array representing multiple UNION clauses. * representing a single UNION clause or an array representing multiple UNION clauses.
* Each union clause can be a string or a `BaseQuery` object which refers to the SQL statement. * Each union clause can be a string or a `BaseQuery` object which refers to the SQL statement.
*/ */
public $union; public $union;
/**
* @var array list of query parameter values indexed by parameter placeholders.
* For example, `array(':name'=>'Dan', ':age'=>31)`.
*/
public $params;
/** /**
* Sets the SELECT part of the query. * Sets the SELECT part of the query.
......
...@@ -238,15 +238,15 @@ class Connection extends \yii\base\ApplicationComponent ...@@ -238,15 +238,15 @@ class Connection extends \yii\base\ApplicationComponent
* [[Driver]] class to support DBMS that is not supported by Yii. * [[Driver]] class to support DBMS that is not supported by Yii.
*/ */
public $driverMap = array( public $driverMap = array(
'pgsql' => '\yii\db\dao\pgsql\Driver', // PostgreSQL 'pgsql' => 'yii\db\dao\pgsql\Driver', // PostgreSQL
'mysqli' => '\yii\db\dao\mysql\Driver', // MySQL 'mysqli' => 'yii\db\dao\mysql\Driver', // MySQL
'mysql' => '\yii\db\dao\mysql\Driver', // MySQL 'mysql' => 'yii\db\dao\mysql\Driver', // MySQL
'sqlite' => '\yii\db\dao\sqlite\Driver', // sqlite 3 'sqlite' => 'yii\db\dao\sqlite\Driver', // sqlite 3
'sqlite2' => '\yii\db\dao\sqlite\Driver', // sqlite 2 'sqlite2' => 'yii\db\dao\sqlite\Driver', // sqlite 2
'mssql' => '\yii\db\dao\mssql\Driver', // Mssql driver on windows hosts 'mssql' => 'yi\db\dao\mssql\Driver', // Mssql driver on windows hosts
'dblib' => '\yii\db\dao\mssql\Driver', // dblib drivers on linux (and maybe others os) hosts 'dblib' => 'yii\db\dao\mssql\Driver', // dblib drivers on linux (and maybe others os) hosts
'sqlsrv' => '\yii\db\dao\mssql\Driver', // Mssql 'sqlsrv' => 'yii\db\dao\mssql\Driver', // Mssql
'oci' => '\yii\db\dao\oci\Driver', // Oracle driver 'oci' => 'yii\db\dao\oci\Driver', // Oracle driver
); );
/** /**
* @var Transaction the currently active transaction * @var Transaction the currently active transaction
......
...@@ -47,6 +47,10 @@ class Query extends BaseQuery ...@@ -47,6 +47,10 @@ class Query extends BaseQuery
* If this property is not set, it means this query represents a SELECT statement. * If this property is not set, it means this query represents a SELECT statement.
*/ */
public $operation; public $operation;
/**
* @var string the SQL statement that this query represents. This is directly set by user.
*/
public $sql;
/** /**
* Generates and returns the SQL statement according to this query. * Generates and returns the SQL statement according to this query.
...@@ -58,6 +62,9 @@ class Query extends BaseQuery ...@@ -58,6 +62,9 @@ class Query extends BaseQuery
*/ */
public function getSql($connection = null) public function getSql($connection = null)
{ {
if ($this->sql !== null) {
return $this->sql;
}
if ($connection === null) { if ($connection === null) {
$connection = \Yii::$application->db; $connection = \Yii::$application->db;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment