Skip to content

Commit 9c4bd16

Browse files
authored
Update according changes in db package (#442)
1 parent a943ba0 commit 9c4bd16

4 files changed

Lines changed: 7 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
- Enh #433: Support column's collation (@Tigrov)
5353
- New #440: Add `Connection::getColumnBuilderClass()` method (@Tigrov)
5454
- New #439: Implement `ArrayMergeBuilder` class (@Tigrov)
55+
- Enh #442: Refactor `DMLQueryBuilder::upsert()` method (@Tigrov)
5556

5657
## 1.3.0 March 21, 2024
5758

src/DMLQueryBuilder.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Yiisoft\Db\Pgsql;
66

77
use InvalidArgumentException;
8-
use Yiisoft\Db\Expression\Expression;
98
use Yiisoft\Db\Query\QueryInterface;
109
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder;
1110

@@ -75,24 +74,13 @@ public function upsert(
7574
return $insertSql;
7675
}
7776

78-
if ($updateColumns === false || $updateNames === []) {
77+
if (empty($updateColumns) || $updateNames === []) {
7978
/** there are no columns to update */
8079
return "$insertSql ON CONFLICT DO NOTHING";
8180
}
8281

83-
if ($updateColumns === true) {
84-
$updateColumns = [];
85-
86-
/** @psalm-var string[] $updateNames */
87-
foreach ($updateNames as $name) {
88-
$updateColumns[$name] = new Expression(
89-
'EXCLUDED.' . $this->quoter->quoteColumnName($name)
90-
);
91-
}
92-
}
93-
9482
$quotedUniqueNames = array_map($this->quoter->quoteColumnName(...), $uniqueNames);
95-
$updates = $this->prepareUpdateSets($table, $updateColumns, $params);
83+
$updates = $this->prepareUpsertSets($table, $updateColumns, $updateNames, $params);
9684

9785
return $insertSql
9886
. ' ON CONFLICT (' . implode(', ', $quotedUniqueNames) . ')'

tests/Provider/QueryBuilderProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public static function upsert(): array
236236
2 => ['address' => 'foo {{city}}', 'status' => 2, 'orders' => new Expression('"T_upsert"."orders" + 1')],
237237
3 => 'INSERT INTO "T_upsert" ("email", "address", "status", "profile_id") ' .
238238
'VALUES (:qp0, :qp1, :qp2, :qp3) ON CONFLICT ("email") ' .
239-
'DO UPDATE SET "address"=:qp4, "status"=:qp5, "orders"="T_upsert"."orders" + 1',
239+
'DO UPDATE SET "address"=:qp4, "status"=2, "orders"="T_upsert"."orders" + 1',
240240
],
241241
'regular values without update part' => [
242242
3 => 'INSERT INTO "T_upsert" ("email", "address", "status", "profile_id") ' .
@@ -249,7 +249,7 @@ public static function upsert(): array
249249
'query with update part' => [
250250
2 => ['address' => 'foo {{city}}', 'status' => 2, 'orders' => new Expression('"T_upsert"."orders" + 1')],
251251
3 => 'INSERT INTO "T_upsert" ("email", "status") SELECT "email", 2 AS "status" FROM "customer" ' .
252-
'WHERE "name" = :qp0 LIMIT 1 ON CONFLICT ("email") DO UPDATE SET "address"=:qp1, "status"=:qp2, "orders"="T_upsert"."orders" + 1',
252+
'WHERE "name" = :qp0 LIMIT 1 ON CONFLICT ("email") DO UPDATE SET "address"=:qp1, "status"=2, "orders"="T_upsert"."orders" + 1',
253253
],
254254
'query without update part' => [
255255
3 => 'INSERT INTO "T_upsert" ("email", "status") SELECT "email", 2 AS "status" FROM "customer" ' .
@@ -280,7 +280,7 @@ public static function upsert(): array
280280
),
281281
2 => ['ts' => 0, '[[orders]]' => new Expression('EXCLUDED.orders + 1')],
282282
3 => 'INSERT INTO {{%T_upsert}} ("email", [[ts]]) SELECT :phEmail AS "email", extract(epoch from now()) * 1000 AS [[ts]] ' .
283-
'ON CONFLICT ("email") DO UPDATE SET "ts"=:qp1, "orders"=EXCLUDED.orders + 1',
283+
'ON CONFLICT ("email") DO UPDATE SET "ts"=0, "orders"=EXCLUDED.orders + 1',
284284
],
285285
'query, values and expressions without update part' => [
286286
1 => (new Query(self::getDb()))

tests/QueryBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ public function testUpdate(
429429
array|string $condition,
430430
array $params,
431431
string $expectedSql,
432-
array $expectedParams,
432+
array $expectedParams = [],
433433
): void {
434434
parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams);
435435
}

0 commit comments

Comments
 (0)