Merge pull request #11616 from cakephp/issue-11186

Fix missing field identifier quoting for COUNT(DISTINCT in SQLServer.
This commit is contained in:
Mark Sch 2018-02-05 14:48:22 +01:00 committed by GitHub
commit df9c7fd96e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View file

@ -302,6 +302,10 @@ class Sqlserver extends DboSource {
$prepend = 'DISTINCT ';
$fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
}
if (strpos($fields[$i], 'COUNT(DISTINCT') !== false) {
$prepend = 'COUNT(DISTINCT ';
$fields[$i] = trim(str_replace('COUNT(DISTINCT', '', $this->_quoteFields($fields[$i])));
}
if (!preg_match('/\s+AS\s+/i', $fields[$i])) {
if (substr($fields[$i], -1) === '*') {

View file

@ -383,6 +383,10 @@ class SqlserverTest extends CakeTestCase {
$result = $this->db->fields($this->model, null, 'DISTINCT Car.country_code');
$expected = array('DISTINCT [Car].[country_code] AS [Car__country_code]');
$this->assertEquals($expected, $result);
$result = $this->db->fields($this->model, null, 'COUNT(DISTINCT Car.country_code)');
$expected = array('COUNT(DISTINCT [Car].[country_code]) AS [Car__country_code]');
$this->assertEquals($expected, $result);
}
/**