Ticket #1195: RawSql_p2.patch

File RawSql_p2.patch, 3.0 KB (added by stefan, 6 months ago)
  • RawSql.php

     
    254254        return $q; 
    255255    } 
    256256 
     257 
     258        /** 
     259     * getCountQuery 
     260     * builds the count query. 
     261     * 
     262     * @return string       the built sql query 
     263     */ 
     264        public function getCountQuery($params = array()) 
     265    { 
     266        //Doing COUNT( DISTINCT rootComponent.id ) 
     267        //This is not correct, if the result is not hydrated by doctrine, but it mimics the behaviour of Doctrine_Query::getCountQuery 
     268        reset($this->_queryComponents); 
     269        $componentAlias = key($this->_queryComponents); 
     270        $tableAlias = $this->getSqlTableAlias($componentAlias); 
     271        $fields = array(); 
     272 
     273        foreach ((array) $this->_queryComponents[$componentAlias]['table']->getIdentifierColumnNames() as $key) { 
     274                $fields[] = $tableAlias . '.' . $key; 
     275        } 
     276 
     277        $q = 'SELECT COUNT( DISTINCT '.implode(',',$fields).') as num_results'; 
     278 
     279        $string = $this->getInheritanceCondition($this->getRootAlias()); 
     280        if ( ! empty($string)) { 
     281            $this->_sqlParts['where'][] = $string; 
     282        } 
     283 
     284        $q .= ( ! empty($this->_sqlParts['from']))?    ' FROM '     . implode(' ', $this->_sqlParts['from']) : ''; 
     285        $q .= ( ! empty($this->_sqlParts['where']))?   ' WHERE '    . implode(' AND ', $this->_sqlParts['where']) : ''; 
     286        $q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : ''; 
     287        $q .= ( ! empty($this->_sqlParts['having']))?  ' HAVING '   . implode(' AND ', $this->_sqlParts['having']) : ''; 
     288 
     289        if ( ! empty($string)) { 
     290            array_pop($this->_sqlParts['where']); 
     291        } 
     292 
     293        return $q; 
     294    } 
     295 
     296        /** 
     297     * count 
     298     * fetches the count of the query 
     299     * 
     300     * This method executes the main query without all the 
     301     * selected fields, ORDER BY part, LIMIT part and OFFSET part. 
     302     * 
     303     * This is an exact copy of the Dql Version 
     304     * 
     305     * @see Doctrine_Query::count() 
     306     * @param array $params        an array of prepared statement parameters 
     307     * @return integer             the count of this query 
     308     */ 
     309    public function count($params = array()) 
     310    { 
     311        $q = $this->getCountQuery(); 
     312 
     313        if ( ! is_array($params)) { 
     314            $params = array($params); 
     315        } 
     316 
     317        $params = array_merge($this->_params['join'], $this->_params['where'], $this->_params['having'], $params); 
     318 
     319        $params = $this->convertEnums($params); 
     320 
     321        $results = $this->getConnection()->fetchAll($q, $params); 
     322 
     323        if (count($results) > 1) { 
     324            $count = count($results); 
     325        } else { 
     326            if (isset($results[0])) { 
     327                $results[0] = array_change_key_case($results[0], CASE_LOWER); 
     328                $count = $results[0]['num_results']; 
     329            } else { 
     330                $count = 0; 
     331            } 
     332        } 
     333 
     334        return (int) $count; 
     335    } 
     336 
    257337    /** 
    258338     * getFields 
    259339     * returns the fields associated with this parser