Skip to content

Commit

Permalink
Add SQLite support for whereJsonContains method (#49401)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleambrosino committed Dec 16, 2023
1 parent 4861acf commit 5d9cf9c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Expand Up @@ -146,6 +146,31 @@ protected function compileJsonLength($column, $operator, $value)
return 'json_array_length('.$field.$path.') '.$operator.' '.$value;
}

/**
* Compile a "JSON contains" statement into SQL.
*
* @param string $column
* @param mixed $value
* @return string
*/
protected function compileJsonContains($column, $value)
{
[$field, $path] = $this->wrapJsonFieldAndPath($column);

return 'exists (select 1 from json_each('.$field.$path.') where '.$this->wrap('json_each.value').' is '.$value.')';
}

/**
* Prepare the binding for a "JSON contains" statement.
*
* @param mixed $binding
* @return mixed
*/
public function prepareBindingForJsonContains($binding)
{
return $binding;
}

/**
* Compile a "JSON contains key" statement into SQL.
*
Expand Down
18 changes: 14 additions & 4 deletions tests/Database/DatabaseQueryBuilderTest.php
Expand Up @@ -5192,10 +5192,15 @@ public function testWhereJsonContainsPostgres()

public function testWhereJsonContainsSqlite()
{
$this->expectException(RuntimeException::class);
$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereJsonContains('options', 'en')->toSql();
$this->assertSame('select * from "users" where exists (select 1 from json_each("options") where "json_each"."value" is ?)', $builder->toSql());
$this->assertEquals(['en'], $builder->getBindings());

$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereJsonContains('options->languages', ['en'])->toSql();
$builder->select('*')->from('users')->whereJsonContains('users.options->language', 'en')->toSql();
$this->assertSame('select * from "users" where exists (select 1 from json_each("users"."options", \'$."language"\') where "json_each"."value" is ?)', $builder->toSql());
$this->assertEquals(['en'], $builder->getBindings());
}

public function testWhereJsonContainsSqlServer()
Expand Down Expand Up @@ -5244,10 +5249,15 @@ public function testWhereJsonDoesntContainPostgres()

public function testWhereJsonDoesntContainSqlite()
{
$this->expectException(RuntimeException::class);
$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereJsonDoesntContain('options', 'en')->toSql();
$this->assertSame('select * from "users" where not exists (select 1 from json_each("options") where "json_each"."value" is ?)', $builder->toSql());
$this->assertEquals(['en'], $builder->getBindings());

$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->whereJsonDoesntContain('options->languages', ['en'])->toSql();
$builder->select('*')->from('users')->whereJsonDoesntContain('users.options->language', 'en')->toSql();
$this->assertSame('select * from "users" where not exists (select 1 from json_each("users"."options", \'$."language"\') where "json_each"."value" is ?)', $builder->toSql());
$this->assertEquals(['en'], $builder->getBindings());
}

public function testWhereJsonDoesntContainSqlServer()
Expand Down

0 comments on commit 5d9cf9c

Please sign in to comment.