Adding query conditions parenthesis fix for Ticket #2663

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5267 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-06-09 20:32:14 +00:00
parent 95bb7e0f04
commit 6866ca42a0
2 changed files with 22 additions and 7 deletions

View file

@ -1514,10 +1514,11 @@ class DboSource extends DataSource {
if (up(trim($key)) == 'NOT') {
$key = 'AND ' . $key;
}
$out[] = 'NOT (' . join(') ' . strtoupper($key) . ' (', $value) . ')';
$not = 'NOT ';
} else {
$out[] = '(' . join(') ' . strtoupper($key) . ' (', $value) . ')';
$not = null;
}
$out[] = $not . '((' . join(') ' . strtoupper($key) . ' (', $value) . '))';
} else {
if (is_string($value) && preg_match('/^\{\$__cakeIdentifier\[(.*)\]__\$}$/', $value, $identifier) && isset($identifier[1])) {
$data .= $this->name($key) . ' = ' . $this->name($identifier[1]);

View file

@ -1552,19 +1552,19 @@ class DboSourceTest extends UnitTestCase {
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array('or' => array( 'score' => 'BETWEEN 4 AND 5', 'rating' => '> 20') ));
$expected = " WHERE (`score` BETWEEN '4' AND '5') OR (`rating` > 20)";
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > 20))";
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array('or' => array('score' => 'BETWEEN 4 AND 5', array('score' => '> 20')) ));
$expected = " WHERE (`score` BETWEEN '4' AND '5') OR (`score` > 20)";
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > 20))";
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array('and' => array( 'score' => 'BETWEEN 4 AND 5', array('score' => '> 20')) ));
$expected = " WHERE (`score` BETWEEN '4' AND '5') AND (`score` > 20)";
$expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > 20))";
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array('published' => 1, 'or' => array('score' => '< 2', array('score' => '> 20')) ));
$expected = " WHERE `published` = 1 AND (`score` < 2) OR (`score` > 20)";
$expected = " WHERE `published` = 1 AND ((`score` < 2) OR (`score` > 20))";
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array(array('Project.removed' => false)));
@ -1590,7 +1590,7 @@ class DboSourceTest extends UnitTestCase {
'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)),
'Enrollment.yearcompleted' => '> 0')
);
$this->assertPattern('/^\s*WHERE\s+NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(`level_of_education_id` IN \(912, 999\)\s*\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+0\s*$/', $result);
$this->assertPattern('/^\s*WHERE\s+NOT\s+\(\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(`level_of_education_id` IN \(912, 999\)\s*\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+0\s*$/', $result);
$result = $this->db->conditions(array('id' => '<> 8'));
$this->assertPattern('/^\s*WHERE\s+`id`\s+<>\s+8\s*$/', $result);
@ -1598,6 +1598,20 @@ class DboSourceTest extends UnitTestCase {
$result = $this->db->conditions(array('TestModel.field' => '= gribe$@()lu'));
$expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'";
$this->assertEqual($result, $expected);
$conditions['NOT'] = array('Listing.expiration' => "BETWEEN 1 AND 100");
$conditions[0]['OR'] = array(
"Listing.title" => "LIKE %term%",
"Listing.description" => "LIKE %term%"
);
$conditions[1]['OR'] = array(
"Listing.title" => "LIKE %term_2%",
"Listing.description" => "LIKE %term_2%"
);
$result = $this->db->conditions($conditions);
$expected = " WHERE NOT ((`Listing`.`expiration` BETWEEN '1' AND '100')) AND ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))";
$this->assertEqual($result, $expected);
}
function testMixedConditionsParsing(){