Browse Source

Quote SELECT identifiers only if autoquoting is enabled.

ADmad 6 years ago
parent
commit
4949b72607

+ 8 - 2
src/Database/QueryCompiler.php

@@ -163,7 +163,6 @@ class QueryCompiler
      */
     protected function _buildSelectPart(array $parts, Query $query, ValueBinder $generator): string
     {
-        $driver = $query->getConnection()->getDriver();
         $select = 'SELECT%s %s%s';
         if ($this->_orderedUnion && $query->clause('union')) {
             $select = '(SELECT%s %s%s';
@@ -171,11 +170,18 @@ class QueryCompiler
         $distinct = $query->clause('distinct');
         $modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $generator);
 
+        $driver = $query->getConnection()->getDriver();
+        $autoQuotingEnabled = $driver->isAutoQuotingEnabled();
         $normalized = [];
         $parts = $this->_stringifyExpressions($parts, $generator);
         foreach ($parts as $k => $p) {
             if (!is_numeric($k)) {
-                $p = $p . ' AS ' . $driver->quoteIdentifier($k);
+                $p = $p . ' AS ';
+                if ($autoQuotingEnabled) {
+                    $p .= $driver->quoteIdentifier($k);
+                } else {
+                    $p .= $k;
+                }
             }
             $normalized[] = $p;
         }

+ 7 - 3
tests/TestCase/Database/Driver/SqlserverTest.php

@@ -330,7 +330,11 @@ class SqlserverTest extends TestCase
         $query->select(['id', 'title'])
             ->from('articles')
             ->offset(10);
-        $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) AS [_cake_page_rownum_] ' .
+        $identifier = '_cake_page_rownum_';
+        if ($connection->getDriver()->isAutoQuotingEnabled()) {
+            $identifier = $connection->getDriver()->quoteIdentifier($identifier);
+        }
+        $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) AS ' . $identifier . ' ' .
             'FROM articles) _cake_paging_ ' .
             'WHERE _cake_paging_._cake_page_rownum_ > 10';
         $this->assertEquals($expected, $query->sql());
@@ -340,7 +344,7 @@ class SqlserverTest extends TestCase
             ->from('articles')
             ->order(['id'])
             ->offset(10);
-        $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY id)) AS [_cake_page_rownum_] ' .
+        $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY id)) AS ' . $identifier . ' ' .
             'FROM articles) _cake_paging_ ' .
             'WHERE _cake_paging_._cake_page_rownum_ > 10';
         $this->assertEquals($expected, $query->sql());
@@ -352,7 +356,7 @@ class SqlserverTest extends TestCase
             ->where(['title' => 'Something'])
             ->limit(10)
             ->offset(50);
-        $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY id)) AS [_cake_page_rownum_] ' .
+        $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY id)) AS ' . $identifier . ' ' .
             'FROM articles WHERE title = :c0) _cake_paging_ ' .
             'WHERE (_cake_paging_._cake_page_rownum_ > 50 AND _cake_paging_._cake_page_rownum_ <= 60)';
         $this->assertEquals($expected, $query->sql());

+ 2 - 2
tests/TestCase/ORM/QueryTest.php

@@ -2794,7 +2794,7 @@ class QueryTest extends TestCase
             ->enableHydration(false)
             ->contain([
                 'Authors' => function ($q) {
-                    return $q->select(['compute' => '(SELECT 2 + 20)'])
+                    return $q->select(['computed' => '(SELECT 2 + 20)'])
                         ->enableAutoFields();
                 },
             ])
@@ -2805,7 +2805,7 @@ class QueryTest extends TestCase
         $this->assertArrayHasKey('author', $result);
         $this->assertNotNull($result['author']);
         $this->assertArrayHasKey('name', $result['author']);
-        $this->assertArrayHasKey('compute', $result);
+        $this->assertArrayHasKey('computed', $result);
     }
 
     /**