Skip to content

Commit

Permalink
Support multiple conflict targets
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Oct 7, 2024
1 parent 97415a5 commit 2ab3cb7
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/mako/database/midgard/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function insertMultiple(array ...$values): bool
/**
* {@inheritDoc}
*/
public function insertOrUpdate(array $insertValues, array $updateValues, ?string $conflictTarget = null): bool
public function insertOrUpdate(array $insertValues, array $updateValues, mixed $conflictTarget = null): bool
{
// Execute "beforeInsert" hooks

Expand Down
2 changes: 1 addition & 1 deletion src/mako/database/query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ public function insertMultiple(array ...$values): bool
/**
* Inserts or updates a row of data into the chosen table.
*/
public function insertOrUpdate(array $insertValues, array $updateValues, ?string $conflictTarget = null): bool
public function insertOrUpdate(array $insertValues, array $updateValues, array $conflictTarget = []): bool
{
$query = $this->compiler->insertOrUpdate($insertValues, $updateValues, $conflictTarget);

Expand Down
2 changes: 1 addition & 1 deletion src/mako/database/query/compilers/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ public function insertMultiple(array ...$values): array
*
* @return array{sql: string, params: array}
*/
public function insertOrUpdate(array $insertValues, array $updateValues, ?string $conflictTarget = null): array
public function insertOrUpdate(array $insertValues, array $updateValues, array $conflictTarget = []): array
{
throw new DatabaseException(vsprintf('The [ %s ] query compiler does not support insert or update queries.', [static::class]));
}
Expand Down
2 changes: 1 addition & 1 deletion src/mako/database/query/compilers/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function insertWithoutValues(): string
/**
* {@inheritDoc}
*/
public function insertOrUpdate(array $insertValues, array $updateValues, ?string $conflictTarget = null): array
public function insertOrUpdate(array $insertValues, array $updateValues, array $conflictTarget = []): array
{
$sql = $this->query->getPrefix()
. $this->insertWithValues($insertValues)
Expand Down
4 changes: 2 additions & 2 deletions src/mako/database/query/compilers/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ public function lock(null|bool|string $lock): string
/**
* {@inheritDoc}
*/
public function insertOrUpdate(array $insertValues, array $updateValues, ?string $conflictTarget = null): array
public function insertOrUpdate(array $insertValues, array $updateValues, array $conflictTarget = []): array
{
$sql = $sql = $this->query->getPrefix()
. $this->insertWithValues($insertValues)
. ' ON CONFLICT (' . $this->escapeIdentifier($conflictTarget) . ') DO UPDATE SET '
. ' ON CONFLICT (' . $this->escapeIdentifiers($conflictTarget) . ') DO UPDATE SET '
. $this->updateColumns($updateValues);

return ['sql' => $sql, 'params' => $this->params];
Expand Down
4 changes: 2 additions & 2 deletions src/mako/database/query/compilers/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ protected function offset(?int $offset): string
/**
* {@inheritDoc}
*/
public function insertOrUpdate(array $insertValues, array $updateValues, ?string $conflictTarget = null): array
public function insertOrUpdate(array $insertValues, array $updateValues, array $conflictTarget = []): array
{
$sql = $sql = $this->query->getPrefix()
. $this->insertWithValues($insertValues)
. ' ON CONFLICT (' . $this->escapeIdentifier($conflictTarget) . ') DO UPDATE SET '
. ' ON CONFLICT (' . $this->escapeIdentifiers($conflictTarget) . ') DO UPDATE SET '
. $this->updateColumns($updateValues);

return ['sql' => $sql, 'params' => $this->params];
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/database/midgard/TimestampedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function testInsertOrUpdate(): void
{
$now = new DateTime;

(new TimestampedFoo)->insertOrUpdate(['value' => 'test_insert1'], ['value' => 'test_insert1'], 'id');
(new TimestampedFoo)->insertOrUpdate(['value' => 'test_insert1'], ['value' => 'test_insert1'], ['id']);

$this->assertSame($now->format('Y-m-d'), (new TimestampedFoo)->where('value', '=', 'test_insert1')->first()->created_at->format('Y-m-d'));
}
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/database/query/compilers/PostgresCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,22 @@ public function testInsertOrUpdate(): void
{
$query = $this->getBuilder();

$query = $query->getCompiler()->insertOrUpdate(['foo' => 'bar'], ['foo' => 'dupe'], 'foo');
$query = $query->getCompiler()->insertOrUpdate(['foo' => 'bar'], ['foo' => 'dupe'], ['foo']);

$this->assertEquals('INSERT INTO "foobar" ("foo") VALUES (?) ON CONFLICT ("foo") DO UPDATE SET "foo" = ?', $query['sql']);
$this->assertEquals(['bar', 'dupe'], $query['params']);
}

/**
*
*/
public function testInsertOrUpdateWithMultipleConstraints(): void
{
$query = $this->getBuilder();

$query = $query->getCompiler()->insertOrUpdate(['foo' => 'bar'], ['foo' => 'dupe'], ['foo', 'bar']);

$this->assertEquals('INSERT INTO "foobar" ("foo") VALUES (?) ON CONFLICT ("foo", "bar") DO UPDATE SET "foo" = ?', $query['sql']);
$this->assertEquals(['bar', 'dupe'], $query['params']);
}
}
15 changes: 14 additions & 1 deletion tests/unit/database/query/compilers/SQLiteCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,22 @@ public function testInsertOrUpdate(): void
{
$query = $this->getBuilder();

$query = $query->getCompiler()->insertOrUpdate(['foo' => 'bar'], ['foo' => 'dupe'], 'foo');
$query = $query->getCompiler()->insertOrUpdate(['foo' => 'bar'], ['foo' => 'dupe'], ['foo']);

$this->assertEquals('INSERT INTO "foobar" ("foo") VALUES (?) ON CONFLICT ("foo") DO UPDATE SET "foo" = ?', $query['sql']);
$this->assertEquals(['bar', 'dupe'], $query['params']);
}

/**
*
*/
public function testInsertOrUpdateWithMultipleConstraints(): void
{
$query = $this->getBuilder();

$query = $query->getCompiler()->insertOrUpdate(['foo' => 'bar'], ['foo' => 'dupe'], ['foo', 'bar']);

$this->assertEquals('INSERT INTO "foobar" ("foo") VALUES (?) ON CONFLICT ("foo", "bar") DO UPDATE SET "foo" = ?', $query['sql']);
$this->assertEquals(['bar', 'dupe'], $query['params']);
}
}

0 comments on commit 2ab3cb7

Please sign in to comment.