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

Commit c9e18a0

Browse files
committed
Merge branch 'erdraug-master'
2 parents 8142957 + 6a5004a commit c9e18a0

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

library/Zend/Db/Select.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Zend_Db_Select
8181
const SQL_ASC = 'ASC';
8282
const SQL_DESC = 'DESC';
8383

84-
const REGEX_COLUMN_EXPR = '/^([\w]*\(([^\(\)]|(?1))*\))$/';
84+
const REGEX_COLUMN_EXPR = '/^([\w]*\s*\(([^\(\)]|(?1))*\))$/';
8585

8686
/**
8787
* Bind variables for query
@@ -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 = trim(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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,4 +1011,45 @@ 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+
1032+
public function testAssembleQueryWithRawSubqueryInSelectBlock() {
1033+
$columns[] = '(SELECT *
1034+
FROM tb2) as subInSelect2';
1035+
$select = $this->_db->select();
1036+
$select->from(array('t' => 'table1'), $columns);
1037+
1038+
$expected = 'SELECT (SELECT * FROM tb2) AS "subInSelect2" FROM "table1" AS "t"';
1039+
1040+
$this->assertEquals($expected, $select->assemble(),
1041+
'Assembling query with raw subquery with "new line" char failed');
1042+
}
1043+
1044+
public function testAssembleQueryWithExpressionInSelectBlock() {
1045+
$columns[] = ' DISTINCT (*) as expr';
1046+
$select = $this->_db->select();
1047+
$select->from(array('t' => 'table1'), $columns);
1048+
1049+
$expected = 'SELECT DISTINCT (*) AS "expr" FROM "table1" AS "t"';
1050+
1051+
$this->assertEquals($expected, $select->assemble(),
1052+
'Assembling query with raw subquery with "new line" char failed');
1053+
}
1054+
10141055
}

0 commit comments

Comments
 (0)