From dc99b7cdebcd3d1ffe2f0ce4e239d6dca2005d21 Mon Sep 17 00:00:00 2001 From: "qiang.xue" Date: Mon, 14 Mar 2011 00:28:38 +0000 Subject: [PATCH] (Fixes issue 1868) --- CHANGELOG | 1 + framework/caching/CDbCache.php | 1 - framework/cli/commands/MigrateCommand.php | 3 -- framework/db/CDbConnection.php | 53 ++++++++--------------- framework/db/CDbMigration.php | 1 - framework/db/CDbTransaction.php | 2 +- framework/db/ar/CActiveRecord.php | 3 -- framework/db/schema/CDbSchema.php | 1 - framework/i18n/CDbMessageSource.php | 5 +-- framework/logging/CDbLogRoute.php | 4 +- framework/web/CDbHttpSession.php | 9 ++-- framework/web/auth/CDbAuthManager.php | 2 - tests/framework/db/CDbConnectionTest.php | 4 -- 13 files changed, 26 insertions(+), 63 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 42ce5be32..323d6dfb2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/framework/caching/CDbCache.php b/framework/caching/CDbCache.php index de9e533cc..f125c54b8 100644 --- a/framework/caching/CDbCache.php +++ b/framework/caching/CDbCache.php @@ -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(); diff --git a/framework/cli/commands/MigrateCommand.php b/framework/cli/commands/MigrateCommand.php index 250efd478..4576d54cd 100644 --- a/framework/cli/commands/MigrateCommand.php +++ b/framework/cli/commands/MigrateCommand.php @@ -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"); } diff --git a/framework/db/CDbConnection.php b/framework/db/CDbConnection.php index 1273869e5..b2891d2e2 100644 --- a/framework/db/CDbConnection.php +++ b/framework/db/CDbConnection.php @@ -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); } /** diff --git a/framework/db/CDbMigration.php b/framework/db/CDbMigration.php index a3e5238c9..e25f60cc0 100644 --- a/framework/db/CDbMigration.php +++ b/framework/db/CDbMigration.php @@ -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; } diff --git a/framework/db/CDbTransaction.php b/framework/db/CDbTransaction.php index d69e97f0e..f397f705e 100644 --- a/framework/db/CDbTransaction.php +++ b/framework/db/CDbTransaction.php @@ -47,7 +47,7 @@ class CDbTransaction extends CComponent public function __construct(CDbConnection $connection) { $this->_connection=$connection; - $this->setActive(true); + $this->_active=true; } /** diff --git a/framework/db/ar/CActiveRecord.php b/framework/db/ar/CActiveRecord.php index 6f72e0b32..27233986d 100644 --- a/framework/db/ar/CActiveRecord.php +++ b/framework/db/ar/CActiveRecord.php @@ -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.')); } diff --git a/framework/db/schema/CDbSchema.php b/framework/db/schema/CDbSchema.php index f5d1c63b0..753f53cba 100644 --- a/framework/db/schema/CDbSchema.php +++ b/framework/db/schema/CDbSchema.php @@ -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; diff --git a/framework/i18n/CDbMessageSource.php b/framework/i18n/CDbMessageSource.php index 92a507f3d..1fe0543cc 100644 --- a/framework/i18n/CDbMessageSource.php +++ b/framework/i18n/CDbMessageSource.php @@ -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))); } diff --git a/framework/logging/CDbLogRoute.php b/framework/logging/CDbLogRoute.php index 3bc1dfe36..8d463a969 100644 --- a/framework/logging/CDbLogRoute.php +++ b/framework/logging/CDbLogRoute.php @@ -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 { diff --git a/framework/web/CDbHttpSession.php b/framework/web/CDbHttpSession.php index 73a475f1b..66ce7b472 100644 --- a/framework/web/CDbHttpSession.php +++ b/framework/web/CDbHttpSession.php @@ -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; } } diff --git a/framework/web/auth/CDbAuthManager.php b/framework/web/auth/CDbAuthManager.php index 607b963e0..e68f49337 100644 --- a/framework/web/auth/CDbAuthManager.php +++ b/framework/web/auth/CDbAuthManager.php @@ -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); } diff --git a/tests/framework/db/CDbConnectionTest.php b/tests/framework/db/CDbConnectionTest.php index 370a868ae..f54ed0c9b 100644 --- a/tests/framework/db/CDbConnectionTest.php +++ b/tests/framework/db/CDbConnectionTest.php @@ -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()