Commit 0062d9c3 authored by doctrine's avatar doctrine

Complete DQL rewrite, Fixed : Ticket #13

parent 94433f90
...@@ -42,10 +42,6 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -42,10 +42,6 @@ class Doctrine_Query extends Doctrine_Access {
private $inheritanceApplied = false; private $inheritanceApplied = false;
private $aggregate = false; private $aggregate = false;
/**
* @var array $connectors component connectors
*/
private $connectors = array();
/** /**
* @var array $tableAliases * @var array $tableAliases
*/ */
...@@ -54,6 +50,10 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -54,6 +50,10 @@ class Doctrine_Query extends Doctrine_Access {
* @var array $tableIndexes * @var array $tableIndexes
*/ */
private $tableIndexes = array(); private $tableIndexes = array();
/**
* @var array $paths
*/
private $paths = array();
/** /**
* @var array $parts SQL query string parts * @var array $parts SQL query string parts
*/ */
...@@ -126,7 +126,6 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -126,7 +126,6 @@ class Doctrine_Query extends Doctrine_Access {
$this->inheritanceApplied = false; $this->inheritanceApplied = false;
$this->aggregate = false; $this->aggregate = false;
$this->data = array(); $this->data = array();
$this->connectors = array();
$this->collections = array(); $this->collections = array();
$this->joined = array(); $this->joined = array();
$this->joins = array(); $this->joins = array();
...@@ -144,7 +143,7 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -144,7 +143,7 @@ class Doctrine_Query extends Doctrine_Access {
* @param array $names fields to be loaded (only used in lazy property loading) * @param array $names fields to be loaded (only used in lazy property loading)
* @return void * @return void
*/ */
private function loadFields(Doctrine_Table $table, $fetchmode, array $names) { private function loadFields(Doctrine_Table $table, $fetchmode, array $names, $cpath) {
$name = $table->getComponentName(); $name = $table->getComponentName();
switch($fetchmode): switch($fetchmode):
...@@ -152,31 +151,32 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -152,31 +151,32 @@ class Doctrine_Query extends Doctrine_Access {
$this->limit = $table->getAttribute(Doctrine::ATTR_COLL_LIMIT); $this->limit = $table->getAttribute(Doctrine::ATTR_COLL_LIMIT);
case Doctrine::FETCH_IMMEDIATE: case Doctrine::FETCH_IMMEDIATE:
if( ! empty($names)) if( ! empty($names))
throw new Doctrine_Exception("Lazy property loading can only be used with fetching strategies lazy, batch and lazyoffset."); $names = array_unique(array_merge($table->getPrimaryKeys(), $names));
else
$names = $table->getColumnNames(); $names = $table->getColumnNames();
break; break;
case Doctrine::FETCH_LAZY_OFFSET: case Doctrine::FETCH_LAZY_OFFSET:
$this->limit = $table->getAttribute(Doctrine::ATTR_COLL_LIMIT); $this->limit = $table->getAttribute(Doctrine::ATTR_COLL_LIMIT);
case Doctrine::FETCH_LAZY: case Doctrine::FETCH_LAZY:
case Doctrine::FETCH_BATCH: case Doctrine::FETCH_BATCH:
$names = array_merge($table->getPrimaryKeys(), $names); $names = array_unique(array_merge($table->getPrimaryKeys(), $names));
break; break;
default: default:
throw new Doctrine_Exception("Unknown fetchmode."); throw new Doctrine_Exception("Unknown fetchmode.");
endswitch; endswitch;
$component = $table->getComponentName(); $component = $table->getComponentName();
$tablename = $this->tableAliases[$cpath];
$this->fetchModes[$tablename] = $fetchmode;
$this->fetchModes[$component] = $fetchmode;
$tablename = $this->tableAliases[$component];
$count = count($this->tables); $count = count($this->tables);
foreach($names as $name) { foreach($names as $name) {
if($count == 0) { if($count == 0) {
$this->parts["columns"][] = $tablename.".".$name; $this->parts["columns"][] = $tablename.".".$name;
} else { } else {
$this->parts["columns"][] = $tablename.".".$name." AS ".$component."__".$name; $this->parts["columns"][] = $tablename.".".$name." AS ".$tablename."__".$name;
} }
} }
} }
...@@ -209,6 +209,8 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -209,6 +209,8 @@ class Doctrine_Query extends Doctrine_Access {
$this->joins = array(); $this->joins = array();
$this->tables = array(); $this->tables = array();
$this->fetchModes = array(); $this->fetchModes = array();
$this->tableIndexes = array();
$this->tableAliases = array();
default: default:
$this->parts[$name] = array(); $this->parts[$name] = array();
$this->$method($args[0]); $this->$method($args[0]);
...@@ -259,6 +261,8 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -259,6 +261,8 @@ class Doctrine_Query extends Doctrine_Access {
$this->joins = array(); $this->joins = array();
$this->tables = array(); $this->tables = array();
$this->fetchModes = array(); $this->fetchModes = array();
$this->tableIndexes = array();
$this->tableAliases = array();
default: default:
$this->parts[$name] = array(); $this->parts[$name] = array();
$this->$method($value); $this->$method($value);
...@@ -324,10 +328,8 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -324,10 +328,8 @@ class Doctrine_Query extends Doctrine_Access {
// get the inheritance maps // get the inheritance maps
$array = array(); $array = array();
foreach($this->tables as $table): foreach($this->tables as $alias => $table):
$component = $table->getComponentName(); $array[$alias][] = $table->getInheritanceMap();
$tableName = $this->tableAliases[$component];
$array[$tableName][] = $table->getInheritanceMap();
endforeach; endforeach;
// apply inheritance maps // apply inheritance maps
...@@ -444,6 +446,7 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -444,6 +446,7 @@ class Doctrine_Query extends Doctrine_Access {
$keys = array_keys($this->tables); $keys = array_keys($this->tables);
$root = $keys[0]; $root = $keys[0];
$stmt = $this->session->execute($query,$params); $stmt = $this->session->execute($query,$params);
$previd = array(); $previd = array();
...@@ -480,14 +483,16 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -480,14 +483,16 @@ class Doctrine_Query extends Doctrine_Access {
} }
$name = $this->tables[$key]->getComponentName(); $name = $key;
if($emptyID) { if($emptyID) {
$pointer = $this->joins[$name]; $pointer = $this->joins[$name];
$alias = $this->tables[$pointer]->getAlias($name); $path = array_search($name, $this->tableAliases);
$alias = end( explode(".", $path));
$fk = $this->tables[$pointer]->getForeignKey($alias); $fk = $this->tables[$pointer]->getForeignKey($alias);
if( ! isset($prev[$pointer]) ) if( ! isset($prev[$pointer]) )
continue; continue;
...@@ -502,7 +507,7 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -502,7 +507,7 @@ class Doctrine_Query extends Doctrine_Access {
if($last instanceof Doctrine_Record) { if($last instanceof Doctrine_Record) {
if( ! $last->hasReference($alias)) { if( ! $last->hasReference($alias)) {
$prev[$name] = $this->getCollection($name); $prev[$name] = $this->getCollection($name);
$last->initReference($prev[$name],$this->connectors[$name]); $last->initReference($prev[$name],$fk);
} }
} }
endswitch; endswitch;
...@@ -517,6 +522,7 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -517,6 +522,7 @@ class Doctrine_Query extends Doctrine_Access {
if($previd[$name] !== $row) { if($previd[$name] !== $row) {
// set internal data // set internal data
$this->tables[$name]->setData($row); $this->tables[$name]->setData($row);
// initialize a new record // initialize a new record
...@@ -528,7 +534,8 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -528,7 +534,8 @@ class Doctrine_Query extends Doctrine_Access {
} else { } else {
$pointer = $this->joins[$name]; $pointer = $this->joins[$name];
$alias = $this->tables[$pointer]->getAlias($name); $path = array_search($name, $this->tableAliases);
$alias = end( explode(".", $path));
$fk = $this->tables[$pointer]->getForeignKey($alias); $fk = $this->tables[$pointer]->getForeignKey($alias);
$last = $prev[$pointer]->getLast(); $last = $prev[$pointer]->getLast();
...@@ -537,7 +544,7 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -537,7 +544,7 @@ class Doctrine_Query extends Doctrine_Access {
case Doctrine_Relation::ONE_AGGREGATE: case Doctrine_Relation::ONE_AGGREGATE:
// one-to-one relation // one-to-one relation
$last->internalSet($this->connectors[$name]->getLocal(), $record->getID()); $last->internalSet($fk->getLocal(), $record->getID());
$last->initSingleReference($record); $last->initSingleReference($record);
...@@ -548,7 +555,7 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -548,7 +555,7 @@ class Doctrine_Query extends Doctrine_Access {
if( ! $last->hasReference($alias)) { if( ! $last->hasReference($alias)) {
$prev[$name] = $this->getCollection($name); $prev[$name] = $this->getCollection($name);
$last->initReference($prev[$name],$this->connectors[$name]); $last->initReference($prev[$name], $fk);
} else { } else {
// previous entry found from identityMap // previous entry found from identityMap
$prev[$name] = $last->get($alias); $prev[$name] = $last->get($alias);
...@@ -610,7 +617,7 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -610,7 +617,7 @@ class Doctrine_Query extends Doctrine_Access {
* @param integer $index * @param integer $index
*/ */
private function getCollection($name) { private function getCollection($name) {
$table = $this->session->getTable($name); $table = $this->tables[$name];
switch($this->fetchModes[$name]): switch($this->fetchModes[$name]):
case Doctrine::FETCH_BATCH: case Doctrine::FETCH_BATCH:
$coll = new Doctrine_Collection_Batch($table); $coll = new Doctrine_Collection_Batch($table);
...@@ -627,6 +634,8 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -627,6 +634,8 @@ class Doctrine_Query extends Doctrine_Access {
case Doctrine::FETCH_LAZY_OFFSET: case Doctrine::FETCH_LAZY_OFFSET:
$coll = new Doctrine_Collection_LazyOffset($table); $coll = new Doctrine_Collection_LazyOffset($table);
break; break;
default:
throw new Doctrine_Exception("Unknown fetchmode");
endswitch; endswitch;
$coll->populate($this); $coll->populate($this);
...@@ -657,6 +666,9 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -657,6 +666,9 @@ class Doctrine_Query extends Doctrine_Access {
} }
/** /**
* DQL PARSER * DQL PARSER
* parses a DQL query
* first splits the query in parts and then uses individual
* parsers for each part
* *
* @param string $query DQL query * @param string $query DQL query
* @return void * @return void
...@@ -733,7 +745,8 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -733,7 +745,8 @@ class Doctrine_Query extends Doctrine_Access {
$name = end($a); $name = end($a);
$this->load($reference, false); $this->load($reference, false);
$tname = $this->tables[$name]->getTableName(); $alias = $this->tableAliases[$reference];
$tname = $this->tables[$alias]->getTableName();
$r = $tname.".".$field; $r = $tname.".".$field;
if(isset($e[1])) if(isset($e[1]))
...@@ -978,9 +991,7 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -978,9 +991,7 @@ class Doctrine_Query extends Doctrine_Access {
$field = array_pop($a); $field = array_pop($a);
$reference = implode(".",$a); $reference = implode(".",$a);
$table = $this->load($reference, false); $table = $this->load($reference, false);
$component = $table->getComponentName(); $func = $this->tableAliases[$reference].".".$field;
$func = $this->tableAliases[$component].".".$field;
return $func; return $func;
} else { } else {
...@@ -990,8 +1001,10 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -990,8 +1001,10 @@ class Doctrine_Query extends Doctrine_Access {
} }
/** /**
* loadHaving * loadHaving
* returns the parsed query part
* *
* @param string $having * @param string $having
* @return string
*/ */
private function loadHaving($having) { private function loadHaving($having) {
$e = self::bracketExplode($having," ","(",")"); $e = self::bracketExplode($having," ","(",")");
...@@ -1009,8 +1022,10 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -1009,8 +1022,10 @@ class Doctrine_Query extends Doctrine_Access {
} }
/** /**
* loadWhere * loadWhere
* returns the parsed query part
* *
* @param string $where * @param string $where
* @return string
*/ */
private function loadWhere($where) { private function loadWhere($where) {
$e = explode(" ",$where); $e = explode(" ",$where);
...@@ -1026,54 +1041,96 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -1026,54 +1041,96 @@ class Doctrine_Query extends Doctrine_Access {
$count = count($a); $count = count($a);
$table = $this->load($reference, false); $table = $this->load($reference, false);
$where = $this->tableAliases[$reference].".".$field." ".$operator." ".$value;
$component = $table->getComponentName();
$where = $this->tableAliases[$component].".".$field." ".$operator." ".$value;
} }
return $where; return $where;
} }
/** /**
* generateAlias
*
* @param string $tableName
* @return string
*/
final public function generateAlias($tableName) {
if(isset($this->tableIndexes[$tableName]))
return $tableName.++$this->tableIndexes[$tableName];
else {
$this->tableIndexes[$tableName] = 1;
return $tableName;
}
}
/**
* getTableAlias
*
* @param string $path
* @return string
*/
final public function getTableAlias($path) {
if( ! isset($this->tableAliases[$path]))
return false;
return $this->tableAliases[$path];
}
/**
* loads a component
*
* @param string $path the path of the loadable component * @param string $path the path of the loadable component
* @param integer $fetchmode optional fetchmode, if not set the components default fetchmode will be used * @param integer $fetchmode optional fetchmode, if not set the components default fetchmode will be used
* @throws DQLException * @throws DQLException
* @return Doctrine_Table
*/ */
final public function load($path, $loadFields = true) { final public function load($path, $loadFields = true) {
$e = preg_split("/[.:]/",$path); $e = preg_split("/[.:]/",$path);
$index = 0; $index = 0;
$currPath = '';
foreach($e as $key => $fullname) { foreach($e as $key => $fullname) {
try { try {
$copy = $e;
$e2 = preg_split("/[-(]/",$fullname); $e2 = preg_split("/[-(]/",$fullname);
$name = $e2[0]; $name = $e2[0];
$currPath .= ".".$name;
if($key == 0) { if($key == 0) {
$currPath = substr($currPath,1);
$table = $this->session->getTable($name); $table = $this->session->getTable($name);
$tname = $table->getTableName(); $tname = $table->getTableName();
$this->tableIndexes[$tname] = 1;
$this->parts["from"][$tname] = true; $this->parts["from"][$tname] = true;
$this->tableAliases[$name] = $tname; $this->tableAliases[$currPath] = $tname;
$tableName = $tname;
} else { } else {
$index += strlen($e[($key - 1)]) + 1; $index += strlen($e[($key - 1)]) + 1;
// the mark here is either '.' or ':' // the mark here is either '.' or ':'
$mark = substr($path,($index - 1),1); $mark = substr($path,($index - 1),1);
if(isset($this->tableAliases[$prevPath])) {
$parent = $table->getComponentName(); $tname = $this->tableAliases[$prevPath];
if(isset($this->tableAliases[$parent])) {
$tname = $this->tableAliases[$parent];
} else } else
$tname = $table->getTableName(); $tname = $table->getTableName();
$fk = $table->getForeignKey($name); $fk = $table->getForeignKey($name);
$name = $fk->getTable()->getComponentName(); $name = $fk->getTable()->getComponentName();
$tname2 = $fk->getTable()->getTableName(); $original = $fk->getTable()->getTableName();
if(isset($this->tableAliases[$currPath])) {
$tname2 = $this->tableAliases[$currPath];
} else
$tname2 = $this->generateAlias($original);
$this->connectors[$name] = $fk; if($original !== $tname2)
$aliasString = $original." AS ".$tname2;
else
$aliasString = $original;
switch($mark): switch($mark):
case ":": case ":":
...@@ -1098,27 +1155,23 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -1098,27 +1155,23 @@ class Doctrine_Query extends Doctrine_Access {
$assocTableName = $asf->getTableName(); $assocTableName = $asf->getTableName();
$this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".id = ".$assocTableName.".".$fk->getLocal(); $this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".id = ".$assocTableName.".".$fk->getLocal();
$this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname2.".id = ".$assocTableName.".".$fk->getForeign();
if($tname == $tname2) {
$tname2 = $tname."2";
$alias = $tname." AS ".$tname2;
} else
$alias = $tname2;
$this->parts["join"][$tname][$tname2] = $join.$alias." ON ".$tname2.".id = ".$assocTableName.".".$fk->getForeign();
} }
$c = $table->getComponentName(); $this->joins[$tname2] = $prevTable;
$this->joins[$name] = $c;
$table = $fk->getTable(); $table = $fk->getTable();
$this->tableAliases[$currPath] = $tname2;
$this->tableAliases[$name] = $tname2; $tableName = $tname2;
} }
if( ! isset($this->tables[$name])) {
$this->tables[$name] = $table; // parse the fetchmode and load table fields
if( ! isset($this->tables[$tableName])) {
$this->tables[$tableName] = $table;
if($loadFields && ! $this->aggregate) { if($loadFields && ! $this->aggregate) {
$fields = array(); $fields = array();
...@@ -1139,10 +1192,12 @@ class Doctrine_Query extends Doctrine_Access { ...@@ -1139,10 +1192,12 @@ class Doctrine_Query extends Doctrine_Access {
$fields = explode(",",substr($e2[2],0,-1)); $fields = explode(",",substr($e2[2],0,-1));
} }
$this->loadFields($table, $fetchmode, $fields); $this->loadFields($table, $fetchmode, $fields, $currPath);
} }
} }
$prevPath = $currPath;
$prevTable = $tableName;
} catch(Exception $e) { } catch(Exception $e) {
throw new DQLException($e->__toString()); throw new DQLException($e->__toString());
} }
......
...@@ -74,7 +74,9 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -74,7 +74,9 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
/** /**
* the constructor * the constructor
* @param PDO $pdo -- database handle *
* @param Doctrine_Manager $manager the manager object
* @param PDO $pdo the database handler
*/ */
public function __construct(Doctrine_Manager $manager,PDO $pdo) { public function __construct(Doctrine_Manager $manager,PDO $pdo) {
$this->dbh = $pdo; $this->dbh = $pdo;
...@@ -562,7 +564,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -562,7 +564,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
if($k == 0) { if($k == 0) {
// record uses auto_increment column // record uses auto_increment column
$id = $this->dbh->lastinsertid(); $id = $this->dbh->lastInsertID();
if( ! $id) if( ! $id)
$id = $table->getMaxIdentifier(); $id = $table->getMaxIdentifier();
......
...@@ -22,12 +22,28 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -22,12 +22,28 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
} }
parent::prepareTables(); parent::prepareTables();
} }
public function testGetPath() {
$this->query->from("User.Group.Email");
$this->assertEqual($this->query->getTableAlias("User"), "entity");
$this->assertEqual($this->query->getTableAlias("User.Group"), "entity2");
$this->query->from("Task.Subtask.Subtask");
$this->assertEqual($this->query->getTableAlias("Task"), "task");
$this->assertEqual($this->query->getTableAlias("Task.Subtask"), "task2");
$this->assertEqual($this->query->getTableAlias("Task.Subtask.Subtask"), "task3");
}
public function testMultiComponentFetching2() { public function testMultiComponentFetching2() {
$this->session->clear(); $this->session->clear();
$query = new Doctrine_Query($this->session); $query = new Doctrine_Query($this->session);
$query->from("User.Email, User.Phonenumber"); $query->from("User.Email, User.Phonenumber");
$users = $query->execute(); $users = $query->execute();
$count = count($this->dbh); $count = count($this->dbh);
...@@ -73,9 +89,11 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -73,9 +89,11 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$query = new Doctrine_Query($this->session); $query = new Doctrine_Query($this->session);
$query->from('User-l.Phonenumber-l'); $query->from('User-l.Phonenumber-l');
$query->having("COUNT(User-l.Phonenumber-l.phonenumber) > 2"); $query->having("COUNT(User.Phonenumber.phonenumber) > 2");
$query->groupby('User.id'); $query->groupby('User.id');
//print Doctrine_Lib::formatSql($query->getQuery());
$users = $query->execute(); $users = $query->execute();
$this->assertEqual($users->count(), 3); $this->assertEqual($users->count(), 3);
...@@ -140,7 +158,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -140,7 +158,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$users = $query->query("FROM User-b WHERE User.Group.name = 'Action Actors'"); $users = $query->query("FROM User-b WHERE User.Group.name = 'Action Actors'");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id FROM entity LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id WHERE (entity2.name = 'Action Actors') AND (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL))"); "SELECT entity.id AS entity__id FROM entity LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id WHERE (entity2.name = 'Action Actors') AND (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL))");
$this->assertTrue($users instanceof Doctrine_Collection); $this->assertTrue($users instanceof Doctrine_Collection);
$this->assertEqual($users->count(),1); $this->assertEqual($users->count(),1);
...@@ -149,7 +167,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -149,7 +167,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$users = $query->query("FROM User-b WHERE User.Group.Phonenumber.phonenumber LIKE '123 123'"); $users = $query->query("FROM User-b WHERE User.Group.Phonenumber.phonenumber LIKE '123 123'");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id FROM entity LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id LEFT JOIN phonenumber ON entity2.id = phonenumber.entity_id WHERE (phonenumber.phonenumber LIKE '123 123') AND (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL))"); "SELECT entity.id AS entity__id FROM entity LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id LEFT JOIN phonenumber ON entity2.id = phonenumber.entity_id WHERE (phonenumber.phonenumber LIKE '123 123') AND (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL))");
$this->assertTrue($users instanceof Doctrine_Collection); $this->assertTrue($users instanceof Doctrine_Collection);
$this->assertEqual($users->count(),1); $this->assertEqual($users->count(),1);
...@@ -630,18 +648,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -630,18 +648,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users[0]->Account->amount,3000); $this->assertEqual($users[0]->Account->amount,3000);
} }
public function testNotValidLazyPropertyFetching() {
$q = new Doctrine_Query($this->session);
$f = false;
try {
$q->from("User(name)");
} catch(Doctrine_Exception $e) {
$f = true;
}
$this->assertTrue($f);
}
public function testValidLazyPropertyFetching() { public function testValidLazyPropertyFetching() {
$q = new Doctrine_Query($this->session); $q = new Doctrine_Query($this->session);
$q->from("User-l(name)"); $q->from("User-l(name)");
...@@ -672,6 +678,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -672,6 +678,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->assertTrue(is_numeric($users[2]->email_id)); $this->assertTrue(is_numeric($users[2]->email_id));
$this->assertEqual($count + 1, count($this->dbh)); $this->assertEqual($count + 1, count($this->dbh));
} }
public function testQueryWithComplexAliases() { public function testQueryWithComplexAliases() {
$q = new Doctrine_Query($this->session); $q = new Doctrine_Query($this->session);
...@@ -725,19 +732,19 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -725,19 +732,19 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$table = $this->session->getTable("Forum_Thread")->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_LAZY); $table = $this->session->getTable("Forum_Thread")->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_LAZY);
$q->from("Forum_Board.Threads"); $q->from("Forum_Board.Threads");
$this->assertEqual($q->getQuery(), "SELECT forum_board.id AS Forum_Board__id, forum_thread.id AS Forum_Thread__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id"); $this->assertEqual($q->getQuery(), "SELECT forum_board.id AS forum_board__id, forum_thread.id AS forum_thread__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id");
$coll = $q->execute(); $coll = $q->execute();
$this->assertEqual($coll->count(), 1); $this->assertEqual($coll->count(), 1);
$q->from("Forum_Board-l.Threads-l"); $q->from("Forum_Board-l.Threads-l");
$this->assertEqual($q->getQuery(), "SELECT forum_board.id AS Forum_Board__id, forum_thread.id AS Forum_Thread__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id"); $this->assertEqual($q->getQuery(), "SELECT forum_board.id AS forum_board__id, forum_thread.id AS forum_thread__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id");
//$this->session->clear(); //$this->session->clear();
$q->from("Forum_Board-l.Threads-l.Entries-l"); $q->from("Forum_Board-l.Threads-l.Entries-l");
$this->assertEqual($q->getQuery(), "SELECT forum_board.id AS Forum_Board__id, forum_thread.id AS Forum_Thread__id, forum_entry.id AS Forum_Entry__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id LEFT JOIN forum_entry ON forum_thread.id = forum_entry.thread_id"); $this->assertEqual($q->getQuery(), "SELECT forum_board.id AS forum_board__id, forum_thread.id AS forum_thread__id, forum_entry.id AS forum_entry__id FROM forum_board LEFT JOIN forum_thread ON forum_board.id = forum_thread.board_id LEFT JOIN forum_entry ON forum_thread.id = forum_entry.thread_id");
$boards = $q->execute(); $boards = $q->execute();
$this->assertEqual($boards->count(), 1); $this->assertEqual($boards->count(), 1);
$count = count($this->dbh); $count = count($this->dbh);
...@@ -845,7 +852,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -845,7 +852,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$users = $query->execute(); $users = $query->execute();
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id FROM entity LEFT JOIN email ON entity.email_id = email.id WHERE (entity.type = 0) ORDER BY entity.name ASC, email.address"); "SELECT entity.id AS entity__id FROM entity LEFT JOIN email ON entity.email_id = email.id WHERE (entity.type = 0) ORDER BY entity.name ASC, email.address");
$this->assertEqual($users->count(),8); $this->assertEqual($users->count(),8);
$this->assertTrue($users[0]->name == "Arnold Schwarzenegger"); $this->assertTrue($users[0]->name == "Arnold Schwarzenegger");
} }
...@@ -853,7 +860,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -853,7 +860,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$query = new Doctrine_Query($this->session); $query = new Doctrine_Query($this->session);
$users = $query->query("FROM User-b"); $users = $query->query("FROM User-b");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id FROM entity WHERE (entity.type = 0)"); "SELECT entity.id AS entity__id FROM entity WHERE (entity.type = 0)");
$this->assertEqual($users[0]->name, "zYne"); $this->assertEqual($users[0]->name, "zYne");
$this->assertTrue($users instanceof Doctrine_Collection_Batch); $this->assertTrue($users instanceof Doctrine_Collection_Batch);
...@@ -862,7 +869,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -862,7 +869,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$query = new Doctrine_Query($this->session); $query = new Doctrine_Query($this->session);
$users = $query->query("FROM User-l"); $users = $query->query("FROM User-l");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id FROM entity WHERE (entity.type = 0)"); "SELECT entity.id AS entity__id FROM entity WHERE (entity.type = 0)");
$this->assertEqual($users[0]->name, "zYne"); $this->assertEqual($users[0]->name, "zYne");
$this->assertTrue($users instanceof Doctrine_Collection_Lazy); $this->assertTrue($users instanceof Doctrine_Collection_Lazy);
...@@ -937,7 +944,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -937,7 +944,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$users = $query->query("FROM User-b.Phonenumber-l WHERE User.Phonenumber.phonenumber LIKE '%123%'"); $users = $query->query("FROM User-b.Phonenumber-l WHERE User.Phonenumber.phonenumber LIKE '%123%'");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id, phonenumber.id AS Phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (phonenumber.phonenumber LIKE '%123%') AND (entity.type = 0)"); "SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (phonenumber.phonenumber LIKE '%123%') AND (entity.type = 0)");
$count = $this->session->getDBH()->count(); $count = $this->session->getDBH()->count();
...@@ -963,7 +970,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -963,7 +970,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$users = $query->query("FROM User-i"); $users = $query->query("FROM User-i");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id, entity.name AS User__name, entity.loginname AS User__loginname, entity.password AS User__password, entity.type AS User__type, entity.created AS User__created, entity.updated AS User__updated, entity.email_id AS User__email_id FROM entity WHERE (entity.type = 0)"); "SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id FROM entity WHERE (entity.type = 0)");
$count = $this->session->getDBH()->count(); $count = $this->session->getDBH()->count();
$this->assertEqual($users[0]->name, "zYne"); $this->assertEqual($users[0]->name, "zYne");
...@@ -980,7 +987,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -980,7 +987,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$users = $query->query("FROM User-b.Phonenumber-b"); $users = $query->query("FROM User-b.Phonenumber-b");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id, phonenumber.id AS Phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0)"); "SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0)");
$this->assertEqual($users->count(),8); $this->assertEqual($users->count(),8);
...@@ -997,24 +1004,24 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -997,24 +1004,24 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$users = $query->query("FROM User-l:Email-b"); $users = $query->query("FROM User-l:Email-b");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id, email.id AS Email__id FROM entity INNER JOIN email ON entity.email_id = email.id WHERE (entity.type = 0)"); "SELECT entity.id AS entity__id, email.id AS email__id FROM entity INNER JOIN email ON entity.email_id = email.id WHERE (entity.type = 0)");
$this->assertEqual($users->count(),8); $this->assertEqual($users->count(),8);
$users = $query->query("FROM Email-b WHERE Email.address LIKE '%@example%'"); $users = $query->query("FROM Email-b WHERE Email.address LIKE '%@example%'");
$this->assertEqual($query->getQuery(), $this->assertEqual($query->getQuery(),
"SELECT email.id AS Email__id FROM email WHERE (email.address LIKE '%@example%')"); "SELECT email.id AS email__id FROM email WHERE (email.address LIKE '%@example%')");
$this->assertEqual($users->count(),8); $this->assertEqual($users->count(),8);
$users = $query->query("FROM User-b WHERE User.name LIKE '%Jack%'"); $users = $query->query("FROM User-b WHERE User.name LIKE '%Jack%'");
$this->assertEqual($query->getQuery(), "SELECT entity.id AS User__id FROM entity WHERE (entity.name LIKE '%Jack%') AND (entity.type = 0)"); $this->assertEqual($query->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE (entity.name LIKE '%Jack%') AND (entity.type = 0)");
$this->assertEqual($users->count(),0); $this->assertEqual($users->count(),0);
$users = $query->query("FROM User-b WHERE User.Phonenumber.phonenumber LIKE '%123%'"); $users = $query->query("FROM User-b WHERE User.Phonenumber.phonenumber LIKE '%123%'");
$this->assertEqual(trim($query->getQuery()), $this->assertEqual(trim($query->getQuery()),
"SELECT entity.id AS User__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (phonenumber.phonenumber LIKE '%123%') AND (entity.type = 0)"); "SELECT entity.id AS entity__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (phonenumber.phonenumber LIKE '%123%') AND (entity.type = 0)");
$this->assertEqual($users->count(),5); $this->assertEqual($users->count(),5);
...@@ -1025,5 +1032,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { ...@@ -1025,5 +1032,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
//$this->assertTrue(isset($values['max'])); //$this->assertTrue(isset($values['max']));
} }
} }
?> ?>
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