Skip to content

Commit

Permalink
feat: add support for INSERT OR IGNORE (#207)
Browse files Browse the repository at this point in the history
feature/insert-ignore
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
taka-oyama committed May 13, 2024
1 parent 6fe148f commit 7b96a24
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v7.4.0 (2024-05-13)

Added
- Support for `INSERT OR IGNORE` (#207)

# v7.3.0 (2024-04-17)

Added
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ services:
depends_on:
- emulator
emulator:
image: gcr.io/cloud-spanner-emulator/emulator
image: "gcr.io/cloud-spanner-emulator/emulator:1.5.17"
8 changes: 8 additions & 0 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class Grammar extends BaseGrammar
use MarksAsNotSupported;
use SharedGrammarCalls;

/**
* @inheritDoc
*/
public function compileInsertOrIgnore(Builder $query, array $values)
{
return Str::replaceFirst('insert', 'insert or ignore', $this->compileInsert($query, $values));
}

/**
* @inheritDoc
*/
Expand Down
55 changes: 42 additions & 13 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Google\Cloud\Core\Exception\ConflictException;
use Google\Cloud\Spanner\Bytes;
use Google\Cloud\Spanner\Duration;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Database\QueryException;
use Illuminate\Support\Carbon;
use LogicException;
Expand Down Expand Up @@ -496,7 +497,7 @@ public function test_ignoreIndex(): void
->toSql();
}

public function testInterleaveTable(): void
public function test_interleaved_table(): void
{
$conn = $this->getDefaultConnection();

Expand Down Expand Up @@ -558,7 +559,7 @@ public function test_insert_numeric_types(): void
$this->assertNull($insertedRow['nullableNumericTest']);
}

public function testInsertDatetime(): void
public function test_insert_datetime_object(): void
{
date_default_timezone_set('Asia/Tokyo');

Expand All @@ -577,6 +578,34 @@ public function testInsertDatetime(): void
$this->assertSame($carbonMax->getTimestamp(), $insertedTimestamp->getTimestamp());
}

public function test_insertOrIgnore(): void
{
$table = __FUNCTION__;
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();

$sb->create($table, function (Blueprint $table) {
$table->integer('id')->nullable()->primary();
$table->string('s', 1)->nullable();
});

$query = $conn->table($table);
$query->truncate();
$this->assertSame(1, $query->insertOrIgnore(['id' => 1, 's' => 'a']));
$this->assertSame(2, $query->insertOrIgnore([
['id' => 1, 's' => 'b'],
['id' => 2, 's' => 'c'],
['id' => 3, 's' => 'd'],
]));

$this->assertSame([
['id' => 1, 's' => 'a'],
['id' => 2, 's' => 'c'],
['id' => 3, 's' => 'd'],
], $query->get()->sortBy('id')->values()->all());

}

public function test_upsert_single_row(): void
{
$table = __FUNCTION__;
Expand Down Expand Up @@ -649,7 +678,7 @@ public function test_upsert_throw_error(): void
$this->assertSame(0, $query->count());
}

public function testWhereDatetime(): void
public function test_where_with_datetime(): void
{
date_default_timezone_set('Asia/Tokyo');

Expand All @@ -670,7 +699,7 @@ public function testWhereDatetime(): void
/**
* null ではない列を null で上書きできるか
*/
public function testUpdateWithNull(): void
public function test_update_with_null(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_TEST;
Expand All @@ -696,10 +725,10 @@ public function testUpdateWithNull(): void
->where('testId', $insertRow['testId'])
->first();

$this->assertSame(null, $afterRow['nullableStringTest']);
$this->assertNull($afterRow['nullableStringTest']);
}

public function testUpdateOrInsert(): void
public function test_updateOrInsert(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_TEST;
Expand All @@ -724,7 +753,7 @@ public function testUpdateOrInsert(): void
$this->assertSame('updated', $record['stringTest']);
}

public function testDeleteOnCascase(): void
public function test_delete_with_cascading_interleaves(): void
{
$conn = $this->getDefaultConnection();

Expand Down Expand Up @@ -767,7 +796,7 @@ public function testDeleteOnCascase(): void
$this->assertDatabaseMissing($tableName, $userItems[1]);
}

public function testInsertBytes(): void
public function test_insert_bytes(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_TEST;
Expand All @@ -784,7 +813,7 @@ public function testInsertBytes(): void
$this->assertSame($bytes->formatAsString(), $insertedBytes->formatAsString());
}

public function testWhereBytes(): void
public function test_Where_with_bytes(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_TEST;
Expand All @@ -799,7 +828,7 @@ public function testWhereBytes(): void
$this->assertSame(0, $qb->where('bytesTest', '=', new Bytes("\x00\x01"))->count());
}

public function testWhereIn(): void
public function test_whereIn(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_TEST;
Expand Down Expand Up @@ -895,7 +924,7 @@ public function test_whereLike(): void
}
}

public function testEscapeCharacter(): void
public function test_escapeCharacter(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_USER;
Expand All @@ -920,7 +949,7 @@ public function testEscapeCharacter(): void
}
}

public function testStaleReads(): void
public function test_stale_reads(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_USER;
Expand All @@ -935,7 +964,7 @@ public function testStaleReads(): void
$this->assertEmpty($stalenessRow);
}

public function testTruncate(): void
public function test_truncate(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_USER;
Expand Down

0 comments on commit 7b96a24

Please sign in to comment.