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

Commit e78669d

Browse files
committed
Merge pull request zendframework#441 from Mayflower/nested-sql-functions
Loosen regex to allow nested function calls in SQL
2 parents e5c1deb + c630cda commit e78669d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

library/Zend/Db/Select.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public function group($spec)
509509
}
510510

511511
foreach ($spec as $val) {
512-
if (preg_match('/^[\w]*\([^\)]*\)$/', (string) $val)) {
512+
if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $val)) {
513513
$val = new Zend_Db_Expr($val);
514514
}
515515
$this->_parts[self::GROUP][] = $val;
@@ -601,7 +601,7 @@ public function order($spec)
601601
$val = trim($matches[1]);
602602
$direction = $matches[2];
603603
}
604-
if (preg_match('/^[\w]*\([^\)]*\)$/', $val)) {
604+
if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $val)) {
605605
$val = new Zend_Db_Expr($val);
606606
}
607607
$this->_parts[self::ORDER][] = array($val, $direction);
@@ -943,7 +943,7 @@ protected function _tableCols($correlationName, $cols, $afterCorrelationName = n
943943
$alias = $m[2];
944944
}
945945
// Check for columns that look like functions and convert to Zend_Db_Expr
946-
if (preg_match('/^[\w]*\([^\)]*\)$/', $col)) {
946+
if (preg_match('/^([\w]*\(([^\)]|(?1))*\))$/', (string) $col)) {
947947
$col = new Zend_Db_Expr($col);
948948
} elseif (preg_match('/(.+)\.(.+)/', $col, $m)) {
949949
$currentCorrelationName = $m[1];

tests/Zend/Db/Select/StaticTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,26 @@ public function testSqlInjectionInColumn()
854854
$this->assertEquals('SELECT "p"."MD5(1); drop table products; -- )" FROM "products" AS "p"', $select->assemble());
855855
}
856856

857+
public function testIfInColumn()
858+
{
859+
$select = $this->_db->select();
860+
$select->from('table1', '*');
861+
$select->join(array('table2'),
862+
'table1.id = table2.id',
863+
array('bar' => 'IF(table2.id IS NOT NULL, 1, 0)'));
864+
$this->assertEquals("SELECT \"table1\".*, IF(table2.id IS NOT NULL, 1, 0) AS \"bar\" FROM \"table1\"\n INNER JOIN \"table2\" ON table1.id = table2.id", $select->assemble());
865+
}
866+
867+
public function testNestedIfInColumn()
868+
{
869+
$select = $this->_db->select();
870+
$select->from('table1', '*');
871+
$select->join(array('table2'),
872+
'table1.id = table2.id',
873+
array('bar' => 'IF(table2.id IS NOT NULL, IF(table2.id2 IS NOT NULL, 1, 2), 0)'));
874+
$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());
875+
}
876+
857877
/**
858878
* @group ZF-378
859879
*/

0 commit comments

Comments
 (0)