diff --git a/CHANGELOG.md b/CHANGELOG.md index 99d2ad24..d30b40d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ - Enh #380: Support column's collation (@Tigrov) - New #385: Add `Connection::getColumnBuilderClass()` method (@Tigrov) - New #384: Implement `ArrayMergeBuilder`, `GreatestBuilder` and `LeastBuilder` classes (@Tigrov) +- Enh #387: Refactor `DMLQueryBuilder::upsert()` method (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/src/DMLQueryBuilder.php b/src/DMLQueryBuilder.php index 429fa2c0..b19eb9ee 100644 --- a/src/DMLQueryBuilder.php +++ b/src/DMLQueryBuilder.php @@ -5,7 +5,6 @@ namespace Yiisoft\Db\Sqlite; use InvalidArgumentException; -use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder; @@ -73,24 +72,14 @@ public function upsert( return $insertSql; } - if ($updateColumns === false || $updateNames === []) { + if (empty($updateColumns) || $updateNames === []) { /** there are no columns to update */ return "$insertSql ON CONFLICT DO NOTHING"; } - if ($updateColumns === true) { - $updateColumns = []; - - /** @psalm-var string[] $updateNames */ - foreach ($updateNames as $name) { - $updateColumns[$name] = new Expression( - 'EXCLUDED.' . $this->quoter->quoteColumnName($name) - ); - } - } - $quotedUniqueNames = array_map($this->quoter->quoteColumnName(...), $uniqueNames); - $updates = $this->prepareUpdateSets($table, $updateColumns, $params); + /** @psalm-suppress PossiblyInvalidArgument */ + $updates = $this->prepareUpsertSets($table, $updateColumns, $updateNames, $params); return $insertSql . ' ON CONFLICT (' . implode(', ', $quotedUniqueNames) . ')' diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 65198551..6705c21b 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -226,7 +226,7 @@ public static function upsert(): array ], 'regular values with update part' => [ 3 => << [ @@ -241,7 +241,7 @@ public static function upsert(): array ], 'query with update part' => [ 3 => << [ @@ -266,7 +266,7 @@ public static function upsert(): array ], 'query, values and expressions with update part' => [ 3 => << [ diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 0aa14801..077dc9ca 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -601,7 +601,7 @@ public function testUpdate( array|string $condition, array $params, string $expectedSql, - array $expectedParams, + array $expectedParams = [], ): void { parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams); }