Skip to content

Commit 2fd5036

Browse files
committed
feat: add Query\Builder::whereInEmbedded
1 parent 1c46ef7 commit 2fd5036

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v8.2.0 (2024-06-17)
2+
3+
Added
4+
- New method `Query/Builder::whereInEmbedded` to allow for querying without parameterizing queries (#)
5+
16
# v8.1.1 (2024-06-03)
27

38
Fixed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ Please note that the following are not required, but are strongly recommended fo
9797
### SQL Mode
9898
Currently only supports Spanner running GoogleSQL (PostgreSQL mode is not supported).
9999

100+
### Query
101+
- [Binding more than 950 parameters in a single query will result in an error](https://cloud.google.com/spanner/quotas#query-limits)
102+
by the server. You may by-pass this limitation by using `Query\Builder::whereInEmbedded(...)` method to embed the
103+
values instead of using query parameters. Using this method may have [negative impact on performance and security](https://cloud.google.com/spanner/docs/sql-best-practices#query-parameters).
104+
100105
### Eloquent
101106
If you use interleaved keys, you MUST define them in the `interleaveKeys` property, or else you won't be able to save.
102107
For more detailed instructions, see `Colopl\Spanner\Tests\Eloquent\ModelTest`.

src/Query/Builder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ public function whereInUnnest(string $column, $values, string $boolean = 'and')
165165
return $this;
166166
}
167167

168+
/**
169+
* @param string $column
170+
* @param iterable<array-key, scalar> $values
171+
* @return $this
172+
*/
173+
public function whereInEmbedded(string $column, iterable $values): static
174+
{
175+
$rawColumn = $this->getGrammar()->wrap($column);
176+
$rawValues = implode(', ', array_map($this->connection->escape(...), iterator_to_array($values)));
177+
$this->whereRaw("{$rawColumn} in ({$rawValues})");
178+
return $this;
179+
}
180+
168181
/**
169182
* @param array<array-key, mixed> $values
170183
* @return array<int, mixed>

tests/Query/BuilderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Google\Cloud\Spanner\Duration;
2929
use Illuminate\Database\Events\QueryExecuted;
3030
use Illuminate\Database\QueryException;
31+
use Illuminate\Support\Arr;
3132
use Illuminate\Support\Carbon;
3233
use LogicException;
3334

@@ -850,6 +851,20 @@ public function test_whereIn(): void
850851
$this->assertSame(2, $conn->table($tableName)->whereIn('bytesTest', [new Bytes(chr(10)), new Bytes(chr(20))])->count());
851852
}
852853

854+
public function test_whereInEmbedded_can_do_more_than_950_parameters(): void
855+
{
856+
$conn = $this->getDefaultConnection();
857+
$tableName = self::TABLE_NAME_USER;
858+
$qb = $conn->table($tableName);
859+
$id1 = $this->generateUuid();
860+
$id2 = $this->generateUuid();
861+
$dummyIds = array_map($this->generateUuid(...), range(0, 950));
862+
863+
$qb->insert([['userId' => $id1, 'name' => 't1'], ['userId' => $id2, 'name' => 't2']]);
864+
$ids = $qb->whereInEmbedded('userId', [$id1, $id2, ...$dummyIds])->pluck('userId');
865+
$this->assertEquals([$id1, $id2], $ids->all());
866+
}
867+
853868
public function test_partitionedDml(): void
854869
{
855870
$conn = $this->getDefaultConnection();

0 commit comments

Comments
 (0)