mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-19 07:50:39 +08:00
moved getPDOType() back to Command to avoid dependency on Schema
fixes #854
This commit is contained in:
@@ -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;
|
||||
@@ -245,6 +245,25 @@ class Command extends \yii\base\Component
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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` (
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace yiiunit\framework\db\cubrid;
|
||||
|
||||
use yiiunit\data\ar\Customer;
|
||||
use yiiunit\framework\db\ActiveRecordTest;
|
||||
|
||||
/**
|
||||
@@ -10,4 +11,26 @@ use yiiunit\framework\db\ActiveRecordTest;
|
||||
class CubridActiveRecordTest extends ActiveRecordTest
|
||||
{
|
||||
public $driverName = 'cubrid';
|
||||
|
||||
/**
|
||||
* cubrid PDO does not support boolean values.
|
||||
* Make sure this does not affect AR layer.
|
||||
*/
|
||||
public function testBooleanAttribute()
|
||||
{
|
||||
$customer = new Customer();
|
||||
$customer->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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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\')';
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user