diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index eafc2d6e423f..ac21bdea7a1a 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -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. * diff --git a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php index 60973ae65bc4..e28313b77764 100755 --- a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php @@ -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; + } } diff --git a/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php index 24e8f4c9ee51..215fa313bd6a 100755 --- a/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php @@ -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. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index f8bea9f66665..2627131afe66 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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();