|
|
@ -7,6 +7,8 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace yii\db; |
|
|
|
namespace yii\db; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use PDO; |
|
|
|
|
|
|
|
use Yii; |
|
|
|
use yii\base\Component; |
|
|
|
use yii\base\Component; |
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
use yii\base\NotSupportedException; |
|
|
|
use yii\base\NotSupportedException; |
|
|
@ -128,7 +130,7 @@ class Connection extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $attributes; |
|
|
|
public $attributes; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var \PDO the PHP PDO instance associated with this DB connection. |
|
|
|
* @var PDO the PHP PDO instance associated with this DB connection. |
|
|
|
* This property is mainly managed by [[open()]] and [[close()]] methods. |
|
|
|
* This property is mainly managed by [[open()]] and [[close()]] methods. |
|
|
|
* When a DB connection is active, this property will represent a PDO instance; |
|
|
|
* When a DB connection is active, this property will represent a PDO instance; |
|
|
|
* otherwise, it will be null. |
|
|
|
* otherwise, it will be null. |
|
|
@ -229,7 +231,7 @@ class Connection extends Component |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var array mapping between PDO driver names and [[Schema]] classes. |
|
|
|
* @var array mapping between PDO driver names and [[Schema]] classes. |
|
|
|
* The keys of the array are PDO driver names while the values the corresponding |
|
|
|
* The keys of the array are PDO driver names while the values the corresponding |
|
|
|
* schema class name or configuration. Please refer to [[\Yii::createObject()]] for |
|
|
|
* schema class name or configuration. Please refer to [[Yii::createObject()]] for |
|
|
|
* details on how to specify a configuration. |
|
|
|
* details on how to specify a configuration. |
|
|
|
* |
|
|
|
* |
|
|
|
* This property is mainly used by [[getSchema()]] when fetching the database schema information. |
|
|
|
* This property is mainly used by [[getSchema()]] when fetching the database schema information. |
|
|
@ -313,12 +315,12 @@ class Connection extends Component |
|
|
|
throw new InvalidConfigException('Connection::dsn cannot be empty.'); |
|
|
|
throw new InvalidConfigException('Connection::dsn cannot be empty.'); |
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
try { |
|
|
|
\Yii::trace('Opening DB connection: ' . $this->dsn, __METHOD__); |
|
|
|
Yii::trace('Opening DB connection: ' . $this->dsn, __METHOD__); |
|
|
|
$this->pdo = $this->createPdoInstance(); |
|
|
|
$this->pdo = $this->createPdoInstance(); |
|
|
|
$this->initConnection(); |
|
|
|
$this->initConnection(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (\PDOException $e) { |
|
|
|
catch (\PDOException $e) { |
|
|
|
\Yii::error("Failed to open DB connection ({$this->dsn}): " . $e->getMessage(), __METHOD__); |
|
|
|
Yii::error("Failed to open DB connection ({$this->dsn}): " . $e->getMessage(), __METHOD__); |
|
|
|
$message = YII_DEBUG ? 'Failed to open DB connection: ' . $e->getMessage() : 'Failed to open DB connection.'; |
|
|
|
$message = YII_DEBUG ? 'Failed to open DB connection: ' . $e->getMessage() : 'Failed to open DB connection.'; |
|
|
|
throw new Exception($message, $e->errorInfo, (int)$e->getCode()); |
|
|
|
throw new Exception($message, $e->errorInfo, (int)$e->getCode()); |
|
|
|
} |
|
|
|
} |
|
|
@ -332,7 +334,7 @@ class Connection extends Component |
|
|
|
public function close() |
|
|
|
public function close() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->pdo !== null) { |
|
|
|
if ($this->pdo !== null) { |
|
|
|
\Yii::trace('Closing DB connection: ' . $this->dsn, __METHOD__); |
|
|
|
Yii::trace('Closing DB connection: ' . $this->dsn, __METHOD__); |
|
|
|
$this->pdo = null; |
|
|
|
$this->pdo = null; |
|
|
|
$this->_schema = null; |
|
|
|
$this->_schema = null; |
|
|
|
$this->_transaction = null; |
|
|
|
$this->_transaction = null; |
|
|
@ -344,11 +346,11 @@ class Connection extends Component |
|
|
|
* This method is called by [[open]] to establish a DB connection. |
|
|
|
* This method is called by [[open]] to establish a DB connection. |
|
|
|
* The default implementation will create a PHP PDO instance. |
|
|
|
* The default implementation will create a PHP PDO instance. |
|
|
|
* You may override this method if the default PDO needs to be adapted for certain DBMS. |
|
|
|
* You may override this method if the default PDO needs to be adapted for certain DBMS. |
|
|
|
* @return \PDO the pdo instance |
|
|
|
* @return PDO the pdo instance |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function createPdoInstance() |
|
|
|
protected function createPdoInstance() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$pdoClass = '\PDO'; |
|
|
|
$pdoClass = 'PDO'; |
|
|
|
if (($pos = strpos($this->dsn, ':')) !== false) { |
|
|
|
if (($pos = strpos($this->dsn, ':')) !== false) { |
|
|
|
$driver = strtolower(substr($this->dsn, 0, $pos)); |
|
|
|
$driver = strtolower(substr($this->dsn, 0, $pos)); |
|
|
|
if ($driver === 'mssql' || $driver === 'dblib' || $driver === 'sqlsrv') { |
|
|
|
if ($driver === 'mssql' || $driver === 'dblib' || $driver === 'sqlsrv') { |
|
|
@ -367,9 +369,9 @@ class Connection extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function initConnection() |
|
|
|
protected function initConnection() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
|
|
if ($this->emulatePrepare !== null && constant('\PDO::ATTR_EMULATE_PREPARES')) { |
|
|
|
if ($this->emulatePrepare !== null && constant('PDO::ATTR_EMULATE_PREPARES')) { |
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare); |
|
|
|
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->charset !== null && in_array($this->getDriverName(), array('pgsql', 'mysql', 'mysqli'))) { |
|
|
|
if ($this->charset !== null && in_array($this->getDriverName(), array('pgsql', 'mysql', 'mysqli'))) { |
|
|
|
$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset)); |
|
|
|
$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset)); |
|
|
@ -428,7 +430,7 @@ class Connection extends Component |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$driver = $this->getDriverName(); |
|
|
|
$driver = $this->getDriverName(); |
|
|
|
if (isset($this->schemaMap[$driver])) { |
|
|
|
if (isset($this->schemaMap[$driver])) { |
|
|
|
$this->_schema = \Yii::createObject($this->schemaMap[$driver]); |
|
|
|
$this->_schema = Yii::createObject($this->schemaMap[$driver]); |
|
|
|
$this->_schema->db = $this; |
|
|
|
$this->_schema->db = $this; |
|
|
|
return $this->_schema; |
|
|
|
return $this->_schema; |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -536,7 +538,7 @@ class Connection extends Component |
|
|
|
if (($pos = strpos($this->dsn, ':')) !== false) { |
|
|
|
if (($pos = strpos($this->dsn, ':')) !== false) { |
|
|
|
return strtolower(substr($this->dsn, 0, $pos)); |
|
|
|
return strtolower(substr($this->dsn, 0, $pos)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return strtolower($this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)); |
|
|
|
return strtolower($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -551,7 +553,7 @@ class Connection extends Component |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getQuerySummary() |
|
|
|
public function getQuerySummary() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$logger = \Yii::getLogger(); |
|
|
|
$logger = Yii::getLogger(); |
|
|
|
$timings = $logger->getProfiling(array('yii\db\Command::query', 'yii\db\Command::execute')); |
|
|
|
$timings = $logger->getProfiling(array('yii\db\Command::query', 'yii\db\Command::execute')); |
|
|
|
$count = count($timings); |
|
|
|
$count = count($timings); |
|
|
|
$time = 0; |
|
|
|
$time = 0; |
|
|
|