|
|
|
@ -7,6 +7,8 @@
|
|
|
|
|
|
|
|
|
|
namespace yii\db; |
|
|
|
|
|
|
|
|
|
use PDO; |
|
|
|
|
use Yii; |
|
|
|
|
use yii\base\Component; |
|
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
use yii\base\NotSupportedException; |
|
|
|
@ -128,7 +130,7 @@ class Connection extends Component
|
|
|
|
|
*/ |
|
|
|
|
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. |
|
|
|
|
* When a DB connection is active, this property will represent a PDO instance; |
|
|
|
|
* otherwise, it will be null. |
|
|
|
@ -229,7 +231,7 @@ class Connection extends Component
|
|
|
|
|
/** |
|
|
|
|
* @var array mapping between PDO driver names and [[Schema]] classes. |
|
|
|
|
* 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. |
|
|
|
|
* |
|
|
|
|
* 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.'); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
\Yii::trace('Opening DB connection: ' . $this->dsn, __METHOD__); |
|
|
|
|
Yii::trace('Opening DB connection: ' . $this->dsn, __METHOD__); |
|
|
|
|
$this->pdo = $this->createPdoInstance(); |
|
|
|
|
$this->initConnection(); |
|
|
|
|
} |
|
|
|
|
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.'; |
|
|
|
|
throw new Exception($message, $e->errorInfo, (int)$e->getCode()); |
|
|
|
|
} |
|
|
|
@ -332,7 +334,7 @@ class Connection extends Component
|
|
|
|
|
public function close() |
|
|
|
|
{ |
|
|
|
|
if ($this->pdo !== null) { |
|
|
|
|
\Yii::trace('Closing DB connection: ' . $this->dsn, __METHOD__); |
|
|
|
|
Yii::trace('Closing DB connection: ' . $this->dsn, __METHOD__); |
|
|
|
|
$this->pdo = null; |
|
|
|
|
$this->_schema = null; |
|
|
|
|
$this->_transaction = null; |
|
|
|
@ -344,11 +346,11 @@ class Connection extends Component
|
|
|
|
|
* This method is called by [[open]] to establish a DB connection. |
|
|
|
|
* 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. |
|
|
|
|
* @return \PDO the pdo instance |
|
|
|
|
* @return PDO the pdo instance |
|
|
|
|
*/ |
|
|
|
|
protected function createPdoInstance() |
|
|
|
|
{ |
|
|
|
|
$pdoClass = '\PDO'; |
|
|
|
|
$pdoClass = 'PDO'; |
|
|
|
|
if (($pos = strpos($this->dsn, ':')) !== false) { |
|
|
|
|
$driver = strtolower(substr($this->dsn, 0, $pos)); |
|
|
|
|
if ($driver === 'mssql' || $driver === 'dblib' || $driver === 'sqlsrv') { |
|
|
|
@ -367,9 +369,9 @@ class Connection extends Component
|
|
|
|
|
*/ |
|
|
|
|
protected function initConnection() |
|
|
|
|
{ |
|
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
|
|
|
if ($this->emulatePrepare !== null && constant('\PDO::ATTR_EMULATE_PREPARES')) { |
|
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare); |
|
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
|
|
|
if ($this->emulatePrepare !== null && constant('PDO::ATTR_EMULATE_PREPARES')) { |
|
|
|
|
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare); |
|
|
|
|
} |
|
|
|
|
if ($this->charset !== null && in_array($this->getDriverName(), array('pgsql', 'mysql', 'mysqli'))) { |
|
|
|
|
$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset)); |
|
|
|
@ -428,7 +430,7 @@ class Connection extends Component
|
|
|
|
|
} else { |
|
|
|
|
$driver = $this->getDriverName(); |
|
|
|
|
if (isset($this->schemaMap[$driver])) { |
|
|
|
|
$this->_schema = \Yii::createObject($this->schemaMap[$driver]); |
|
|
|
|
$this->_schema = Yii::createObject($this->schemaMap[$driver]); |
|
|
|
|
$this->_schema->db = $this; |
|
|
|
|
return $this->_schema; |
|
|
|
|
} else { |
|
|
|
@ -536,7 +538,7 @@ class Connection extends Component
|
|
|
|
|
if (($pos = strpos($this->dsn, ':')) !== false) { |
|
|
|
|
return strtolower(substr($this->dsn, 0, $pos)); |
|
|
|
|
} 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() |
|
|
|
|
{ |
|
|
|
|
$logger = \Yii::getLogger(); |
|
|
|
|
$logger = Yii::getLogger(); |
|
|
|
|
$timings = $logger->getProfiling(array('yii\db\Command::query', 'yii\db\Command::execute')); |
|
|
|
|
$count = count($timings); |
|
|
|
|
$time = 0; |
|
|
|
|