diff --git a/framework/yii/db/Command.php b/framework/yii/db/Command.php index 7f2d81d..bfb8a26 100644 --- a/framework/yii/db/Command.php +++ b/framework/yii/db/Command.php @@ -181,7 +181,7 @@ class Command extends \yii\base\Component { $this->prepare(); if ($dataType === null) { - $this->pdoStatement->bindParam($name, $value, $this->db->schema->getPdoType($value)); + $this->pdoStatement->bindParam($name, $value, $this->getPdoType($value)); } elseif ($length === null) { $this->pdoStatement->bindParam($name, $value, $dataType); } elseif ($driverOptions === null) { @@ -208,7 +208,7 @@ class Command extends \yii\base\Component { $this->prepare(); if ($dataType === null) { - $this->pdoStatement->bindValue($name, $value, $this->db->schema->getPdoType($value)); + $this->pdoStatement->bindValue($name, $value, $this->getPdoType($value)); } else { $this->pdoStatement->bindValue($name, $value, $dataType); } @@ -236,7 +236,7 @@ class Command extends \yii\base\Component $type = $value[1]; $value = $value[0]; } else { - $type = $this->db->schema->getPdoType($value); + $type = $this->getPdoType($value); } $this->pdoStatement->bindValue($name, $value, $type); $this->_params[$name] = $value; @@ -246,6 +246,25 @@ class Command extends \yii\base\Component } /** + * Determines the PDO type for the given PHP data value. + * @param mixed $data the data whose PDO type is to be determined + * @return integer the PDO type + * @see http://www.php.net/manual/en/pdo.constants.php + */ + private function getPdoType($data) + { + static $typeMap = array( // php type => PDO type + 'boolean' => \PDO::PARAM_BOOL, + 'integer' => \PDO::PARAM_INT, + 'string' => \PDO::PARAM_STR, + 'resource' => \PDO::PARAM_LOB, + 'NULL' => \PDO::PARAM_NULL, + ); + $type = gettype($data); + 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/yii/db/Schema.php b/framework/yii/db/Schema.php index 396e944..1d86616 100644 --- a/framework/yii/db/Schema.php +++ b/framework/yii/db/Schema.php @@ -377,23 +377,4 @@ abstract class Schema extends Object return 'string'; } } - - /** - * Determines the PDO type for the give PHP data value. - * @param mixed $data the data whose PDO type is to be determined - * @return integer the PDO type - * @see http://www.php.net/manual/en/pdo.constants.php - */ - public function getPdoType($data) - { - static $typeMap = array( // php type => PDO type - 'boolean' => \PDO::PARAM_BOOL, - 'integer' => \PDO::PARAM_INT, - 'string' => \PDO::PARAM_STR, - 'resource' => \PDO::PARAM_LOB, - 'NULL' => \PDO::PARAM_NULL, - ); - $type = gettype($data); - return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR; - } } diff --git a/framework/yii/db/cubrid/Schema.php b/framework/yii/db/cubrid/Schema.php index 99624f6..ba7fcae 100644 --- a/framework/yii/db/cubrid/Schema.php +++ b/framework/yii/db/cubrid/Schema.php @@ -237,23 +237,4 @@ class Schema extends \yii\db\Schema } return $tableNames; } - - /** - * Determines the PDO type for the give PHP data value. - * @param mixed $data the data whose PDO type is to be determined - * @return integer the PDO type - * @see http://www.php.net/manual/en/pdo.constants.php - */ - public function getPdoType($data) - { - static $typeMap = array( - 'boolean' => \PDO::PARAM_INT, // CUBRID PDO does not support PARAM_BOOL - 'integer' => \PDO::PARAM_INT, - 'string' => \PDO::PARAM_STR, - 'resource' => \PDO::PARAM_LOB, - 'NULL' => \PDO::PARAM_NULL, - ); - $type = gettype($data); - return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR; - } } diff --git a/tests/unit/data/cubrid.sql b/tests/unit/data/cubrid.sql index bfaf85c..3dcfa37 100644 --- a/tests/unit/data/cubrid.sql +++ b/tests/unit/data/cubrid.sql @@ -72,9 +72,7 @@ CREATE TABLE `tbl_type` ( `float_col2` double DEFAULT '1.23', `blob_col` blob, `numeric_col` decimal(5,2) DEFAULT '33.22', - `time` timestamp NOT NULL DEFAULT '2002-01-01 00:00:00', - `bool_col` smallint NOT NULL, - `bool_col2` smallint DEFAULT 1 + `time` timestamp NOT NULL DEFAULT '2002-01-01 00:00:00' ); CREATE TABLE `tbl_composite_fk` ( diff --git a/tests/unit/framework/db/CommandTest.php b/tests/unit/framework/db/CommandTest.php index 7b16c76..d9eb0e7 100644 --- a/tests/unit/framework/db/CommandTest.php +++ b/tests/unit/framework/db/CommandTest.php @@ -219,6 +219,29 @@ class CommandTest extends DatabaseTestCase $this->assertTrue(is_array($result) && isset($result[0])); } + // getPDOType is currently private +// public function testGetPDOType() +// { +// $values = array( +// array(null, \PDO::PARAM_NULL), +// array('', \PDO::PARAM_STR), +// array('hello', \PDO::PARAM_STR), +// array(0, \PDO::PARAM_INT), +// array(1, \PDO::PARAM_INT), +// array(1337, \PDO::PARAM_INT), +// array(true, \PDO::PARAM_BOOL), +// array(false, \PDO::PARAM_BOOL), +// array($fp=fopen(__FILE__, 'rb'), \PDO::PARAM_LOB), +// ); +// +// $command = $this->getConnection()->createCommand(); +// +// foreach($values as $value) { +// $this->assertEquals($value[1], $command->getPdoType($value[0])); +// } +// fclose($fp); +// } + public function testInsert() { } diff --git a/tests/unit/framework/db/SchemaTest.php b/tests/unit/framework/db/SchemaTest.php index dce6e20..2a3015d 100644 --- a/tests/unit/framework/db/SchemaTest.php +++ b/tests/unit/framework/db/SchemaTest.php @@ -11,29 +11,6 @@ use yii\db\Schema; */ class SchemaTest extends DatabaseTestCase { - - public function testGetPDOType() - { - $values = array( - array(null, \PDO::PARAM_NULL), - array('', \PDO::PARAM_STR), - array('hello', \PDO::PARAM_STR), - array(0, \PDO::PARAM_INT), - array(1, \PDO::PARAM_INT), - array(1337, \PDO::PARAM_INT), - array(true, \PDO::PARAM_BOOL), - array(false, \PDO::PARAM_BOOL), - array($fp=fopen(__FILE__, 'rb'), \PDO::PARAM_LOB), - ); - - $schema = $this->getConnection()->schema; - - foreach($values as $value) { - $this->assertEquals($value[1], $schema->getPdoType($value[0])); - } - fclose($fp); - } - public function testFindTableNames() { /** @var Schema $schema */ diff --git a/tests/unit/framework/db/cubrid/CubridActiveRecordTest.php b/tests/unit/framework/db/cubrid/CubridActiveRecordTest.php index dd48f44..2d2db15 100644 --- a/tests/unit/framework/db/cubrid/CubridActiveRecordTest.php +++ b/tests/unit/framework/db/cubrid/CubridActiveRecordTest.php @@ -1,6 +1,7 @@ name = 'boolean customer'; + $customer->email = 'mail@example.com'; + $customer->status = true; + $customer->save(false); + + $customer->refresh(); + $this->assertEquals(1, $customer->status); + + $customer->status = false; + $customer->save(false); + + $customer->refresh(); + $this->assertEquals(0, $customer->status); + } } diff --git a/tests/unit/framework/db/cubrid/CubridCommandTest.php b/tests/unit/framework/db/cubrid/CubridCommandTest.php index 895f548..45d3c1c 100644 --- a/tests/unit/framework/db/cubrid/CubridCommandTest.php +++ b/tests/unit/framework/db/cubrid/CubridCommandTest.php @@ -31,7 +31,7 @@ class CubridCommandTest extends CommandTest $command->bindParam(':email', $email); $this->assertEquals($name, $command->queryScalar()); - $sql = "INSERT INTO tbl_type (int_col, char_col, char_col2, enum_col, float_col, blob_col, numeric_col, bool_col, bool_col2) VALUES (:int_col, '', :char_col, :enum_col, :float_col, CHAR_TO_BLOB(:blob_col), :numeric_col, :bool_col, :bool_col2)"; + $sql = "INSERT INTO tbl_type (int_col, char_col, char_col2, enum_col, float_col, blob_col, numeric_col) VALUES (:int_col, '', :char_col, :enum_col, :float_col, CHAR_TO_BLOB(:blob_col), :numeric_col)"; $command = $db->createCommand($sql); $intCol = 123; $charCol = 'abc'; @@ -39,16 +39,12 @@ class CubridCommandTest extends CommandTest $floatCol = 1.23; $blobCol = "\x10\x11\x12"; $numericCol = '1.23'; - $boolCol = false; - $boolCol2 = true; $command->bindParam(':int_col', $intCol); $command->bindParam(':char_col', $charCol); $command->bindParam(':enum_col', $enumCol); $command->bindParam(':float_col', $floatCol); $command->bindParam(':blob_col', $blobCol); $command->bindParam(':numeric_col', $numericCol); - $command->bindParam(':bool_col', $boolCol); - $command->bindParam(':bool_col2', $boolCol2); $this->assertEquals(1, $command->execute()); $sql = 'SELECT * FROM tbl_type'; @@ -59,8 +55,6 @@ class CubridCommandTest extends CommandTest $this->assertEquals($floatCol, $row['float_col']); $this->assertEquals($blobCol, fread($row['blob_col'], 3)); $this->assertEquals($numericCol, $row['numeric_col']); - $this->assertEquals($boolCol, $row['bool_col']); - $this->assertEquals($boolCol2, $row['bool_col2']); // bindValue $sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')'; diff --git a/tests/unit/framework/db/cubrid/CubridSchemaTest.php b/tests/unit/framework/db/cubrid/CubridSchemaTest.php index 6a1f6a2..9a0c139 100644 --- a/tests/unit/framework/db/cubrid/CubridSchemaTest.php +++ b/tests/unit/framework/db/cubrid/CubridSchemaTest.php @@ -10,26 +10,4 @@ use yiiunit\framework\db\SchemaTest; class CubridSchemaTest extends SchemaTest { public $driverName = 'cubrid'; - - public function testGetPDOType() - { - $values = array( - null => \PDO::PARAM_NULL, - '' => \PDO::PARAM_STR, - 'hello' => \PDO::PARAM_STR, - 0 => \PDO::PARAM_INT, - 1 => \PDO::PARAM_INT, - 1337 => \PDO::PARAM_INT, - true => \PDO::PARAM_INT, // CUBRID PDO does not support PARAM_BOOL - false => \PDO::PARAM_INT, // CUBRID PDO does not support PARAM_BOOL - ); - - $schema = $this->getConnection()->schema; - - foreach($values as $value => $type) { - $this->assertEquals($type, $schema->getPdoType($value)); - } - $this->assertEquals(\PDO::PARAM_LOB, $schema->getPdoType($fp=fopen(__FILE__, 'rb'))); - fclose($fp); - } }