Commit beda312f authored by zYne's avatar zYne

DQL Limit now works with prepared queries

parent f3c0a27d
...@@ -101,6 +101,13 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -101,6 +101,13 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
* @return string * @return string
*/ */
abstract public function getQuery(); abstract public function getQuery();
/**
* limitSubqueryUsed
*/
public function isLimitSubqueryUsed() {
return false;
}
/** /**
* remove * remove
* *
...@@ -269,6 +276,9 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -269,6 +276,9 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
$keys = array_keys($this->tables); $keys = array_keys($this->tables);
$root = $keys[0]; $root = $keys[0];
if($this->isLimitSubqueryUsed())
$params = array_merge($params, $params);
$stmt = $this->session->execute($query,$params); $stmt = $this->session->execute($query,$params);
$previd = array(); $previd = array();
......
...@@ -35,6 +35,10 @@ class Doctrine_Query extends Doctrine_Hydrate { ...@@ -35,6 +35,10 @@ class Doctrine_Query extends Doctrine_Hydrate {
* @param boolean $needsSubquery * @param boolean $needsSubquery
*/ */
private $needsSubquery = false; private $needsSubquery = false;
/**
* @param boolean $limitSubqueryUsed
*/
private $limitSubqueryUsed = false;
/** /**
* count * count
* *
...@@ -242,6 +246,12 @@ class Doctrine_Query extends Doctrine_Hydrate { ...@@ -242,6 +246,12 @@ class Doctrine_Query extends Doctrine_Hydrate {
} }
return false; return false;
} }
/**
* @return boolean
*/
public function isLimitSubqueryUsed() {
return true;
}
/** /**
* returns the built sql query * returns the built sql query
* *
...@@ -254,8 +264,10 @@ class Doctrine_Query extends Doctrine_Hydrate { ...@@ -254,8 +264,10 @@ class Doctrine_Query extends Doctrine_Hydrate {
$needsSubQuery = false; $needsSubQuery = false;
$subquery = ''; $subquery = '';
if( ! empty($this->parts['limit']) && $this->needsSubquery) if( ! empty($this->parts['limit']) && $this->needsSubquery) {
$needsSubQuery = true; $needsSubQuery = true;
$this->limitSubqueryUsed = true;
}
// build the basic query // build the basic query
$q = "SELECT ".implode(", ",$this->parts["select"]). $q = "SELECT ".implode(", ",$this->parts["select"]).
......
...@@ -98,7 +98,33 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { ...@@ -98,7 +98,33 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
'SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity INNER JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity INNER JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0) LIMIT 5 OFFSET 2) AND (entity.type = 0)'); 'SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity INNER JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity INNER JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0) LIMIT 5 OFFSET 2) AND (entity.type = 0)');
} }
public function testLimitWithPreparedQueries() { public function testLimitWithPreparedQueries() {
$q = new Doctrine_Query();
$q->from("User(id).Phonenumber(id)");
$q->where("User.name = ?");
$q->limit(5);
$users = $q->execute(array('zYne'));
$this->assertEqual($users->count(), 1);
$count = $this->dbh->count();
$users[0]->Phonenumber[0];
$this->assertEqual($count, $this->dbh->count());
$this->assertEqual($q->getQuery(),
'SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity WHERE entity.name = ? AND (entity.type = 0) LIMIT 5) AND entity.name = ? AND (entity.type = 0)');
$q = new Doctrine_Query();
$q->from("User(id).Phonenumber(id)");
$q->where("User.name LIKE ? || User.name LIKE ?");
$q->limit(5);
$users = $q->execute(array('%zYne%', '%Arnold%'));
$this->assertEqual($users->count(), 2);
$count = $this->dbh->count();
$users[0]->Phonenumber[0];
$this->assertEqual($count, $this->dbh->count());
$this->assertEqual($q->getQuery(),
"SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity WHERE (entity.name LIKE ? OR entity.name LIKE ?) AND (entity.type = 0) LIMIT 5) AND (entity.name LIKE ? OR entity.name LIKE ?) AND (entity.type = 0)");
} }
public function testLimitWithManyToManyLeftJoin() { public function testLimitWithManyToManyLeftJoin() {
$q = new Doctrine_Query($this->session); $q = new Doctrine_Query($this->session);
......
...@@ -28,7 +28,7 @@ require_once("QueryLimitTestCase.php"); ...@@ -28,7 +28,7 @@ require_once("QueryLimitTestCase.php");
error_reporting(E_ALL); error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests"); $test = new GroupTest("Doctrine Framework Unit Tests");
/**
$test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_SessionTestCase()); $test->addTestCase(new Doctrine_SessionTestCase());
...@@ -64,10 +64,10 @@ $test->addTestCase(new Doctrine_ValidatorTestCase()); ...@@ -64,10 +64,10 @@ $test->addTestCase(new Doctrine_ValidatorTestCase());
$test->addTestCase(new Doctrine_CollectionTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase());
$test->addTestCase(new Doctrine_QueryTestCase()); $test->addTestCase(new Doctrine_QueryTestCase());
*/
$test->addTestCase(new Doctrine_Query_Limit_TestCase()); $test->addTestCase(new Doctrine_Query_Limit_TestCase());
$test->addTestCase(new Doctrine_RawSql_TestCase()); //$test->addTestCase(new Doctrine_RawSql_TestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
......
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