From 7914695e6a72251cca3086422b81e2179de785be Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 15 Sep 2006 10:48:59 +0000 Subject: [PATCH] DQL update: AND an OR as equivalent operators for && and || --- Doctrine/Query.php | 18 ++++-- Doctrine/Query/Condition.php | 6 +- tests/QueryTestCase.php | 116 ++++++++++++++++++++++++----------- 3 files changed, 97 insertions(+), 43 deletions(-) diff --git a/Doctrine/Query.php b/Doctrine/Query.php index 7220b947e..5ca6c9d85 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -541,11 +541,12 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { * */ public static function bracketExplode($str,$d,$e1 = '(',$e2 = ')') { - if(is_array($d)) + if(is_array($d)) { + $a = preg_split('/('.implode('|', $d).')/', $str); + $d = stripslashes($d[0]); + } else $a = explode("$d",$str); - else - $a = explode("$d",$str); - + $i = 0; $term = array(); foreach($a as $key=>$val) { @@ -803,6 +804,13 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { if( ! $this->aggregate) $this->loadFields($table, $fetchmode, $fields, $currPath); } + /** + * parseAggregateFunction + * + * @param string $func + * @param string $reference + * @return string + */ public function parseAggregateFunction($func,$reference) { $pos = strpos($func,"("); @@ -833,7 +841,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { } } } - final public function parseAggregateValues($fullName, $tableName, array $exploded, $currPath) { + public function parseAggregateValues($fullName, $tableName, array $exploded, $currPath) { $this->aggregate = true; $pos = strpos($fullName,"("); $name = substr($fullName, 0, $pos); diff --git a/Doctrine/Query/Condition.php b/Doctrine/Query/Condition.php index 56f204d47..1da6a23b5 100644 --- a/Doctrine/Query/Condition.php +++ b/Doctrine/Query/Condition.php @@ -1,5 +1,5 @@ 1) { $ret = array(); foreach($parts as $part) { @@ -23,7 +23,7 @@ abstract class Doctrine_Query_Condition extends Doctrine_Query_Part { $r = implode(" AND ",$ret); } else { - $parts = Doctrine_Query::bracketExplode($str," || ","(",")"); + $parts = Doctrine_Query::bracketExplode($str, array(' \|\| ', ' OR '), "(", ")"); if(count($parts) > 1) { $ret = array(); foreach($parts as $part) { diff --git a/tests/QueryTestCase.php b/tests/QueryTestCase.php index a9394c537..5485b7afb 100644 --- a/tests/QueryTestCase.php +++ b/tests/QueryTestCase.php @@ -29,6 +29,87 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { parent::prepareTables(); $this->connection->clear(); } + public function testBracktExplode() { + $str = "item OR item || item"; + $parts = Doctrine_Query::bracketExplode($str, array(' \|\| ', ' OR '), "(", ")"); + + $this->assertEqual($parts, array('item','item','item')); + + } + + public function testConditionParser() { + $query = new Doctrine_Query($this->connection); + + $query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'"); + + $sql = "SELECT entity.id AS entity__id FROM entity WHERE (entity.name LIKE 'z%' OR entity.name LIKE 's%') AND (entity.type = 0)"; + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(User.name LIKE 'z%') || (User.name LIKE 's%')"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("((User.name LIKE 'z%') || (User.name LIKE 's%'))"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(((User.name LIKE 'z%') || (User.name LIKE 's%')))"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(((User.name LIKE 'z%') || User.name LIKE 's%'))"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(User.name LIKE 'z%') || User.name LIKE 's%' && User.name LIKE 'a%'"); + + $sql = "SELECT entity.id AS entity__id FROM entity WHERE ((entity.name LIKE 'z%' OR entity.name LIKE 's%') AND entity.name LIKE 'a%') AND (entity.type = 0)"; + + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%'"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("((((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%')"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(((((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%'))"); + $this->assertEqual($query->getQuery(), $sql); + + } + + public function testConditionParser2() { + $query = new Doctrine_Query($this->connection); + + $query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'"); + + $sql = "SELECT entity.id AS entity__id FROM entity WHERE (entity.name LIKE 'z%' OR entity.name LIKE 's%') AND (entity.type = 0)"; + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(User.name LIKE 'z%') OR (User.name LIKE 's%')"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("((User.name LIKE 'z%') OR (User.name LIKE 's%'))"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(((User.name LIKE 'z%') OR (User.name LIKE 's%')))"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(((User.name LIKE 'z%') OR User.name LIKE 's%'))"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(User.name LIKE 'z%') OR User.name LIKE 's%' AND User.name LIKE 'a%'"); + + $sql = "SELECT entity.id AS entity__id FROM entity WHERE ((entity.name LIKE 'z%' OR entity.name LIKE 's%') AND entity.name LIKE 'a%') AND (entity.type = 0)"; + + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(((User.name LIKE 'z%') OR User.name LIKE 's%')) AND User.name LIKE 'a%'"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("((((User.name LIKE 'z%') OR User.name LIKE 's%')) AND User.name LIKE 'a%')"); + $this->assertEqual($query->getQuery(), $sql); + + $query->where("(((((User.name LIKE 'z%') OR User.name LIKE 's%')) AND User.name LIKE 'a%'))"); + $this->assertEqual($query->getQuery(), $sql); + } + public function testUnknownFunction() { $q = new Doctrine_Query(); $f = false; @@ -288,41 +369,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertEqual(($count + 1), $this->dbh->count()); } - public function testConditionParser() { - $query = new Doctrine_Query($this->connection); - - $query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'"); - - $sql = "SELECT entity.id AS entity__id FROM entity WHERE (entity.name LIKE 'z%' OR entity.name LIKE 's%') AND (entity.type = 0)"; - $this->assertEqual($query->getQuery(), $sql); - - $query->where("(User.name LIKE 'z%') || (User.name LIKE 's%')"); - $this->assertEqual($query->getQuery(), $sql); - - $query->where("((User.name LIKE 'z%') || (User.name LIKE 's%'))"); - $this->assertEqual($query->getQuery(), $sql); - - $query->where("(((User.name LIKE 'z%') || (User.name LIKE 's%')))"); - $this->assertEqual($query->getQuery(), $sql); - - $query->where("(((User.name LIKE 'z%') || User.name LIKE 's%'))"); - $this->assertEqual($query->getQuery(), $sql); - - $query->where("(User.name LIKE 'z%') || User.name LIKE 's%' && User.name LIKE 'a%'"); - - $sql = "SELECT entity.id AS entity__id FROM entity WHERE ((entity.name LIKE 'z%' OR entity.name LIKE 's%') AND entity.name LIKE 'a%') AND (entity.type = 0)"; - - $this->assertEqual($query->getQuery(), $sql); - - $query->where("(((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%'"); - $this->assertEqual($query->getQuery(), $sql); - - $query->where("((((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%')"); - $this->assertEqual($query->getQuery(), $sql); - - $query->where("(((((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%'))"); - $this->assertEqual($query->getQuery(), $sql); - } public function testSelfReferencing() { $query = new Doctrine_Query($this->connection);