You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.1 KiB

13 years ago
<?php
/**
13 years ago
* DbDependency class file.
13 years ago
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
13 years ago
/**
13 years ago
* DbDependency represents a dependency based on the query result of a SQL statement.
13 years ago
*
* If the query result (a scalar) changes, the dependency is considered as changed.
* To specify the SQL statement, set {@link sql} 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>
* @since 2.0
13 years ago
*/
13 years ago
class DbDependency extends CacheDependency
13 years ago
{
/**
* @var string the ID of a {@link CDbConnection} application component. Defaults to 'db'.
*/
13 years ago
public $connectionID = 'db';
13 years ago
/**
* @var string the SQL statement whose result is used to determine if the dependency has been changed.
* Note, the SQL statement should return back a single value.
*/
public $sql;
/**
* @var array parameters (name=>value) to be bound to the SQL statement specified by {@link sql}.
* @since 1.1.4
*/
public $params;
private $_db;
/**
* Constructor.
* @param string $sql the SQL statement whose result is used to determine if the dependency has been changed.
*/
13 years ago
public function __construct($sql = null)
13 years ago
{
13 years ago
$this->sql = $sql;
13 years ago
}
/**
* PHP sleep magic method.
* This method ensures that the database instance is set null because it contains resource handles.
* @return array
*/
public function __sleep()
{
13 years ago
$this->_db = null;
13 years ago
return array_keys((array)$this);
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the value of the global state.
* @return mixed the data needed to determine if dependency has been changed.
*/
13 years ago
protected function generateDependencyData()
13 years ago
{
13 years ago
if ($this->sql !== null) {
$db = $this->getDbConnection();
$command = $db->createCommand($this->sql);
if (is_array($this->params)) {
foreach ($this->params as $name => $value) {
$command->bindValue($name, $value);
}
13 years ago
}
13 years ago
if ($db->queryCachingDuration > 0) {
13 years ago
// temporarily disable and re-enable query caching
13 years ago
$duration = $db->queryCachingDuration;
$db->queryCachingDuration = 0;
$result = $command->queryRow();
$db->queryCachingDuration = $duration;
} else {
$result = $command->queryRow();
13 years ago
}
return $result;
13 years ago
} else {
throw new CException(Yii::t('yii', 'DbDependency.sql cannot be empty.'));
13 years ago
}
}
/**
* @return CDbConnection the DB connection instance
* @throws CException if {@link connectionID} does not point to a valid application component.
*/
protected function getDbConnection()
{
13 years ago
if ($this->_db !== null) {
13 years ago
return $this->_db;
13 years ago
} else {
if (($this->_db = \Yii::$application->getComponent($this->connectionID)) instanceof CDbConnection) {
13 years ago
return $this->_db;
13 years ago
} 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.',
array('{id}' => $this->connectionID)));
}
13 years ago
}
}
}