Skip to content
Merged
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- New #387: Realize `Schema::loadResultColumn()` method (@Tigrov)
- New #393: Use `DateTimeColumn` class for datetime column types (@Tigrov)
- Enh #396, #409: Refactor constraints (@Tigrov)
- New #394, #395, #398, #425: Implement `Command::upsertReturning()` method (@Tigrov, @vjik)
- New #394, #395, #398, #425, #435: Implement `Command::upsertReturning()` method (@Tigrov, @vjik)
- Enh #394, #395: Refactor `Command::insertWithReturningPks()` method (@Tigrov)
- Chg #399: Rename `insertWithReturningPks()` to `insertReturningPks()` in `Command` and `DMLQueryBuilder` classes (@Tigrov)
- Enh #403: Refactor `DMLQueryBuilder::upsert()`, allow use `EXCLUDED` table alias to access inserted values (@Tigrov)
Expand Down
27 changes: 17 additions & 10 deletions src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use function array_combine;
use function array_diff;
use function array_diff_key;
use function array_fill_keys;
use function array_intersect;
use function array_intersect_key;
use function array_keys;
use function array_map;
Expand Down Expand Up @@ -134,13 +134,10 @@ public function upsertReturning(
$tableSchema = $this->schema->getTableSchema($table);
$returnColumns ??= $tableSchema?->getColumnNames();

$upsertSql = $this->upsert($table, $insertColumns, $updateColumns, $params);

if (empty($returnColumns)) {
return $upsertSql;
return $this->upsert($table, $insertColumns, $updateColumns, $params);
}

$quoter = $this->quoter;
[$uniqueNames, $insertNames] = $this->prepareUpsertColumns($table, $insertColumns, $updateColumns);
/** @var TableSchema $tableSchema */
$primaryKeys = $tableSchema->getPrimaryKey();
Expand All @@ -150,20 +147,30 @@ public function upsertReturning(
$insertColumns = array_combine($insertNames, $insertColumns);
}


if (empty($uniqueColumns)) {
$upsertSql = $this->upsert($table, $insertColumns, $updateColumns, $params);
$returnValues = $this->prepareColumnValues($tableSchema, $returnColumns, $insertColumns, $params);

return $upsertSql . ';' . $this->buildSimpleSelect($returnValues);
}

if (is_array($updateColumns) && !empty(array_intersect($uniqueColumns, array_keys($updateColumns)))) {
throw new NotSupportedException(
__METHOD__ . '() is not supported by MySQL when updating primary key or unique values.'
);
if (is_array($updateColumns)
&& !empty($uniqueUpdateValues = array_intersect_key($updateColumns, array_fill_keys($uniqueColumns, null)))
) {
if (!is_array($insertColumns)
|| $uniqueUpdateValues !== array_intersect_key($insertColumns, $uniqueUpdateValues)
) {
throw new NotSupportedException(
__METHOD__ . '() is not supported by MySQL when updating different primary key or unique values.'
);
}

$updateColumns = array_diff_key($updateColumns, $uniqueUpdateValues);
}

$quoter = $this->quoter;
$quotedTable = $quoter->quoteTableName($table);
$upsertSql = $this->upsert($table, $insertColumns, $updateColumns, $params);
$isAutoIncrement = count($primaryKeys) === 1 && $tableSchema->getColumn($primaryKeys[0])?->isAutoIncrement();

if ($isAutoIncrement) {
Expand Down
27 changes: 26 additions & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,12 +612,37 @@ public function testUpsertReturningWithUpdatingPrimaryKeyOrUnique(

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage(
'Yiisoft\Db\Mysql\DMLQueryBuilder::upsertReturning() is not supported by MySQL when updating primary key or unique values.'
'Yiisoft\Db\Mysql\DMLQueryBuilder::upsertReturning() is not supported by MySQL when updating different primary key or unique values.'
);

$qb->upsertReturning($table, $insertColumns, $updateColumns);
}

public function testUpsertReturningWithSameUpdatingPrimaryKeyOrUnique(): void
{
$db = $this->getConnection(true);
$qb = $db->getQueryBuilder();

$params = [];
$sql = $qb->upsertReturning('category', ['id' => 1, 'name' => 'Books'], ['id' => 1, 'name' => 'Audio'], params: $params);

$this->assertSame(
'INSERT INTO `category` (`id`, `name`)'
. ' SELECT `id`, `name` FROM (SELECT 1 AS `id`, :qp0 AS `name`) AS EXCLUDED'
. ' ON DUPLICATE KEY UPDATE `name`=:qp1, `id`=LAST_INSERT_ID(`category`.`id`);'
. 'SELECT `id`, `name` FROM `category` WHERE `id` = LAST_INSERT_ID()',
$sql,
);

$this->assertEquals(
[
':qp0' => new Param('Books', DataType::STRING),
':qp1' => new Param('Audio', DataType::STRING),
],
$params,
);
}

#[TestWith(['order_item', ['subtotal' => 1], ['subtotal' => 10]])]
#[TestWith(['without_pk', ['email' => null, 'name' => 'John'], ['name' => 'John']])]
public function testUpsertReturningWithNullPrimaryKeyOrUnique(
Expand Down
Loading