Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add SQLite support for whereJsonContains method #49401

Merged
merged 1 commit into from Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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