Commit e67d0b3c by Qiang Xue

WIP

parent 2018503c
...@@ -272,12 +272,13 @@ class Command extends \yii\base\Component ...@@ -272,12 +272,13 @@ class Command extends \yii\base\Component
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
if (is_array($value)) { if (is_array($value)) {
$this->_pendingParams[$name] = $value; $this->_pendingParams[$name] = $value;
$this->params[$name] = $value[0];
} else { } else {
$type = $this->db->getSchema()->getPdoType($value); $type = $this->db->getSchema()->getPdoType($value);
$this->_pendingParams[$name] = [$value, $type]; $this->_pendingParams[$name] = [$value, $type];
}
$this->params[$name] = $value; $this->params[$name] = $value;
} }
}
return $this; return $this;
} }
......
...@@ -643,8 +643,7 @@ class Connection extends Component ...@@ -643,8 +643,7 @@ class Connection extends Component
if (($pos = strpos($this->dsn, ':')) !== false) { if (($pos = strpos($this->dsn, ':')) !== false) {
$this->_driverName = strtolower(substr($this->dsn, 0, $pos)); $this->_driverName = strtolower(substr($this->dsn, 0, $pos));
} else { } else {
$this->open(); $this->_driverName = strtolower($this->getReadPdo()->getAttribute(PDO::ATTR_DRIVER_NAME));
$this->_driverName = strtolower($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
} }
} }
return $this->_driverName; return $this->_driverName;
......
...@@ -369,9 +369,7 @@ abstract class Schema extends Object ...@@ -369,9 +369,7 @@ abstract class Schema extends Object
return $str; return $str;
} }
$pdo = $this->db->getReadPdo(); if (($value = $this->db->getReadPdo()->quote($str)) !== false) {
if (($value = $pdo->quote($str)) !== false) {
return $value; return $value;
} else { } else {
// the driver doesn't support quote (e.g. oci) // the driver doesn't support quote (e.g. oci)
......
...@@ -143,8 +143,9 @@ class Schema extends \yii\db\Schema ...@@ -143,8 +143,9 @@ class Schema extends \yii\db\Schema
*/ */
protected function loadTableSchema($name) protected function loadTableSchema($name)
{ {
$this->db->open(); $pdo = $this->db->getReadPdo();
$tableInfo = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name);
$tableInfo = $pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name);
if (!isset($tableInfo[0]['NAME'])) { if (!isset($tableInfo[0]['NAME'])) {
return null; return null;
...@@ -161,7 +162,7 @@ class Schema extends \yii\db\Schema ...@@ -161,7 +162,7 @@ class Schema extends \yii\db\Schema
$table->columns[$column->name] = $column; $table->columns[$column->name] = $column;
} }
$primaryKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $table->name); $primaryKeys = $pdo->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $table->name);
foreach ($primaryKeys as $key) { foreach ($primaryKeys as $key) {
$column = $table->columns[$key['ATTR_NAME']]; $column = $table->columns[$key['ATTR_NAME']];
$column->isPrimaryKey = true; $column->isPrimaryKey = true;
...@@ -171,7 +172,7 @@ class Schema extends \yii\db\Schema ...@@ -171,7 +172,7 @@ class Schema extends \yii\db\Schema
} }
} }
$foreignKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name); $foreignKeys = $pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name);
foreach ($foreignKeys as $key) { foreach ($foreignKeys as $key) {
if (isset($table->foreignKeys[$key['FK_NAME']])) { if (isset($table->foreignKeys[$key['FK_NAME']])) {
$table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME']; $table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME'];
...@@ -265,8 +266,8 @@ class Schema extends \yii\db\Schema ...@@ -265,8 +266,8 @@ class Schema extends \yii\db\Schema
*/ */
protected function findTableNames($schema = '') protected function findTableNames($schema = '')
{ {
$this->db->open(); $pdo = $this->db->getReadPdo();
$tables = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE); $tables =$pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE);
$tableNames = []; $tableNames = [];
foreach ($tables as $table) { foreach ($tables as $table) {
// do not list system tables // do not list system tables
......
...@@ -239,8 +239,8 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -239,8 +239,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/ */
protected function isOldMssql() protected function isOldMssql()
{ {
$this->db->open(); $pdo = $this->db->getReadPdo();
$version = preg_split("/\./", $this->db->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION)); $version = preg_split("/\./", $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION));
return $version[0] < 11; return $version[0] < 11;
} }
} }
...@@ -132,7 +132,11 @@ EOD; ...@@ -132,7 +132,11 @@ EOD;
if ($value !== null) { if ($value !== null) {
$value = (int) $value; $value = (int) $value;
} else { } else {
// use master connection to get the biggest PK value
$enableSlave = $this->db->enableSlave;
$this->db->enableSlave = false;
$value = (int) $this->db->createCommand("SELECT MAX(\"{$tableSchema->primaryKey}\") FROM \"{$tableSchema->name}\"")->queryScalar(); $value = (int) $this->db->createCommand("SELECT MAX(\"{$tableSchema->primaryKey}\") FROM \"{$tableSchema->name}\"")->queryScalar();
$this->db->enableSlave = $enableSlave;
$value++; $value++;
} }
......
...@@ -195,7 +195,12 @@ EOD; ...@@ -195,7 +195,12 @@ EOD;
public function getLastInsertID($sequenceName = '') public function getLastInsertID($sequenceName = '')
{ {
if ($this->db->isActive) { if ($this->db->isActive) {
return $this->db->createCommand("SELECT {$sequenceName}.CURRVAL FROM DUAL")->queryScalar(); // get the last insert id from the master connection
$enableSlave = $this->db->enableSlave;
$this->db->enableSlave = false;
$id = $this->db->createCommand("SELECT {$sequenceName}.CURRVAL FROM DUAL")->queryScalar();
$this->db->enableSlave = $enableSlave;
return $id;
} else { } else {
throw new InvalidCallException('DB Connection is not active.'); throw new InvalidCallException('DB Connection is not active.');
} }
......
...@@ -136,8 +136,8 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -136,8 +136,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
$command .= "ALTER TABLE $tableName $enable TRIGGER ALL; "; $command .= "ALTER TABLE $tableName $enable TRIGGER ALL; ";
} }
#enable to have ability to alter several tables // enable to have ability to alter several tables
$this->db->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true); $this->db->getWritePdo()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
return $command; return $command;
} }
......
...@@ -120,7 +120,10 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -120,7 +120,10 @@ class QueryBuilder extends \yii\db\QueryBuilder
if ($value === null) { if ($value === null) {
$key = reset($table->primaryKey); $key = reset($table->primaryKey);
$tableName = $db->quoteTableName($tableName); $tableName = $db->quoteTableName($tableName);
$enableSlave = $this->db->enableSlave;
$this->db->enableSlave = false;
$value = $db->createCommand("SELECT MAX('$key') FROM $tableName")->queryScalar(); $value = $db->createCommand("SELECT MAX('$key') FROM $tableName")->queryScalar();
$this->db->enableSlave = $enableSlave;
} else { } else {
$value = (int) $value - 1; $value = (int) $value - 1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment