Skip to content

Commit 877fc8a

Browse files
authored
Merge branch 'master' into fix-94
2 parents d099553 + 0fc2b6c commit 877fc8a

File tree

5 files changed

+227
-119
lines changed

5 files changed

+227
-119
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@
5454
- Enh #383: Refactor `TableSchema` and `Schema` classes (@Tigrov)
5555
- Enh #386: Support column's collation (@Tigrov)
5656
- New #391: Add `Connection::getColumnBuilderClass()` method (@Tigrov)
57-
- New #390: Implement `ArrayMergeBuilder`, `GreatestBuilder`, `LeastBuilder`, `LengthBuilder`, `LongestBuilder`
57+
- New #390, #396: Implement `ArrayMergeBuilder`, `GreatestBuilder`, `LeastBuilder`, `LengthBuilder`, `LongestBuilder`
5858
and `ShortestBuilder` classes (@Tigrov)
59+
- Enh #393: Refactor `DMLQueryBuilder::upsert()` method (@Tigrov)
5960

6061
## 1.2.0 March 21, 2024
6162

src/Builder/ArrayMergeBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ protected function buildFromExpression(MultiOperandFunction $expression, array &
4242
}
4343

4444
$unions = implode(' UNION ', $selects);
45+
$orderBy = $expression->getOrdered() ? ' WITHIN GROUP (ORDER BY value)' : '';
4546

4647
return <<<SQL
47-
(SELECT '[' + STRING_AGG('"' + STRING_ESCAPE(value, 'json') + '"', ',') + ']' AS value FROM ($unions) AS t)
48+
(SELECT '[' + STRING_AGG('"' + STRING_ESCAPE(value, 'json') + '"', ',')$orderBy + ']' AS value FROM ($unions) AS t)
4849
SQL;
4950
}
5051
}

src/DMLQueryBuilder.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use InvalidArgumentException;
1010
use Yiisoft\Db\Exception\InvalidConfigException;
1111
use Yiisoft\Db\Exception\NotSupportedException;
12-
use Yiisoft\Db\Expression\Expression;
1312
use Yiisoft\Db\Query\QueryInterface;
1413
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder;
1514

@@ -213,7 +212,7 @@ private function prepareUpsertParts(
213212

214213
foreach ($columnNames as $name) {
215214
$quotedName = $this->quoter->quoteColumnName($name);
216-
$constraintCondition[] = "$quotedTableName.$quotedName=[EXCLUDED].$quotedName";
215+
$constraintCondition[] = "$quotedTableName.$quotedName=EXCLUDED.$quotedName";
217216
}
218217

219218
$onCondition[] = $constraintCondition;
@@ -227,18 +226,18 @@ private function prepareUpsertParts(
227226

228227
$mergeSql = 'MERGE ' . $quotedTableName . ' WITH (HOLDLOCK) USING ('
229228
. (!empty($placeholders) ? 'VALUES (' . implode(', ', $placeholders) . ')' : $values)
230-
. ') AS [EXCLUDED] (' . implode(', ', $quotedInsertNames) . ') ' . "ON ($on)";
229+
. ') AS EXCLUDED (' . implode(', ', $quotedInsertNames) . ') ' . "ON ($on)";
231230

232231
$insertValues = [];
233232

234233
foreach ($quotedInsertNames as $quotedName) {
235-
$insertValues[] = '[EXCLUDED].' . $quotedName;
234+
$insertValues[] = 'EXCLUDED.' . $quotedName;
236235
}
237236

238237
$insertSql = 'INSERT (' . implode(', ', $quotedInsertNames) . ')'
239238
. ' VALUES (' . implode(', ', $insertValues) . ')';
240239

241-
if ($updateColumns === false || $updateNames === []) {
240+
if (empty($updateColumns) || $updateNames === []) {
242241
/** there are no columns to update */
243242
return [
244243
$mergeSql,
@@ -247,16 +246,7 @@ private function prepareUpsertParts(
247246
];
248247
}
249248

250-
if ($updateColumns === true) {
251-
$updateColumns = [];
252-
253-
/** @psalm-var string[] $updateNames */
254-
foreach ($updateNames as $name) {
255-
$updateColumns[$name] = new Expression('[EXCLUDED].' . $this->quoter->quoteColumnName($name));
256-
}
257-
}
258-
259-
$updates = $this->prepareUpdateSets($table, $updateColumns, $params);
249+
$updates = $this->prepareUpsertSets($table, $updateColumns, $updateNames, $params);
260250

261251
return [
262252
$mergeSql,

0 commit comments

Comments
 (0)