Commit 92d65ab7 by Carsten Brandt

fix issue with postgreSQL and batch inserting boolean values

fixes #4654
parent f1dd83e2
......@@ -80,6 +80,7 @@ Yii Framework 2 Change Log
- Bug #4514: Fixed Request class crashing when empty CSRF token value is sent in cookie (cebe)
- Bug #4519: `yii\base\Model::isAttributeRequired()` should check if the `when` option of the validator is set (thiagotalma)
- Bug #4592: Fixed `yii help` command was listing incorrect action names for methods like `actionSayNO` (samdark)
- Bug #4654: Fixed issue with PostgreSQL and inserting boolean values with batch insert (cebe)
- Bug: Fixed inconsistent return of `\yii\console\Application::runAction()` (samdark)
- Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul)
- Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue)
......
......@@ -84,11 +84,13 @@ abstract class Schema extends Object
*/
private $_builder;
/**
* @return \yii\db\ColumnSchema
* @throws \yii\base\InvalidConfigException
*/
protected function createColumnSchema() {
protected function createColumnSchema()
{
return Yii::createObject('yii\db\ColumnSchema');
}
......
......@@ -159,4 +159,44 @@ class QueryBuilder extends \yii\db\QueryBuilder
. $this->db->quoteColumnName($column) . ' TYPE '
. $this->getColumnType($type);
}
/**
* @inheritdoc
*/
public function batchInsert($table, $columns, $rows)
{
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
}
$values = [];
foreach ($rows as $row) {
$vs = [];
foreach ($row as $i => $value) {
if (!is_array($value) && isset($columnSchemas[$columns[$i]])) {
$value = $columnSchemas[$columns[$i]]->dbTypecast($value);
}
if (is_string($value)) {
$value = $this->db->quoteValue($value);
} elseif ($value === true) {
$value = 'TRUE';
} elseif ($value === false) {
$value = 'FALSE';
} elseif ($value === null) {
$value = 'NULL';
}
$vs[] = $value;
}
$values[] = '(' . implode(', ', $vs) . ')';
}
foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name);
}
return 'INSERT INTO ' . $this->db->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
}
}
......@@ -20,11 +20,27 @@ class PostgreSQLCommandTest extends CommandTest
$this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql);
}
public function testBatchInsert()
public function testBooleanValuesInsert()
{
parent::testBatchInsert();
$db = $this->getConnection();
$command = $db->createCommand();
$command->insert('bool_values', ['bool_col' => true]);
$this->assertEquals(1, $command->execute());
$command = $this->getConnection()->createCommand();
$command = $db->createCommand();
$command->insert('bool_values', ['bool_col' => false]);
$this->assertEquals(1, $command->execute());
$command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = TRUE;');
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = FALSE;');
$this->assertEquals(1, $command->queryScalar());
}
public function testBooleanValuesBatchInsert()
{
$db = $this->getConnection();
$command = $db->createCommand();
$command->batchInsert('bool_values',
['bool_col'], [
[true],
......@@ -32,5 +48,10 @@ class PostgreSQLCommandTest extends CommandTest
]
);
$this->assertEquals(2, $command->execute());
$command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = TRUE;');
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = FALSE;');
$this->assertEquals(1, $command->queryScalar());
}
}
\ No newline at end of file
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