bugfix in WhereClause: negate didn't wrap multiple clauses in extra parens

Fixes #41
This commit is contained in:
Sergey Tsalkov
2017-10-15 19:10:35 +00:00
parent 22e49b280e
commit 8e19d9d6c3
2 changed files with 13 additions and 3 deletions

View File

@@ -873,8 +873,8 @@ class WhereClause {
$args = array_merge($args, $clause_args);
}
if ($this->type == 'and') $sql = implode(' AND ', $sql);
else $sql = implode(' OR ', $sql);
if ($this->type == 'and') $sql = sprintf('(%s)', implode(' AND ', $sql));
else $sql = sprintf('(%s)', implode(' OR ', $sql));
if ($this->negate) $sql = '(NOT ' . $sql . ')';
return array($sql, $args);

View File

@@ -58,7 +58,17 @@ class WhereClauseTest extends SimpleTest {
$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->add('username=%s', 'Bart');
$where->add('username=%s', 'Abe');