Skip to content

Commit 2674c0e

Browse files
Fixed the json_type is not a function error. (#45)
Replaced usage of `json_type` with `json_get_type`. Extended tests to send the query to the database. closes #43
1 parent 47f7b38 commit 2674c0e

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/Query/Grammar.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,24 @@ public function prepareBindingsForUpdate(array $bindings, array $values)
5252

5353
protected function whereNull(Builder $query, $where)
5454
{
55-
return $this->modifyNullJsonExtract(parent::whereNull($query, $where));
55+
if ($this->isJsonSelector($where['column'])) {
56+
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
57+
58+
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) = \'NULL\')';
59+
}
60+
61+
return parent::whereNull($query, $where);
5662
}
5763

5864
protected function whereNotNull(Builder $query, $where)
5965
{
60-
return $this->modifyNullJsonExtract(parent::whereNotNull($query, $where));
61-
}
66+
if ($this->isJsonSelector($where['column'])) {
67+
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
6268

63-
protected function modifyNullJsonExtract($statement)
64-
{
65-
return str_replace('json_extract(', 'JSON_EXTRACT_JSON(', $statement);
69+
return '(JSON_EXTRACT_JSON('.$field.$path.') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON('.$field.$path.')) != \'NULL\')';
70+
}
71+
72+
return parent::whereNotNull($query, $where);
6673
}
6774

6875
protected function wrapJsonSelector($value)

tests/Hybrid/Json/JsonWhereTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,48 @@ public function where_null()
109109

110110
$this->assertEquals(
111111
// @TODO check docs
112-
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') is null OR json_type(JSON_EXTRACT_JSON(data, 'value1')) = 'NULL')",
112+
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') IS NULL OR JSON_GET_TYPE(JSON_EXTRACT_JSON(data, 'value1')) = 'NULL')",
113113
$query->toSql()
114114
);
115+
116+
if (! $this->runHybridIntegrations()) {
117+
return;
118+
}
119+
120+
[, $id2, $id3,] = $this->insertJsonData([
121+
['value1' => ['value2' => 'string']],
122+
['value1' => null],
123+
[null],
124+
['value1' => ['value2' => 1]],
125+
]);
126+
127+
$this->assertEquals($id2, $query->get()[0]->id);
128+
$this->assertEquals($id3, $query->get()[1]->id);
115129
}
116130

117131
/** @test */
118132
public function where_not_null()
119133
{
120-
$query = DB::table('test')->whereNotNull('data->value1');
134+
$query = DB::table('test')->whereNotNull('data->value1')->orderBy('id');
121135

122136
$this->assertEquals(
123137
// @TODO check docs
124-
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') is not null AND json_type(JSON_EXTRACT_JSON(data, 'value1')) != 'NULL')",
138+
"select * from `test` where (JSON_EXTRACT_JSON(data, 'value1') IS NOT NULL AND JSON_GET_TYPE(JSON_EXTRACT_JSON(data, 'value1')) != 'NULL') order by `id` asc",
125139
$query->toSql()
126140
);
141+
142+
if (! $this->runHybridIntegrations()) {
143+
return;
144+
}
145+
146+
[$id1, , , $id4] = $this->insertJsonData([
147+
['value1' => ['value2' => 'string']],
148+
['value1' => null],
149+
[null],
150+
['value1' => ['value2' => 1]],
151+
]);
152+
153+
$this->assertEquals($id1, $query->get()[0]->id);
154+
$this->assertEquals($id4, $query->get()[1]->id);
127155
}
128156
}

0 commit comments

Comments
 (0)