Skip to content

Commit 480b930

Browse files
authored
feature: fail at a lower level to catch all use case (#76)
1 parent da511c2 commit 480b930

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Changed
88
- Path for auth and session pool files have moved from `storage/framework/cache/spanner` to `storage/framework/spanner/{auth|session}`.
99
- Default Session Not Found Error Mode was changed from `MAINTAIN_SESSION_POOL` to `CLEAR_SESSION_POOL` (wasn't fully confident at the time, but I think it should be safe to assume it's working now).
1010
- Schema\Builder::getAllTables() now returns rows with `name` and `type` fields instead of list of strings (was implemented incorrectly).
11+
- Exception previously thrown in `Query/Builder` for `sharedLock`, `lockForUpdate`, `insertGetId` was moved to `Query/Grammar`.
12+
- Query/Builder::lock will now throw `BadMethodCallException` if called. Was ignored in previous versions.
1113

1214
# v4.7.0 (Not released yet)
1315

src/Query/Builder.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace Colopl\Spanner\Query;
1919

20-
use Colopl\Spanner\Concerns\MarksAsNotSupported;
2120
use Colopl\Spanner\Connection;
2221
use Illuminate\Contracts\Support\Arrayable;
2322
use Illuminate\Database\Query\Builder as BaseBuilder;
@@ -28,8 +27,7 @@ class Builder extends BaseBuilder
2827
use Concerns\AppliesForceIndex,
2928
Concerns\UsesMutations,
3029
Concerns\UsesPartitionedDml,
31-
Concerns\UsesStaleReads,
32-
MarksAsNotSupported;
30+
Concerns\UsesStaleReads;
3331

3432
/**
3533
* @var Connection
@@ -44,14 +42,6 @@ public function insert(array $values)
4442
return parent::insert($this->prepareInsertForDml($values));
4543
}
4644

47-
/**
48-
* @inheritDoc
49-
*/
50-
public function insertGetId(array $values, $sequence = null)
51-
{
52-
$this->markAsNotSupported('insertGetId');
53-
}
54-
5545
/**
5646
* @inheritDoc
5747
*/
@@ -76,22 +66,6 @@ public function truncate()
7666
}
7767
}
7868

79-
/**
80-
* @inheritDoc
81-
*/
82-
public function sharedLock()
83-
{
84-
$this->markAsNotSupported('shared lock');
85-
}
86-
87-
/**
88-
* @inheritDoc
89-
*/
90-
public function lockForUpdate()
91-
{
92-
$this->markAsNotSupported('lock for update');
93-
}
94-
9569
/**
9670
* NOTE: We will attempt to bind column names included in UNNEST() here.
9771
* @see https://cloud.google.com/spanner/docs/lexical#query-parameters

src/Query/Grammar.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Colopl\Spanner\Query;
1919

20+
use Colopl\Spanner\Concerns\MarksAsNotSupported;
2021
use Colopl\Spanner\Concerns\SharedGrammarCalls;
2122
use Colopl\Spanner\Query\Builder as SpannerBuilder;
2223
use Illuminate\Database\Query\Builder;
@@ -26,6 +27,7 @@
2627

2728
class Grammar extends BaseGrammar
2829
{
30+
use MarksAsNotSupported;
2931
use SharedGrammarCalls;
3032

3133
/**
@@ -36,6 +38,22 @@ protected function compileFrom(Builder $query, $table)
3638
return parent::compileFrom($query, $table).$this->compileForceIndex($query);
3739
}
3840

41+
/**
42+
* @inheritDoc
43+
*/
44+
public function compileInsertGetId(Builder $query, $values, $sequence)
45+
{
46+
$this->markAsNotSupported('insertGetId');
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
protected function compileLock(Builder $query, $value)
53+
{
54+
$this->markAsNotSupported('explicit locking');
55+
}
56+
3957
/**
4058
* @inheritDoc
4159
*/

tests/Query/BuilderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Colopl\Spanner\Tests\Query;
1919

20+
use BadMethodCallException;
2021
use Colopl\Spanner\Connection;
2122
use Colopl\Spanner\Query\Builder;
2223
use Colopl\Spanner\Tests\TestCase;
@@ -826,4 +827,44 @@ public function testTruncate(): void
826827

827828
$this->assertDatabaseCount($tableName, 0);
828829
}
830+
831+
public function test_insertGetId(): void
832+
{
833+
$this->expectException(BadMethodCallException::class);
834+
$this->expectExceptionMessage('Cloud Spanner does not support insertGetId');
835+
836+
$conn = $this->getDefaultConnection();
837+
$qb = $conn->table(self::TABLE_NAME_USER);
838+
$qb->insertGetId(['userId' => $this->generateUuid(), 'name' => 'first']);
839+
}
840+
841+
public function test_lock(): void
842+
{
843+
$this->expectException(BadMethodCallException::class);
844+
$this->expectExceptionMessage('Cloud Spanner does not support explicit locking');
845+
846+
$conn = $this->getDefaultConnection();
847+
$qb = $conn->table(self::TABLE_NAME_USER);
848+
$qb->lock()->get();
849+
}
850+
851+
public function test_lockForUpdate(): void
852+
{
853+
$this->expectException(BadMethodCallException::class);
854+
$this->expectExceptionMessage('Cloud Spanner does not support explicit locking');
855+
856+
$conn = $this->getDefaultConnection();
857+
$qb = $conn->table(self::TABLE_NAME_USER);
858+
$qb->lockForUpdate()->get();
859+
}
860+
861+
public function test_sharedLock(): void
862+
{
863+
$this->expectException(BadMethodCallException::class);
864+
$this->expectExceptionMessage('Cloud Spanner does not support explicit locking');
865+
866+
$conn = $this->getDefaultConnection();
867+
$qb = $conn->table(self::TABLE_NAME_USER);
868+
$qb->sharedLock()->get();
869+
}
829870
}

0 commit comments

Comments
 (0)