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] Support JSON SELECT queries on SQLite #25328

Merged
merged 1 commit into from
Aug 26, 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
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