(Fixes issue 1868)

This commit is contained in:
qiang.xue
2011-03-14 00:28:38 +00:00
parent 487cc6f600
commit dc99b7cdeb
13 changed files with 26 additions and 63 deletions

View File

@@ -33,6 +33,7 @@ Version 1.1.7 to be released
- Enh #1792: Enhanced CGridView: on ajax error a proper message is composed and displayed or optionally sent to the custom error handler (mdomba)
- Enh #1816: Added $dumpLogs parameter to CLogger::flush() so that log messages can be forced to be dumped at will (Qiang)
- Enh #1843: Added 'uncheckValue' option to CHtml::activeRadioButtonList and CHtml::activeCheckBoxList. It allows to avoid hidden field rendering (creocoder, Sam Dark)
- Enh #1868: CDbConnection will now automatically open a DB connection before executing a SQL statement (Qiang)
- Enh #1937: Added support to use custom input ID for input fields that need AJAX-based validation (Qiang)
- Enh #1993: Allow AR relations across separate db connections (Qiang)
- Enh #1996: Added support for using parameter binding with class-based actions (Qiang)

View File

@@ -73,7 +73,6 @@ class CDbCache extends CCache
$db=$this->getDbConnection();
$db->setActive(true);
if($this->autoCreateCacheTable)
{
$sql="DELETE FROM {$this->cacheTableName} WHERE expire>0 AND expire<".time();

View File

@@ -413,10 +413,7 @@ class MigrateCommand extends CConsoleCommand
if($this->_db!==null)
return $this->_db;
else if(($this->_db=Yii::app()->getComponent($this->connectionID)) instanceof CDbConnection)
{
$this->_db->setActive(true);
return $this->_db;
}
else
die("Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n");
}

View File

