Changeset 4714 for branches

Show
Ignore:
Timestamp:
07/26/08 20:44:54 (6 months ago)
Author:
guilhermeblanco
Message:

Fixes #1195.

Location:
branches
Files:
2 added
4 modified

Legend:

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

    r4708 r4714  
    11761176                    case 'pgsql': 
    11771177                        // pgsql needs special nested LIMIT subquery 
    1178                         $subquery = 'SELECT doctrine_subquery_alias.' . $idColumnName . ' FROM (' . $subquery . ') AS doctrine_subquery_alias'; 
     1178                        $subquery = 'SELECT ' 
     1179                            . $this->_conn->quoteIdentifier('doctrine_subquery_alias.' . $idColumnName) 
     1180                            . ' FROM (' . $subquery . ') AS '  
     1181                            . $this->_conn->quoteIdentifier('doctrine_subquery_alias'); 
    11791182                        break; 
    11801183                } 
  • branches/0.11/lib/Doctrine/RawSql.php

    r4572 r4714  
    7373            return $this; 
    7474        } 
    75         if ( ! isset($this->parts[$queryPartName])) { 
     75        if ( ! isset($this->_sqlParts[$queryPartName])) { 
    7676            $this->_sqlParts[$queryPartName] = array(); 
    7777        } 
     
    221221        } 
    222222         
     223        $q = 'SELECT '; 
     224 
     225        if ($this->_sqlParts['distinct'] == true) { 
     226            $q .= 'DISTINCT '; 
     227        } 
     228 
    223229        // first add the fields of the root component 
    224230        reset($this->_queryComponents); 
    225231        $componentAlias = key($this->_queryComponents); 
    226232 
    227         $q = 'SELECT ' . implode(', ', $select[$componentAlias]); 
     233        $q .= implode(', ', $select[$componentAlias]); 
    228234        unset($select[$componentAlias]); 
    229235 
     
    235241 
    236242        $string = $this->getInheritanceCondition($this->getRootAlias()); 
     243 
    237244        if ( ! empty($string)) { 
    238245            $this->_sqlParts['where'][] = $string; 
    239246        } 
    240         $copy = $this->_sqlParts; 
    241         unset($copy['select']); 
    242247 
    243248        $q .= ( ! empty($this->_sqlParts['from']))?    ' FROM '     . implode(' ', $this->_sqlParts['from']) : ''; 
     
    253258        } 
    254259        return $q; 
     260    } 
     261     
     262    /** 
     263     * getCountQuery 
     264     * builds the count query. 
     265     * 
     266     * @return string       the built sql query 
     267     */ 
     268        public function getCountQuery($params = array()) 
     269    { 
     270        //Doing COUNT( DISTINCT rootComponent.id ) 
     271        //This is not correct, if the result is not hydrated by doctrine, but it mimics the behaviour of Doctrine_Query::getCountQuery 
     272        reset($this->_queryComponents); 
     273        $componentAlias = key($this->_queryComponents); 
     274        $tableAlias = $this->getSqlTableAlias($componentAlias); 
     275        $fields = array(); 
     276 
     277        foreach ((array) $this->_queryComponents[$componentAlias]['table']->getIdentifierColumnNames() as $key) { 
     278                $fields[] = $tableAlias . '.' . $key; 
     279        } 
     280 
     281        $q = 'SELECT COUNT( DISTINCT '.implode(',',$fields).') as num_results'; 
     282 
     283        $string = $this->getInheritanceCondition($this->getRootAlias()); 
     284        if ( ! empty($string)) { 
     285            $this->_sqlParts['where'][] = $string; 
     286        } 
     287 
     288        $q .= ( ! empty($this->_sqlParts['from']))?    ' FROM '     . implode(' ', $this->_sqlParts['from']) : ''; 
     289        $q .= ( ! empty($this->_sqlParts['where']))?   ' WHERE '    . implode(' AND ', $this->_sqlParts['where']) : ''; 
     290        $q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : ''; 
     291        $q .= ( ! empty($this->_sqlParts['having']))?  ' HAVING '   . implode(' AND ', $this->_sqlParts['having']) : ''; 
     292 
     293        if ( ! empty($string)) { 
     294            array_pop($this->_sqlParts['where']); 
     295        } 
     296 
     297        return $q; 
     298    } 
     299 
     300        /** 
     301     * count 
     302     * fetches the count of the query 
     303     * 
     304     * This method executes the main query without all the 
     305     * selected fields, ORDER BY part, LIMIT part and OFFSET part. 
     306     * 
     307     * This is an exact copy of the Dql Version 
     308     * 
     309     * @see Doctrine_Query::count() 
     310     * @param array $params        an array of prepared statement parameters 
     311     * @return integer             the count of this query 
     312     */ 
     313    public function count($params = array()) 
     314    { 
     315        $q = $this->getCountQuery(); 
     316 
     317        if ( ! is_array($params)) { 
     318            $params = array($params); 
     319        } 
     320 
     321        $params = array_merge($this->_params['join'], $this->_params['where'], $this->_params['having'], $params); 
     322 
     323        $params = $this->convertEnums($params); 
     324 
     325        $results = $this->getConnection()->fetchAll($q, $params); 
     326 
     327        if (count($results) > 1) { 
     328            $count = count($results); 
     329        } else { 
     330            if (isset($results[0])) { 
     331                $results[0] = array_change_key_case($results[0], CASE_LOWER); 
     332                $count = $results[0]['num_results']; 
     333            } else { 
     334                $count = 0; 
     335            } 
     336        } 
     337 
     338        return (int) $count; 
    255339    } 
    256340 
  • branches/0.11/tests/run.php

    r4697 r4714  
    8787$tickets->addTestCase(new Doctrine_Ticket_1175_TestCase()); 
    8888$tickets->addTestCase(new Doctrine_Ticket_1192_TestCase()); 
     89$tickets->addTestCase(new Doctrine_Ticket_1195_TestCase()); 
    8990$tickets->addTestCase(new Doctrine_Ticket_1205_TestCase()); 
    9091$tickets->addTestCase(new Doctrine_Ticket_1206_TestCase()); 
  • branches/1.0/lib/Doctrine/RawSql.php

    r4684 r4714  
    222222         
    223223        $q = 'SELECT '; 
    224         if($this->_sqlParts['distinct'] == true) $q .= 'DISTINCT '; 
     224 
     225        if ($this->_sqlParts['distinct'] == true) { 
     226            $q .= 'DISTINCT '; 
     227        } 
    225228 
    226229        // first add the fields of the root component 
    227230        reset($this->_queryComponents); 
    228231        $componentAlias = key($this->_queryComponents); 
     232         
    229233        $q .= implode(', ', $select[$componentAlias]); 
    230234        unset($select[$componentAlias]); 
     
    237241 
    238242        $string = $this->getInheritanceCondition($this->getRootAlias()); 
     243 
    239244        if ( ! empty($string)) { 
    240245            $this->_sqlParts['where'][] = $string;