mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
Fixes #15194: Fixed yii\db\QueryBuilder::insert() to preserve passed params when building a INSERT INTO ... SELECT query for MSSQL, PostgreSQL and SQLite
This commit is contained in:
committed by
Alexander Makarov
parent
1453eeaa4a
commit
b7d5393a08
@ -6,6 +6,7 @@ Yii Framework 2 Change Log
|
|||||||
|
|
||||||
- Bug #14276: Fixed I18N format with dotted parameters (developeruz)
|
- Bug #14276: Fixed I18N format with dotted parameters (developeruz)
|
||||||
- Bug #14604: Fixed `yii\validators\CompareValidator` `compareAttribute` does not work if `compareAttribute` form ID has been changed (mikk150)
|
- Bug #14604: Fixed `yii\validators\CompareValidator` `compareAttribute` does not work if `compareAttribute` form ID has been changed (mikk150)
|
||||||
|
- Bug #15194: Fixed `yii\db\QueryBuilder::insert()` to preserve passed params when building a `INSERT INTO ... SELECT` query for MSSQL, PostgreSQL and SQLite (sergeymakinen)
|
||||||
- Enh #15135: Automatic completion for help in bash and zsh (Valkeru)
|
- Enh #15135: Automatic completion for help in bash and zsh (Valkeru)
|
||||||
- Enh #14662: Added support for custom `Content-Type` specification to `yii\web\JsonResponseFormatter` (Kolyunya)
|
- Enh #14662: Added support for custom `Content-Type` specification to `yii\web\JsonResponseFormatter` (Kolyunya)
|
||||||
- Enh #14568: Refactored migration templates to use `safeUp()` and `safeDown()` methods (Kolyunya)
|
- Enh #14568: Refactored migration templates to use `safeUp()` and `safeDown()` methods (Kolyunya)
|
||||||
|
|||||||
@ -179,7 +179,7 @@ class QueryBuilder extends \yii\base\BaseObject
|
|||||||
$placeholders = [];
|
$placeholders = [];
|
||||||
$values = ' DEFAULT VALUES';
|
$values = ' DEFAULT VALUES';
|
||||||
if ($columns instanceof \yii\db\Query) {
|
if ($columns instanceof \yii\db\Query) {
|
||||||
list($names, $values, $params) = $this->prepareInsertSelectSubQuery($columns, $schema);
|
list($names, $values, $params) = $this->prepareInsertSelectSubQuery($columns, $schema, $params);
|
||||||
} else {
|
} else {
|
||||||
foreach ($columns as $name => $value) {
|
foreach ($columns as $name => $value) {
|
||||||
$names[] = $schema->quoteColumnName($name);
|
$names[] = $schema->quoteColumnName($name);
|
||||||
|
|||||||
@ -1777,6 +1777,7 @@ abstract class QueryBuilderTest extends DatabaseTestCase
|
|||||||
'is_active' => false,
|
'is_active' => false,
|
||||||
'related_id' => null,
|
'related_id' => null,
|
||||||
],
|
],
|
||||||
|
[],
|
||||||
$this->replaceQuotes('INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]]) VALUES (:qp0, :qp1, :qp2, :qp3, :qp4)'),
|
$this->replaceQuotes('INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]]) VALUES (:qp0, :qp1, :qp2, :qp3, :qp4)'),
|
||||||
[
|
[
|
||||||
':qp0' => 'test@example.com',
|
':qp0' => 'test@example.com',
|
||||||
@ -1792,11 +1793,64 @@ abstract class QueryBuilderTest extends DatabaseTestCase
|
|||||||
'{{%type}}.[[related_id]]' => null,
|
'{{%type}}.[[related_id]]' => null,
|
||||||
'[[time]]' => new Expression('now()'),
|
'[[time]]' => new Expression('now()'),
|
||||||
],
|
],
|
||||||
|
[],
|
||||||
'INSERT INTO {{%type}} ({{%type}}.[[related_id]], [[time]]) VALUES (:qp0, now())',
|
'INSERT INTO {{%type}} ({{%type}}.[[related_id]], [[time]]) VALUES (:qp0, now())',
|
||||||
[
|
[
|
||||||
':qp0' => null,
|
':qp0' => null,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
'carry passed params' => [
|
||||||
|
'customer',
|
||||||
|
[
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
'name' => 'sergeymakinen',
|
||||||
|
'address' => '{{city}}',
|
||||||
|
'is_active' => false,
|
||||||
|
'related_id' => null,
|
||||||
|
'col' => new Expression('CONCAT(:phFoo, :phBar)', [':phFoo' => 'foo']),
|
||||||
|
],
|
||||||
|
[':phBar' => 'bar'],
|
||||||
|
$this->replaceQuotes('INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]], [[col]]) VALUES (:qp1, :qp2, :qp3, :qp4, :qp5, CONCAT(:phFoo, :phBar))'),
|
||||||
|
[
|
||||||
|
':phBar' => 'bar',
|
||||||
|
':qp1' => 'test@example.com',
|
||||||
|
':qp2' => 'sergeymakinen',
|
||||||
|
':qp3' => '{{city}}',
|
||||||
|
':qp4' => false,
|
||||||
|
':qp5' => null,
|
||||||
|
':phFoo' => 'foo',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'carry passed params (query)' => [
|
||||||
|
'customer',
|
||||||
|
(new Query())
|
||||||
|
->select([
|
||||||
|
'email',
|
||||||
|
'name',
|
||||||
|
'address',
|
||||||
|
'is_active',
|
||||||
|
'related_id',
|
||||||
|
])
|
||||||
|
->from('customer')
|
||||||
|
->where([
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
'name' => 'sergeymakinen',
|
||||||
|
'address' => '{{city}}',
|
||||||
|
'is_active' => false,
|
||||||
|
'related_id' => null,
|
||||||
|
'col' => new Expression('CONCAT(:phFoo, :phBar)', [':phFoo' => 'foo']),
|
||||||
|
]),
|
||||||
|
[':phBar' => 'bar'],
|
||||||
|
$this->replaceQuotes('INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]]) SELECT [[email]], [[name]], [[address]], [[is_active]], [[related_id]] FROM [[customer]] WHERE ([[email]]=:qp1) AND ([[name]]=:qp2) AND ([[address]]=:qp3) AND ([[is_active]]=:qp4) AND ([[related_id]] IS NULL) AND ([[col]]=CONCAT(:phFoo, :phBar))'),
|
||||||
|
[
|
||||||
|
':phBar' => 'bar',
|
||||||
|
':qp1' => 'test@example.com',
|
||||||
|
':qp2' => 'sergeymakinen',
|
||||||
|
':qp3' => '{{city}}',
|
||||||
|
':qp4' => false,
|
||||||
|
':phFoo' => 'foo',
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1804,12 +1858,13 @@ abstract class QueryBuilderTest extends DatabaseTestCase
|
|||||||
* @dataProvider insertProvider
|
* @dataProvider insertProvider
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param array $columns
|
* @param array $columns
|
||||||
|
* @param array $params
|
||||||
* @param string $expectedSQL
|
* @param string $expectedSQL
|
||||||
* @param array $expectedParams
|
* @param array $expectedParams
|
||||||
*/
|
*/
|
||||||
public function testInsert($table, $columns, $expectedSQL, $expectedParams)
|
public function testInsert($table, $columns, $params, $expectedSQL, $expectedParams)
|
||||||
{
|
{
|
||||||
$actualParams = [];
|
$actualParams = $params;
|
||||||
$actualSQL = $this->getQueryBuilder()->insert($table, $columns, $actualParams);
|
$actualSQL = $this->getQueryBuilder()->insert($table, $columns, $actualParams);
|
||||||
$this->assertSame($expectedSQL, $actualSQL);
|
$this->assertSame($expectedSQL, $actualSQL);
|
||||||
$this->assertSame($expectedParams, $actualParams);
|
$this->assertSame($expectedParams, $actualParams);
|
||||||
|
|||||||
Reference in New Issue
Block a user