Changeset 2285 for branches

Show
Ignore:
Timestamp:
12/08/10 13:05:01 (18 months ago)
Author:
nick_ramsay
Message:

[1.5] Removed all "select"-related methods from Database class. re #173

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/1.5/libs/Database.php

    r2197 r2285  
    2727class Database { 
    2828 
    29         public $select = array(); 
    30         public $table = ''; 
    31         public $where = array(); 
    32         public $orderby = ''; 
    33         public $limit = ''; 
    34         public $prepare_array = array(); 
    35         public $cache = true; 
    36         public $query_only = false; 
    3729        public $h; 
    3830        public $dbh; 
     
    481473        } 
    482474 
    483         /** 
    484          * Gets data from the database 
    485          * 
    486          * @param string $table - table name without a prefix. 
    487          * @param array $prepare array - optional if calling directly 
    488          * @return array|false - object array of data 
    489          */ 
    490         public function getData($h, $table = 'posts', $prepare_array = array()) 
    491         { 
    492                 if ($prepare_array) { 
    493                         $this->prepare_array = $prepare_array; 
    494                 } 
    495  
    496                 if (!$this->prepare_array) { 
    497                         return FALSE; 
    498                 } 
    499  
    500                 if (empty($this->prepare_array[1])) { 
    501                         // there aren't any %d or %s parameters to fill in, so we'll skip the prepare function 
    502                         if ($this->cache) { 
    503                                 $h->smartCache('on', $table, 60, $this->prepare_array[0]); 
    504                         } // start using cache 
    505                         $data = $h->db->get_results($this->prepare_array[0]); // ignoring the prepare function. 
    506                 } else { 
    507                         $query = $h->db->prepare($this->prepare_array); 
    508                         if ($this->cache) { 
    509                                 $h->smartCache('on', $table, 60, $query); 
    510                         } // start using cache 
    511                         $data = $h->db->get_results($query); 
    512                 } 
    513  
    514                 $h->smartCache('off'); // stop using cache 
    515  
    516                 $this->emptyObject(); // reset the object or we'll confuse subsequent DB calls. 
    517  
    518                 if ($data) { 
    519                         return $data; 
    520                 } 
    521                 return FALSE; 
    522         } 
    523  
    524         /** 
    525          * Build an SQL "SELECT" query 
    526          * 
    527          * @param array $select - associative array of select terms 
    528          * @param string $table - abbreviated table name, e.g. posts, users, comments 
    529          * @param array $where - associative array of where terms, e.g. array('id = %d' = 5, 'name = %s' = 'tony') 
    530          * @param string $orderby - e.g. post_date DESC 
    531          * @param string $limit - "X, Y" 
    532          * @param bool $cache - cache results 
    533          * @param bool $query_only - return just the query, not the results? 
    534          * @return array|false 
    535          */ 
    536         public function select($h, $select = array(), $table = '', $where = array(), $orderby = '', $limit = '', $cache = true, $query_only = false) 
    537         { 
    538                 // for flexibility, we want to use object properties: 
    539                 $this->fillObject($select, $table, $where, $orderby, $limit, $cache, $query_only); 
    540  
    541                 // plugin hook 
    542                 $h->pluginHook('database_select'); 
    543  
    544                 $this->prepare_array = array(); 
    545                 $this->prepare_array[0] = "temp"; // placeholder to be later filled with the SQL query. 
    546                 // set SELECT: 
    547                 $select = ($this->select) ? $this->buildSelect() : ''; 
    548  
    549                 // set TABLE: 
    550                 $table = ($this->table) ? DB_PREFIX.$this->table : TABLE_POSTS; // defaults to TABLE_POSTS 
    551                 // set WHERE 
    552                 $where = ($this->where) ? $this->buildWhere() : ''; 
    553  
    554                 // set ORDER BY 
    555                 $orderby = ($this->orderby) ? ' ORDER BY '.$this->orderby : ''; 
    556  
    557                 // set LIMIT 
    558                 $limit = ($this->limit) ? ' LIMIT '.$this->limit : ''; 
    559  
    560                 // Build query: 
    561                 $sql = "SELECT ".$select." FROM ".$table.$where.$orderby.$limit; 
    562  
    563                 $this->prepare_array[0] = $sql; 
    564  
    565                 if ($this->query_only) { 
    566                         return $this->prepare_array; 
    567                 } 
    568  
    569                 /*      Example: 
    570                   $this->prepare_array[0] is "SELECT user_id FROM hotaru_users WHERE user_id = %d" 
    571                   $this->prepare_array[1] is "5", where 5 fills the %d 
    572                  */ 
    573  
    574                 // get the data and return it 
    575                 return $this->getData($h, $this->table); 
    576         } 
    577475 
    578476        /** 
     
    625523        } 
    626524 
    627         /** 
    628          * Build the SELECT string 
    629          * 
    630          * @return string 
    631          */ 
    632         private function buildSelect() 
    633         { 
    634                 if (!$this->select) { 
    635                         return ''; 
    636                 } 
    637  
    638                 $select = ""; // the new select string we make from the $this->select array 
    639  
    640                 foreach ($this->select as $key => $value) { 
    641                         // e.g. 
    642                         // $select[0] = 'post_id'; 
    643                         // $select[1] = array('blah %s blah'=>'value for %s'); 
    644                         // Push the values of %s and %d into the prepare_array 
    645                         if (is_array($value)) { 
    646                                 foreach ($value as $k => $v) { 
    647                                         $select .= $k.', '; 
    648                                         array_push($this->prepare_array, $v); 
    649                                 } 
    650                         } else { 
    651                                 // otherwise add the single value to the select string 
    652                                 $select .= $value.', '; 
    653                         } 
    654                 } 
    655                 $select = rstrtrim($select, ", "); // strip off trailing AND 
    656  
    657                 return $select; 
    658         } 
    659  
    660         /** 
    661          * Build the WHERE string 
    662          * 
    663          * @return string 
    664          */ 
    665         private function buildWhere() 
    666         { 
    667                 if (!$this->where) { 
    668                         return ''; 
    669                 } 
    670  
    671                 $filter = " WHERE "; 
    672                 foreach ($this->where as $key => $value) { 
    673                         $filter .= $key." AND "; // e.g. " post_tags LIKE %s " 
    674                         // Push the values of %s and %d into the prepare_array 
    675                         // sometimes the filter might contain multiple values, eg. 
    676                         // WHERE post_status = %s OR post_status = %s. In that case, 
    677                         // the values are stored in an array, e.g. array('top', 'new'). 
    678                         if (is_array($value)) { 
    679                                 foreach ($value as $v) { 
    680                                         array_push($this->prepare_array, $v); 
    681                                 } 
    682                         } else { 
    683                                 // otherwise, push the single value into $this->prepare_array: 
    684                                 array_push($this->prepare_array, $value); 
    685                         } 
    686                 } 
    687                 $filter = rstrtrim($filter, " AND "); // strip off trailing AND 
    688  
    689                 return $filter; 
    690         } 
    691525 
    692526        /** 
     
    821655                } 
    822656        } 
    823  
    824         /** 
    825          * Empty Database Object 
    826          */ 
    827         private function emptyObject() 
    828         { 
    829                 $this->select = array(); 
    830                 $this->table = ''; 
    831                 $this->where = array(); 
    832                 $this->orderby = ''; 
    833                 $this->limit = ''; 
    834                 $this->prepare_array = array(); 
    835                 $this->cache = true; 
    836                 $this->query_only = false; 
    837         } 
    838657}