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
File renamed without changes.
4 changes: 4 additions & 0 deletions src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public function upsert(
}
}

if ($updateColumns === []) {
return $insertSql;
}

/** @var array $params */
$updateSql = 'WITH "EXCLUDED" ('
. implode(', ', $insertNames)
Expand Down
18 changes: 11 additions & 7 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,42 @@ public function upsert(): array
],
'values and expressions' => [
3 => <<<SQL
INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())
WITH "EXCLUDED" (`email`, [[ts]]) AS (VALUES (:qp0, CURRENT_TIMESTAMP)) UPDATE {{%T_upsert}} SET [[ts]]=(SELECT [[ts]] FROM `EXCLUDED`) WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[ts]]) VALUES (:qp0, CURRENT_TIMESTAMP);
SQL,
],
'values and expressions with update part' => [
3 => <<<SQL
INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())
WITH "EXCLUDED" (`email`, [[ts]]) AS (VALUES (:qp0, CURRENT_TIMESTAMP)) UPDATE {{%T_upsert}} SET [[orders]]=T_upsert.orders + 1 WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[ts]]) VALUES (:qp0, CURRENT_TIMESTAMP);
SQL,
],
'values and expressions without update part' => [
3 => <<<SQL
INSERT INTO {{%T_upsert}} ({{%T_upsert}}.[[email]], [[ts]]) VALUES (:qp0, now())
INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[ts]]) VALUES (:qp0, CURRENT_TIMESTAMP)
SQL,
],
'query, values and expressions with update part' => [
3 => <<<SQL
WITH "EXCLUDED" (`email`, [[time]]) AS (SELECT :phEmail AS `email`, now() AS [[time]]) UPDATE {{%T_upsert}} SET `ts`=:qp1, [[orders]]=T_upsert.orders + 1 WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[time]]) SELECT :phEmail AS `email`, now() AS [[time]];
WITH "EXCLUDED" (`email`, [[ts]]) AS (SELECT :phEmail AS `email`, CURRENT_TIMESTAMP AS [[ts]]) UPDATE {{%T_upsert}} SET `ts`=:qp1, [[orders]]=T_upsert.orders + 1 WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[ts]]) SELECT :phEmail AS `email`, CURRENT_TIMESTAMP AS [[ts]];
SQL,
],
'query, values and expressions without update part' => [
3 => <<<SQL
WITH "EXCLUDED" (`email`, [[time]]) AS (SELECT :phEmail AS `email`, now() AS [[time]]) UPDATE {{%T_upsert}} SET `ts`=:qp1, [[orders]]=T_upsert.orders + 1 WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[time]]) SELECT :phEmail AS `email`, now() AS [[time]];
INSERT OR IGNORE INTO {{%T_upsert}} (`email`, [[ts]]) SELECT :phEmail AS `email`, CURRENT_TIMESTAMP AS [[ts]]
SQL,
],
'no columns to update' => [
3 => <<<SQL
INSERT OR IGNORE INTO `T_upsert_1` (`a`) VALUES (:qp0)
SQL,
],
// @todo - SQL code have a bug. Need fix in next PR
'no columns to update with unique' => [
3 => <<<SQL
WITH "EXCLUDED" (`email`) AS (VALUES (:qp0)) UPDATE {{%T_upsert}} SET WHERE {{%T_upsert}}.`email`=(SELECT `email` FROM `EXCLUDED`); INSERT OR IGNORE INTO {{%T_upsert}} (`email`) VALUES (:qp0);
INSERT OR IGNORE INTO {{%T_upsert}} (`email`) VALUES (:qp0)
SQL,
],
'no unique columns in table - simple insert' => [
3 => <<<SQL
INSERT INTO {{%animal}} (`type`) VALUES (:qp0)
SQL,
],
];
Expand Down
29 changes: 15 additions & 14 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Db\Sqlite\Tests;

use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand All @@ -16,7 +15,6 @@
use Yiisoft\Db\Schema\Schema;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest;
use Yiisoft\Db\Tests\Support\Assert;

/**
* @group sqlite
Expand Down Expand Up @@ -708,24 +706,27 @@ public function testUpsert(
string $table,
array|QueryInterface $insertColumns,
array|bool $updateColumns,
string|array $expectedSQL,
string $expectedSQL,
array $expectedParams
): void {
$db = $this->getConnection(true);

$actualParams = [];
$actualSQL = $db->getQueryBuilder()->upsert($table, $insertColumns, $updateColumns, $actualParams);

if (is_string($expectedSQL)) {
$this->assertSame($expectedSQL, $actualSQL);
} else {
$this->assertContains($actualSQL, $expectedSQL);
}

if (ArrayHelper::isAssociative($expectedParams)) {
$this->assertSame($expectedParams, $actualParams);
} else {
Assert::isOneOf($actualParams, $expectedParams);
}
$this->assertSame($expectedSQL, $actualSQL);

$this->assertSame($expectedParams, $actualParams);
}

/**
* @dataProvider \Yiisoft\Db\Sqlite\Tests\Provider\QueryBuilderProvider::upsert()
*/
public function testUpsertExecute(
string $table,
array|QueryInterface $insertColumns,
array|bool $updateColumns
): void {
parent::testUpsertExecute($table, $insertColumns, $updateColumns);
}
}