Skip to content

Commit 5427028

Browse files
authored
Support table name in update columns (#1065)
1 parent 488894b commit 5427028

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
- Chg #1063: `AbstractConnection::createBatchQueryResult()` passes parameters `indexBy` and `resultCallback` to
158158
`BatchQueryResult` being created (@vjik)
159159
- Enh #1064: Remove duplicate code in favor of the `DbArrayHelper::normalizeExpressions()` method (@rustamwin)
160+
- Enh #1065: Allow to use table name as a column prefix in update queries (@Tigrov)
160161

161162
## 1.3.0 March 21, 2024
162163

src/QueryBuilder/AbstractDMLQueryBuilder.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,27 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu
406406
*
407407
* @return string[]
408408
*/
409-
protected function prepareUpdateSets(string $table, array $columns, array &$params, bool $forUpsert = false): array
410-
{
409+
protected function prepareUpdateSets(
410+
string $table,
411+
array $columns,
412+
array &$params,
413+
bool $forUpsert = false,
414+
bool $useTableName = false,
415+
): array {
411416
$sets = [];
412417
$columns = $this->normalizeColumnNames($columns);
413418
$tableColumns = $this->schema->getTableSchema($table)?->getColumns() ?? [];
414419
$typecastColumns = $this->typecasting ? $tableColumns : [];
415420
$queryBuilder = $this->queryBuilder;
416421
$quoter = $this->quoter;
417422

423+
if ($useTableName) {
424+
$quotedTableName = $quoter->quoteTableName($table);
425+
$columnPrefix = "$quotedTableName.";
426+
} else {
427+
$columnPrefix = '';
428+
}
429+
418430
foreach ($columns as $name => $value) {
419431
if (isset($typecastColumns[$name])) {
420432
$value = $typecastColumns[$name]->dbTypecast($value);
@@ -436,7 +448,7 @@ protected function prepareUpdateSets(string $table, array $columns, array &$para
436448
$builtValue = $queryBuilder->buildValue($value, $params);
437449
}
438450

439-
$sets[] = "$quotedName=$builtValue";
451+
$sets[] = "$columnPrefix$quotedName=$builtValue";
440452
}
441453

442454
return $sets;

tests/Provider/QueryBuilderProvider.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ public static function update(): array
12331233
':qp0' => new Param('{{test}}', DataType::STRING),
12341234
],
12351235
],
1236-
[
1236+
'from table' => [
12371237
'{{table}}',
12381238
['name' => '{{tmp}}.{{name}}'],
12391239
[],
@@ -1248,7 +1248,7 @@ public static function update(): array
12481248
':qp0' => new Param('{{tmp}}.{{name}}', DataType::STRING),
12491249
],
12501250
],
1251-
[
1251+
'from array' => [
12521252
'{{table}}',
12531253
['name' => '{{tmp}}.{{name}}'],
12541254
[],
@@ -1263,7 +1263,7 @@ public static function update(): array
12631263
':qp0' => new Param('{{tmp}}.{{name}}', DataType::STRING),
12641264
],
12651265
],
1266-
[
1266+
'from table w/ condition' => [
12671267
'{{table}}',
12681268
['name' => '{{tmp}}.{{name}}'],
12691269
['id' => 1],
@@ -1278,7 +1278,7 @@ public static function update(): array
12781278
':qp0' => new Param('{{tmp}}.{{name}}', DataType::STRING),
12791279
],
12801280
],
1281-
[
1281+
'from expression' => [
12821282
'{{table}}',
12831283
['name' => '{{tmp}}.{{name}}'],
12841284
[],
@@ -1293,7 +1293,7 @@ public static function update(): array
12931293
':qp0' => new Param('{{tmp}}.{{name}}', DataType::STRING),
12941294
],
12951295
],
1296-
[
1296+
'from query' => [
12971297
'{{table}}',
12981298
['name' => '{{tmp}}.{{name}}'],
12991299
[],
@@ -1308,7 +1308,7 @@ public static function update(): array
13081308
':qp0' => new Param('{{tmp}}.{{name}}', DataType::STRING),
13091309
],
13101310
],
1311-
[
1311+
'from query w/ alias' => [
13121312
'{{table}}',
13131313
['name' => '{{tmp}}'],
13141314
[],

0 commit comments

Comments
 (0)