Skip to content
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

[5.4] Can't Wrap DB Names Containing Spaces When Aliasing #17312

Merged
merged 2 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ public function wrap($value, $prefixAlias = false)
*/
protected function wrapAliasedValue($value, $prefixAlias = false)
{
$segments = explode(' ', $value);
$segments = preg_split( '/\s+as\s+/i', $value );

// If we are wrapping a table we need to prefix the alias with the table prefix
// as well in order to generate proper syntax. If this is a column of course
// no prefix is necessary. The condition will be true when from wrapTable.
if ($prefixAlias) {
$segments[2] = $this->tablePrefix.$segments[2];
$segments[1] = $this->tablePrefix.$segments[1];
}

return $this->wrap(
$segments[0]).' as '.$this->wrapValue($segments[2]
$segments[0]).' as '.$this->wrapValue($segments[1]
);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public function testAliasWrappingAsWholeConstant()
$builder->select('x.y as foo.bar')->from('baz');
$this->assertEquals('select "x"."y" as "foo.bar" from "baz"', $builder->toSql());
}

public function testAliasWrappingWithSpacesInDatabaseName()
{
$builder = $this->getBuilder();
$builder->select('w x.y.z as foo.bar')->from('baz');
$this->assertEquals('select "w x"."y"."z" as "foo.bar" from "baz"', $builder->toSql());
}

public function testAddingSelects()
{
Expand Down