Skip to content

Commit

Permalink
Fix the quoteName method for an alias of the column containing a peri…
Browse files Browse the repository at this point in the history
…od (#148)

* Fix the quoteName method for the alias of the column containing the period

* Add missing name quotes for MS SQL Server
  • Loading branch information
csthomas authored and wilsonge committed Dec 19, 2018
1 parent f84c22a commit a5fd31d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Tests/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,12 @@ public function testQuoteName()
'Tests the left-right quotes on a dotted string.'
);

$this->assertThat(
$this->instance->quoteName('a.test', 'a.test'),
$this->equalTo('[a].[test] AS [a.test]'),
'Tests the left-right quotes on a dotted string for column alias.'
);

$this->assertThat(
$this->instance->quoteName('a.te]st'),
$this->equalTo('[a].[te]]st]'),
Expand Down
12 changes: 9 additions & 3 deletions src/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ public function quoteName($name, $as = null)

if ($as !== null)
{
$name .= ' AS ' . $this->quoteNameString($as);
$name .= ' AS ' . $this->quoteNameString($as, true);
}

return $name;
Expand Down Expand Up @@ -1509,19 +1509,25 @@ public function quoteName($name, $as = null)
/**
* Quote string coming from quoteName call.
*
* @param string $name Identifier name to be quoted.
* @param string $name Identifier name to be quoted.
* @param boolean $asSinglePart Treat the name as a single part of the identifier.
*
* @return string Quoted identifier string.
*
* @since __DEPLOY_VERSION__
*/
protected function quoteNameString($name)
protected function quoteNameString($name, $asSinglePart = false)
{
$q = $this->nameQuote . $this->nameQuote;

// Double quote reserved keyword
$name = str_replace($q[1], $q[1] . $q[1], $name);

if ($asSinglePart)
{
return $q[0] . $name . $q[1];
}

return $q[0] . str_replace('.', "$q[1].$q[0]", $name) . $q[1];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sqlsrv/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SqlsrvDriver extends DatabaseDriver
* @var string
* @since 1.0
*/
protected $nameQuote;
protected $nameQuote = '[]';

/**
* The null or zero representation of a timestamp for the database driver. This should be
Expand Down

0 comments on commit a5fd31d

Please sign in to comment.