From e67d0b3c39a8a2691f0b1ec33e33d76b66140dd1 Mon Sep 17 00:00:00 2001
From: Qiang Xue <qiang.xue@gmail.com>
Date: Fri, 11 Jul 2014 14:01:44 -0400
Subject: [PATCH] WIP

---
 framework/db/Command.php             |  3 ++-
 framework/db/Connection.php          |  3 +--
 framework/db/Schema.php              |  4 +---
 framework/db/cubrid/Schema.php       | 13 +++++++------
 framework/db/mssql/QueryBuilder.php  |  4 ++--
 framework/db/oci/QueryBuilder.php    |  4 ++++
 framework/db/oci/Schema.php          |  7 ++++++-
 framework/db/pgsql/QueryBuilder.php  |  4 ++--
 framework/db/sqlite/QueryBuilder.php |  3 +++
 9 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/framework/db/Command.php b/framework/db/Command.php
index 16bb5b4..f7419ca 100644
--- a/framework/db/Command.php
+++ b/framework/db/Command.php
@@ -272,11 +272,12 @@ class Command extends \yii\base\Component
         foreach ($values as $name => $value) {
             if (is_array($value)) {
                 $this->_pendingParams[$name] = $value;
+                $this->params[$name] = $value[0];
             } else {
                 $type = $this->db->getSchema()->getPdoType($value);
                 $this->_pendingParams[$name] = [$value, $type];
+                $this->params[$name] = $value;
             }
-            $this->params[$name] = $value;
         }
 
         return $this;
diff --git a/framework/db/Connection.php b/framework/db/Connection.php
index 89413ca..413d13b 100644
--- a/framework/db/Connection.php
+++ b/framework/db/Connection.php
@@ -643,8 +643,7 @@ class Connection extends Component
             if (($pos = strpos($this->dsn, ':')) !== false) {
                 $this->_driverName = strtolower(substr($this->dsn, 0, $pos));
             } else {
-                $this->open();
-                $this->_driverName = strtolower($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
+                $this->_driverName = strtolower($this->getReadPdo()->getAttribute(PDO::ATTR_DRIVER_NAME));
             }
         }
         return $this->_driverName;
diff --git a/framework/db/Schema.php b/framework/db/Schema.php
index 0796059..01f91b7 100644
--- a/framework/db/Schema.php
+++ b/framework/db/Schema.php
@@ -369,9 +369,7 @@ abstract class Schema extends Object
             return $str;
         }
 
-        $pdo = $this->db->getReadPdo();
-
-        if (($value = $pdo->quote($str)) !== false) {
+        if (($value = $this->db->getReadPdo()->quote($str)) !== false) {
             return $value;
         } else {
             // the driver doesn't support quote (e.g. oci)
diff --git a/framework/db/cubrid/Schema.php b/framework/db/cubrid/Schema.php
index a44d003..1df8559 100644
--- a/framework/db/cubrid/Schema.php
+++ b/framework/db/cubrid/Schema.php
@@ -143,8 +143,9 @@ class Schema extends \yii\db\Schema
      */
     protected function loadTableSchema($name)
     {
-        $this->db->open();
-        $tableInfo = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name);
+        $pdo = $this->db->getReadPdo();
+
+        $tableInfo = $pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name);
 
         if (!isset($tableInfo[0]['NAME'])) {
             return null;
@@ -161,7 +162,7 @@ class Schema extends \yii\db\Schema
             $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) {
             $column = $table->columns[$key['ATTR_NAME']];
             $column->isPrimaryKey = true;
@@ -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) {
             if (isset($table->foreignKeys[$key['FK_NAME']])) {
                 $table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME'];
@@ -265,8 +266,8 @@ class Schema extends \yii\db\Schema
      */
     protected function findTableNames($schema = '')
     {
-        $this->db->open();
-        $tables = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE);
+        $pdo = $this->db->getReadPdo();
+        $tables =$pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE);
         $tableNames = [];
         foreach ($tables as $table) {
             // do not list system tables
diff --git a/framework/db/mssql/QueryBuilder.php b/framework/db/mssql/QueryBuilder.php
index 6852c60..d77cda0 100644
--- a/framework/db/mssql/QueryBuilder.php
+++ b/framework/db/mssql/QueryBuilder.php
@@ -239,8 +239,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
      */
     protected function isOldMssql()
     {
-        $this->db->open();
-        $version = preg_split("/\./", $this->db->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION));
+        $pdo = $this->db->getReadPdo();
+        $version = preg_split("/\./", $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION));
         return $version[0] < 11;
     }
 }
diff --git a/framework/db/oci/QueryBuilder.php b/framework/db/oci/QueryBuilder.php
index 1119cd3..9c096bf 100644
--- a/framework/db/oci/QueryBuilder.php
+++ b/framework/db/oci/QueryBuilder.php
@@ -132,7 +132,11 @@ EOD;
         if ($value !== null) {
             $value = (int) $value;
         } 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();
+            $this->db->enableSlave = $enableSlave;
             $value++;
         }
 
diff --git a/framework/db/oci/Schema.php b/framework/db/oci/Schema.php
index 621fa6d..aacf54b 100644
--- a/framework/db/oci/Schema.php
+++ b/framework/db/oci/Schema.php
@@ -195,7 +195,12 @@ EOD;
     public function getLastInsertID($sequenceName = '')
     {
         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 {
             throw new InvalidCallException('DB Connection is not active.');
         }
diff --git a/framework/db/pgsql/QueryBuilder.php b/framework/db/pgsql/QueryBuilder.php
index 621c51d..475a5fb 100644
--- a/framework/db/pgsql/QueryBuilder.php
+++ b/framework/db/pgsql/QueryBuilder.php
@@ -136,8 +136,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
             $command .= "ALTER TABLE $tableName $enable TRIGGER ALL; ";
         }
 
-        #enable to have ability to alter several tables
-        $this->db->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
+        // enable to have ability to alter several tables
+        $this->db->getWritePdo()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
 
         return $command;
     }
diff --git a/framework/db/sqlite/QueryBuilder.php b/framework/db/sqlite/QueryBuilder.php
index 34ebb52..00217ff 100644
--- a/framework/db/sqlite/QueryBuilder.php
+++ b/framework/db/sqlite/QueryBuilder.php
@@ -120,7 +120,10 @@ class QueryBuilder extends \yii\db\QueryBuilder
             if ($value === null) {
                 $key = reset($table->primaryKey);
                 $tableName = $db->quoteTableName($tableName);
+                $enableSlave = $this->db->enableSlave;
+                $this->db->enableSlave = false;
                 $value = $db->createCommand("SELECT MAX('$key') FROM $tableName")->queryScalar();
+                $this->db->enableSlave = $enableSlave;
             } else {
                 $value = (int) $value - 1;
             }
--
libgit2 0.27.1