Skip to content

Commit 0b2e1eb

Browse files
Fixed WhereNull and WhereNotNull for raw expressions
1 parent 1be74cf commit 0b2e1eb

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

src/Query/Grammar.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function compileOptions(array $options): string
2626
if (! empty($optionString)) {
2727
$optionString .= ',';
2828
}
29-
$optionString .= $key.'='.$value;
29+
$optionString .= $key . '=' . $value;
3030
}
3131

3232
return "OPTION ({$optionString})";
@@ -124,21 +124,23 @@ public function prepareBindingsForUpdate(array $bindings, array $values)
124124

125125
protected function whereNull(Builder $query, $where)
126126
{
127-
if ($this->isJsonSelector($where['column'])) {
127+
$columnValue = (string) $this->getValue($where['column']);
128+
if ($this->isJsonSelector($columnValue)) {
128129
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
129130

130-
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) = \'NULL\')';
131+
return '(JSON_EXTRACT_JSON(' . $field . $path . ') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON(' . $field . $path . ')) = \'NULL\')';
131132
}
132133

133134
return parent::whereNull($query, $where);
134135
}
135136

136137
protected function whereNotNull(Builder $query, $where)
137138
{
138-
if ($this->isJsonSelector($where['column'])) {
139+
$columnValue = (string) $this->getValue($where['column']);
140+
if ($this->isJsonSelector($columnValue)) {
139141
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
140142

141-
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) != \'NULL\')';
143+
return '(JSON_EXTRACT_JSON(' . $field . $path . ') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON(' . $field . $path . ')) != \'NULL\')';
142144
}
143145

144146
return parent::whereNotNull($query, $where);
@@ -190,7 +192,7 @@ protected function wrapJsonFieldAndPath($column)
190192
return "'$part'";
191193
}, $parts);
192194

193-
$path = count($parts) ? ', '.implode(', ', $parts) : '';
195+
$path = count($parts) ? ', ' . implode(', ', $parts) : '';
194196

195197
return [$field, $path];
196198
}
@@ -203,7 +205,7 @@ protected function wrapJsonFieldAndPath($column)
203205
*/
204206
protected function wrapUnion($sql)
205207
{
206-
return 'SELECT * FROM ('.$sql.')';
208+
return 'SELECT * FROM (' . $sql . ')';
207209
}
208210

209211
/**
@@ -231,7 +233,7 @@ protected function compileUnion(array $union)
231233
{
232234
$conjunction = $union['all'] ? ' union all ' : ' union ';
233235

234-
return $conjunction.'('.$union['query']->toSql().')';
236+
return $conjunction . '(' . $union['query']->toSql() . ')';
235237
}
236238

237239
/**
@@ -250,18 +252,18 @@ public function compileSelect(Builder $query)
250252
}
251253

252254
if (! empty($query->unionOrders) || isset($query->unionLimit) || isset($query->unionOffset)) {
253-
$sql = 'SELECT * FROM ('.$sql.') ';
255+
$sql = 'SELECT * FROM (' . $sql . ') ';
254256

255257
if (! empty($query->unionOrders)) {
256-
$sql .= ' '.$this->compileOrders($query, $query->unionOrders);
258+
$sql .= ' ' . $this->compileOrders($query, $query->unionOrders);
257259
}
258260

259261
if (isset($query->unionLimit)) {
260-
$sql .= ' '.$this->compileLimit($query, $query->unionLimit);
262+
$sql .= ' ' . $this->compileLimit($query, $query->unionLimit);
261263
}
262264

263265
if (isset($query->unionOffset)) {
264-
$sql .= ' '.$this->compileUnionOffset($query, $query->unionOffset);
266+
$sql .= ' ' . $this->compileUnionOffset($query, $query->unionOffset);
265267
}
266268
}
267269

@@ -295,10 +297,10 @@ private function compileOffsetWithLimit($offset, $limit): string
295297
// Add a huge LIMIT clause
296298
if (! isset($limit)) {
297299
// 9223372036854775807 - max 64-bit integer
298-
return ' LIMIT 9223372036854775807 OFFSET '.(int) $offset;
300+
return ' LIMIT 9223372036854775807 OFFSET ' . (int) $offset;
299301
}
300302

301-
return ' OFFSET '.(int) $offset;
303+
return ' OFFSET ' . (int) $offset;
302304
}
303305

304306
/**

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)