From d4887a050f1673da963e38152fdd657daae79433 Mon Sep 17 00:00:00 2001 From: Alex Komarov Date: Tue, 13 Oct 2015 13:50:54 +0300 Subject: [PATCH] Change "new line" char on space char instead of blank char to preserve a very rare case with raw subquery with "new line" chars inside --- library/Zend/Db/Select.php | 2 +- tests/Zend/Db/Select/StaticTest.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/library/Zend/Db/Select.php b/library/Zend/Db/Select.php index 168669d503..13ca8defb2 100644 --- a/library/Zend/Db/Select.php +++ b/library/Zend/Db/Select.php @@ -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 = 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 f47d7d8639..41d4720063 100644 --- a/tests/Zend/Db/Select/StaticTest.php +++ b/tests/Zend/Db/Select/StaticTest.php @@ -1023,10 +1023,22 @@ public function testAssembleQueryWithSubqueryInSelectBlock() { $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"'; + $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'); + } + }