Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.0] feature: use laravel's forceIndex implementation #114

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0 [Not released Yet]

Added
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 @@ -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;
Expand All @@ -431,17 +431,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