Skip to content

Commit

Permalink
Quote SELECT identifiers only if autoquoting is enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jan 17, 2020
1 parent 5a827cf commit 4949b72
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/Database/QueryCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,25 @@ protected function _sqlCompiler(string &$sql, Query $query, ValueBinder $generat
*/
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';
}
$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;
}
Expand Down
10 changes: 7 additions & 3 deletions tests/TestCase/Database/Driver/SqlserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,11 @@ public function testSelectLimitOldServer()
$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());
Expand All @@ -340,7 +344,7 @@ public function testSelectLimitOldServer()
->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());
Expand All @@ -352,7 +356,7 @@ public function testSelectLimitOldServer()
->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());
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2794,7 +2794,7 @@ public function testAutoFieldsWithContainQueryBuilder()
->enableHydration(false)
->contain([
'Authors' => function ($q) {
return $q->select(['compute' => '(SELECT 2 + 20)'])
return $q->select(['computed' => '(SELECT 2 + 20)'])
->enableAutoFields();
},
])
Expand All @@ -2805,7 +2805,7 @@ public function testAutoFieldsWithContainQueryBuilder()
$this->assertArrayHasKey('author', $result);
$this->assertNotNull($result['author']);
$this->assertArrayHasKey('name', $result['author']);
$this->assertArrayHasKey('compute', $result);
$this->assertArrayHasKey('computed', $result);
}

/**
Expand Down

0 comments on commit 4949b72

Please sign in to comment.