Skip to content

Commit

Permalink
Add whereJsonContains() to SQL Server
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Jun 4, 2018
1 parent 56290ed commit 8038c3d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
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

0 comments on commit 8038c3d

Please sign in to comment.