Skip to content

Commit

Permalink
[5.6] Handle unquoted JSON selector for MYSQL (#24817)
Browse files Browse the repository at this point in the history
* Return unquoted Json Select

I can't see a practical use where we would need quoted output.
Defaulting the wrapper as unquoted using ->>

* Backward compatible

* Style fixes

* Style fixes

* Create Associated test

* Fix Style
  • Loading branch information
chrishubert authored and taylorotwell committed Jul 12, 2018
1 parent 6f7116e commit 21709c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,15 @@ protected function wrapValue($value)
*/
protected function wrapJsonSelector($value)
{
$path = explode('->', $value);
$delimiter = str_contains($value, '->>')
? '->>'
: '->';

$path = explode($delimiter, $value);

$field = $this->wrapSegments(explode('.', array_shift($path)));

return sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) {
return sprintf('%s'.$delimiter.'\'$.%s\'', $field, collect($path)->map(function ($part) {
return '"'.$part.'"';
})->implode('.'));
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,15 @@ public function testMySqlWrappingJsonWithBooleanAndIntegerThatLooksLikeOne()
$this->assertEquals('select * from `users` where `items`->\'$."available"\' = true and `items`->\'$."active"\' = false and `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 `items`->>\'$."sku"\' = ?', $builder->toSql());
$this->assertCount(1, $builder->getRawBindings()['where']);
$this->assertEquals('foo-bar', $builder->getRawBindings()['where'][0]);
}

public function testMySqlWrappingJson()
{
$builder = $this->getMySqlBuilder();
Expand Down

0 comments on commit 21709c9

Please sign in to comment.