CodeIgniter installed
[living-lab-site.git] / system / database / DB_result.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * Database Result Class
20  *
21  * This is the platform-independent result class.
22  * This class will not be called directly. Rather, the adapter
23  * class for the specific database will extend and instantiate it.
24  *
25  * @category    Database
26  * @author              ExpressionEngine Dev Team
27  * @link                http://codeigniter.com/user_guide/database/
28  */
29 class CI_DB_result {
30
31         var $conn_id                  = NULL;
32         var $result_id                = NULL;
33         var $result_array             = array();
34         var $result_object            = array();
35     var $custom_result_object = array();
36         var $current_row              = 0;
37         var $num_rows                 = 0;
38         var $row_data                 = NULL;
39
40
41         /**
42          * Query result.  Acts as a wrapper function for the following functions.
43          *
44          * @access      public
45          * @param       string  can be "object" or "array"
46          * @return      mixed   either a result object or array
47          */
48         function result($type = 'object')
49         {
50         if ($type == 'array') return $this->result_array();
51         else if ($type == 'object') return $this->result_object();
52         else return $this->custom_result_object($type);
53         }
54
55         // --------------------------------------------------------------------
56
57     /**
58      * Custom query result.
59      *
60      * @param  class_name  A string that represents the type of object you want back
61      * @return array of objects
62      */
63     function custom_result_object($class_name)
64     {
65         if (array_key_exists($class_name, $this->custom_result_object))
66         {
67             return $this->custom_result_object[$class_name];
68         }
69         
70         if ($this->result_id === FALSE OR $this->num_rows() == 0)
71         {
72             return array();
73         }
74
75         // add the data to the object
76         $this->_data_seek(0);
77         $result_object = array();
78                 while ($row = $this->_fetch_object())
79         {
80             $object = new $class_name();
81             foreach ($row as $key => $value)
82             {
83                 $object->$key = $value;
84             }
85                         $result_object[] = $object;
86                 }
87
88         // return the array
89         return $this->custom_result_object[$class_name] = $result_object;
90     }
91
92         /**
93          * Query result.  "object" version.
94          *
95          * @access      public
96          * @return      object
97          */
98         function result_object()
99         {
100                 if (count($this->result_object) > 0)
101                 {
102                         return $this->result_object;
103                 }
104
105                 // In the event that query caching is on the result_id variable
106                 // will return FALSE since there isn't a valid SQL resource so
107                 // we'll simply return an empty array.
108                 if ($this->result_id === FALSE OR $this->num_rows() == 0)
109                 {
110                         return array();
111                 }
112
113                 $this->_data_seek(0);
114                 while ($row = $this->_fetch_object())
115                 {
116                         $this->result_object[] = $row;
117                 }
118
119                 return $this->result_object;
120         }
121
122         // --------------------------------------------------------------------
123
124         /**
125          * Query result.  "array" version.
126          *
127          * @access      public
128          * @return      array
129          */
130         function result_array()
131         {
132                 if (count($this->result_array) > 0)
133                 {
134                         return $this->result_array;
135                 }
136
137                 // In the event that query caching is on the result_id variable
138                 // will return FALSE since there isn't a valid SQL resource so
139                 // we'll simply return an empty array.
140                 if ($this->result_id === FALSE OR $this->num_rows() == 0)
141                 {
142                         return array();
143                 }
144
145                 $this->_data_seek(0);
146                 while ($row = $this->_fetch_assoc())
147                 {
148                         $this->result_array[] = $row;
149                 }
150
151                 return $this->result_array;
152         }
153
154         // --------------------------------------------------------------------
155
156         /**
157          * Query result.  Acts as a wrapper function for the following functions.
158          *
159          * @access      public
160          * @param       string
161          * @param       string  can be "object" or "array"
162          * @return      mixed   either a result object or array
163          */
164         function row($n = 0, $type = 'object')
165         {
166                 if ( ! is_numeric($n))
167                 {
168                         // We cache the row data for subsequent uses
169                         if ( ! is_array($this->row_data))
170                         {
171                                 $this->row_data = $this->row_array(0);
172                         }
173
174                         // array_key_exists() instead of isset() to allow for MySQL NULL values
175                         if (array_key_exists($n, $this->row_data))
176                         {
177                                 return $this->row_data[$n];
178                         }
179                         // reset the $n variable if the result was not achieved
180                         $n = 0;
181                 }
182
183         if ($type == 'object') return $this->row_object($n);
184         else if ($type == 'array') return $this->row_array($n);
185         else return $this->custom_row_object($n, $type);
186         }
187
188         // --------------------------------------------------------------------
189
190         /**
191          * Assigns an item into a particular column slot
192          *
193          * @access      public
194          * @return      object
195          */
196         function set_row($key, $value = NULL)
197         {
198                 // We cache the row data for subsequent uses
199                 if ( ! is_array($this->row_data))
200                 {
201                         $this->row_data = $this->row_array(0);
202                 }
203
204                 if (is_array($key))
205                 {
206                         foreach ($key as $k => $v)
207                         {
208                                 $this->row_data[$k] = $v;
209                         }
210
211                         return;
212                 }
213
214                 if ($key != '' AND ! is_null($value))
215                 {
216                         $this->row_data[$key] = $value;
217                 }
218         }
219
220         // --------------------------------------------------------------------
221
222     /**
223          * Returns a single result row - custom object version
224          *
225          * @access      public
226          * @return      object
227          */
228         function custom_row_object($n, $type)
229         {
230                 $result = $this->custom_result_object($type);
231
232                 if (count($result) == 0)
233                 {
234                         return $result;
235                 }
236
237                 if ($n != $this->current_row AND isset($result[$n]))
238                 {
239                         $this->current_row = $n;
240                 }
241
242                 return $result[$this->current_row];
243         }
244
245     /**
246          * Returns a single result row - object version
247          *
248          * @access      public
249          * @return      object
250          */
251         function row_object($n = 0)
252         {
253                 $result = $this->result_object();
254
255                 if (count($result) == 0)
256                 {
257                         return $result;
258                 }
259
260                 if ($n != $this->current_row AND isset($result[$n]))
261                 {
262                         $this->current_row = $n;
263                 }
264
265                 return $result[$this->current_row];
266         }
267
268         // --------------------------------------------------------------------
269
270         /**
271          * Returns a single result row - array version
272          *
273          * @access      public
274          * @return      array
275          */
276         function row_array($n = 0)
277         {
278                 $result = $this->result_array();
279
280                 if (count($result) == 0)
281                 {
282                         return $result;
283                 }
284
285                 if ($n != $this->current_row AND isset($result[$n]))
286                 {
287                         $this->current_row = $n;
288                 }
289
290                 return $result[$this->current_row];
291         }
292
293
294         // --------------------------------------------------------------------
295
296         /**
297          * Returns the "first" row
298          *
299          * @access      public
300          * @return      object
301          */
302         function first_row($type = 'object')
303         {
304                 $result = $this->result($type);
305
306                 if (count($result) == 0)
307                 {
308                         return $result;
309                 }
310                 return $result[0];
311         }
312
313         // --------------------------------------------------------------------
314
315         /**
316          * Returns the "last" row
317          *
318          * @access      public
319          * @return      object
320          */
321         function last_row($type = 'object')
322         {
323                 $result = $this->result($type);
324
325                 if (count($result) == 0)
326                 {
327                         return $result;
328                 }
329                 return $result[count($result) -1];
330         }
331
332         // --------------------------------------------------------------------
333
334         /**
335          * Returns the "next" row
336          *
337          * @access      public
338          * @return      object
339          */
340         function next_row($type = 'object')
341         {
342                 $result = $this->result($type);
343
344                 if (count($result) == 0)
345                 {
346                         return $result;
347                 }
348
349                 if (isset($result[$this->current_row + 1]))
350                 {
351                         ++$this->current_row;
352                 }
353
354                 return $result[$this->current_row];
355         }
356
357         // --------------------------------------------------------------------
358
359         /**
360          * Returns the "previous" row
361          *
362          * @access      public
363          * @return      object
364          */
365         function previous_row($type = 'object')
366         {
367                 $result = $this->result($type);
368
369                 if (count($result) == 0)
370                 {
371                         return $result;
372                 }
373
374                 if (isset($result[$this->current_row - 1]))
375                 {
376                         --$this->current_row;
377                 }
378                 return $result[$this->current_row];
379         }
380
381         // --------------------------------------------------------------------
382
383         /**
384          * The following functions are normally overloaded by the identically named
385          * methods in the platform-specific driver -- except when query caching
386          * is used.  When caching is enabled we do not load the other driver.
387          * These functions are primarily here to prevent undefined function errors
388          * when a cached result object is in use.  They are not otherwise fully
389          * operational due to the unavailability of the database resource IDs with
390          * cached results.
391          */
392         function num_rows() { return $this->num_rows; }
393         function num_fields() { return 0; }
394         function list_fields() { return array(); }
395         function field_data() { return array(); }
396         function free_result() { return TRUE; }
397         function _data_seek() { return TRUE; }
398         function _fetch_assoc() { return array(); }
399         function _fetch_object() { return array(); }
400
401 }
402 // END DB_result class
403
404 /* End of file DB_result.php */
405 /* Location: ./system/database/DB_result.php */