From 2dd3e9743a1438bc266bc99d6362fd5e8107d104 Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 29 Aug 2006 20:09:31 +0000 Subject: [PATCH] Doctrine::ATTR_QUERY_LIMIT constant added --- Doctrine.php | 23 +++++++++++++++++++++-- Doctrine/Configurable.php | 5 +++-- Doctrine/Manager.php | 6 ++---- Doctrine/Query.php | 13 +++++++------ tests/QueryLimitTestCase.php | 14 ++++++++++++++ 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Doctrine.php b/Doctrine.php index cd3fc671e..a3bf2e4b6 100644 --- a/Doctrine.php +++ b/Doctrine.php @@ -149,6 +149,25 @@ final class Doctrine { * collection limit attribute */ const ATTR_COLL_LIMIT = 16; + /** + * query limit + */ + const ATTR_QUERY_LIMIT = 17; + + + /** + * LIMIT CONSTANTS + */ + + /** + * constant for row limiting + */ + const LIMIT_ROWS = 1; + /** + * constant for record limiting + */ + const LIMIT_RECORDS = 2; + @@ -312,10 +331,10 @@ final class Doctrine { } } /** - * method for making a single file of most used doctrine components + * method for making a single file of most used doctrine runtime components * * including the compiled file instead of multiple files (in worst - * cases dozens of files) can improve performance by order of magnitude + * cases dozens of files) can improve performance by an order of magnitude * * @throws Doctrine_Exception * @return void diff --git a/Doctrine/Configurable.php b/Doctrine/Configurable.php index 7104c5355..2d403e758 100644 --- a/Doctrine/Configurable.php +++ b/Doctrine/Configurable.php @@ -120,7 +120,8 @@ abstract class Doctrine_Configurable { break; case Doctrine::ATTR_VLD: - + case Doctrine::ATTR_QUERY_LIMIT: + break; case Doctrine::ATTR_CACHE: if($value != Doctrine::CACHE_SQLITE && $value != Doctrine::CACHE_NONE) @@ -154,7 +155,7 @@ abstract class Doctrine_Configurable { final public function getAttribute($attribute) { $attribute = (int) $attribute; - if($attribute < 1 || $attribute > 16) + if($attribute < 1 || $attribute > 17) throw new InvalidKeyException(); if( ! isset($this->attributes[$attribute])) { diff --git a/Doctrine/Manager.php b/Doctrine/Manager.php index b47f24c45..535c08a96 100644 --- a/Doctrine/Manager.php +++ b/Doctrine/Manager.php @@ -79,10 +79,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera if( ! $init) { $init = true; $attributes = array( - Doctrine::ATTR_CACHE_DIR => "%ROOT%".DIRECTORY_SEPARATOR."cachedir", Doctrine::ATTR_FETCHMODE => Doctrine::FETCH_IMMEDIATE, - Doctrine::ATTR_CACHE_TTL => 100, - Doctrine::ATTR_CACHE_SIZE => 100, Doctrine::ATTR_CACHE => Doctrine::CACHE_NONE, Doctrine::ATTR_BATCH_SIZE => 5, Doctrine::ATTR_COLL_LIMIT => 5, @@ -91,7 +88,8 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera Doctrine::ATTR_PK_TYPE => Doctrine::INCREMENT_KEY, Doctrine::ATTR_LOCKMODE => 1, Doctrine::ATTR_VLD => false, - Doctrine::ATTR_CREATE_TABLES => true + Doctrine::ATTR_CREATE_TABLES => true, + Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS ); foreach($attributes as $attribute => $value) { $old = $this->getAttribute($attribute); diff --git a/Doctrine/Query.php b/Doctrine/Query.php index 695f27acf..5d8a9f017 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -263,8 +263,10 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $needsSubQuery = false; $subquery = ''; + $k = array_keys($this->tables); + $table = $this->tables[$k[0]]; - if( ! empty($this->parts['limit']) && $this->needsSubquery) { + if( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) { $needsSubQuery = true; $this->limitSubqueryUsed = true; } @@ -277,8 +279,6 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $a[] = $tname; } $q .= implode(", ",$a); - $k = array_keys($this->tables); - $table = $this->tables[$k[0]]; if($needsSubQuery) $subquery = 'SELECT DISTINCT '.$table->getTableName().".".$table->getIdentifier(). @@ -317,15 +317,16 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $subquery .= ( ! empty($this->parts['having']))?" HAVING ".implode(" ",$this->parts["having"]):''; } - $modifyLimit = false; + $modifyLimit = true; if( ! empty($this->parts["limit"]) || ! empty($this->parts["offset"])) { + if($needsSubQuery) { $subquery = $this->connection->modifyLimitQuery($subquery,$this->parts["limit"],$this->parts["offset"]); $field = $table->getTableName().'.'.$table->getIdentifier(); array_unshift($this->parts['where'], $field.' IN ('.$subquery.')'); - } else - $modifyLimit = true; + $modifyLimit = false; + } } $q .= ( ! empty($this->parts['where']))?" WHERE ".implode(" AND ",$this->parts["where"]):''; diff --git a/tests/QueryLimitTestCase.php b/tests/QueryLimitTestCase.php index 0c0f91b9e..6fa275ac4 100644 --- a/tests/QueryLimitTestCase.php +++ b/tests/QueryLimitTestCase.php @@ -189,6 +189,20 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($users->count(), 3); } + public function testLimitAttribute() { + $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_ROWS); + + $this->connection->clear(); + $q = new Doctrine_Query(); + $q->from("User")->where("User.Group.id = ?")->orderby("User.id DESC")->limit(5); + $users = $q->execute(array(3)); + + $this->assertEqual($users->count(), 3); + + $this->assertEqual($q->getQuery(), "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 LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id WHERE entity2.id = ? AND (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL)) ORDER BY entity.id DESC LIMIT 5"); + + $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_RECORDS); + } public function testLimitWithNormalManyToMany() { $coll = new Doctrine_Collection($this->connection->getTable("Photo")); $tag = new Tag();