Commit 7914695e authored by zYne's avatar zYne

DQL update: AND an OR as equivalent operators for && and ||

parent 120d1856
......@@ -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);
......
<?php
require_once("Part.php");
Doctrine::autoload("Doctrine_Query_Part");
abstract class Doctrine_Query_Condition extends Doctrine_Query_Part {
/**
......@@ -13,7 +13,7 @@ abstract class Doctrine_Query_Condition extends Doctrine_Query_Part {
final public function parse($str) {
$tmp = trim($str);
$parts = Doctrine_Query::bracketExplode($str," && ","(",")");
$parts = Doctrine_Query::bracketExplode($str, array(' \&\& ', ' AND '), "(", ")");
if(count($parts) > 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) {
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment