Explorar o código

Solve problem with detection of expressions in column if there are additional space chars in it.

Alex Komarov %!s(int64=10) %!d(string=hai) anos
pai
achega
2dc5433cb0
Modificáronse 2 ficheiros con 13 adicións e 2 borrados
  1. 2 2
      library/Zend/Db/Select.php
  2. 11 0
      tests/Zend/Db/Select/StaticTest.php

+ 2 - 2
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 @@ class Zend_Db_Select
             $currentCorrelationName = $correlationName;
             if (is_string($col)) {
                 // Check for a column matching "<column> AS <alias>" 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];

+ 11 - 0
tests/Zend/Db/Select/StaticTest.php

@@ -1041,4 +1041,15 @@ FROM tb2) as subInSelect2';
                             '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');
+    }
+
 }