From e0cd52fb651c56d16259c518386d0a0a628b11c6 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 15 Jan 2013 18:32:08 -0500 Subject: [PATCH] moved Connection::getPdoType() to Command. --- framework/db/Command.php | 23 ++++++++++++++++++++--- framework/db/Connection.php | 17 ----------------- tests/unit/framework/db/ConnectionTest.php | 9 --------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/framework/db/Command.php b/framework/db/Command.php index d7817bf..7c83839 100644 --- a/framework/db/Command.php +++ b/framework/db/Command.php @@ -144,7 +144,7 @@ class Command extends \yii\base\Component { $this->prepare(); if ($dataType === null) { - $this->pdoStatement->bindParam($name, $value, $this->connection->getPdoType(gettype($value))); + $this->pdoStatement->bindParam($name, $value, $this->getPdoType(gettype($value))); } elseif ($length === null) { $this->pdoStatement->bindParam($name, $value, $dataType); } elseif ($driverOptions === null) { @@ -171,7 +171,7 @@ class Command extends \yii\base\Component { $this->prepare(); if ($dataType === null) { - $this->pdoStatement->bindValue($name, $value, $this->connection->getPdoType(gettype($value))); + $this->pdoStatement->bindValue($name, $value, $this->getPdoType(gettype($value))); } else { $this->pdoStatement->bindValue($name, $value, $dataType); } @@ -199,7 +199,7 @@ class Command extends \yii\base\Component $type = $value[1]; $value = $value[0]; } else { - $type = $this->connection->getPdoType(gettype($value)); + $type = $this->getPdoType(gettype($value)); } $this->pdoStatement->bindValue($name, $value, $type); $this->_params[$name] = $value; @@ -209,6 +209,23 @@ class Command extends \yii\base\Component } /** + * Determines the PDO type for the give PHP data type. + * @param string $type The PHP type (obtained by `gettype()` call). + * @return integer the corresponding PDO type + * @see http://www.php.net/manual/en/pdo.constants.php + */ + private function getPdoType($type) + { + static $typeMap = array( + 'boolean' => \PDO::PARAM_BOOL, + 'integer' => \PDO::PARAM_INT, + 'string' => \PDO::PARAM_STR, + 'NULL' => \PDO::PARAM_NULL, + ); + return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR; + } + + /** * Executes the SQL statement. * This method should only be used for executing non-query SQL statement, such as `INSERT`, `DELETE`, `UPDATE` SQLs. * No result set will be returned. diff --git a/framework/db/Connection.php b/framework/db/Connection.php index 0136437..0b04b2a 100644 --- a/framework/db/Connection.php +++ b/framework/db/Connection.php @@ -550,23 +550,6 @@ class Connection extends \yii\base\ApplicationComponent } /** - * Determines the PDO type for the give PHP data type. - * @param string $type The PHP type (obtained by `gettype()` call). - * @return integer the corresponding PDO type - * @see http://www.php.net/manual/en/pdo.constants.php - */ - public function getPdoType($type) - { - static $typeMap = array( - 'boolean' => \PDO::PARAM_BOOL, - 'integer' => \PDO::PARAM_INT, - 'string' => \PDO::PARAM_STR, - 'NULL' => \PDO::PARAM_NULL, - ); - return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR; - } - - /** * Returns the name of the DB driver for the current [[dsn]]. * @return string name of the DB driver */ diff --git a/tests/unit/framework/db/ConnectionTest.php b/tests/unit/framework/db/ConnectionTest.php index e035eb2..907dda6 100644 --- a/tests/unit/framework/db/ConnectionTest.php +++ b/tests/unit/framework/db/ConnectionTest.php @@ -76,15 +76,6 @@ class ConnectionTest extends \yiiunit\MysqlTestCase $this->assertEquals('(column)', $connection->quoteColumnName('(column)')); } - function testGetPdoType() - { - $connection = $this->getConnection(false); - $this->assertEquals(\PDO::PARAM_BOOL, $connection->getPdoType('boolean')); - $this->assertEquals(\PDO::PARAM_INT, $connection->getPdoType('integer')); - $this->assertEquals(\PDO::PARAM_STR, $connection->getPdoType('string')); - $this->assertEquals(\PDO::PARAM_NULL, $connection->getPdoType('NULL')); - } - function testAttribute() {