From 129fa3360a02b27f1ef7d4869983018bf490f0fb Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 15 May 2007 15:08:23 +0000 Subject: [PATCH] new DQL model now supports WHERE clauses and subqueries --- draft/new-core/Hydrate.php | 15 +++---- draft/new-core/Query.php | 36 ++++++++--------- tests/Query/OrderbyTestCase.php | 4 +- tests/Query/WhereTestCase.php | 69 +++++++++++++++++++-------------- 4 files changed, 66 insertions(+), 58 deletions(-) diff --git a/draft/new-core/Hydrate.php b/draft/new-core/Hydrate.php index 44432adbc..6182dd863 100644 --- a/draft/new-core/Hydrate.php +++ b/draft/new-core/Hydrate.php @@ -166,7 +166,7 @@ class Doctrine_Hydrate2 { if ( ! isset($this->parts[$name])) { throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name); - } + } $this->parts[$name] = array($part); } /** @@ -174,9 +174,8 @@ class Doctrine_Hydrate2 * * @return void */ - public function copyAliases(Doctrine_Hydrate $query) + public function copyAliases($query) { - $this->tableAliases = $query->getTableAliases(); $this->aliasHandler = $query->aliasHandler; return $this; @@ -307,9 +306,11 @@ class Doctrine_Hydrate2 /** * _fetch * - * + * @param array $params prepared statement parameters + * @param integer $fetchMode the fetchmode + * @see Doctrine::FETCH_* constants */ - public function _fetch($params = array(), $return = Doctrine::FETCH_RECORD) + public function _fetch($params = array(), $fetchMode = Doctrine::FETCH_RECORD) { $params = $this->conn->convertBooleans(array_merge($this->params, $params)); @@ -319,7 +320,7 @@ class Doctrine_Hydrate2 $query = $this->view->getSelectSql(); } - if ($this->isLimitSubqueryUsed() && + if ($this->isLimitSubqueryUsed() && $this->conn->getDBH()->getAttribute(Doctrine::ATTR_DRIVER_NAME) !== 'mysql') { $params = array_merge($params, $params); @@ -374,7 +375,7 @@ class Doctrine_Hydrate2 */ public function execute($params = array(), $return = Doctrine::FETCH_RECORD) { - $array = (array) $this->_fetch($params = array(), $return = Doctrine::FETCH_RECORD); + $array = (array) $this->_fetch($params, $return = Doctrine::FETCH_RECORD); if (empty($this->_aliasMap)) { throw new Doctrine_Hydrate_Exception("Couldn't execute query. Component alias map was empty."); diff --git a/draft/new-core/Query.php b/draft/new-core/Query.php index 155deb7fe..16bbb1903 100644 --- a/draft/new-core/Query.php +++ b/draft/new-core/Query.php @@ -316,14 +316,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable list($dql, $alias) = $value; $sql = $this->createSubquery()->parseQuery($dql, false)->getQuery(); - - reset($this->tableAliases); - - $tableAlias = current($this->tableAliases); - reset($this->compAliases); - - $componentAlias = key($this->compAliases); + + + reset($this->_aliasMap); + $componentAlias = key($this->_aliasMap); + $tableAlias = $this->getTableAlias($componentAlias); $sqlAlias = $tableAlias . '__' . count($this->aggregateMap); @@ -1045,7 +1043,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable */ public function addWhere($where, $params = array()) { - if(is_array($params)) { + if (is_array($params)) { $this->params = array_merge($this->params, $params); } else { $this->params[] = $params; @@ -1068,10 +1066,16 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable * adds conditions to the HAVING part of the query * * @param string $having DQL HAVING part + * @param mixed $params an array of parameters or a simple scalar * @return Doctrine_Query */ - public function addHaving($having) + public function addHaving($having, $params = array()) { + if (is_array($params)) { + $this->params = array_merge($this->params, $params); + } else { + $this->params[] = $params; + } return $this->getParser('having')->parse($having, true); } /** @@ -1150,11 +1154,8 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable */ public function where($where, $params = array()) { - if(is_array($params)) { - $this->params = array_merge($this->params, $params); - } else { - $this->params[] = $params; - } + $this->params = (array) $params; + return $this->getParser('where')->parse($where); } /** @@ -1167,11 +1168,8 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable */ public function having($having, $params) { - if(is_array($params)) { - $this->params = array_merge($this->params, $params); - } else { - $this->params[] = $params; - } + $this->params = (array) $params; + return $this->getParser('having')->parse($having); } /** diff --git a/tests/Query/OrderbyTestCase.php b/tests/Query/OrderbyTestCase.php index 468b96913..1fda9a3b5 100644 --- a/tests/Query/OrderbyTestCase.php +++ b/tests/Query/OrderbyTestCase.php @@ -34,7 +34,7 @@ class Doctrine_Query_Orderby_TestCase extends Doctrine_UnitTestCase { public function testOrderByAggregateValueIsSupported() { - $q = new Doctrine_Query(); + $q = new Doctrine_Query2(); $q->select('u.name, COUNT(p.phonenumber) count') ->from('User u') @@ -45,7 +45,7 @@ class Doctrine_Query_Orderby_TestCase extends Doctrine_UnitTestCase } public function testOrderByRandomIsSupported() { - $q = new Doctrine_Query(); + $q = new Doctrine_Query2(); $q->select('u.name, RANDOM() rand') ->from('User u') diff --git a/tests/Query/WhereTestCase.php b/tests/Query/WhereTestCase.php index 7da065d0d..b8ffb86be 100644 --- a/tests/Query/WhereTestCase.php +++ b/tests/Query/WhereTestCase.php @@ -31,14 +31,16 @@ * @since 1.0 * @version $Revision$ */ -class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { +class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase +{ public function prepareData() { } public function prepareTables() { $this->tables = array('entity'); parent::prepareTables(); } - public function testDirectParameterSetting() { + public function testDirectParameterSetting() + { $this->connection->clear(); $user = new User(); @@ -55,14 +57,15 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users[0]->name, 'someone'); } - public function testDirectMultipleParameterSetting() { + public function testDirectMultipleParameterSetting() + { $user = new User(); $user->name = 'someone.2'; $user->save(); $q = new Doctrine_Query2(); - $q->from('User')->addWhere('User.id IN (?, ?)', array(1,2)); + $q->from('User')->addWhere('User.id IN (?, ?)', array(1, 2)); $users = $q->execute(); @@ -70,10 +73,13 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users[0]->name, 'someone'); $this->assertEqual($users[1]->name, 'someone.2'); } - public function testDirectMultipleParameterSetting2() { + public function testDirectMultipleParameterSetting2() + { $q = new Doctrine_Query2(); - $q->from('User')->where('User.id IN (?, ?)', array(1,2)); + $q->from('User')->where('User.id IN (?, ?)', array(1, 2)); + + $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e WHERE e.id IN (?, ?) AND (e.type = 0)'); $users = $q->execute(); @@ -82,7 +88,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users[1]->name, 'someone.2'); // the parameters and where part should be reseted - $q->where('User.id IN (?, ?)', array(1,2)); + $q->where('User.id IN (?, ?)', array(1, 2)); $users = $q->execute(); @@ -90,7 +96,8 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users[0]->name, 'someone'); $this->assertEqual($users[1]->name, 'someone.2'); } - public function testNotInExpression() { + public function testNotInExpression() + { $q = new Doctrine_Query2(); $q->from('User u')->addWhere('u.id NOT IN (?)', array(1)); @@ -99,7 +106,8 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users->count(), 1); $this->assertEqual($users[0]->name, 'someone.2'); } - public function testExistsExpression() { + public function testExistsExpression() + { $q = new Doctrine_Query2(); $user = new User(); @@ -109,7 +117,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { // find all users which have groups try { - $q->from('User u')->where('EXISTS (FROM Groupuser(id) WHERE Groupuser.user_id = u.id)'); + $q->from('User u')->where('EXISTS (SELECT Groupuser.id FROM Groupuser WHERE Groupuser.user_id = u.id)'); $this->pass(); } catch(Doctrine_Exception $e) { $this->fail(); @@ -119,12 +127,13 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users[0]->name, 'someone with a group'); } - public function testNotExistsExpression() { + public function testNotExistsExpression() + { $q = new Doctrine_Query2(); // find all users which don't have groups try { - $q->from('User u')->where('NOT EXISTS (FROM Groupuser(id) WHERE Groupuser.user_id = u.id)'); + $q->from('User u')->where('NOT EXISTS (SELECT Groupuser.id FROM Groupuser WHERE Groupuser.user_id = u.id)'); $this->pass(); } catch(Doctrine_Exception $e) { $this->fail(); @@ -134,10 +143,12 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users[0]->name, 'someone'); $this->assertEqual($users[1]->name, 'someone.2'); } - public function testComponentAliases() { + public function testComponentAliases() + { $q = new Doctrine_Query2(); $q->from('User u')->addWhere('u.id IN (?, ?)', array(1,2)); + $users = $q->execute(); $this->assertEqual($users->count(), 2); @@ -145,7 +156,8 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users[1]->name, 'someone.2'); } - public function testComponentAliases2() { + public function testComponentAliases2() + { $q = new Doctrine_Query2(); $q->from('User u')->addWhere('u.name = ?', array('someone')); @@ -155,47 +167,44 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users->count(), 1); $this->assertEqual($users[0]->name, 'someone'); } - public function testComponentAliases3() { - - $users = $this->connection->query("FROM User u WHERE u.name = ?", array('someone')); - - $this->assertEqual($users->count(), 1); - $this->assertEqual($users[0]->name, 'someone'); - } - public function testOperatorWithNoTrailingSpaces() { + public function testOperatorWithNoTrailingSpaces() + { $q = new Doctrine_Query2(); - $q->from('User')->where("User.name='someone'"); + $q->select('User.id')->from('User')->where("User.name='someone'"); $users = $q->execute(); $this->assertEqual($users->count(), 1); $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id FROM entity e WHERE e.name = 'someone' AND (e.type = 0)"); } - public function testOperatorWithNoTrailingSpaces2() { + public function testOperatorWithNoTrailingSpaces2() + { $q = new Doctrine_Query2(); - $q->from('User')->where("User.name='foo.bar'"); + $q->select('User.id')->from('User')->where("User.name='foo.bar'"); $users = $q->execute(); $this->assertEqual($users->count(), 0); $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id FROM entity e WHERE e.name = 'foo.bar' AND (e.type = 0)"); } - public function testOperatorWithSingleTrailingSpace() { + public function testOperatorWithSingleTrailingSpace() + { $q = new Doctrine_Query2(); - $q->from('User')->where("User.name= 'foo.bar'"); + $q->select('User.id')->from('User')->where("User.name= 'foo.bar'"); $users = $q->execute(); $this->assertEqual($users->count(), 0); $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id FROM entity e WHERE e.name = 'foo.bar' AND (e.type = 0)"); } - public function testOperatorWithSingleTrailingSpace2() { + public function testOperatorWithSingleTrailingSpace2() + { $q = new Doctrine_Query2(); - - $q->from('User')->where("User.name ='foo.bar'"); + + $q->select('User.id')->from('User')->where("User.name ='foo.bar'"); $users = $q->execute(); $this->assertEqual($users->count(), 0);