Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
- New #1029, #1048, #1069: Add functions as expressions (@Tigrov)
- Enh #1038: Add ability to pass `FROM` clause to `CommandInterface::update()` and `DMLQueryBuilderInterface::update()` methods (@rustamwin)
- Enh #1038: Allow passing `ExpressionInterface` as condition in `CommandInterface::update()` and `DMLQueryBuilderInterface::update()` methods (@rustamwin)
- Enh #1042: Refactor `AbstractDMLQueryBuilder` class to `upsert()` method (@Tigrov)
- Enh #1042, #1084: Refactor `AbstractDMLQueryBuilder` class to `upsert()` method (@Tigrov)
- New #1040, #1043: Add `DateTimeValue` class (@vjik, @Tigrov)
- Enh #1045: Support multi-operand functions in `CommandInterface::upsert()` and `DMLQueryBuilderInterface::upsert()`
methods (@Tigrov)
Expand Down
11 changes: 5 additions & 6 deletions src/QueryBuilder/AbstractDMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ protected function extractColumnNames(array|Iterator $rows, array $columns): arr
/**
* Prepare select-subQuery and field names for `INSERT INTO ... SELECT` SQL statement.
*
* @param QueryInterface $columns Object, which represents a select query.
* @param QueryInterface $query Object, which represents a select query.
* @param array $params The parameters to bind to the generated SQL statement. These parameters will be included
* in the result, with the more parameters generated during the query building process.
*
Expand All @@ -325,10 +325,10 @@ protected function extractColumnNames(array|Iterator $rows, array $columns): arr
*
* @psalm-param ParamsType $params
*/
protected function getQueryColumnNames(QueryInterface $columns, array &$params = []): array
protected function getQueryColumnNames(QueryInterface $query, array &$params = []): array
{
/** @psalm-var string[] $select */
$select = $columns->getSelect();
$select = $query->getSelect();

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

return $names;
return $this->getNormalizeColumnNames($names);
}

/**
Expand Down Expand Up @@ -504,10 +504,9 @@ protected function prepareUpsertColumns(
if ($insertColumns instanceof QueryInterface) {
$insertNames = $this->getQueryColumnNames($insertColumns);
} else {
$insertNames = array_keys($insertColumns);
$insertNames = $this->getNormalizeColumnNames(array_keys($insertColumns));
}

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

if ($updateColumns === true) {
Expand Down
9 changes: 1 addition & 8 deletions tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1944,14 +1944,7 @@ public function testGetExpressionBuilder(): void
);
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::insert
*
* @throws Exception
* @throws InvalidConfigException
* @throws InvalidArgumentException
* @throws NotSupportedException
*/
#[DataProviderExternal(QueryBuilderProvider::class, 'insert')]
public function testInsert(
string $table,
array|QueryInterface $columns,
Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ public static function insert(): array
'carry passed params (query)' => [
'customer',
(new Query(static::getDb()))
->select(['email', 'name', 'address', 'is_active', 'related_id'])
->select(['email', 'customer.name', 'address', 'is_active', 'related_id'])
->from('customer')
->where(
[
Expand All @@ -1118,7 +1118,7 @@ public static function insert(): array
[':phBar' => 'bar'],
static::replaceQuotes(
<<<SQL
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))
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))
SQL
),
[
Expand Down