Changeset 4794

Show
Ignore:
Timestamp:
08/23/08 11:20:10 (11 months ago)
Author:
romanb
Message:

limit-subquery and query cache adjustments.

Location:
branches/1.0
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/lib/Doctrine/Query.php

    r4793 r4794  
    176176    protected $_sql; 
    177177 
    178  
    179178    /** 
    180179     * create 
     
    424423    { 
    425424        $tableAlias = $this->getTableAlias($componentAlias); 
    426         $table      = $this->_queryComponents[$componentAlias]['table']; 
     425        $table = $this->_queryComponents[$componentAlias]['table']; 
    427426 
    428427        if ( ! isset($this->_pendingFields[$componentAlias])) { 
     
    10691068        if ( ! empty($this->_sqlParts['limit']) && $this->_needsSubquery && 
    10701069                $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) { 
    1071             $this->_isLimitSubqueryUsed = true; 
    1072             $needsSubQuery = true; 
     1070            // We do not need a limit-subquery if only fields from the root component are 
     1071            // selected and DISTINCT is used (i.e. DQL: SELECT DISTINCT u.id FROM User u LEFT JOIN u.phonenumbers LIMIT 5). 
     1072            if (count($this->_pendingFields) > 1 || ! isset($this->_pendingFields[$this->getRootAlias()]) 
     1073                    || ! $this->_sqlParts['distinct']) { 
     1074                $this->_isLimitSubqueryUsed = true; 
     1075                $needsSubQuery = true; 
     1076            } 
    10731077        } 
    10741078 
  • branches/1.0/lib/Doctrine/Query/Abstract.php

    r4768 r4794  
    898898 
    899899        if ( ! $this->_view) { 
    900             if ($this->_queryCache || $this->_conn->getAttribute(Doctrine::ATTR_QUERY_CACHE)) { 
     900            if ($this->_queryCache !== false && ($this->_queryCache || $this->_conn->getAttribute(Doctrine::ATTR_QUERY_CACHE))) { 
    901901                $queryCacheDriver = $this->getQueryCacheDriver(); 
    902902                // calculate hash for dql query 
     
    16511651     * @return Doctrine_Hydrate         this object 
    16521652     */ 
    1653     public function useQueryCache(Doctrine_Cache_Interface $driver, $timeToLive = null) 
    1654     { 
     1653    public function useQueryCache($driver = true, $timeToLive = null) 
     1654    { 
     1655        if ($driver !== null && $driver !== true && $driver !== false && ! ($driver instanceOf Doctrine_Cache_Interface)) { 
     1656            $msg = 'First argument should be instance of Doctrine_Cache_Interface or null.'; 
     1657            throw new Doctrine_Query_Exception($msg); 
     1658        } 
    16551659        $this->_queryCache = $driver; 
     1660         
    16561661        return $this->setQueryCacheLifeSpan($timeToLive); 
    16571662    } 
  • branches/1.0/tests/Query/CacheTestCase.php

    r4217 r4794  
    227227    } 
    228228     
     229    public function testQueryCacheCanBeDisabledForSingleQuery() 
     230    { 
     231        $cache = new Doctrine_Cache_Array(); 
     232        $q = new Doctrine_Query(); 
     233        $q->select('u.name')->from('User u')->leftJoin('u.Phonenumber p')->where('u.name = ?', 'walhala') 
     234                ->useQueryCache(false); 
     235         
     236        $coll = $q->execute(); 
     237         
     238        $this->assertEqual($cache->count(), 0); 
     239        $this->assertEqual(count($coll), 0); 
     240 
     241        $coll = $q->execute(); 
     242 
     243        $this->assertEqual($cache->count(), 0); 
     244        $this->assertEqual(count($coll), 0); 
     245    } 
     246     
    229247} 
  • branches/1.0/tests/Query/LimitTestCase.php

    r4716 r4794  
    136136    { 
    137137        $q = new Doctrine_Query(); 
    138         $q->select('User.name')->from('User')->where("User.Phonenumber.phonenumber LIKE '%123%'")->orderby('User.Email.address')->limit(5); 
     138        $q->select('User.name')->distinct()->from('User')->where("User.Phonenumber.phonenumber LIKE '%123%'")->orderby('User.Email.address')->limit(5); 
    139139         
    140140 
     
    302302        $this->assertEqual($q->getSql(), "SELECT p.id AS p__id, p.name AS p__name, t.id AS t__id, t.tag AS t__tag FROM photo p LEFT JOIN phototag p2 ON p.id = p2.photo_id LEFT JOIN tag t ON t.id = p2.tag_id WHERE p.id IN (SELECT DISTINCT p3.id FROM photo p3 LEFT JOIN phototag p4 ON p3.id = p4.photo_id LEFT JOIN tag t2 ON t2.id = p4.tag_id ORDER BY t2.id DESC LIMIT 10) ORDER BY t.id DESC"); 
    303303    } 
     304     
     305    public function testLimitSubqueryNotNeededIfSelectSingleFieldDistinct() 
     306    { 
     307        $q = new Doctrine_Query(); 
     308        $q->select('u.id')->distinct()->from('User u LEFT JOIN u.Phonenumber p'); 
     309        $q->where('u.name = ?'); 
     310        $q->limit(5); 
     311 
     312        $users = $q->execute(array('zYne')); 
     313        $this->assertEqual(1, $users->count()); 
     314         
     315        $this->assertEqual($q->getSql(), "SELECT DISTINCT e.id AS e__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.name = ? AND (e.type = 0) LIMIT 5"); 
     316    } 
    304317}