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

[5.6] Add whereJsonContains() to SQL Server #24448

Merged
merged 1 commit into from
Jun 5, 2018
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ public function whereJsonContains($column, $value, $boolean = 'and', $not = fals
$this->wheres[] = compact('type', 'column', 'value', 'boolean', 'not');

if (! $value instanceof Expression) {
$this->addBinding(json_encode($value));
$this->addBinding($this->grammar->prepareBindingForJsonContains($value));
}

return $this;
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,17 @@ protected function compileJsonContains($column, $value)
throw new RuntimeException('This database engine does not support JSON contains operations.');
}

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

/**
* Compile the "group by" portions of the query.
*
Expand Down
27 changes: 27 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ protected function whereDate(Builder $query, $where)
return 'cast('.$this->wrap($where['column']).' as date) '.$where['operator'].' '.$value;
}

/**
* Compile a "JSON contains" statement into SQL.
*
* @param string $column
* @param string $value
* @return string
*/
protected function compileJsonContains($column, $value)
{
$from = $column[0] == '[' ?
'openjson('.$column.')' :
substr_replace($column, 'openjson', 0, strlen('json_value'));

return $value.' in (select [value] from '.$from.')';
}

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

/**
* Create a full ANSI offset clause for the query.
*
Expand Down
39 changes: 31 additions & 8 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,11 @@ public function testWhereRowValuesArityMismatch()

public function testWhereJsonContainsMySql()
{
$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereJsonContains('options', ['en']);
$this->assertEquals('select * from `users` where json_contains(`options`, ?)', $builder->toSql());
$this->assertEquals(['["en"]'], $builder->getBindings());

$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereJsonContains('options->languages', ['en']);
$this->assertEquals('select * from `users` where json_contains(`options`->\'$."languages"\', ?)', $builder->toSql());
Expand All @@ -2647,6 +2652,11 @@ public function testWhereJsonContainsMySql()

public function testWhereJsonContainsPostgres()
{
$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->whereJsonContains('options', ['en']);
$this->assertEquals('select * from "users" where ("options")::jsonb @> ?', $builder->toSql());
$this->assertEquals(['["en"]'], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->whereJsonContains('options->languages', ['en']);
$this->assertEquals('select * from "users" where ("options"->\'languages\')::jsonb @> ?', $builder->toSql());
Expand All @@ -2667,13 +2677,22 @@ public function testWhereJsonContainsSqlite()
$builder->select('*')->from('users')->whereJsonContains('options->languages', ['en'])->toSql();
}

/**
* @expectedException \RuntimeException
*/
public function testWhereJsonContainsSqlServer()
{
$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->whereJsonContains('options->languages', ['en'])->toSql();
$builder->select('*')->from('users')->whereJsonContains('options', true);
$this->assertEquals('select * from [users] where ? in (select [value] from openjson([options]))', $builder->toSql());
$this->assertEquals(['true'], $builder->getBindings());

$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->whereJsonContains('options->languages', 'en');
$this->assertEquals('select * from [users] where ? in (select [value] from openjson([options], \'$."languages"\'))', $builder->toSql());
$this->assertEquals(['en'], $builder->getBindings());

$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonContains('options->languages', new Raw("'en'"));
$this->assertEquals('select * from [users] where [id] = ? or \'en\' in (select [value] from openjson([options], \'$."languages"\'))', $builder->toSql());
$this->assertEquals([1], $builder->getBindings());
}

public function testWhereJsonDoesntContainMySql()
Expand Down Expand Up @@ -2711,13 +2730,17 @@ public function testWhereJsonDoesntContainSqlite()
$builder->select('*')->from('users')->whereJsonDoesntContain('options->languages', ['en'])->toSql();
}

/**
* @expectedException \RuntimeException
*/
public function testWhereJsonDoesntContainSqlServer()
{
$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->whereJsonDoesntContain('options->languages', ['en'])->toSql();
$builder->select('*')->from('users')->whereJsonDoesntContain('options->languages', 'en');
$this->assertEquals('select * from [users] where not ? in (select [value] from openjson([options], \'$."languages"\'))', $builder->toSql());
$this->assertEquals(['en'], $builder->getBindings());

$builder = $this->getSqlServerBuilder();
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonDoesntContain('options->languages', new Raw("'en'"));
$this->assertEquals('select * from [users] where [id] = ? or not \'en\' in (select [value] from openjson([options], \'$."languages"\'))', $builder->toSql());
$this->assertEquals([1], $builder->getBindings());
}

public function testFromSub()
Expand Down