Skip to content

Commit dc9ab2a

Browse files
committed
feat: whereNotInArray support
1 parent cb4fdd7 commit dc9ab2a

3 files changed

Lines changed: 60 additions & 14 deletions

File tree

src/Query/Builder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,31 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
163163
* @param string $column
164164
* @param mixed $value
165165
* @param string $boolean
166+
* @param bool $not
166167
* @return $this
167168
*/
168-
public function whereInArray(string $column, $value, string $boolean = 'and')
169+
public function whereInArray(string $column, $value, string $boolean = 'and', bool $not = false)
169170
{
170171
$type = 'InArray';
171172

172-
$this->wheres[] = compact('type', 'column', 'value', 'boolean');
173+
$this->wheres[] = compact('type', 'column', 'value', 'boolean', 'not');
173174

174175
$this->addBinding($value);
175176

176177
return $this;
177178
}
178179

180+
/**
181+
* @param string $column
182+
* @param mixed $value
183+
* @param string $boolean
184+
* @return $this
185+
*/
186+
public function whereNotInArray(string $column, mixed $value, string $boolean = 'and'): static
187+
{
188+
return $this->whereInArray($column, $value, $boolean, true);
189+
}
190+
179191
/**
180192
* @param string|Expression $column
181193
* @param array<int, mixed>|Arrayable<int, mixed> $values

src/Query/Grammar.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ protected function compileIndexHint(Builder $query, $indexHint)
127127
*/
128128
protected function whereInArray(Builder $query, $where)
129129
{
130-
return '? in unnest(' . $this->wrap($where['column']) . ')';
130+
return '?'
131+
. ($where['not'] ? ' not' : '')
132+
. ' in unnest(' . $this->wrap($where['column']) . ')';
131133
}
132134

133135
/**

tests/Query/SpannerArrayTest.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,53 @@ public function testSearchInArray(): void
5858
$qb = $conn->table($tableName);
5959
$qb->insert($insertValues);
6060

61-
$qb = $conn->table($tableName);
62-
$result = $qb->whereInArray('int64Array', 0)->get();
63-
$this->assertCount(1, $result);
61+
$qb = $conn->table($tableName)->whereInArray('int64Array', 0);
62+
$this->assertSame("select * from `{$tableName}` where ? in unnest(`int64Array`)", $qb->toSql());
63+
$this->assertCount(1, $qb->get());
6464

65-
$qb = $conn->table($tableName);
66-
$result = $qb->whereInArray('int64Array', 1)->get();
67-
$this->assertCount(2, $result);
65+
$qb = $conn->table($tableName)->whereInArray('int64Array', 1);
66+
$this->assertSame("select * from `{$tableName}` where ? in unnest(`int64Array`)", $qb->toSql());
67+
$this->assertCount(2, $qb->get());
6868

69-
$qb = $conn->table($tableName);
70-
$result = $qb->whereInArray('int64Array', 2)->get();
71-
$this->assertCount(3, $result);
69+
$qb = $conn->table($tableName)->whereInArray('int64Array', 2);
70+
$this->assertSame("select * from `{$tableName}` where ? in unnest(`int64Array`)", $qb->toSql());
71+
$this->assertCount(3, $qb->get());
7272

73+
$qb = $conn->table($tableName)->whereInArray('int64Array', 300);
74+
$this->assertSame("select * from `{$tableName}` where ? in unnest(`int64Array`)", $qb->toSql());
75+
$this->assertCount(0, $qb->get());
76+
}
77+
78+
public function testSearchNotInArray(): void
79+
{
80+
$conn = $this->getDefaultConnection();
81+
$tableName = self::TABLE_NAME_ARRAY_TEST;
82+
83+
$testDataCount = 10;
84+
$insertValues = [];
85+
for ($i = 0; $i < $testDataCount; $i++) {
86+
$row = $this->generateArrayTestRow();
87+
$row['int64Array'] = [$i, $i + 1, $i + 2];
88+
$insertValues[] = $row;
89+
}
7390
$qb = $conn->table($tableName);
74-
$result = $qb->whereInArray('int64Array', 300)->get();
75-
$this->assertCount(0, $result);
91+
$qb->insert($insertValues);
92+
93+
$qb = $conn->table($tableName)->whereNotInArray('int64Array', 0);
94+
$this->assertSame("select * from `{$tableName}` where ? not in unnest(`int64Array`)", $qb->toSql());
95+
$this->assertCount(9, $qb->get());
96+
97+
$qb = $conn->table($tableName)->whereNotInArray('int64Array', 1);
98+
$this->assertSame("select * from `{$tableName}` where ? not in unnest(`int64Array`)", $qb->toSql());
99+
$this->assertCount(8, $qb->get());
100+
101+
$qb = $conn->table($tableName)->whereNotInArray('int64Array', 2);
102+
$this->assertSame("select * from `{$tableName}` where ? not in unnest(`int64Array`)", $qb->toSql());
103+
$this->assertCount(7, $qb->get());
104+
105+
$qb = $conn->table($tableName)->whereNotInArray('int64Array', 300);
106+
$this->assertSame("select * from `{$tableName}` where ? not in unnest(`int64Array`)", $qb->toSql());
107+
$this->assertCount(10, $qb->get());
76108
}
77109

78110
public function testUpdateArray(): void

0 commit comments

Comments
 (0)