Skip to content

Commit

Permalink
Database: fixed auto adding operator
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 27, 2013
1 parent c5c5983 commit 4a97e26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
19 changes: 10 additions & 9 deletions Nette/Database/Table/SqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ public function addWhere($condition, $parameters = array())
$replace = NULL;
$placeholderNum = 0;
foreach ($args as $arg) {
preg_match('#(?:.*?\?.*?){' . $placeholderNum . '}((?:(<|>|=|LIKE|IN)\s*)?(%)?\?(%)?)#', $condition, $match, PREG_OFFSET_CAPTURE);
preg_match('#(?:.*?\?.*?){' . $placeholderNum . '}(((?:&|\||^|~|\+|-|\*|/|%|\(|,|<|>|=|ALL|AND|ANY|BETWEEN|EXISTS|IN|LIKE|OR|NOT|SOME)\s*)?\?)#', $condition, $match, PREG_OFFSET_CAPTURE);
$hasOperator = ($match[1][0] === '?' && $match[1][1] === 0) ? TRUE : !empty($match[2][0]);

if ($arg === NULL) {
if (!empty($match[2][0])) {
if ($hasOperator) {
throw new Nette\InvalidArgumentException('Column operator does not accept NULL argument.');
}
$replace = 'IS NULL';
Expand Down Expand Up @@ -198,23 +199,23 @@ public function addWhere($condition, $parameters = array())
} elseif ($arg instanceof SqlLiteral) {
$this->parameters[] = $arg;
} elseif (is_array($arg)) {
if (!empty($match[2][0])) {
if ($match[2][0] !== 'IN') {
if ($hasOperator) {
if (trim($match[2][0]) !== 'IN') {
throw new Nette\InvalidArgumentException('Column operator does not accept array argument.');
}
} else {
$match[2][0] = 'IN';
$match[2][0] = 'IN ';
}

if (!$arg) {
$replace = $match[2][0] . ' (NULL)';
$replace = $match[2][0] . '(NULL)';
} else {
$replace = $match[2][0] . ' (?)';
$replace = $match[2][0] . '(?)';
$this->parameters[] = $arg;
}
} else {
if (!empty($match[2][0])) {
$replace = $match[2][0] . ' ?';
if ($hasOperator) {
$replace = $match[2][0] . '?';
} else {
$replace = '= ?';
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Nette/Database/SqlBuilder.addWhere().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ $sqlBuilder[7] = new SqlBuilder('book', $connection, $reflection);
$sqlBuilder[7]->addWhere('id = ? OR id ? OR id IN ? OR id LIKE ? OR id > ?', 1, 2, array(1, 2), "%test", 3);
$sqlBuilder[7]->addWhere('name', "var");

// auto operator tests
$sqlBuilder[8] = new SqlBuilder('book', $connection, $reflection);
$sqlBuilder[8]->addWhere('FOO(?)', 1);
$sqlBuilder[8]->addWhere('FOO(id, ?)', 1);
$sqlBuilder[8]->addWhere('id & ? = ?', 1, 1);
$sqlBuilder[8]->addWhere('?', 1);
$sqlBuilder[8]->addWhere('NOT ? OR ?', 1, 1);
$sqlBuilder[8]->addWhere('? + ? - ? / ? * ? % ?', 1, 1, 1, 1, 1, 1);

switch ($driverName) {
case 'mysql':
Assert::equal('SELECT * FROM `book` WHERE (`id` = ? OR `id` IS NULL)', $sqlBuilder[0]->buildSelectQuery());
Expand All @@ -67,6 +76,7 @@ switch ($driverName) {
Assert::equal('SELECT * FROM `book` WHERE (`id` = ? OR `id` = ? OR `id` IN (?))', $sqlBuilder[5]->buildSelectQuery());
Assert::equal('SELECT * FROM `book` WHERE (`id` IN (NULL))', $sqlBuilder[6]->buildSelectQuery());
Assert::equal('SELECT * FROM `book` WHERE (`id` = ? OR `id` = ? OR `id` IN (?) OR `id` LIKE ? OR `id` > ?) AND (`name` = ?)', $sqlBuilder[7]->buildSelectQuery());
Assert::equal('SELECT * FROM `book` WHERE (FOO(?)) AND (FOO(`id`, ?)) AND (`id` & ? = ?) AND (?) AND (NOT ? OR ?) AND (? + ? - ? / ? * ? % ?)', $sqlBuilder[8]->buildSelectQuery());
break;

case 'pgsql':
Expand All @@ -78,6 +88,7 @@ switch ($driverName) {
Assert::equal('SELECT * FROM "book" WHERE ("id" = ? OR "id" = ? OR "id" IN (?))', $sqlBuilder[5]->buildSelectQuery());
Assert::equal('SELECT * FROM "book" WHERE ("id" IN (NULL))', $sqlBuilder[6]->buildSelectQuery());
Assert::equal('SELECT * FROM "book" WHERE ("id" = ? OR "id" = ? OR "id" IN (?) OR "id" LIKE ? OR "id" > ?) AND ("name" = ?)', $sqlBuilder[7]->buildSelectQuery());
Assert::equal('SELECT * FROM `book` WHERE (FOO(?)) AND (FOO("id", ?)) AND ("id" & ? = ?) AND (?) AND (NOT ? OR ?) AND (? + ? - ? / ? * ? % ?)', $sqlBuilder[8]->buildSelectQuery());
break;
}

Expand Down

0 comments on commit 4a97e26

Please sign in to comment.