diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 043f9b79c6a7..11c3412a45e7 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -966,16 +966,15 @@ protected function wrapJsonSelector($value) * Split the given JSON selector into the field and the optional path and wrap them separately. * * @param string $column - * @param string $delimiter * @return array */ - protected function wrapJsonFieldAndPath($column, $delimiter = '->') + protected function wrapJsonFieldAndPath($column) { - $parts = explode($delimiter, $column, 2); + $parts = explode('->', $column, 2); $field = $this->wrap($parts[0]); - $path = count($parts) > 1 ? ', '.$this->wrapJsonPath($parts[1], $delimiter) : ''; + $path = count($parts) > 1 ? ', '.$this->wrapJsonPath($parts[1], '->') : ''; return [$field, $path]; } diff --git a/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php index 20b1daac4a0a..e7a1eead5295 100755 --- a/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php @@ -3,7 +3,6 @@ namespace Illuminate\Database\Query\Grammars; use Illuminate\Support\Arr; -use Illuminate\Support\Str; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\JsonExpression; @@ -319,16 +318,8 @@ protected function wrapValue($value) */ protected function wrapJsonSelector($value) { - $delimiter = Str::contains($value, '->>') ? '->>' : '->'; + list($field, $path) = $this->wrapJsonFieldAndPath($value); - list($field, $path) = $this->wrapJsonFieldAndPath($value, $delimiter); - - $selector = 'json_extract('.$field.$path.')'; - - if ($delimiter === '->>') { - $selector = 'json_unquote('.$selector.')'; - } - - return $selector; + return 'json_unquote(json_extract('.$field.$path.'))'; } } diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 11cecd8c62ba..f5e86011e206 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -2031,7 +2031,7 @@ public function testMySqlWrappingJsonWithString() { $builder = $this->getMySqlBuilder(); $builder->select('*')->from('users')->where('items->sku', '=', 'foo-bar'); - $this->assertEquals('select * from `users` where json_extract(`items`, \'$."sku"\') = ?', $builder->toSql()); + $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."sku"\')) = ?', $builder->toSql()); $this->assertCount(1, $builder->getRawBindings()['where']); $this->assertEquals('foo-bar', $builder->getRawBindings()['where'][0]); } @@ -2040,43 +2040,28 @@ public function testMySqlWrappingJsonWithInteger() { $builder = $this->getMySqlBuilder(); $builder->select('*')->from('users')->where('items->price', '=', 1); - $this->assertEquals('select * from `users` where json_extract(`items`, \'$."price"\') = ?', $builder->toSql()); + $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."price"\')) = ?', $builder->toSql()); } public function testMySqlWrappingJsonWithDouble() { $builder = $this->getMySqlBuilder(); $builder->select('*')->from('users')->where('items->price', '=', 1.5); - $this->assertEquals('select * from `users` where json_extract(`items`, \'$."price"\') = ?', $builder->toSql()); + $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."price"\')) = ?', $builder->toSql()); } public function testMySqlWrappingJsonWithBoolean() { $builder = $this->getMySqlBuilder(); $builder->select('*')->from('users')->where('items->available', '=', true); - $this->assertEquals('select * from `users` where json_extract(`items`, \'$."available"\') = true', $builder->toSql()); + $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."available"\')) = true', $builder->toSql()); } public function testMySqlWrappingJsonWithBooleanAndIntegerThatLooksLikeOne() { $builder = $this->getMySqlBuilder(); $builder->select('*')->from('users')->where('items->available', '=', true)->where('items->active', '=', false)->where('items->number_available', '=', 0); - $this->assertEquals('select * from `users` where json_extract(`items`, \'$."available"\') = true and json_extract(`items`, \'$."active"\') = false and json_extract(`items`, \'$."number_available"\') = ?', $builder->toSql()); - } - - public function testMySqlWrappingJsonWithoutQuote() - { - $builder = $this->getMySqlBuilder(); - $builder->select('*')->from('users')->where('items->>sku', '=', 'foo-bar'); - $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."sku"\')) = ?', $builder->toSql()); - $this->assertCount(1, $builder->getRawBindings()['where']); - $this->assertEquals('foo-bar', $builder->getRawBindings()['where'][0]); - - $builder = $this->getMySqlBuilder(); - $builder->select('*')->from('users')->where('items->>price->>in_usd', '=', 'foo-bar'); - $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."price"."in_usd"\')) = ?', $builder->toSql()); - $this->assertCount(1, $builder->getRawBindings()['where']); - $this->assertEquals('foo-bar', $builder->getRawBindings()['where'][0]); + $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."available"\')) = true and json_unquote(json_extract(`items`, \'$."active"\')) = false and json_unquote(json_extract(`items`, \'$."number_available"\')) = ?', $builder->toSql()); } public function testMySqlWrappingJson() @@ -2087,15 +2072,15 @@ public function testMySqlWrappingJson() $builder = $this->getMySqlBuilder(); $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()); + $this->assertEquals('select json_unquote(json_extract(`items`, \'$."price"\')) from `users` where json_unquote(json_extract(`users`.`items`, \'$."price"\')) = ? order by json_unquote(json_extract(`items`, \'$."price"\')) asc', $builder->toSql()); $builder = $this->getMySqlBuilder(); $builder->select('*')->from('users')->where('items->price->in_usd', '=', 1); - $this->assertEquals('select * from `users` where json_extract(`items`, \'$."price"."in_usd"\') = ?', $builder->toSql()); + $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."price"."in_usd"\')) = ?', $builder->toSql()); $builder = $this->getMySqlBuilder(); $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()); + $this->assertEquals('select * from `users` where json_unquote(json_extract(`items`, \'$."price"."in_usd"\')) = ? and json_unquote(json_extract(`items`, \'$."age"\')) = ?', $builder->toSql()); } public function testPostgresWrappingJson()