@@ -429,14 +429,11 @@ class CDbConnection extends CApplicationComponent
* for more details about how to pass an array as the query. If this parameter is not given,
* you will have to call query builder methods of {@link CDbCommand} to build the DB query.
* @return CDbCommand the DB command
* @throws CException if the connection is not active
*/
public function createCommand($query=null)
{
if($this->getActive())
return new CDbCommand($this,$query);
else
throw new CDbException(Yii::t('yii','CDbConnection is inactive and cannot perform any DB operations.'));
$this->setActive(true);
return new CDbCommand($this,$query);
}
/**
@@ -456,23 +453,17 @@ class CDbConnection extends CApplicationComponent
/**
* Starts a transaction.
* @return CDbTransaction the transaction initiated
* @throws CException if the connection is not active
*/
public function beginTransaction()
{
if($this->getActive())
{
$this->_pdo->beginTransaction();
return $this->_transaction=new CDbTransaction($this);
}
else
throw new CDbException(Yii::t('yii','CDbConnection is inactive and cannot perform any DB operations.'));
$this->setActive(true);
$this->_pdo->beginTransaction();
return $this->_transaction=new CDbTransaction($this);
}
/**
* Returns the database schema for the current connection
* @return CDbSchema the database schema for the current connection
* @throws CException if the connection is not active yet
*/
public function getSchema()
{
@@ -480,9 +471,7 @@ class CDbConnection extends CApplicationComponent
return $this->_schema;
else
{
if(!$this->getActive())
throw new CDbException(Yii::t('yii','CDbConnection is inactive and cannot perform any DB operations.'));
$driver=strtolower($this->getDriverName());
$driver=$this->getDriverName();
if(isset($this->driverMap[$driver]))
return $this->_schema=Yii::createComponent($this->driverMap[$driver], $this);
else
@@ -509,10 +498,8 @@ class CDbConnection extends CApplicationComponent
*/
public function getLastInsertID($sequenceName='')
{
if($this->getActive())
return $this->_pdo->lastInsertId($sequenceName);
else
throw new CDbException(Yii::t('yii','CDbConnection is inactive and cannot perform any DB operations.'));
$this->setActive(true);
return $this->_pdo->lastInsertId($sequenceName);
}
/**
@@ -526,15 +513,11 @@ class CDbConnection extends CApplicationComponent
if(is_int($str) || is_float($str))
return $str;
if($this->getActive())
{
if(($value=$this->_pdo->quote($str))!==false)
return $value;
else // the driver doesn't support quote (e.g. oci)
return "'" . addcslashes(str_replace("'", "''", $str), "\000\n\r\\\032") . "'";
}
else
throw new CDbException(Yii::t('yii','CDbConnection is inactive and cannot perform any DB operations.'));
$this->setActive(true);
if(($value=$this->_pdo->quote($str))!==false)
return $value;
else // the driver doesn't support quote (e.g. oci)
return "'" . addcslashes(str_replace("'", "''", $str), "\000\n\r\\\032") . "'";
}
/**
@@ -662,7 +645,9 @@ class CDbConnection extends CApplicationComponent
*/
public function getDriverName()
{
return $this->getAttribute(PDO::ATTR_DRIVER_NAME);
if(($pos=strpos($this->connectionString, ':'))!==false)
return strtolower(substr($this->connectionString, 0, $pos));
// return $this->getAttribute(PDO::ATTR_DRIVER_NAME);
}
/**
@@ -728,10 +713,8 @@ class CDbConnection extends CApplicationComponent
*/
public function getAttribute($name)
{
if($this->getActive())
return $this->_pdo->getAttribute($name);
else
throw new CDbException(Yii::t('yii','CDbConnection is inactive and cannot perform any DB operations.'));
$this->setActive(true);
return $this->_pdo->getAttribute($name);
}
/**

View File

@@ -126,7 +126,6 @@ abstract class CDbMigration extends CComponent
$this->_db=Yii::app()->getComponent('db');
if(!$this->_db instanceof CDbConnection)
throw new CException(Yii::t('yii', 'The "db" application component must be configured to be a CDbConnection object.'));
$this->_db->setActive(true);
}
return $this->_db;
}

View File

@@ -47,7 +47,7 @@ class CDbTransaction extends CComponent
public function __construct(CDbConnection $connection)
{
$this->_connection=$connection;
$this->setActive(true);
$this->_active=true;
}
/**

View File

@@ -606,10 +606,7 @@ abstract class CActiveRecord extends CModel
{
self::$db=Yii::app()->getDb();
if(self::$db instanceof CDbConnection)
{
self::$db->setActive(true);
return self::$db;
}
else
throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
}

View File

@@ -43,7 +43,6 @@ abstract class CDbSchema extends CComponent
*/
public function __construct($conn)
{
$conn->setActive(true);
$this->_connection=$conn;
foreach($conn->schemaCachingExclude as $name)
$this->_cacheExclude[$name]=true;

View File

@@ -102,9 +102,8 @@ class CDbMessageSource extends CMessageSource
{
if($this->_db===null)
{
if(($this->_db=Yii::app()->getComponent($this->connectionID)) instanceof CDbConnection)
$this->_db->setActive(true);
else
$this->_db=Yii::app()->getComponent($this->connectionID);
if(!$this->_db instanceof CDbConnection)
throw new CException(Yii::t('yii','CDbMessageSource.connectionID is invalid. Please make sure "{id}" refers to a valid database application component.',
array('{id}'=>$this->connectionID)));
}

View File

@@ -67,11 +67,9 @@ class CDbLogRoute extends CLogRoute
{
parent::init();
$db=$this->getDbConnection();
$db->setActive(true);
if($this->autoCreateLogTable)
{
$db=$this->getDbConnection();
$sql="DELETE FROM {$this->logTableName} WHERE 0=1";
try
{

View File

@@ -128,11 +128,10 @@ CREATE TABLE $tableName
*/
public function openSession($savePath,$sessionName)
{
$db=$this->getDbConnection();
$db->setActive(true);
if($this->autoCreateSessionTable)
{
$db=$this->getDbConnection();
$db->setActive(true);
$sql="DELETE FROM {$this->sessionTableName} WHERE expire<".time();
try
{
@@ -216,10 +215,8 @@ WHERE expire>$now AND id=:id
*/
public function gcSession($maxLifetime)
{
$db=$this->getDbConnection();
$db->setActive(true);
$sql="DELETE FROM {$this->sessionTableName} WHERE expire<".time();
$db->createCommand($sql)->execute();
$this->getDbConnection()->createCommand($sql)->execute();
return true;
}
}

View File

@@ -55,8 +55,6 @@ class CDbAuthManager extends CAuthManager
public function init()
{
parent::init();
$this->getDbConnection()->setActive(true);
$this->_usingSqlite=!strncmp($this->getDbConnection()->getDriverName(),'sqlite',6);
}

View File

@@ -72,10 +72,6 @@ class CDbConnectionTest extends CTestCase
$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
$command=$this->_connection->createCommand($sql);
$this->assertTrue($command instanceof CDbCommand);
$this->_connection->active=false;
$this->setExpectedException('CException');
$this->_connection->createCommand($sql);
}
public function testLastInsertID()