Skip to content

Commit

Permalink
Support JSON SELECT queries on SQLite (#25328)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed Aug 26, 2018
1 parent e5ef727 commit e1883c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
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 @@ -932,6 +932,17 @@ protected function wrapJsonSelector($value)
throw new RuntimeException('This database engine does not support JSON operations.');
}

/**
* Wrap the given JSON path.
*
* @param string $value
* @return string
*/
protected function wrapJsonPath($value)
{
return '\'$."'.str_replace('->', '"."', $value).'"\'';
}

/**
* Determine if the given string is a JSON selector.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,23 @@ public function compileTruncate(Builder $query)
'delete from '.$this->wrapTable($query->from) => [],
];
}

/**
* Wrap the given JSON selector.
*
* @param string $value
* @return string
*/
protected function wrapJsonSelector($value)
{
$parts = explode('->', $value, 2);

$field = $this->wrap($parts[0]);

$path = count($parts) > 1 ? ', '.$this->wrapJsonPath($parts[1]) : '';

$selector = 'json_extract('.$field.$path.')';

return $selector;
}
}
11 changes: 0 additions & 11 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,17 +472,6 @@ protected function wrapJsonSelector($value)
return 'json_value('.$field.', '.$this->wrapJsonPath($parts[0]).')';
}

/**
* Wrap the given JSON path.
*
* @param string $value
* @return string
*/
protected function wrapJsonPath($value)
{
return '\'$."'.str_replace('->', '"."', $value).'"\'';
}

/**
* Wrap a table in keyword identifiers.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,21 @@ public function testSqlServerWrappingJson()
$this->assertEquals('select * from [users] where json_value([items], \'$."price"."in_usd"\') = ? and json_value([items], \'$."age"\') = ?', $builder->toSql());
}

public function testSqliteWrappingJson()
{
$builder = $this->getSQLiteBuilder();
$builder->select('items->price')->from('users')->where('users.items->price', '=', 1)->orderBy('items->price');
$this->assertEquals('select json_extract("items", \'$."price"\') from "users" where json_extract("users"."items", \'$."price"\') = ? order by json_extract("items", \'$."price"\') asc', $builder->toSql());

$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->where('items->price->in_usd', '=', 1);
$this->assertEquals('select * from "users" where json_extract("items", \'$."price"."in_usd"\') = ?', $builder->toSql());

$builder = $this->getSQLiteBuilder();
$builder->select('*')->from('users')->where('items->price->in_usd', '=', 1)->where('items->age', '=', 2);
$this->assertEquals('select * from "users" where json_extract("items", \'$."price"."in_usd"\') = ? and json_extract("items", \'$."age"\') = ?', $builder->toSql());
}

public function testSQLiteOrderBy()
{
$builder = $this->getSQLiteBuilder();
Expand Down

0 comments on commit e1883c0

Please sign in to comment.