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 {
* @return string
*/
abstract public function getQuery();
/**
* limitSubqueryUsed
*/
public function isLimitSubqueryUsed() {
return false;
}
/**
* remove
*
......@@ -268,7 +275,10 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
default:
$keys = array_keys($this->tables);
$root = $keys[0];
if($this->isLimitSubqueryUsed())
$params = array_merge($params, $params);
$stmt = $this->session->execute($query,$params);
$previd = array();
......
......@@ -30,11 +30,15 @@ class Doctrine_Query extends Doctrine_Hydrate {
/**
* @param array $subqueryAliases the table aliases needed in some LIMIT subqueries
*/
private $subqueryAliases = array();
private $subqueryAliases = array();
/**
* @param boolean $needsSubquery
*/
private $needsSubquery = false;
private $needsSubquery = false;
/**
* @param boolean $limitSubqueryUsed
*/
private $limitSubqueryUsed = false;
/**
* count
*
......@@ -242,6 +246,12 @@ class Doctrine_Query extends Doctrine_Hydrate {
}
return false;
}
/**
* @return boolean
*/
public function isLimitSubqueryUsed() {
return true;
}
/**
* returns the built sql query
*
......@@ -250,12 +260,14 @@ class Doctrine_Query extends Doctrine_Hydrate {
public function getQuery() {
if(empty($this->parts["select"]) || empty($this->parts["from"]))
return false;
$needsSubQuery = false;
$subquery = '';
if( ! empty($this->parts['limit']) && $this->needsSubquery)
if( ! empty($this->parts['limit']) && $this->needsSubquery) {
$needsSubQuery = true;
$this->limitSubqueryUsed = true;
}
// build the basic query
$q = "SELECT ".implode(", ",$this->parts["select"]).
......
......@@ -94,12 +94,38 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
$users[3]->Phonenumber[0];
$this->assertEqual($count, $this->dbh->count());
$this->assertEqual($this->query->getQuery(),
$this->assertEqual($this->query->getQuery(),
'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() {
}
$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() {
$q = new Doctrine_Query($this->session);
$q->from("User.Group")->limit(5);
......
......@@ -28,7 +28,7 @@ require_once("QueryLimitTestCase.php");
error_reporting(E_ALL);
$test = new GroupTest("Doctrine Framework Unit Tests");
/**
$test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_SessionTestCase());
......@@ -64,10 +64,10 @@ $test->addTestCase(new Doctrine_ValidatorTestCase());
$test->addTestCase(new Doctrine_CollectionTestCase());
$test->addTestCase(new Doctrine_QueryTestCase());
*/
$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_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