Commit 004f3cb4 authored by doctrine's avatar doctrine

DQL: Simpler FROM syntax

parent a0d11413
......@@ -174,6 +174,8 @@ class Doctrine_Query extends Doctrine_Access {
case "from":
$this->parts['columns'] = array();
$this->joins = array();
$this->tables = array();
$this->fetchModes = array();
default:
$this->parts[$name] = array();
$this->$method($args[0]);
......@@ -219,6 +221,8 @@ class Doctrine_Query extends Doctrine_Access {
case "from":
$this->parts['columns'] = array();
$this->joins = array();
$this->tables = array();
$this->fetchModes = array();
default:
$this->parts[$name] = array();
$this->$method($value);
......@@ -413,6 +417,7 @@ class Doctrine_Query extends Doctrine_Access {
$colls = array();
foreach($array as $data) {
/**
* remove duplicated data rows and map data into objects
......@@ -436,7 +441,7 @@ class Doctrine_Query extends Doctrine_Access {
} else {
if($row[$ids] === null)
continue;
}
}
$name = $this->tables[$key]->getComponentName();
......@@ -644,7 +649,7 @@ class Doctrine_Query extends Doctrine_Access {
$reference = implode(".",$a);
$name = end($a);
$this->load($reference);
$this->load($reference, false);
$tname = $this->tables[$name]->getTableName();
$r = $tname.".".$field;
......@@ -691,42 +696,36 @@ class Doctrine_Query extends Doctrine_Access {
private function parseFrom($str) {
foreach(explode(",",trim($str)) as $reference) {
$reference = trim($reference);
$e = explode("-",$reference);
$reference = $e[0];
$table = $this->load($reference);
if(isset($e[1])) {
switch(strtolower($e[1])):
case "i":
case "immediate":
$fetchmode = Doctrine::FETCH_IMMEDIATE;
break;
case "b":
case "batch":
$fetchmode = Doctrine::FETCH_BATCH;
break;
case "l":
case "lazy":
$fetchmode = Doctrine::FETCH_LAZY;
break;
case "o":
case "offset":
$fetchmode = Doctrine::FETCH_OFFSET;
break;
case "lo":
case "lazyoffset":
$fetchmode = Doctrine::FETCH_LAZYOFFSET;
default:
throw new DQLException("Unknown fetchmode '$e[1]'. The availible fetchmodes are 'i', 'b' and 'l'.");
endswitch;
} else
$fetchmode = $table->getAttribute(Doctrine::ATTR_FETCHMODE);
if( ! $this->aggregate) {
$this->loadFields($table,$fetchmode);
}
}
}
private function parseFetchMode($mode) {
switch(strtolower($mode)):
case "i":
case "immediate":
$fetchmode = Doctrine::FETCH_IMMEDIATE;
break;
case "b":
case "batch":
$fetchmode = Doctrine::FETCH_BATCH;
break;
case "l":
case "lazy":
$fetchmode = Doctrine::FETCH_LAZY;
break;
case "o":
case "offset":
$fetchmode = Doctrine::FETCH_OFFSET;
break;
case "lo":
case "lazyoffset":
$fetchmode = Doctrine::FETCH_LAZYOFFSET;
default:
throw new DQLException("Unknown fetchmode '$mode'. The availible fetchmodes are 'i', 'b' and 'l'.");
endswitch;
return $fetchmode;
}
/**
* DQL WHERE PARSER
* parses the where part of the query string
......@@ -826,6 +825,7 @@ class Doctrine_Query extends Doctrine_Access {
$r = array_shift($e);
$a = explode(".",$r);
if(count($a) > 1) {
$field = array_pop($a);
$operator = array_shift($e);
......@@ -852,9 +852,9 @@ class Doctrine_Query extends Doctrine_Access {
break;
case Doctrine_Relation::MANY_AGGREGATE:
case Doctrine_Relation::MANY_COMPOSITE:
// subquery needed
// subquery needed
$where = $objTable->getComponentName().".".$field." ".$operator." ".$value;
$b = $fk->getTable()->getComponentName();
$graph = new Doctrine_Query($this->session);
......@@ -863,10 +863,10 @@ class Doctrine_Query extends Doctrine_Access {
break;
endswitch;
} else
$this->load($reference);
$this->load($reference, false);
} else
$this->load($reference);
$this->load($reference, false);
}
return $where;
}
......@@ -875,20 +875,22 @@ class Doctrine_Query extends Doctrine_Access {
* @param integer $fetchmode optional fetchmode, if not set the components default fetchmode will be used
* @throws DQLException
*/
final public function load($path, $fetchmode = Doctrine::FETCH_LAZY) {
final public function load($path, $loadFields = true) {
$e = preg_split("/[.:]/",$path);
$index = 0;
foreach($e as $key => $name) {
foreach($e as $key => $name) {
try {
$e2 = explode("-",$name);
$name = $e2[0];
if($key == 0) {
$table = $this->session->getTable($name);
if(count($e) == 1) {
$tname = $table->getTableName();
$this->parts["from"][$tname] = true;
}
$tname = $table->getTableName();
$this->parts["from"][$tname] = true;
} else {
$index += strlen($e[($key - 1)]) + 1;
......@@ -937,18 +939,26 @@ class Doctrine_Query extends Doctrine_Access {
}
$table = $fk->getTable();
}
if( ! isset($this->tables[$name])) {
$this->tables[$name] = $table;
$this->tables[$name] = $table;
if($loadFields && ! $this->aggregate) {
if(isset($e2[1])) {
$fetchmode = $this->parseFetchMode($e2[1]);
} else
$fetchmode = $table->getAttribute(Doctrine::ATTR_FETCHMODE);
$this->loadFields($table, $fetchmode);
}
}
} catch(Exception $e) {
throw new DQLException($e->getMessage(),$e->getCode());
}
}
return $table;
}
}
......
......@@ -7,7 +7,9 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->tables[] = "Forum_Thread";
parent::prepareTables();
}
public function testQueryWithComplexAliases() {
$q = new Doctrine_Query($this->session);
$board = new Forum_Board();
$table = $board->getTable();
......@@ -38,7 +40,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->assertTrue($thread->Entries[0] instanceof Forum_Entry);
$this->session->flush();
$q = new Doctrine_Query($this->session);
$board->getTable()->clear();
$board = $board->getTable()->find($board->getID());
......@@ -55,15 +57,20 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$coll = $q->execute();
$this->assertEqual($coll->count(), 1);
$q->from("Forum_Board, Forum_Board.Threads");
$table = $this->session->getTable("Forum_Board")->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");
$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();
$this->assertEqual($coll->count(), 1);
$q->from("Forum_Board-l, Forum_Board.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");
$q->from("Forum_Board-l, Forum_Board.Threads-l, Forum_Board.Threads.Entries-l");
$q->from("Forum_Board.Threads.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");
$boards = $q->execute();
$this->assertEqual($boards->count(), 1);
......@@ -71,17 +78,19 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($boards[0]->Threads->count(), 1);
$this->assertEqual(count($this->dbh), $count);
$this->assertEqual($boards[0]->Threads[0]->Entries->count(), 1);
$this->assertEqual(count($this->dbh), $count);
}
public function testQueryWithAliases() {
$query = new Doctrine_Query($this->session);
$task = new Task();
$task->name = "Task 1";
$task->ResourceAlias[0]->name = "Resource 1";
$task->ResourceAlias[1]->name = "Resource 2";
$task->save();
$query = new Doctrine_Query($this->session);
$coll = $query->query("FROM Task WHERE Task.ResourceAlias.name = 'Resource 1'");
$this->assertEqual($coll->count(), 1);
......@@ -159,6 +168,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$query = new Doctrine_Query($this->session);
$query->from("User-b")->orderby("User.name ASC, User.Email.address");
$users = $query->execute();
$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");
$this->assertEqual($users->count(),8);
......@@ -230,7 +240,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->clearCache();
$users = $query->query("FROM User-b, User.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()),
"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)");
......@@ -285,7 +295,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
//$this->clearCache();
$users = $query->query("FROM User-b, User.Phonenumber-b");
$users = $query->query("FROM User-b.Phonenumber-b");
$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)");
......@@ -301,7 +311,8 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$users = $query->query("FROM User-b, User:Email-b");
$users = $query->query("FROM User-l:Email-b");
$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)");
......@@ -335,13 +346,13 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
"SELECT entity.id AS User__id FROM entity WHERE (entity.id IN (SELECT user_id FROM groupuser WHERE group_id IN (SELECT entity.id AS Group__id FROM entity, phonenumber WHERE (phonenumber.phonenumber LIKE '123 123') AND (entity.type = 1)))) AND (entity.type = 0)");
$this->assertTrue($users instanceof Doctrine_Collection);
$this->assertEqual($users->count(),1);
/**
$values = $query->query("SELECT COUNT(User.name) AS users, MAX(User.name) AS max FROM User");
$this->assertEqual(trim($query->getQuery()),"SELECT COUNT(entity.name) AS users, MAX(entity.name) AS max FROM entity WHERE (entity.type = 0)");
$this->assertTrue(is_array($values));
$this->assertTrue(isset($values['users']));
$this->assertTrue(isset($values['max']));
}
*/
}
}
?>
......@@ -23,7 +23,7 @@ error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests");
//$test->addTestCase(new Sensei_UnitTestCase());
/**
$test->addTestCase(new Doctrine_SessionTestCase());
$test->addTestCase(new Doctrine_TableTestCase());
......@@ -45,7 +45,7 @@ $test->addTestCase(new Doctrine_ConfigurableTestCase());
$test->addTestCase(new Doctrine_Collection_OffsetTestCase());
$test->addTestCase(new Doctrine_CollectionTestCase());
*/
$test->addTestCase(new Doctrine_QueryTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
......
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