Skip to content

Commit 70ef758

Browse files
Fixed WhereNull and WhereNotNull for raw expressions (#92)
* Fixed WhereNull and WhereNotNull for raw expressions --------- Co-authored-by: AdalbertMemSQL <AdalbertMemSQL@users.noreply.github.com>
1 parent 1be74cf commit 70ef758

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

src/Query/Grammar.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,43 @@ public function prepareBindingsForUpdate(array $bindings, array $values)
122122
return parent::prepareBindingsForUpdate($bindings, $values);
123123
}
124124

125+
/**
126+
* Transforms expressions to their scalar types.
127+
*
128+
* @param \Illuminate\Contracts\Database\Query\Expression|string|int|float $expression
129+
* @return string|int|float
130+
*/
131+
public function getValue($expression)
132+
{
133+
if ($this->isExpression($expression)) {
134+
return $this->getValue($expression->getValue($this));
135+
}
136+
137+
return $expression;
138+
}
139+
125140
protected function whereNull(Builder $query, $where)
126141
{
127-
if ($this->isJsonSelector($where['column'])) {
142+
$columnValue = (string) $this->getValue($where['column']);
143+
if ($this->isJsonSelector($columnValue)) {
128144
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
129145

130146
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) = \'NULL\')';
131147
}
132148

133-
return parent::whereNull($query, $where);
149+
return $this->wrap($where['column']).' is null';
134150
}
135151

136152
protected function whereNotNull(Builder $query, $where)
137153
{
138-
if ($this->isJsonSelector($where['column'])) {
154+
$columnValue = (string) $this->getValue($where['column']);
155+
if ($this->isJsonSelector($columnValue)) {
139156
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
140157

141158
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) != \'NULL\')';
142159
}
143160

144-
return parent::whereNotNull($query, $where);
161+
return $this->wrap($where['column']).' is not null';
145162
}
146163

147164
protected function wrapJsonSelector($value)

tests/Hybrid/Json/JsonWhereTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,33 @@ public function where_null()
128128
$this->assertEquals($id3, $query->get()[1]->id);
129129
}
130130

131+
/** @test */
132+
public function where_null_raw()
133+
{
134+
$query = DB::table('test')->whereNull(DB::raw('(SELECT NULL)'))->orderBy('id');
135+
136+
$this->assertEquals(
137+
'select * from `test` where (SELECT NULL) is null order by `id` asc',
138+
$query->toSql()
139+
);
140+
141+
if (! $this->runHybridIntegrations()) {
142+
return;
143+
}
144+
145+
[$id1, $id2, $id3, $id4] = $this->insertJsonData([
146+
['value1' => ['value2' => 'string']],
147+
['value1' => null],
148+
[null],
149+
['value1' => ['value2' => 1]],
150+
]);
151+
152+
$this->assertEquals($id1, $query->get()[0]->id);
153+
$this->assertEquals($id2, $query->get()[1]->id);
154+
$this->assertEquals($id3, $query->get()[2]->id);
155+
$this->assertEquals($id4, $query->get()[3]->id);
156+
}
157+
131158
/** @test */
132159
public function where_not_null()
133160
{
@@ -153,4 +180,31 @@ public function where_not_null()
153180
$this->assertEquals($id1, $query->get()[0]->id);
154181
$this->assertEquals($id4, $query->get()[1]->id);
155182
}
183+
184+
/** @test */
185+
public function where_not_null_raw()
186+
{
187+
$query = DB::table('test')->whereNotNull(DB::raw('(SELECT 1)'))->orderBy('id');
188+
189+
$this->assertEquals(
190+
'select * from `test` where (SELECT 1) is not null order by `id` asc',
191+
$query->toSql()
192+
);
193+
194+
if (! $this->runHybridIntegrations()) {
195+
return;
196+
}
197+
198+
[$id1, $id2, $id3, $id4] = $this->insertJsonData([
199+
['value1' => ['value2' => 'string']],
200+
['value1' => null],
201+
[null],
202+
['value1' => ['value2' => 1]],
203+
]);
204+
205+
$this->assertEquals($id1, $query->get()[0]->id);
206+
$this->assertEquals($id2, $query->get()[1]->id);
207+
$this->assertEquals($id3, $query->get()[2]->id);
208+
$this->assertEquals($id4, $query->get()[3]->id);
209+
}
156210
}

0 commit comments

Comments
 (0)