Skip to content

Commit 5355261

Browse files
authored
Fix insert from query with prefixed selected columns (#1084)
1 parent 3e017bb commit 5355261

4 files changed

Lines changed: 12 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
- New #1029, #1048, #1069: Add functions as expressions (@Tigrov)
140140
- Enh #1038: Add ability to pass `FROM` clause to `CommandInterface::update()` and `DMLQueryBuilderInterface::update()` methods (@rustamwin)
141141
- Enh #1038: Allow passing `ExpressionInterface` as condition in `CommandInterface::update()` and `DMLQueryBuilderInterface::update()` methods (@rustamwin)
142-
- Enh #1042: Refactor `AbstractDMLQueryBuilder` class to `upsert()` method (@Tigrov)
142+
- Enh #1042, #1084: Refactor `AbstractDMLQueryBuilder` class to `upsert()` method (@Tigrov)
143143
- New #1040, #1043: Add `DateTimeValue` class (@vjik, @Tigrov)
144144
- Enh #1045: Support multi-operand functions in `CommandInterface::upsert()` and `DMLQueryBuilderInterface::upsert()`
145145
methods (@Tigrov)

src/QueryBuilder/AbstractDMLQueryBuilder.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ protected function prepareBatchInsertValues(string $table, iterable $rows, array
279279
*/
280280
protected function extractColumnNames(array|Iterator $rows, array $columns): array
281281
{
282-
$columns = $this->getNormalizeColumnNames($columns);
282+
$columns = $this->getNormalizedColumnNames($columns);
283283

284284
if (!empty($columns)) {
285285
return $columns;
@@ -312,7 +312,7 @@ protected function extractColumnNames(array|Iterator $rows, array $columns): arr
312312
/**
313313
* Prepare select-subQuery and field names for `INSERT INTO ... SELECT` SQL statement.
314314
*
315-
* @param QueryInterface $columns Object, which represents a select query.
315+
* @param QueryInterface $query Object, which represents a select query.
316316
* @param array $params The parameters to bind to the generated SQL statement. These parameters will be included
317317
* in the result, with the more parameters generated during the query building process.
318318
*
@@ -325,10 +325,10 @@ protected function extractColumnNames(array|Iterator $rows, array $columns): arr
325325
*
326326
* @psalm-param ParamsType $params
327327
*/
328-
protected function getQueryColumnNames(QueryInterface $columns, array &$params = []): array
328+
protected function getQueryColumnNames(QueryInterface $query, array &$params = []): array
329329
{
330330
/** @psalm-var string[] $select */
331-
$select = $columns->getSelect();
331+
$select = $query->getSelect();
332332

333333
if (empty($select) || in_array('*', $select, true)) {
334334
throw new InvalidArgumentException('Expected select query object with enumerated (named) parameters');
@@ -352,7 +352,7 @@ protected function getQueryColumnNames(QueryInterface $columns, array &$params =
352352
}
353353
}
354354

355-
return $names;
355+
return $this->getNormalizedColumnNames($names);
356356
}
357357

358358
/**
@@ -504,10 +504,9 @@ protected function prepareUpsertColumns(
504504
if ($insertColumns instanceof QueryInterface) {
505505
$insertNames = $this->getQueryColumnNames($insertColumns);
506506
} else {
507-
$insertNames = array_keys($insertColumns);
507+
$insertNames = $this->getNormalizedColumnNames(array_keys($insertColumns));
508508
}
509509

510-
$insertNames = $this->getNormalizeColumnNames($insertNames);
511510
$uniqueNames = $this->getTableUniqueColumnNames($table, $insertNames, $constraints);
512511

513512
if ($updateColumns === true) {
@@ -571,7 +570,7 @@ protected function normalizeColumnNames(array $columns): array
571570
{
572571
/** @var string[] $columnNames */
573572
$columnNames = array_keys($columns);
574-
$normalizedNames = $this->getNormalizeColumnNames($columnNames);
573+
$normalizedNames = $this->getNormalizedColumnNames($columnNames);
575574

576575
return array_combine($normalizedNames, $columns);
577576
}
@@ -583,7 +582,7 @@ protected function normalizeColumnNames(array $columns): array
583582
*
584583
* @return string[] Normalized column names.
585584
*/
586-
protected function getNormalizeColumnNames(array $columns): array
585+
protected function getNormalizedColumnNames(array $columns): array
587586
{
588587
foreach ($columns as &$name) {
589588
$name = $this->quoter->ensureColumnName($name);

tests/AbstractQueryBuilderTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,14 +1944,7 @@ public function testGetExpressionBuilder(): void
19441944
);
19451945
}
19461946

1947-
/**
1948-
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::insert
1949-
*
1950-
* @throws Exception
1951-
* @throws InvalidConfigException
1952-
* @throws InvalidArgumentException
1953-
* @throws NotSupportedException
1954-
*/
1947+
#[DataProviderExternal(QueryBuilderProvider::class, 'insert')]
19551948
public function testInsert(
19561949
string $table,
19571950
array|QueryInterface $columns,

tests/Provider/QueryBuilderProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ public static function insert(): array
11031103
'carry passed params (query)' => [
11041104
'customer',
11051105
(new Query(static::getDb()))
1106-
->select(['email', 'name', 'address', 'is_active', 'related_id'])
1106+
->select(['email', 'customer.name', 'address', 'is_active', 'related_id'])
11071107
->from('customer')
11081108
->where(
11091109
[
@@ -1118,7 +1118,7 @@ public static function insert(): array
11181118
[':phBar' => 'bar'],
11191119
static::replaceQuotes(
11201120
<<<SQL
1121-
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]] = FALSE) AND ([[related_id]] IS NULL) AND ([[col]] = CONCAT(:phFoo, :phBar))
1121+
INSERT INTO [[customer]] ([[email]], [[name]], [[address]], [[is_active]], [[related_id]]) SELECT [[email]], [[customer]].[[name]], [[address]], [[is_active]], [[related_id]] FROM [[customer]] WHERE ([[email]] = :qp1) AND ([[name]] = :qp2) AND ([[address]] = :qp3) AND ([[is_active]] = FALSE) AND ([[related_id]] IS NULL) AND ([[col]] = CONCAT(:phFoo, :phBar))
11221122
SQL
11231123
),
11241124
[

0 commit comments

Comments
 (0)