فهرست منبع

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

Alex Komarov 10 سال پیش
والد
کامیت
2dc5433cb0
2فایلهای تغییر یافته به همراه13 افزوده شده و 2 حذف شده
  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_ASC        = 'ASC';
     const SQL_DESC       = 'DESC';
     const SQL_DESC       = 'DESC';
 
 
-    const REGEX_COLUMN_EXPR = '/^([\w]*\(([^\(\)]|(?1))*\))$/';
+    const REGEX_COLUMN_EXPR = '/^([\w]*\s*\(([^\(\)]|(?1))*\))$/';
 
 
     /**
     /**
      * Bind variables for query
      * Bind variables for query
@@ -940,7 +940,7 @@ class Zend_Db_Select
             $currentCorrelationName = $correlationName;
             $currentCorrelationName = $correlationName;
             if (is_string($col)) {
             if (is_string($col)) {
                 // Check for a column matching "<column> AS <alias>" and extract the alias name
                 // 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)) {
                 if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) {
                     $col = $m[1];
                     $col = $m[1];
                     $alias = $m[2];
                     $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');
                             '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');
+    }
+
 }
 }