Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Commit

Permalink
Fixed DB expressions detection in column
Browse files Browse the repository at this point in the history
  • Loading branch information
foxel committed May 19, 2015
1 parent d036788 commit 1dce43c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions library/Zend/Db/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Zend_Db_Select
const SQL_ASC = 'ASC';
const SQL_DESC = 'DESC';

const REGEX_COLUMN_EXPR = '/^([\w]*\(([^\(\)]|(?1))*\))$/';

/**
* Bind variables for query
*
Expand Down Expand Up @@ -509,7 +511,7 @@ public function group($spec)
}

foreach ($spec as $val) {
if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $val)) {
if (preg_match(self::REGEX_COLUMN_EXPR, (string) $val)) {
$val = new Zend_Db_Expr($val);
}
$this->_parts[self::GROUP][] = $val;
Expand Down Expand Up @@ -601,7 +603,7 @@ public function order($spec)
$val = trim($matches[1]);
$direction = $matches[2];
}
if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $val)) {
if (preg_match(self::REGEX_COLUMN_EXPR, (string) $val)) {
$val = new Zend_Db_Expr($val);
}
$this->_parts[self::ORDER][] = array($val, $direction);
Expand Down Expand Up @@ -943,7 +945,7 @@ protected function _tableCols($correlationName, $cols, $afterCorrelationName = n
$alias = $m[2];
}
// Check for columns that look like functions and convert to Zend_Db_Expr
if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $col)) {
if (preg_match(self::REGEX_COLUMN_EXPR, (string) $col)) {
$col = new Zend_Db_Expr($col);
} elseif (preg_match('/(.+)\.(.+)/', $col, $m)) {
$currentCorrelationName = $m[1];
Expand Down
20 changes: 20 additions & 0 deletions tests/Zend/Db/Select/StaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,26 @@ public function testNestedIfInColumn()
$this->assertEquals("SELECT \"table1\".*, IF(table2.id IS NOT NULL, IF(table2.id2 IS NOT NULL, 1, 2), 0) AS \"bar\" FROM \"table1\"\n INNER JOIN \"table2\" ON table1.id = table2.id", $select->assemble());
}

public function testDeepNestedIfInColumn()
{
$select = $this->_db->select();
$select->from('table1', '*');
$select->join(array('table2'),
'table1.id = table2.id',
array('bar' => 'IF(table2.id IS NOT NULL, IF(table2.id2 IS NOT NULL, SUM(1), 2), 0)'));
$this->assertEquals("SELECT \"table1\".*, IF(table2.id IS NOT NULL, IF(table2.id2 IS NOT NULL, SUM(1), 2), 0) AS \"bar\" FROM \"table1\"\n INNER JOIN \"table2\" ON table1.id = table2.id", $select->assemble());
}

public function testNestedUnbalancedParenthesesInColumn()
{
$select = $this->_db->select();
$select->from('table1', '*');
$select->join(array('table2'),
'table1.id = table2.id',
array('bar' => 'IF(SUM()'));
$this->assertEquals("SELECT \"table1\".*, \"table2\".\"IF(SUM()\" AS \"bar\" FROM \"table1\"\n INNER JOIN \"table2\" ON table1.id = table2.id", $select->assemble());
}

/**
* @group ZF-378
*/
Expand Down

0 comments on commit 1dce43c

Please sign in to comment.