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

feat: add support for whereNotInUnnest #225

Merged
merged 1 commit into from
Aug 7, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v8.3.0 (2024-09-02)

- add support for `Query\Builder::whereNotInUnnest(...)` (#225)

# v8.2.0 (2024-08-05)

> [!NOTE]
Expand Down
15 changes: 13 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,31 @@ public function whereInArray(string $column, $value, string $boolean = 'and')
* @param string $boolean
* @return $this
*/
public function whereInUnnest(string $column, $values, string $boolean = 'and')
public function whereInUnnest(string $column, $values, string $boolean = 'and', bool $not = false)
{
$type = 'InUnnest';

// prevent getBindings() from flattening the array by wrapping it in a class
$values = ($values instanceof Nested) ? $values : new Nested($values);

$this->wheres[] = compact('type', 'column', 'values', 'boolean');
$this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not');

$this->addBinding($values);

return $this;
}

/**
* @param string $column
* @param array<array-key, mixed>|Arrayable<array-key, mixed>|Nested $values
* @param string $boolean
* @return $this
*/
public function whereNotInUnnest(string $column, array|Arrayable|Nested $values, string $boolean = 'and'): static
{
return $this->whereInUnnest($column, $values, $boolean, true);
}

/**
* @param array<array-key, mixed> $values
* @return array<int, mixed>
Expand Down
12 changes: 8 additions & 4 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function whereInArray(Builder $query, $where)

/**
* @param Builder $query
* @param array $where
* @param array $where{ values: Nested, column: string, not: bool }
* @return string
*/
protected function whereInUnnest(Builder $query, $where)
Expand All @@ -132,9 +132,13 @@ protected function whereInUnnest(Builder $query, $where)
throw new RuntimeException('Invalid Type:'.get_class($values).' given. '.Nested::class.' expected.');
}

return (count($values) > 0)
? $this->wrap($where['column']).' in unnest(?)'
: '0 = 1';
if (count($values) <= 0) {
return '0 = 1';
}

return $this->wrap($where['column'])
. ($where['not'] ? ' not' : '')
. ' in unnest(?)';
}

/**
Expand Down
3 changes: 0 additions & 3 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
use Colopl\Spanner\Schema\Blueprint;
use Colopl\Spanner\Tests\TestCase;
use Colopl\Spanner\TimestampBound\ExactStaleness;
use Google\Cloud\Core\Exception\ConflictException;
use Google\Cloud\Core\Exception\DeadlineExceededException;
use Google\Cloud\Spanner\Bytes;
use Google\Cloud\Spanner\Duration;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Database\QueryException;
use Illuminate\Support\Carbon;
use LogicException;
Expand Down
26 changes: 26 additions & 0 deletions tests/Query/UnnestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,30 @@ public function test_whereInUnnest__with_more_than_950_parameters(): void
$expected = collect([$id1, $id2])->sort()->values()->all();
$this->assertSame($expected, $given);
}


public function test_whereNotInUnnest(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_TEST;

$testDataCount = 3;
$insertValues = [];
for ($i = 0; $i < $testDataCount; $i++) {
$insertValues[] = $this->generateTestRow();
}
$qb = $conn->table($tableName);
$qb->insert($insertValues);

$ids = $qb->pluck('testId')->sort()->values();

$qb = $qb->whereNotInUnnest('testId', [$ids->first()]);
$sql = $qb->toSql();
$results = $qb->get('testId')->pluck('testId')->sort()->values();

$this->assertSame('select * from `Test` where `testId` not in unnest(?)', $sql);
$this->assertCount(2, $results);
$this->assertSame($ids->skip(1)->values()->all(), $results->all());
}

}
Loading