Skip to content

Commit

Permalink
[10.x] Fix whereNull queries with raw expressions for the MySql gramm…
Browse files Browse the repository at this point in the history
…ar (#46538)
  • Loading branch information
GeniJaho committed Mar 21, 2023
1 parent aad9b2a commit d5662cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class MySqlGrammar extends Grammar
*/
protected function whereNull(Builder $query, $where)
{
if ($this->isJsonSelector($where['column'])) {
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
$columnValue = (string) $this->getValue($where['column']);

if ($this->isJsonSelector($columnValue)) {
[$field, $path] = $this->wrapJsonFieldAndPath($columnValue);

return '(json_extract('.$field.$path.') is null OR json_type(json_extract('.$field.$path.')) = \'NULL\')';
}
Expand All @@ -41,8 +43,10 @@ protected function whereNull(Builder $query, $where)
*/
protected function whereNotNull(Builder $query, $where)
{
if ($this->isJsonSelector($where['column'])) {
[$field, $path] = $this->wrapJsonFieldAndPath($where['column']);
$columnValue = (string) $this->getValue($where['column']);

if ($this->isJsonSelector($columnValue)) {
[$field, $path] = $this->wrapJsonFieldAndPath($columnValue);

return '(json_extract('.$field.$path.') is not null AND json_type(json_extract('.$field.$path.')) != \'NULL\')';
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,19 @@ public function testBasicWhereNulls()
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testBasicWhereNullExpressionsMysql()
{
$builder = $this->getMysqlBuilder();
$builder->select('*')->from('users')->whereNull(new Raw('id'));
$this->assertSame('select * from `users` where id is null', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getMysqlBuilder();
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereNull(new Raw('id'));
$this->assertSame('select * from `users` where `id` = ? or id is null', $builder->toSql());
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testJsonWhereNullMysql()
{
$builder = $this->getMySqlBuilder();
Expand All @@ -1425,6 +1438,20 @@ public function testJsonWhereNotNullMysql()
$this->assertSame('select * from `users` where (json_extract(`items`, \'$."id"\') is not null AND json_type(json_extract(`items`, \'$."id"\')) != \'NULL\')', $builder->toSql());
}

public function testJsonWhereNullExpressionMysql()
{
$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereNull(new Raw('items->id'));
$this->assertSame('select * from `users` where (json_extract(`items`, \'$."id"\') is null OR json_type(json_extract(`items`, \'$."id"\')) = \'NULL\')', $builder->toSql());
}

public function testJsonWhereNotNullExpressionMysql()
{
$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereNotNull(new Raw('items->id'));
$this->assertSame('select * from `users` where (json_extract(`items`, \'$."id"\') is not null AND json_type(json_extract(`items`, \'$."id"\')) != \'NULL\')', $builder->toSql());
}

public function testArrayWhereNulls()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit d5662cb

Please sign in to comment.