From a5fd31db83795a3b290537248e842c76927533de Mon Sep 17 00:00:00 2001 From: Tomasz Narloch Date: Wed, 19 Dec 2018 23:57:44 +0100 Subject: [PATCH] Fix the quoteName method for an alias of the column containing a period (#148) * Fix the quoteName method for the alias of the column containing the period * Add missing name quotes for MS SQL Server --- Tests/DriverTest.php | 6 ++++++ src/DatabaseDriver.php | 12 +++++++++--- src/Sqlsrv/SqlsrvDriver.php | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Tests/DriverTest.php b/Tests/DriverTest.php index 39fe3161a..cff5f61e8 100644 --- a/Tests/DriverTest.php +++ b/Tests/DriverTest.php @@ -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]'), diff --git a/src/DatabaseDriver.php b/src/DatabaseDriver.php index 467fb647f..c39f98a15 100644 --- a/src/DatabaseDriver.php +++ b/src/DatabaseDriver.php @@ -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; @@ -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]; } diff --git a/src/Sqlsrv/SqlsrvDriver.php b/src/Sqlsrv/SqlsrvDriver.php index 2fd3ccac0..0a93a78dd 100644 --- a/src/Sqlsrv/SqlsrvDriver.php +++ b/src/Sqlsrv/SqlsrvDriver.php @@ -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