bugfix in WhereClause: negate didn't wrap multiple clauses in extra parens
Fixes #41
This commit is contained in:
@@ -873,8 +873,8 @@ class WhereClause {
|
|||||||
$args = array_merge($args, $clause_args);
|
$args = array_merge($args, $clause_args);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->type == 'and') $sql = implode(' AND ', $sql);
|
if ($this->type == 'and') $sql = sprintf('(%s)', implode(' AND ', $sql));
|
||||||
else $sql = implode(' OR ', $sql);
|
else $sql = sprintf('(%s)', implode(' OR ', $sql));
|
||||||
|
|
||||||
if ($this->negate) $sql = '(NOT ' . $sql . ')';
|
if ($this->negate) $sql = '(NOT ' . $sql . ')';
|
||||||
return array($sql, $args);
|
return array($sql, $args);
|
||||||
|
|||||||
@@ -58,7 +58,17 @@ class WhereClauseTest extends SimpleTest {
|
|||||||
$this->assert($result[0]['age'] === '15');
|
$this->assert($result[0]['age'] === '15');
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_6_or() {
|
function test_6_negate_two() {
|
||||||
|
$where = new WhereClause('and');
|
||||||
|
$where->add('password=%s', 'hello');
|
||||||
|
$where->add('username=%s', 'Bart');
|
||||||
|
$where->negate();
|
||||||
|
|
||||||
|
$result = DB::query("SELECT * FROM accounts WHERE %l", $where);
|
||||||
|
$this->assert(count($result) === 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_7_or() {
|
||||||
$where = new WhereClause('or');
|
$where = new WhereClause('or');
|
||||||
$where->add('username=%s', 'Bart');
|
$where->add('username=%s', 'Bart');
|
||||||
$where->add('username=%s', 'Abe');
|
$where->add('username=%s', 'Abe');
|
||||||
|
|||||||
Reference in New Issue
Block a user