From 6866ca42a0d32419a11edc739c2676b37bcb1eec Mon Sep 17 00:00:00 2001 From: nate Date: Sat, 9 Jun 2007 20:32:14 +0000 Subject: [PATCH] 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 --- cake/libs/model/datasources/dbo_source.php | 5 ++-- .../model/datasources/dbo_source.test.php | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 8257c06b1..1c45709ec 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -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]); diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index 4106107fe..4f717daef 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -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(){