blob: 70c40171fa36b722332f24db050b9430ae2c0acf [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Database Result Class
32 *
33 * This is the platform-independent result class.
34 * This class will not be called directly. Rather, the adapter
35 * class for the specific database will extend and instantiate it.
36 *
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_result {
42
Derek Jonesd01e4a62011-07-01 18:04:45 -050043 var $conn_id = NULL;
44 var $result_id = NULL;
45 var $result_array = array();
46 var $result_object = array();
47 var $custom_result_object = array();
48 var $current_row = 0;
49 var $num_rows = 0;
50 var $row_data = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +000051
52
53 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050054 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +000055 *
56 * @access public
57 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +020058 * @return mixed either a result object or array
59 */
Derek Allard2067d1a2008-11-13 22:59:24 +000060 function result($type = 'object')
Barry Mienydd671972010-10-04 16:33:58 +020061 {
Greg Akere70e92b2011-04-25 10:50:53 -050062 if ($type == 'array') return $this->result_array();
63 else if ($type == 'object') return $this->result_object();
64 else return $this->custom_result_object($type);
Derek Allard2067d1a2008-11-13 22:59:24 +000065 }
66
67 // --------------------------------------------------------------------
68
Greg Akere70e92b2011-04-25 10:50:53 -050069 /**
70 * Custom query result.
71 *
72 * @param class_name A string that represents the type of object you want back
73 * @return array of objects
74 */
75 function custom_result_object($class_name)
76 {
77 if (array_key_exists($class_name, $this->custom_result_object))
78 {
79 return $this->custom_result_object[$class_name];
80 }
John Crepezzi7b474302011-01-15 18:17:01 -050081
Greg Akere70e92b2011-04-25 10:50:53 -050082 if ($this->result_id === FALSE OR $this->num_rows() == 0)
83 {
84 return array();
85 }
Razican114ab092011-04-25 17:26:45 +020086
Greg Akere70e92b2011-04-25 10:50:53 -050087 // add the data to the object
88 $this->_data_seek(0);
89 $result_object = array();
90
John Crepezzi7b474302011-01-15 18:17:01 -050091 while ($row = $this->_fetch_object())
Greg Akere70e92b2011-04-25 10:50:53 -050092 {
93 $object = new $class_name();
94
95 foreach ($row as $key => $value)
96 {
97 $object->$key = $value;
98 }
99
John Crepezzi7b474302011-01-15 18:17:01 -0500100 $result_object[] = $object;
101 }
102
Greg Akere70e92b2011-04-25 10:50:53 -0500103 // return the array
104 return $this->custom_result_object[$class_name] = $result_object;
105 }
106
107 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -0500108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500110 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 *
112 * @access public
113 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200114 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 function result_object()
116 {
117 if (count($this->result_object) > 0)
118 {
119 return $this->result_object;
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
122 // In the event that query caching is on the result_id variable
123 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // we'll simply return an empty array.
125 if ($this->result_id === FALSE OR $this->num_rows() == 0)
126 {
127 return array();
128 }
129
130 $this->_data_seek(0);
131 while ($row = $this->_fetch_object())
132 {
133 $this->result_object[] = $row;
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 return $this->result_object;
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 // --------------------------------------------------------------------
140
141 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500142 * Query result. "array" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 *
144 * @access public
145 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200146 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 function result_array()
148 {
149 if (count($this->result_array) > 0)
150 {
151 return $this->result_array;
152 }
153
Barry Mienydd671972010-10-04 16:33:58 +0200154 // In the event that query caching is on the result_id variable
155 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 // we'll simply return an empty array.
157 if ($this->result_id === FALSE OR $this->num_rows() == 0)
158 {
159 return array();
160 }
161
162 $this->_data_seek(0);
163 while ($row = $this->_fetch_assoc())
164 {
165 $this->result_array[] = $row;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 return $this->result_array;
169 }
170
171 // --------------------------------------------------------------------
172
173 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500174 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 *
176 * @access public
177 * @param string
178 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +0200179 * @return mixed either a result object or array
180 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 function row($n = 0, $type = 'object')
182 {
183 if ( ! is_numeric($n))
184 {
185 // We cache the row data for subsequent uses
186 if ( ! is_array($this->row_data))
187 {
188 $this->row_data = $this->row_array(0);
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // array_key_exists() instead of isset() to allow for MySQL NULL values
192 if (array_key_exists($n, $this->row_data))
193 {
194 return $this->row_data[$n];
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196 // reset the $n variable if the result was not achieved
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 $n = 0;
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Greg Akere70e92b2011-04-25 10:50:53 -0500200 if ($type == 'object') return $this->row_object($n);
201 else if ($type == 'array') return $this->row_array($n);
202 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 // --------------------------------------------------------------------
206
207 /**
208 * Assigns an item into a particular column slot
209 *
210 * @access public
211 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200212 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 function set_row($key, $value = NULL)
214 {
215 // We cache the row data for subsequent uses
216 if ( ! is_array($this->row_data))
217 {
218 $this->row_data = $this->row_array(0);
219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 if (is_array($key))
222 {
223 foreach ($key as $k => $v)
224 {
225 $this->row_data[$k] = $v;
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return;
229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 if ($key != '' AND ! is_null($value))
232 {
233 $this->row_data[$key] = $value;
234 }
235 }
236
237 // --------------------------------------------------------------------
238
Greg Akere70e92b2011-04-25 10:50:53 -0500239 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500240 * Returns a single result row - custom object version
241 *
242 * @access public
243 * @return object
244 */
245 function custom_row_object($n, $type)
246 {
247 $result = $this->custom_result_object($type);
248
249 if (count($result) == 0)
250 {
251 return $result;
252 }
253
254 if ($n != $this->current_row AND isset($result[$n]))
255 {
256 $this->current_row = $n;
257 }
258
259 return $result[$this->current_row];
260 }
261
Greg Akere70e92b2011-04-25 10:50:53 -0500262 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * Returns a single result row - object version
264 *
265 * @access public
266 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200267 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 function row_object($n = 0)
269 {
270 $result = $this->result_object();
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if (count($result) == 0)
273 {
274 return $result;
275 }
276
277 if ($n != $this->current_row AND isset($result[$n]))
278 {
279 $this->current_row = $n;
280 }
281
282 return $result[$this->current_row];
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Returns a single result row - array version
289 *
290 * @access public
291 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 function row_array($n = 0)
294 {
295 $result = $this->result_array();
296
297 if (count($result) == 0)
298 {
299 return $result;
300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 if ($n != $this->current_row AND isset($result[$n]))
303 {
304 $this->current_row = $n;
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 return $result[$this->current_row];
308 }
309
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
312
313 /**
314 * Returns the "first" row
315 *
316 * @access public
317 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200318 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 function first_row($type = 'object')
320 {
321 $result = $this->result($type);
322
323 if (count($result) == 0)
324 {
325 return $result;
326 }
327 return $result[0];
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // --------------------------------------------------------------------
331
332 /**
333 * Returns the "last" row
334 *
335 * @access public
336 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200337 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 function last_row($type = 'object')
339 {
340 $result = $this->result($type);
341
342 if (count($result) == 0)
343 {
344 return $result;
345 }
346 return $result[count($result) -1];
Barry Mienydd671972010-10-04 16:33:58 +0200347 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000348
349 // --------------------------------------------------------------------
350
351 /**
352 * Returns the "next" row
353 *
354 * @access public
355 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 function next_row($type = 'object')
358 {
359 $result = $this->result($type);
360
361 if (count($result) == 0)
362 {
363 return $result;
364 }
365
366 if (isset($result[$this->current_row + 1]))
367 {
368 ++$this->current_row;
369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 return $result[$this->current_row];
372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // --------------------------------------------------------------------
375
376 /**
377 * Returns the "previous" row
378 *
379 * @access public
380 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200381 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 function previous_row($type = 'object')
383 {
384 $result = $this->result($type);
385
386 if (count($result) == 0)
387 {
388 return $result;
389 }
390
391 if (isset($result[$this->current_row - 1]))
392 {
393 --$this->current_row;
394 }
395 return $result[$this->current_row];
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * The following functions are normally overloaded by the identically named
402 * methods in the platform-specific driver -- except when query caching
Derek Jones37f4b9c2011-07-01 17:56:50 -0500403 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * These functions are primarily here to prevent undefined function errors
Derek Jones37f4b9c2011-07-01 17:56:50 -0500405 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * operational due to the unavailability of the database resource IDs with
407 * cached results.
408 */
409 function num_rows() { return $this->num_rows; }
410 function num_fields() { return 0; }
411 function list_fields() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200412 function field_data() { return array(); }
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 function free_result() { return TRUE; }
414 function _data_seek() { return TRUE; }
Barry Mienydd671972010-10-04 16:33:58 +0200415 function _fetch_assoc() { return array(); }
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418}
419// END DB_result class
420
421/* End of file DB_result.php */
John Crepezzi7b474302011-01-15 18:17:01 -0500422/* Location: ./system/database/DB_result.php */