Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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