diff --git a/CHANGELOG.md b/CHANGELOG.md index 760e98d9..ce5dca4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# v6.0.0 [Not released Yet] + +Changed +- [Breaking] Match `Query\Builder::forceIndex()` behavior with laravel's (`forceIndex` property no longer exists). (#114) + # v5.2.0 [Not released Yet] Added diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 94637a25..342f6d05 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -24,8 +24,7 @@ class Builder extends BaseBuilder { - use Concerns\AppliesForceIndex, - Concerns\UsesMutations, + use Concerns\UsesMutations, Concerns\UsesPartitionedDml, Concerns\UsesStaleReads; diff --git a/src/Query/Concerns/AppliesForceIndex.php b/src/Query/Concerns/AppliesForceIndex.php deleted file mode 100644 index 8f65a6f3..00000000 --- a/src/Query/Concerns/AppliesForceIndex.php +++ /dev/null @@ -1,36 +0,0 @@ -forceIndex = $indexName; - return $this; - } -} diff --git a/src/Query/Grammar.php b/src/Query/Grammar.php index 84498c4d..390b360f 100644 --- a/src/Query/Grammar.php +++ b/src/Query/Grammar.php @@ -19,10 +19,10 @@ use Colopl\Spanner\Concerns\MarksAsNotSupported; use Colopl\Spanner\Concerns\SharedGrammarCalls; -use Colopl\Spanner\Query\Builder as SpannerBuilder; use Illuminate\Database\Query\Builder; use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Database\Query\Grammars\Grammar as BaseGrammar; +use Illuminate\Database\Query\IndexHint; use RuntimeException; class Grammar extends BaseGrammar @@ -30,14 +30,6 @@ class Grammar extends BaseGrammar use MarksAsNotSupported; use SharedGrammarCalls; - /** - * @inheritDoc - */ - protected function compileFrom(Builder $query, $table) - { - return parent::compileFrom($query, $table).$this->compileForceIndex($query); - } - /** * @inheritDoc */ @@ -64,15 +56,19 @@ public function compileTruncate(Builder $query) /** * @param Builder $query + * @param IndexHint $indexHint * @return string */ - protected function compileForceIndex(Builder $query) + protected function compileIndexHint(Builder $query, $indexHint) { - if ($query instanceof SpannerBuilder) { - return $query->forceIndex ? "@{FORCE_INDEX=$query->forceIndex}" : ''; + if ($indexHint->index === null) { + return ''; } - throw new RuntimeException('Unexpected Builder: '.get_class($query).' Expected: '.SpannerBuilder::class); + return match ($indexHint->type) { + 'force' => "@{FORCE_INDEX={$indexHint->index}}", + default => $this->markAsNotSupported('index type: ' . $indexHint->type), + }; } /** diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index 08c75fb9..d350b60f 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -422,7 +422,7 @@ public function testPaginate(): void $this->assertEquals(100, $pagination->total()); } - public function testForceIndex(): void + public function test_forceIndex(): void { $conn = $this->getDefaultConnection(); $tableName = self::TABLE_NAME_USER; @@ -431,10 +431,10 @@ public function testForceIndex(): void $this->assertEquals('select * from `User`', $qb->toSql()); $qb->forceIndex('test_index_name'); - $this->assertEquals('select * from `User`@{FORCE_INDEX=test_index_name}', $qb->toSql()); + $this->assertEquals('select * from `User` @{FORCE_INDEX=test_index_name}', $qb->toSql()); $qb->forceIndex('test_index_name2'); - $this->assertEquals('select * from `User`@{FORCE_INDEX=test_index_name2}', $qb->toSql()); + $this->assertEquals('select * from `User` @{FORCE_INDEX=test_index_name2}', $qb->toSql()); $qb->forceIndex(null); $this->assertEquals('select * from `User`', $qb->toSql()); @@ -442,6 +442,28 @@ public function testForceIndex(): void $this->assertInstanceOf(Builder::class, $qb->forceIndex(null)); } + public function test_useIndex(): void + { + $this->expectExceptionMessage('Cloud Spanner does not support index type: hint'); + $this->expectException(BadMethodCallException::class); + + $this->getDefaultConnection() + ->table(self::TABLE_NAME_USER) + ->useIndex('test_index_name2') + ->toSql(); + } + + public function test_ignoreIndex(): void + { + $this->expectExceptionMessage('Cloud Spanner does not support index type: ignore'); + $this->expectException(BadMethodCallException::class); + + $this->getDefaultConnection() + ->table(self::TABLE_NAME_USER) + ->ignoreIndex('test_index_name2') + ->toSql(); + } + public function testInterleaveTable(): void { $conn = $this->getDefaultConnection();