Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- New #420, #427: Implement `ArrayMergeBuilder`, `LongestBuilder` and `ShortestBuilder` classes (@Tigrov)
- Enh #423: Refactor `DMLQueryBuilder::upsert()` method (@Tigrov)
- Chg #428: Update expression namespaces according to changes in `yiisoft/db` package (@Tigrov)
- Enh #442: Update `DMLQueryBuilder::update()` method to adapt changes in `yiisoft/db` (@rustamwin)

## 1.2.0 March 21, 2024

Expand Down
14 changes: 14 additions & 0 deletions src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder;
use Yiisoft\Db\Schema\TableSchema;
Expand Down Expand Up @@ -61,6 +62,19 @@ public function resetSequence(string $table, int|string|null $value = null): str
EXECUTE autoincrement_stmt";
}

public function update(
string $table,
array $columns,
array|string|ExpressionInterface $condition,
array|string|ExpressionInterface|null $from = null,
array &$params = []
): string {
if ($from !== null) {
throw new NotSupportedException('MySQL does not support FROM clause in UPDATE statement.');
}
return parent::update($table, $columns, $condition, null, $params);
}

public function upsert(
string $table,
array|QueryInterface $insertColumns,
Expand Down
10 changes: 8 additions & 2 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Mysql\Tests\Provider\CommandProvider;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
Expand Down Expand Up @@ -118,12 +119,17 @@ public function testInsertReturningPksWithSubqueryAndNoAutoincrement(): void
public function testUpdate(
string $table,
array $columns,
array|string $conditions,
array|ExpressionInterface|string $conditions,
array|ExpressionInterface|string|null $from,
array $params,
array $expectedValues,
int $expectedCount,
): void {
parent::testUpdate($table, $columns, $conditions, $params, $expectedValues, $expectedCount);
if ($from !== null) {
$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('MySQL does not support FROM clause in UPDATE statement.');
}
parent::testUpdate($table, $columns, $conditions, $from, $params, $expectedValues, $expectedCount);
}

#[DataProviderExternal(CommandProvider::class, 'upsert')]
Expand Down
12 changes: 10 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,16 @@ public static function buildColumnDefinition(): array
$values[PseudoType::UUID_PK_SEQ][0] = 'binary(16) PRIMARY KEY';
$values['uuidPrimaryKey()'][0] = 'binary(16) PRIMARY KEY';
$values['defaultValue($expression)'] = ['int DEFAULT 3', ColumnBuilder::integer()->defaultValue(3)];
$values['timestamp(6)'] = ['timestamp(6) DEFAULT CURRENT_TIMESTAMP(6)', ColumnBuilder::timestamp(6)->defaultValue(new Expression('CURRENT_TIMESTAMP(6)'))];
$values['timestamp(null)'] = ['timestamp DEFAULT CURRENT_TIMESTAMP', ColumnBuilder::timestamp(null)->defaultValue(new Expression('CURRENT_TIMESTAMP'))];
$values['timestamp(6)'] = [
'timestamp(6) DEFAULT CURRENT_TIMESTAMP(6)',
ColumnBuilder::timestamp(6)->defaultValue(new Expression('CURRENT_TIMESTAMP(6)')),
];
$values['timestamp(null)'] = [
'timestamp DEFAULT CURRENT_TIMESTAMP',
ColumnBuilder::timestamp(null)->defaultValue(
new Expression('CURRENT_TIMESTAMP')
),
];
}

return $values;
Expand Down
9 changes: 7 additions & 2 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,17 @@ public function testResetSequence(): void
public function testUpdate(
string $table,
array $columns,
array|string $condition,
array|ExpressionInterface|string $condition,
array|ExpressionInterface|string|null $from,
array $params,
string $expectedSql,
array $expectedParams = [],
): void {
parent::testUpdate($table, $columns, $condition, $params, $expectedSql, $expectedParams);
if ($from !== null) {
$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('MySQL does not support FROM clause in UPDATE statement.');
}
parent::testUpdate($table, $columns, $condition, $from, $params, $expectedSql, $expectedParams);
}

#[DataProviderExternal(QueryBuilderProvider::class, 'upsert')]
Expand Down
Loading