Skip to content

Commit

Permalink
fix SQL function usage with multiple parameters, fixes #110
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkez committed Feb 15, 2022
1 parent ebaf85c commit c79cdff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/db/cortex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2723,9 +2723,14 @@ public function prepareFilter($cond, $engine, $db, $fieldConf=null, $primary='id
list($parts, $args) = $this->convertNamedParams($parts, $args);
$ncond = [];
foreach ($parts as &$part) {
// enhanced IN handling
if (is_int(strpos($part, '?'))) {
// arg handling
$argCount = substr_count($part, '?');
if ($argCount > 1) {
// function parameters like `foo(?,?,?)`
$ncond = array_merge($ncond, array_splice($args, 0, $argCount));
} elseif ($argCount === 1) {
$val = array_shift($args);
// enhanced IN operator args expansion
if (is_int($pos = strpos($part, ' IN ?'))) {
if ($val instanceof CortexCollection)
$val = $val->getAll('_id',TRUE);
Expand All @@ -2734,7 +2739,9 @@ public function prepareFilter($cond, $engine, $db, $fieldConf=null, $primary='id
$bindMarks = str_repeat('?,',count($val) - 1).'?';
$part = substr($part, 0, $pos).' IN ('.$bindMarks.') ';
$ncond = array_merge($ncond, $val);
} elseif($val === null &&
}
// comparison against NULL
elseif($val === null &&
preg_match('/((?:\S[\w\-]+\S.?)+)\s*'.
'(!?==?)\s*(?:\?|:\w+)/i',$part,$match)) {
$part = ' '.$match[1].' IS '.($match[2][0]=='!'?'NOT ':'').'NULL ';
Expand Down
17 changes: 16 additions & 1 deletion test/test_common.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,22 @@ function run()
10,20,30,40,50,60),
'merge multiple filters');

$qp = new \DB\CortexQueryParser();

$test->expect(
$qp->prepareFilter(['foo > bar'],'sql', $f3->DB) === ['`foo` > `bar`'],
'auto-escape fields'
);
$test->expect(
$qp->prepareFilter(['created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)'],'sql', $f3->DB) === ['`created_at` > DATE_SUB(NOW(), INTERVAL 1 DAY)'],
'respect function when auto-escaping'
);
$test->expect(
$qp->prepareFilter(['foo(?,?,3,?)',1,2,4],'sql', $f3->DB) === ['foo(?,?,3,?)',1,2,4],
'query parser: correct function args'
);

///////////////////////////////////
return $test->results();
}
}
}

0 comments on commit c79cdff

Please sign in to comment.