Changeset 80

Show
Ignore:
Timestamp:
05/26/2007 02:52:47 AM (19 months ago)
Author:
canglan
Message:

added db->count_records() to support records count for active record functions

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/database/DB_active_rec.php

    r24 r80  
    696696 
    697697        /** 
     698         * Count Records 
     699         * 
     700         * Count table records by using active record conditions 
     701         * please use count_all() if you don't need active record conditions 
     702         * performance of count_records() is 25% slower than count_all() 
     703         * 
     704         * @access      public 
     705         * @param       string 
     706         * @return      string 
     707         */ 
     708        function count_records($table = '') 
     709        { 
     710                if ($table == '') 
     711                { 
     712                        return '0'; 
     713                } 
     714                else 
     715                { 
     716                        $this->from($table); 
     717                } 
     718         
     719                $sql = $this->_compile_select(TRUE); 
     720 
     721                $query  = $this->query($sql); 
     722                $result = $query->result(); 
     723                $this->_reset_select(); 
     724                return $result[0]->numrows; 
     725        } 
     726         
     727        // -------------------------------------------------------------------- 
     728 
     729        /** 
    698730         * Use Table - DEPRECATED 
    699731         * 
     
    747779         * 
    748780         * @access      private 
     781         * @param       boolean $count TRUE for counting records (to be used by count_records()) 
    749782         * @return      string 
    750783         */ 
    751         function _compile_select() 
     784        function _compile_select($count = FALSE) 
    752785        { 
    753786                $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; 
    754          
    755                 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select); 
     787                 
     788                if ($count == FALSE) 
     789                { 
     790                        $select = '*'; 
     791                } 
     792                else 
     793                { 
     794                        $select = 'COUNT(*) AS numrows'; 
     795                } 
     796                 
     797                $sql .= (count($this->ar_select) == 0) ? $select : implode(', ', $this->ar_select); 
    756798 
    757799                if (count($this->ar_from) > 0)