Skip to content

Commit f9c797a

Browse files
authored
Fix Grammar::substituteBindingsIntoRawSql fails during unnesting (#246)
1 parent c21ad52 commit f9c797a

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/Connection.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Closure;
2121
use Colopl\Spanner\Query\Builder as QueryBuilder;
2222
use Colopl\Spanner\Query\Grammar as QueryGrammar;
23+
use Colopl\Spanner\Query\Nested;
2324
use Colopl\Spanner\Query\Parameterizer as QueryParameterizer;
2425
use Colopl\Spanner\Query\Processor as QueryProcessor;
2526
use Colopl\Spanner\Schema\Builder as SchemaBuilder;
@@ -445,10 +446,14 @@ protected function prepareBinding(BaseQueryGrammar $grammar, mixed $value): mixe
445446

446447
/**
447448
* @inheritDoc
448-
* @param scalar|list<mixed>|null $value
449+
* @param scalar|list<mixed>|Nested|null $value
449450
*/
450451
public function escape($value, $binary = false)
451452
{
453+
if ($value instanceof Nested) {
454+
$value = $value->toArray();
455+
}
456+
452457
return is_array($value)
453458
? $this->escapeArray($value, $binary)
454459
: parent::escape($value, $binary);

tests/ConnectionTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Colopl\Spanner\Connection;
2121
use Colopl\Spanner\Events\MutatingData;
22+
use Colopl\Spanner\Query\Nested;
2223
use Colopl\Spanner\Session\SessionInfo;
2324
use Colopl\Spanner\TimestampBound\ExactStaleness;
2425
use Colopl\Spanner\TimestampBound\MaxStaleness;
@@ -600,6 +601,15 @@ public function test_escape_nested_array(): void
600601
$this->assertSame('[]', $conn->escape([[]]));
601602
}
602603

604+
public function test_escape_nested_object(): void
605+
{
606+
$conn = $this->getDefaultConnection();
607+
$this->assertSame(
608+
"[false, true, 0, 1, \"a\", 1.1]",
609+
$conn->escape(new Nested([false, true, 0, 1, "a", 1.1]))
610+
);
611+
}
612+
603613
public function test_getTablePrefix(): void
604614
{
605615
config()->set('database.connections.main.prefix', 'test_');

tests/Query/UnnestTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
class UnnestTest extends TestCase
2323
{
24+
/** @see https://cloud.google.com/spanner/quotas#query_limits */
25+
public const SPANNER_PARAMETERS_LIMIT = 950;
26+
2427
public function test_whereInUnnest(): void
2528
{
2629
$conn = $this->getDefaultConnection();
@@ -65,7 +68,7 @@ public function test_whereInUnnest__with_more_than_950_parameters(): void
6568
$qb = $conn->table($tableName);
6669
$id1 = $this->generateUuid();
6770
$id2 = $this->generateUuid();
68-
$dummyIds = array_map($this->generateUuid(...), range(0, 950));
71+
$dummyIds = array_map($this->generateUuid(...), range(0, self::SPANNER_PARAMETERS_LIMIT));
6972

7073
$qb->insert([['userId' => $id1, 'name' => 't1'], ['userId' => $id2, 'name' => 't2']]);
7174
$given = $qb->whereInUnnest('userId', [$id1, $id2, ...$dummyIds])->pluck('userId')->sort()->values()->all();
@@ -97,5 +100,4 @@ public function test_whereNotInUnnest(): void
97100
$this->assertCount(2, $results);
98101
$this->assertSame($ids->skip(1)->values()->all(), $results->all());
99102
}
100-
101103
}

0 commit comments

Comments
 (0)