[5.6] Fix JSON queries with table names on SQL Server #24542
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
JSON queries on SQL Server don't yet support columns with table names:
Table names have been broken because JSON queries work differently on SQL Server: While MySQL and PostgreSQL use the arrow operator (
column->path
), SQL Server uses JSON functions.This led to incorrect SQL:
where [users].json_value([options], '$."language"') = ?
.Unfortunately, the fix requires some refactoring. We have to move the use of
wrapJsonSelector()
fromwrapValue()
up towrap()
. We could integrate it intoDatabase\Grammar::wrap()
, but I think that it rather belongs intoDatabase\Query\Grammars\Grammar
since it doesn't concernSchema
grammars.I also used the opportunity to improve the SQL Server grammar by extracting
wrapJsonPath()
. It's a better solution than replacing the names of JSON functions in SQL strings.I added table name tests for all databases.
The PR is also a preparation for supporting JSON queries on MariaDB (laravel/ideas#811):
When replacing MySQL's arrow operator with the equivalent
JSON_EXTRACT()
, we would have run into the same wrapping problems.