Browse Source

Merge branch 'erdraug-master'

Enrico Zimuel 10 years ago
parent
commit
c9e18a0d18
2 changed files with 43 additions and 1 deletions
  1. 2 1
      library/Zend/Db/Select.php
  2. 41 0
      tests/Zend/Db/Select/StaticTest.php

+ 2 - 1
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,6 +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 = trim(str_replace("\n", ' ', $col));
                 if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) {
                     $col = $m[1];
                     $alias = $m[2];

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

@@ -1011,4 +1011,45 @@ class Zend_Db_Select_StaticTest extends Zend_Db_Select_TestCommon
         $this->assertEquals($expected, $select->assemble());
     }
 
+    /**
+     * Test a problem with assembling subqueries with joins in SELECT block. That problem is caused by "new line" char which brakes regexp detection of "AS"-case
+     */
+    public function testAssembleQueryWithSubqueryInSelectBlock() {
+        $subSelect = $this->_db->select();
+        $subSelect->from(array('st1' => 'subTable1'), 'col1')
+                  ->join(array('st2' => 'subTable2'), 'st1.fk_id=st2.fk_id', 'col2');
+
+        $columns[] = '('.$subSelect->assemble() . ') as subInSelect';
+        $select = $this->_db->select();
+        $select->from(array('t' => 'table1'), $columns);
+
+        $expected = 'SELECT (SELECT "st1"."col1", "st2"."col2" FROM "subTable1" AS "st1"  INNER JOIN "subTable2" AS "st2" ON st1.fk_id=st2.fk_id) AS "subInSelect" FROM "table1" AS "t"';
+
+        $this->assertEquals($expected, $select->assemble(),
+                            'Assembling query with subquery with join failed');
+    }
+
+    public function testAssembleQueryWithRawSubqueryInSelectBlock() {
+        $columns[] = '(SELECT *
+FROM tb2) as subInSelect2';
+        $select = $this->_db->select();
+        $select->from(array('t' => 'table1'), $columns);
+
+        $expected = 'SELECT (SELECT * FROM tb2) AS "subInSelect2" FROM "table1" AS "t"';
+
+        $this->assertEquals($expected, $select->assemble(),
+                            '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');
+    }
+
 }