Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
/**
* MySQL does not support UPDATE ... FROM ...
*/
return parent::update($table, $columns, $condition, null, $params);
}

public function upsert(
string $table,
array|QueryInterface $insertColumns,
Expand Down
7 changes: 4 additions & 3 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,12 @@ public function testInsertReturningPksWithSubqueryAndNoAutoincrement(): void
public function testUpdate(
string $table,
array $columns,
array|string $conditions,
array $params,
array|ExpressionInterface|string $conditions,
array|ExpressionInterface|string|null $from,
array $expectedValues,
int $expectedCount,
): void {
parent::testUpdate($table, $columns, $conditions, $params, $expectedValues, $expectedCount);
parent::testUpdate($table, $columns, $conditions, $from, $expectedValues, $expectedCount);
}

#[DataProviderExternal(CommandProvider::class, 'upsert')]
Expand Down
57 changes: 55 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,51 @@ public static function upsert(): array
return $upsert;
}

public static function update(): array
{
$data = parent::update();
foreach ($data as $key => $item) {
if ($item[3] !== null) {
unset($data[$key]);
}
}
return array_merge(
$data,
[
[
'{{table}}',
['name' => '{{tmp}}.{{name}}'],
[],
'tmp',
[],
self::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp0
SQL
),
[
':qp0' => new Param('{{tmp}}.{{name}}', DataType::STRING),
],
],
[
'{{table}}',
['name' => '{{tmp}}.{{name}}'],
[],
['tmp' => self::getDb()->select()->from('{{tmp}}')],
[],
self::replaceQuotes(
<<<SQL
UPDATE [[table]] SET [[name]]=:qp0
SQL
),
[
':qp0' => new Param('{{tmp}}.{{name}}', DataType::STRING),
],
],
]
);
}

public static function upsertReturning(): array
{
$upsert = self::upsert();
Expand Down Expand Up @@ -298,8 +343,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
5 changes: 3 additions & 2 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,13 @@ 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);
parent::testUpdate($table, $columns, $condition, $from, $params, $expectedSql, $expectedParams);
}

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