Skip to content

Commit

Permalink
Fixed WhereNull and WhereNotNull for raw expressions (#92)
Browse files Browse the repository at this point in the history
* Fixed WhereNull and WhereNotNull for raw expressions

---------

Co-authored-by: AdalbertMemSQL <[email protected]>
  • Loading branch information
AdalbertMemSQL and AdalbertMemSQL authored Oct 28, 2024
1 parent 1be74cf commit 70ef758
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,43 @@ public function prepareBindingsForUpdate(array $bindings, array $values)
return parent::prepareBindingsForUpdate($bindings, $values);
}

/**
* Transforms expressions to their scalar types.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string|int|float $expression
* @return string|int|float
*/
public function getValue($expression)
{
if ($this->isExpression($expression)) {
return $this->getValue($expression->getValue($this));
}

return $expression;
}

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

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

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

protected function whereNotNull(Builder $query, $where)
{
if ($this->isJsonSelector($where['column'])) {
$columnValue = (string) $this->getValue($where['column']);
if ($this->isJsonSelector($columnValue)) {
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);

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

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

protected function wrapJsonSelector($value)
Expand Down
54 changes: 54 additions & 0 deletions tests/Hybrid/Json/JsonWhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ public function where_null()
$this->assertEquals($id3, $query->get()[1]->id);
}

/** @test */
public function where_null_raw()
{
$query = DB::table('test')->whereNull(DB::raw('(SELECT NULL)'))->orderBy('id');

$this->assertEquals(
'select * from `test` where (SELECT NULL) is null order by `id` asc',
$query->toSql()
);

if (! $this->runHybridIntegrations()) {
return;
}

[$id1, $id2, $id3, $id4] = $this->insertJsonData([
['value1' => ['value2' => 'string']],
['value1' => null],
[null],
['value1' => ['value2' => 1]],
]);

$this->assertEquals($id1, $query->get()[0]->id);
$this->assertEquals($id2, $query->get()[1]->id);
$this->assertEquals($id3, $query->get()[2]->id);
$this->assertEquals($id4, $query->get()[3]->id);
}

/** @test */
public function where_not_null()
{
Expand All @@ -153,4 +180,31 @@ public function where_not_null()
$this->assertEquals($id1, $query->get()[0]->id);
$this->assertEquals($id4, $query->get()[1]->id);
}

/** @test */
public function where_not_null_raw()
{
$query = DB::table('test')->whereNotNull(DB::raw('(SELECT 1)'))->orderBy('id');

$this->assertEquals(
'select * from `test` where (SELECT 1) is not null order by `id` asc',
$query->toSql()
);

if (! $this->runHybridIntegrations()) {
return;
}

[$id1, $id2, $id3, $id4] = $this->insertJsonData([
['value1' => ['value2' => 'string']],
['value1' => null],
[null],
['value1' => ['value2' => 1]],
]);

$this->assertEquals($id1, $query->get()[0]->id);
$this->assertEquals($id2, $query->get()[1]->id);
$this->assertEquals($id3, $query->get()[2]->id);
$this->assertEquals($id4, $query->get()[3]->id);
}
}

0 comments on commit 70ef758

Please sign in to comment.