Skip to content

Commit

Permalink
Add Builder::whereJsonContains()
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed May 25, 2018
1 parent 3976a43 commit 52ada11
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,39 @@ public function orWhereRowValues($columns, $operator, $values)
return $this->whereRowValues($columns, $operator, $values, 'or');
}

/**
* Add a "where JSON contains" clause to the query.
*
* @param string $column
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function whereJsonContains($column, $value, $boolean = 'and')
{
$type = 'JsonContains';

$this->wheres[] = compact('type', 'column', 'value', 'boolean');

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

return $this;
}

/**
* Add a "or where JSON contains" clause to the query.
*
* @param string $column
* @param mixed $value
* @return $this
*/
public function orWhereJsonContains($column, $value)
{
return $this->whereJsonContains($column, $value, 'or');
}

/**
* Handles dynamic "where" clauses to the query.
*
Expand Down
30 changes: 30 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Query\Grammars;

use RuntimeException;
use Illuminate\Support\Arr;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\JoinClause;
Expand Down Expand Up @@ -489,6 +490,35 @@ protected function whereRowValues(Builder $query, $where)
return '('.implode(', ', $where['columns']).') '.$where['operator'].' ('.$values.')';
}

/**
* Compile a "where JSON contains" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereJsonContains(Builder $query, $where)
{
$column = $this->wrap($where['column']);

$value = $this->parameter($where['value']);

return $this->compileJsonContains($column, $value);
}

/**
* Compile a "JSON contains" statement into SQL.
*
* @param string $column
* @param string $value
* @return string
* @throws \RuntimeException
*/
protected function compileJsonContains($column, $value)
{
throw new RuntimeException('The database engine in use is not yet supported.');
}

/**
* Compile the "group by" portions of the query.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public function compileSelect(Builder $query)
return $sql;
}

/**
* Compile a "JSON contains" statement into SQL.
*
* @param string $column
* @param string $value
* @return string
*/
protected function compileJsonContains($column, $value)
{
return 'json_contains('.$column.', '.$value.')';
}

/**
* Compile a single union statement.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ protected function dateBasedWhere($type, Builder $query, $where)
return 'extract('.$type.' from '.$this->wrap($where['column']).') '.$where['operator'].' '.$value;
}

/**
* Compile a "JSON contains" statement into SQL.
*
* @param string $column
* @param string $value
* @return string
*/
protected function compileJsonContains($column, $value)
{
$column = str_replace('->>', '->', $column);

return '('.$column.')::jsonb @> '.$value;
}

/**
* Compile the lock into SQL.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,50 @@ public function testWhereRowValuesArityMismatch()
$builder->select('*')->from('orders')->whereRowValues(['last_update'], '<', [1, 2]);
}

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

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

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

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', '=', 1)->orWhereJsonContains('options->languages', new Raw("'[\"en\"]'"));
$this->assertEquals('select * from "users" where "id" = ? or ("options"->\'languages\')::jsonb @> \'["en"]\'', $builder->toSql());
$this->assertEquals([1], $builder->getBindings());
}

/**
* @expectedException \RuntimeException
*/
public function testWhereJsonContainsSqlite()
{
$builder = $this->getSQLiteBuilder();
$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();
}

public function testFromSub()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 52ada11

Please sign in to comment.