diff --git a/library/Zend/Db/Select.php b/library/Zend/Db/Select.php index 13ca8defb2..d7b5c337ba 100644 --- a/library/Zend/Db/Select.php +++ b/library/Zend/Db/Select.php @@ -81,7 +81,7 @@ class Zend_Db_Select const SQL_ASC = 'ASC'; const SQL_DESC = 'DESC'; - const REGEX_COLUMN_EXPR = '/^([\w]*\(([^\(\)]|(?1))*\))$/'; + const REGEX_COLUMN_EXPR = '/^([\w]*\s*\(([^\(\)]|(?1))*\))$/'; /** * Bind variables for query @@ -940,7 +940,7 @@ protected function _tableCols($correlationName, $cols, $afterCorrelationName = n $currentCorrelationName = $correlationName; if (is_string($col)) { // Check for a column matching " AS " and extract the alias name - $col = str_replace("\n",' ',$col); + $col = trim(str_replace("\n",' ',$col)); if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) { $col = $m[1]; $alias = $m[2]; diff --git a/tests/Zend/Db/Select/StaticTest.php b/tests/Zend/Db/Select/StaticTest.php index 41d4720063..b72a55d522 100644 --- a/tests/Zend/Db/Select/StaticTest.php +++ b/tests/Zend/Db/Select/StaticTest.php @@ -1041,4 +1041,15 @@ public function testAssembleQueryWithRawSubqueryInSelectBlock() { 'Assembling query with raw subquery with "new line" char failed'); } + public function testAssembleQueryWithExpressionInSelectBlock() { + $columns[] = ' DISTINCT (*) as expr'; + $select = $this->_db->select(); + $select->from(array('t' => 'table1'), $columns); + + $expected = 'SELECT DISTINCT (*) AS "expr" FROM "table1" AS "t"'; + + $this->assertEquals($expected, $select->assemble(), + 'Assembling query with raw subquery with "new line" char failed'); + } + }