Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 1396c59

Browse files
committed
Solve problem with subqueries in SELECT block
It solves executing queries with subqueries in SELECT block. "New line" char (which is added in _renderFrom function while imploding joins) causes problem in regexp for "AS" case detection
1 parent c57660e commit 1396c59

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

library/Zend/Db/Select.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ protected function _tableCols($correlationName, $cols, $afterCorrelationName = n
940940
$currentCorrelationName = $correlationName;
941941
if (is_string($col)) {
942942
// Check for a column matching "<column> AS <alias>" and extract the alias name
943+
$col = str_replace("\n",'',$col);
943944
if (preg_match('/^(.+)\s+' . self::SQL_AS . '\s+(.+)$/i', $col, $m)) {
944945
$col = $m[1];
945946
$alias = $m[2];

tests/Zend/Db/Select/StaticTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,4 +1011,22 @@ public function testOrderOfDeepUnbalancedConditionalFieldWithDirection()
10111011
$this->assertEquals($expected, $select->assemble());
10121012
}
10131013

1014+
/**
1015+
* 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
1016+
*/
1017+
public function testAssembleQueryWithSubqueryInSelectBlock() {
1018+
$subSelect = $this->_db->select();
1019+
$subSelect->from(array('st1' => 'subTable1'), 'col1')
1020+
->join(array('st2' => 'subTable2'), 'st1.fk_id=st2.fk_id', 'col2');
1021+
1022+
$columns[] = '('.$subSelect->assemble() . ') as subInSelect';
1023+
$select = $this->_db->select();
1024+
$select->from(array('t' => 'table1'), $columns);
1025+
1026+
$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"';
1027+
1028+
$this->assertEquals($expected, $select->assemble(),
1029+
'Assembling query with subquery with join failed');
1030+
}
1031+
10141032
}

0 commit comments

Comments
 (0)