Skip to content

Commit 6c5fc7b

Browse files
authored
Adapt to db changes (#422)
1 parent f2d31b3 commit 6c5fc7b

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
- New #420, #427: Implement `ArrayMergeBuilder`, `LongestBuilder` and `ShortestBuilder` classes (@Tigrov)
5454
- Enh #423: Refactor `DMLQueryBuilder::upsert()` method (@Tigrov)
5555
- Chg #428: Update expression namespaces according to changes in `yiisoft/db` package (@Tigrov)
56+
- Enh #442: Update `DMLQueryBuilder::update()` method to adapt changes in `yiisoft/db` (@rustamwin)
5657

5758
## 1.2.0 March 21, 2024
5859

src/DMLQueryBuilder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use InvalidArgumentException;
88
use Yiisoft\Db\Exception\NotSupportedException;
9+
use Yiisoft\Db\Expression\ExpressionInterface;
910
use Yiisoft\Db\Query\QueryInterface;
1011
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder;
1112
use Yiisoft\Db\Schema\TableSchema;
@@ -61,6 +62,19 @@ public function resetSequence(string $table, int|string|null $value = null): str
6162
EXECUTE autoincrement_stmt";
6263
}
6364

65+
public function update(
66+
string $table,
67+
array $columns,
68+
array|string|ExpressionInterface $condition,
69+
array|string|ExpressionInterface|null $from = null,
70+
array &$params = []
71+
): string {
72+
if ($from !== null) {
73+
throw new NotSupportedException('MySQL does not support FROM clause in UPDATE statement.');
74+
}
75+
return parent::update($table, $columns, $condition, null, $params);
76+
}
77+
6478
public function upsert(
6579
string $table,
6680
array|QueryInterface $insertColumns,

tests/CommandTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\Attributes\DataProviderExternal;
88
use Yiisoft\Db\Exception\NotSupportedException;
9+
use Yiisoft\Db\Expression\ExpressionInterface;
910
use Yiisoft\Db\Mysql\Tests\Provider\CommandProvider;
1011
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
1112
use Yiisoft\Db\Query\Query;
@@ -118,12 +119,17 @@ public function testInsertReturningPksWithSubqueryAndNoAutoincrement(): void
118119
public function testUpdate(
119120
string $table,
120121
array $columns,
121-
array|string $conditions,
122+
array|ExpressionInterface|string $conditions,
123+
array|ExpressionInterface|string|null $from,
122124
array $params,
123125
array $expectedValues,
124126
int $expectedCount,
125127
): void {
126-
parent::testUpdate($table, $columns, $conditions, $params, $expectedValues, $expectedCount);
128+
if ($from !== null) {
129+
$this->expectException(NotSupportedException::class);
130+
$this->expectExceptionMessage('MySQL does not support FROM clause in UPDATE statement.');
131+
}
132+
parent::testUpdate($table, $columns, $conditions, $from, $params, $expectedValues, $expectedCount);
127133
}
128134

129135
#[DataProviderExternal(CommandProvider::class, 'upsert')]

tests/Provider/QueryBuilderProvider.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,16 @@ public static function buildColumnDefinition(): array
298298
$values[PseudoType::UUID_PK_SEQ][0] = 'binary(16) PRIMARY KEY';
299299
$values['uuidPrimaryKey()'][0] = 'binary(16) PRIMARY KEY';
300300
$values['defaultValue($expression)'] = ['int DEFAULT 3', ColumnBuilder::integer()->defaultValue(3)];
301-
$values['timestamp(6)'] = ['timestamp(6) DEFAULT CURRENT_TIMESTAMP(6)', ColumnBuilder::timestamp(6)->defaultValue(new Expression('CURRENT_TIMESTAMP(6)'))];
302-
$values['timestamp(null)'] = ['timestamp DEFAULT CURRENT_TIMESTAMP', ColumnBuilder::timestamp(null)->defaultValue(new Expression('CURRENT_TIMESTAMP'))];
301+
$values['timestamp(6)'] = [
302+
'timestamp(6) DEFAULT CURRENT_TIMESTAMP(6)',
303+
ColumnBuilder::timestamp(6)->defaultValue(new Expression('CURRENT_TIMESTAMP(6)')),
304+
];
305+
$values['timestamp(null)'] = [
306+
'timestamp DEFAULT CURRENT_TIMESTAMP',
307+
ColumnBuilder::timestamp(null)->defaultValue(
308+
new Expression('CURRENT_TIMESTAMP')
309+
),
310+
];
303311
}
304312

305313
return $values;

tests/QueryBuilderTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,17 @@ public function testResetSequence(): void
568568
public function testUpdate(
569569
string $table,
570570
array $columns,
571-
array|string $condition,
571+
array|ExpressionInterface|string $condition,
572+
array|ExpressionInterface|string|null $from,
572573
array $params,
573574
string $expectedSql,
574575
array $expectedParams = [],
575576
): void {
576-
parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams);
577+
if ($from !== null) {
578+
$this->expectException(NotSupportedException::class);
579+
$this->expectExceptionMessage('MySQL does not support FROM clause in UPDATE statement.');
580+
}
581+
parent::testUpdate($table, $columns, $condition, $from, $params, $expectedSql, $expectedParams);
577582
}
578583

579584
#[DataProviderExternal(QueryBuilderProvider::class, 'upsert')]

0 commit comments

Comments
 (0)