DQL update: AND an OR as equivalent operators for && and ||
This commit is contained in:
parent
120d18565c
commit
7914695e6a
@ -541,11 +541,12 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function bracketExplode($str,$d,$e1 = '(',$e2 = ')') {
|
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);
|
$a = explode("$d",$str);
|
||||||
else
|
|
||||||
$a = explode("$d",$str);
|
|
||||||
|
|
||||||
$i = 0;
|
$i = 0;
|
||||||
$term = array();
|
$term = array();
|
||||||
foreach($a as $key=>$val) {
|
foreach($a as $key=>$val) {
|
||||||
@ -803,6 +804,13 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
if( ! $this->aggregate)
|
if( ! $this->aggregate)
|
||||||
$this->loadFields($table, $fetchmode, $fields, $currPath);
|
$this->loadFields($table, $fetchmode, $fields, $currPath);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* parseAggregateFunction
|
||||||
|
*
|
||||||
|
* @param string $func
|
||||||
|
* @param string $reference
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function parseAggregateFunction($func,$reference) {
|
public function parseAggregateFunction($func,$reference) {
|
||||||
$pos = strpos($func,"(");
|
$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;
|
$this->aggregate = true;
|
||||||
$pos = strpos($fullName,"(");
|
$pos = strpos($fullName,"(");
|
||||||
$name = substr($fullName, 0, $pos);
|
$name = substr($fullName, 0, $pos);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once("Part.php");
|
Doctrine::autoload("Doctrine_Query_Part");
|
||||||
|
|
||||||
abstract class Doctrine_Query_Condition extends 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) {
|
final public function parse($str) {
|
||||||
$tmp = trim($str);
|
$tmp = trim($str);
|
||||||
|
|
||||||
$parts = Doctrine_Query::bracketExplode($str," && ","(",")");
|
$parts = Doctrine_Query::bracketExplode($str, array(' \&\& ', ' AND '), "(", ")");
|
||||||
if(count($parts) > 1) {
|
if(count($parts) > 1) {
|
||||||
$ret = array();
|
$ret = array();
|
||||||
foreach($parts as $part) {
|
foreach($parts as $part) {
|
||||||
@ -23,7 +23,7 @@ abstract class Doctrine_Query_Condition extends Doctrine_Query_Part {
|
|||||||
$r = implode(" AND ",$ret);
|
$r = implode(" AND ",$ret);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$parts = Doctrine_Query::bracketExplode($str," || ","(",")");
|
$parts = Doctrine_Query::bracketExplode($str, array(' \|\| ', ' OR '), "(", ")");
|
||||||
if(count($parts) > 1) {
|
if(count($parts) > 1) {
|
||||||
$ret = array();
|
$ret = array();
|
||||||
foreach($parts as $part) {
|
foreach($parts as $part) {
|
||||||
|
@ -29,6 +29,87 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
|
|||||||
parent::prepareTables();
|
parent::prepareTables();
|
||||||
$this->connection->clear();
|
$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() {
|
public function testUnknownFunction() {
|
||||||
$q = new Doctrine_Query();
|
$q = new Doctrine_Query();
|
||||||
$f = false;
|
$f = false;
|
||||||
@ -288,41 +369,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
|
|||||||
$this->assertEqual(($count + 1), $this->dbh->count());
|
$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() {
|
public function testSelfReferencing() {
|
||||||
$query = new Doctrine_Query($this->connection);
|
$query = new Doctrine_Query($this->connection);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user