DB_result.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Database Result Class
00020  *
00021  * This is the platform-independent result class.
00022  * This class will not be called directly. Rather, the adapter
00023  * class for the specific database will extend and instantiate it.
00024  *
00025  * @category    Database
00026  * @author              ExpressionEngine Dev Team
00027  * @link                http://codeigniter.com/user_guide/database/
00028  */
00029 class CI_DB_result {
00030 
00031         var $conn_id            = NULL;
00032         var $result_id          = NULL;
00033         var $result_array       = array();
00034         var $result_object      = array();
00035         var $current_row        = 0;
00036         var $num_rows           = 0;
00037         var $row_data           = NULL;
00038 
00039 
00040         /**
00041          * Query result.  Acts as a wrapper function for the following functions.
00042          *
00043          * @access      public
00044          * @param       string  can be "object" or "array"
00045          * @return      mixed   either a result object or array 
00046          */     
00047         function result($type = 'object')
00048         {       
00049                 return ($type == 'object') ? $this->result_object() : $this->result_array();
00050         }
00051 
00052         // --------------------------------------------------------------------
00053 
00054         /**
00055          * Query result.  "object" version.
00056          *
00057          * @access      public
00058          * @return      object
00059          */     
00060         function result_object()
00061         {
00062                 if (count($this->result_object) > 0)
00063                 {
00064                         return $this->result_object;
00065                 }
00066                 
00067                 // In the event that query caching is on the result_id variable 
00068                 // will return FALSE since there isn't a valid SQL resource so 
00069                 // we'll simply return an empty array.
00070                 if ($this->result_id === FALSE OR $this->num_rows() == 0)
00071                 {
00072                         return array();
00073                 }
00074 
00075                 $this->_data_seek(0);
00076                 while ($row = $this->_fetch_object())
00077                 {
00078                         $this->result_object[] = $row;
00079                 }
00080                 
00081                 return $this->result_object;
00082         }
00083         
00084         // --------------------------------------------------------------------
00085 
00086         /**
00087          * Query result.  "array" version.
00088          *
00089          * @access      public
00090          * @return      array
00091          */     
00092         function result_array()
00093         {
00094                 if (count($this->result_array) > 0)
00095                 {
00096                         return $this->result_array;
00097                 }
00098 
00099                 // In the event that query caching is on the result_id variable 
00100                 // will return FALSE since there isn't a valid SQL resource so 
00101                 // we'll simply return an empty array.
00102                 if ($this->result_id === FALSE OR $this->num_rows() == 0)
00103                 {
00104                         return array();
00105                 }
00106 
00107                 $this->_data_seek(0);
00108                 while ($row = $this->_fetch_assoc())
00109                 {
00110                         $this->result_array[] = $row;
00111                 }
00112                 
00113                 return $this->result_array;
00114         }
00115 
00116         // --------------------------------------------------------------------
00117 
00118         /**
00119          * Query result.  Acts as a wrapper function for the following functions.
00120          *
00121          * @access      public
00122          * @param       string
00123          * @param       string  can be "object" or "array"
00124          * @return      mixed   either a result object or array 
00125          */     
00126         function row($n = 0, $type = 'object')
00127         {
00128                 if ( ! is_numeric($n))
00129                 {
00130                         // We cache the row data for subsequent uses
00131                         if ( ! is_array($this->row_data))
00132                         {
00133                                 $this->row_data = $this->row_array(0);
00134                         }
00135                 
00136                         // array_key_exists() instead of isset() to allow for MySQL NULL values
00137                         if (array_key_exists($n, $this->row_data))
00138                         {
00139                                 return $this->row_data[$n];
00140                         }
00141                         // reset the $n variable if the result was not achieved                 
00142                         $n = 0;
00143                 }
00144                 
00145                 return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
00146         }
00147 
00148         // --------------------------------------------------------------------
00149 
00150         /**
00151          * Assigns an item into a particular column slot
00152          *
00153          * @access      public
00154          * @return      object
00155          */     
00156         function set_row($key, $value = NULL)
00157         {
00158                 // We cache the row data for subsequent uses
00159                 if ( ! is_array($this->row_data))
00160                 {
00161                         $this->row_data = $this->row_array(0);
00162                 }
00163         
00164                 if (is_array($key))
00165                 {
00166                         foreach ($key as $k => $v)
00167                         {
00168                                 $this->row_data[$k] = $v;
00169                         }
00170                         
00171                         return;
00172                 }
00173         
00174                 if ($key != '' AND ! is_null($value))
00175                 {
00176                         $this->row_data[$key] = $value;
00177                 }
00178         }
00179 
00180         // --------------------------------------------------------------------
00181 
00182         /**
00183          * Returns a single result row - object version
00184          *
00185          * @access      public
00186          * @return      object
00187          */     
00188         function row_object($n = 0)
00189         {
00190                 $result = $this->result_object();
00191                 
00192                 if (count($result) == 0)
00193                 {
00194                         return $result;
00195                 }
00196 
00197                 if ($n != $this->current_row AND isset($result[$n]))
00198                 {
00199                         $this->current_row = $n;
00200                 }
00201 
00202                 return $result[$this->current_row];
00203         }
00204 
00205         // --------------------------------------------------------------------
00206 
00207         /**
00208          * Returns a single result row - array version
00209          *
00210          * @access      public
00211          * @return      array
00212          */     
00213         function row_array($n = 0)
00214         {
00215                 $result = $this->result_array();
00216 
00217                 if (count($result) == 0)
00218                 {
00219                         return $result;
00220                 }
00221                         
00222                 if ($n != $this->current_row AND isset($result[$n]))
00223                 {
00224                         $this->current_row = $n;
00225                 }
00226                 
00227                 return $result[$this->current_row];
00228         }
00229 
00230                 
00231         // --------------------------------------------------------------------
00232 
00233         /**
00234          * Returns the "first" row
00235          *
00236          * @access      public
00237          * @return      object
00238          */     
00239         function first_row($type = 'object')
00240         {
00241                 $result = $this->result($type);
00242 
00243                 if (count($result) == 0)
00244                 {
00245                         return $result;
00246                 }
00247                 return $result[0];
00248         }
00249         
00250         // --------------------------------------------------------------------
00251 
00252         /**
00253          * Returns the "last" row
00254          *
00255          * @access      public
00256          * @return      object
00257          */     
00258         function last_row($type = 'object')
00259         {
00260                 $result = $this->result($type);
00261 
00262                 if (count($result) == 0)
00263                 {
00264                         return $result;
00265                 }
00266                 return $result[count($result) -1];
00267         }       
00268 
00269         // --------------------------------------------------------------------
00270 
00271         /**
00272          * Returns the "next" row
00273          *
00274          * @access      public
00275          * @return      object
00276          */     
00277         function next_row($type = 'object')
00278         {
00279                 $result = $this->result($type);
00280 
00281                 if (count($result) == 0)
00282                 {
00283                         return $result;
00284                 }
00285 
00286                 if (isset($result[$this->current_row + 1]))
00287                 {
00288                         ++$this->current_row;
00289                 }
00290                                 
00291                 return $result[$this->current_row];
00292         }
00293         
00294         // --------------------------------------------------------------------
00295 
00296         /**
00297          * Returns the "previous" row
00298          *
00299          * @access      public
00300          * @return      object
00301          */     
00302         function previous_row($type = 'object')
00303         {
00304                 $result = $this->result($type);
00305 
00306                 if (count($result) == 0)
00307                 {
00308                         return $result;
00309                 }
00310 
00311                 if (isset($result[$this->current_row - 1]))
00312                 {
00313                         --$this->current_row;
00314                 }
00315                 return $result[$this->current_row];
00316         }
00317 
00318         // --------------------------------------------------------------------
00319 
00320         /**
00321          * The following functions are normally overloaded by the identically named
00322          * methods in the platform-specific driver -- except when query caching
00323          * is used.  When caching is enabled we do not load the other driver.
00324          * These functions are primarily here to prevent undefined function errors
00325          * when a cached result object is in use.  They are not otherwise fully
00326          * operational due to the unavailability of the database resource IDs with
00327          * cached results.
00328          */
00329         function num_rows() { return $this->num_rows; }
00330         function num_fields() { return 0; }
00331         function list_fields() { return array(); }
00332         function field_data() { return array(); }       
00333         function free_result() { return TRUE; }
00334         function _data_seek() { return TRUE; }
00335         function _fetch_assoc() { return array(); }     
00336         function _fetch_object() { return array(); }
00337         
00338 }
00339 // END DB_result class
00340 
00341 /* End of file DB_result.php */
00342 /* Location: ./system/database/DB_result.php */