Skip to content

Commit e1ede99

Browse files
taka-oyamazeriyoshit-matsuno-777
committed
[6.0] feature: use laravel's forceIndex implementation (#114)
* [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]>
1 parent 3a9880b commit e1ede99

File tree

5 files changed

+40
-54
lines changed

5 files changed

+40
-54
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v6.0.0 [Not released Yet]
2+
3+
Changed
4+
- [Breaking] Match `Query\Builder::forceIndex()` behavior with laravel's (`forceIndex` property no longer exists). (#114)
5+
16
# v5.3.0 (2023-11-17)
27

38
Fixed

src/Query/Builder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
class Builder extends BaseBuilder
2626
{
27-
use Concerns\AppliesForceIndex,
28-
Concerns\UsesMutations,
27+
use Concerns\UsesMutations,
2928
Concerns\UsesPartitionedDml,
3029
Concerns\UsesStaleReads;
3130

src/Query/Concerns/AppliesForceIndex.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Query/Grammar.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,17 @@
1919

2020
use Colopl\Spanner\Concerns\MarksAsNotSupported;
2121
use Colopl\Spanner\Concerns\SharedGrammarCalls;
22-
use Colopl\Spanner\Query\Builder as SpannerBuilder;
2322
use Illuminate\Database\Query\Builder;
2423
use Illuminate\Contracts\Database\Query\Expression;
2524
use Illuminate\Database\Query\Grammars\Grammar as BaseGrammar;
25+
use Illuminate\Database\Query\IndexHint;
2626
use RuntimeException;
2727

2828
class Grammar extends BaseGrammar
2929
{
3030
use MarksAsNotSupported;
3131
use SharedGrammarCalls;
3232

33-
/**
34-
* @inheritDoc
35-
*/
36-
protected function compileFrom(Builder $query, $table)
37-
{
38-
return parent::compileFrom($query, $table).$this->compileForceIndex($query);
39-
}
40-
4133
/**
4234
* @inheritDoc
4335
*/
@@ -64,15 +56,19 @@ public function compileTruncate(Builder $query)
6456

6557
/**
6658
* @param Builder $query
59+
* @param IndexHint $indexHint
6760
* @return string
6861
*/
69-
protected function compileForceIndex(Builder $query)
62+
protected function compileIndexHint(Builder $query, $indexHint)
7063
{
71-
if ($query instanceof SpannerBuilder) {
72-
return $query->forceIndex ? "@{FORCE_INDEX=$query->forceIndex}" : '';
64+
if ($indexHint->index === null) {
65+
return '';
7366
}
7467

75-
throw new RuntimeException('Unexpected Builder: '.get_class($query).' Expected: '.SpannerBuilder::class);
68+
return match ($indexHint->type) {
69+
'force' => "@{FORCE_INDEX={$indexHint->index}}",
70+
default => $this->markAsNotSupported('index type: ' . $indexHint->type),
71+
};
7672
}
7773

7874
/**

tests/Query/BuilderTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public function testPaginate(): void
423423
$this->assertEquals(100, $pagination->total());
424424
}
425425

426-
public function testForceIndex(): void
426+
public function test_forceIndex(): void
427427
{
428428
$conn = $this->getDefaultConnection();
429429
$tableName = self::TABLE_NAME_USER;
@@ -432,17 +432,39 @@ public function testForceIndex(): void
432432
$this->assertEquals('select * from `User`', $qb->toSql());
433433

434434
$qb->forceIndex('test_index_name');
435-
$this->assertEquals('select * from `User`@{FORCE_INDEX=test_index_name}', $qb->toSql());
435+
$this->assertEquals('select * from `User` @{FORCE_INDEX=test_index_name}', $qb->toSql());
436436

437437
$qb->forceIndex('test_index_name2');
438-
$this->assertEquals('select * from `User`@{FORCE_INDEX=test_index_name2}', $qb->toSql());
438+
$this->assertEquals('select * from `User` @{FORCE_INDEX=test_index_name2}', $qb->toSql());
439439

440440
$qb->forceIndex(null);
441441
$this->assertEquals('select * from `User`', $qb->toSql());
442442

443443
$this->assertInstanceOf(Builder::class, $qb->forceIndex(null));
444444
}
445445

446+
public function test_useIndex(): void
447+
{
448+
$this->expectExceptionMessage('Cloud Spanner does not support index type: hint');
449+
$this->expectException(BadMethodCallException::class);
450+
451+
$this->getDefaultConnection()
452+
->table(self::TABLE_NAME_USER)
453+
->useIndex('test_index_name2')
454+
->toSql();
455+
}
456+
457+
public function test_ignoreIndex(): void
458+
{
459+
$this->expectExceptionMessage('Cloud Spanner does not support index type: ignore');
460+
$this->expectException(BadMethodCallException::class);
461+
462+
$this->getDefaultConnection()
463+
->table(self::TABLE_NAME_USER)
464+
->ignoreIndex('test_index_name2')
465+
->toSql();
466+
}
467+
446468
public function testInterleaveTable(): void
447469
{
448470
$conn = $this->getDefaultConnection();

0 commit comments

Comments
 (0)