-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Allow Proper Wrapping of Database Names Containing Spaces When Aliasi… #17292
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…ng Columns (Perfectly Legal in SQL Server) Currently, when using SQL Server, if you have a database which includes spaces, the grammar handles wrapping it fine. So this: My Sample Database.dbo.my_table.my_column Becomes: [My Sample Database].[dbo].[my_table].[my_column] But if you also try to alias a column when the database contains spaces, it breaks. This: My Sample Database.dbo.my_table.my_column AS my_aliased_column Becomes: [My] AS [Sample] I have traced the error to Database/Grammar using explode( ' ' ) - the code thinks the spaces in the database name are the spaces around the "AS". You can fix this by using regex instead of explode, splitting on the "AS" more accurately. Just replace the explode with this: $segments = preg_split( '/\s+as\s+/i', $value ); Please note: When you have an Eloquent belongsToMany method that has to include the database name (eg. we need to join across DBs), this type of alias is created automatically, generating this error.
This should be overriden for only the Sqlite database grammer |
@morloderex Maybe to SQL Server Grammar then, but MySQL, PostgreSQL and Sqlite also supports spaces on names, so I guess this bug is affecting all of the Grammars... |
Needs tests before it can be merged. |
Please send to 5.4 branch. |
I have opened a pull request to 5.4, as asked: #17312 |
taylorotwell
pushed a commit
that referenced
this pull request
Jan 16, 2017
* Can't Wrap DB Names Containing Spaces When Aliasing Database names containing spaces are perfectly legal in Microsoft SQL Server. Currently, when using SQL Server, if you have a database which includes spaces, the grammar handles wrapping it fine. So this: My Sample Database.dbo.my_table.my_column Correctly becomes: [My Sample Database].[dbo].[my_table].[my_column] *But* if you also try to alias a column when the database contains spaces, it breaks. This: My Sample Database.dbo.my_table.my_column AS my_aliased_column Becomes: [My] AS [Sample] I have traced the error to Database/Grammar/wrapAliasedValue() using explode( ' ' ) - the code thinks the spaces in the database name are the spaces around the "AS". We can fix this by using regex instead of explode, splitting on the "AS" more accurately. Just replace the explode with this: $segments = preg_split( '/\s+as\s+/i', $value ); Note: When you have an Eloquent belongsToMany method that has to include the database name (eg. we need to join across DBs), this type of alias is created automatically, generating this error. I originally committed this to 5.3 (#17292) and was asked to commit here instead. * Adding Tests RE: #17312
symfony-splitter
pushed a commit
to illuminate/database
that referenced
this pull request
Jan 16, 2017
* Can't Wrap DB Names Containing Spaces When Aliasing Database names containing spaces are perfectly legal in Microsoft SQL Server. Currently, when using SQL Server, if you have a database which includes spaces, the grammar handles wrapping it fine. So this: My Sample Database.dbo.my_table.my_column Correctly becomes: [My Sample Database].[dbo].[my_table].[my_column] *But* if you also try to alias a column when the database contains spaces, it breaks. This: My Sample Database.dbo.my_table.my_column AS my_aliased_column Becomes: [My] AS [Sample] I have traced the error to Database/Grammar/wrapAliasedValue() using explode( ' ' ) - the code thinks the spaces in the database name are the spaces around the "AS". We can fix this by using regex instead of explode, splitting on the "AS" more accurately. Just replace the explode with this: $segments = preg_split( '/\s+as\s+/i', $value ); Note: When you have an Eloquent belongsToMany method that has to include the database name (eg. we need to join across DBs), this type of alias is created automatically, generating this error. I originally committed this to 5.3 (laravel/framework#17292) and was asked to commit here instead. * Adding Tests RE: laravel/framework#17312
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
…ng Columns (Perfectly Legal in SQL Server)
Currently, when using SQL Server, if you have a database which includes spaces, the grammar handles wrapping it fine.
So this:
My Sample Database.dbo.my_table.my_column
Becomes:
[My Sample Database].[dbo].[my_table].[my_column]
But if you also try to alias a column when the database contains spaces, it breaks.
This:
My Sample Database.dbo.my_table.my_column AS my_aliased_column
Becomes:
[My] AS [Sample]
I have traced the error to Database/Grammar using explode( ' ' ) - the code thinks the spaces in the database name are the spaces around the "AS".
You can fix this by using regex instead of explode, splitting on the "AS" more accurately.
Just replace the explode with this:
$segments = preg_split( '/\s+as\s+/i', $value );
Please note: When you have an Eloquent belongsToMany method that has to include the database name (eg. we need to join across DBs), this type of alias is created automatically, generating this error.