Skip to content

Commit

Permalink
[6.0] feature: use laravel's forceIndex implementation (#114)
Browse files Browse the repository at this point in the history
* [6.0] feature: use laravel's forceIndex implementation

* Update tests/Query/BuilderTest.php

Co-authored-by: Go Kudo <[email protected]>

* Update CHANGELOG.md

Co-authored-by: t-matsuno-777 <[email protected]>

---------

Co-authored-by: Go Kudo <[email protected]>
Co-authored-by: t-matsuno-777 <[email protected]>
  • Loading branch information
3 people committed Aug 22, 2023
1 parent 9bbc8c0 commit 38fd86b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 54 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.2 (2023-08-22)

Fixed
Expand Down
3 changes: 1 addition & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

class Builder extends BaseBuilder
{
use Concerns\AppliesForceIndex,
Concerns\UsesMutations,
use Concerns\UsesMutations,
Concerns\UsesPartitionedDml,
Concerns\UsesStaleReads;

Expand Down
36 changes: 0 additions & 36 deletions src/Query/Concerns/AppliesForceIndex.php

This file was deleted.

22 changes: 9 additions & 13 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,17 @@

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
{
use MarksAsNotSupported;
use SharedGrammarCalls;

/**
* @inheritDoc
*/
protected function compileFrom(Builder $query, $table)
{
return parent::compileFrom($query, $table).$this->compileForceIndex($query);
}

/**
* @inheritDoc
*/
Expand All @@ -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),
};
}

/**
Expand Down
28 changes: 25 additions & 3 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,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;
Expand All @@ -432,17 +432,39 @@ 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());

$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();
Expand Down

0 comments on commit 38fd86b

Please sign in to comment.