blob: 730443222fa113e4b339d316679a7dcb105c19c6 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?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
Andrey Andreev24276a32012-01-08 02:44:38 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24276a32012-01-08 02:44:38 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Database Result Class
30 *
31 * This is the platform-independent result class.
32 * This class will not be called directly. Rather, the adapter
33 * class for the specific database will extend and instantiate it.
34 *
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
39class CI_DB_result {
40
Andrey Andreev24276a32012-01-08 02:44:38 +020041 public $conn_id = NULL;
42 public $result_id = NULL;
43 public $result_array = array();
44 public $result_object = array();
45 public $custom_result_object = array();
46 public $current_row = 0;
47 public $num_rows = 0;
48 public $row_data = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +000049
50 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050051 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +000052 *
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +020054 * @return mixed either a result object or array
55 */
Andrey Andreevbc95e472011-10-20 09:44:48 +030056 public function result($type = 'object')
Barry Mienydd671972010-10-04 16:33:58 +020057 {
Andrey Andreev24276a32012-01-08 02:44:38 +020058 if ($type === 'array') return $this->result_array();
59 elseif ($type === 'object') return $this->result_object();
Greg Akere70e92b2011-04-25 10:50:53 -050060 else return $this->custom_result_object($type);
Derek Allard2067d1a2008-11-13 22:59:24 +000061 }
62
63 // --------------------------------------------------------------------
64
Greg Akere70e92b2011-04-25 10:50:53 -050065 /**
66 * Custom query result.
67 *
Andrey Andreev24276a32012-01-08 02:44:38 +020068 * @param string A string that represents the type of object you want back
69 * @return array of objects
Greg Akere70e92b2011-04-25 10:50:53 -050070 */
Andrey Andreevbc95e472011-10-20 09:44:48 +030071 public function custom_result_object($class_name)
Greg Akere70e92b2011-04-25 10:50:53 -050072 {
73 if (array_key_exists($class_name, $this->custom_result_object))
74 {
75 return $this->custom_result_object[$class_name];
76 }
John Crepezzi7b474302011-01-15 18:17:01 -050077
Greg Akere70e92b2011-04-25 10:50:53 -050078 if ($this->result_id === FALSE OR $this->num_rows() == 0)
79 {
80 return array();
81 }
Razican114ab092011-04-25 17:26:45 +020082
Greg Akere70e92b2011-04-25 10:50:53 -050083 // add the data to the object
84 $this->_data_seek(0);
85 $result_object = array();
86
John Crepezzi7b474302011-01-15 18:17:01 -050087 while ($row = $this->_fetch_object())
Greg Akere70e92b2011-04-25 10:50:53 -050088 {
89 $object = new $class_name();
Greg Akere70e92b2011-04-25 10:50:53 -050090 foreach ($row as $key => $value)
91 {
92 $object->$key = $value;
93 }
Andrey Andreevbc95e472011-10-20 09:44:48 +030094
John Crepezzi7b474302011-01-15 18:17:01 -050095 $result_object[] = $object;
96 }
97
Greg Akere70e92b2011-04-25 10:50:53 -050098 // return the array
99 return $this->custom_result_object[$class_name] = $result_object;
100 }
101
102 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -0500103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500105 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200108 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300109 public function result_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
111 if (count($this->result_object) > 0)
112 {
113 return $this->result_object;
114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
116 // In the event that query caching is on the result_id variable
117 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // we'll simply return an empty array.
119 if ($this->result_id === FALSE OR $this->num_rows() == 0)
120 {
121 return array();
122 }
123
124 $this->_data_seek(0);
125 while ($row = $this->_fetch_object())
126 {
127 $this->result_object[] = $row;
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 return $this->result_object;
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500136 * Query result. "array" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200139 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300140 public function result_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
142 if (count($this->result_array) > 0)
143 {
144 return $this->result_array;
145 }
146
Barry Mienydd671972010-10-04 16:33:58 +0200147 // In the event that query caching is on the result_id variable
148 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // we'll simply return an empty array.
150 if ($this->result_id === FALSE OR $this->num_rows() == 0)
151 {
152 return array();
153 }
154
155 $this->_data_seek(0);
156 while ($row = $this->_fetch_assoc())
157 {
158 $this->result_array[] = $row;
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 return $this->result_array;
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500167 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @param string
170 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +0200171 * @return mixed either a result object or array
172 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300173 public function row($n = 0, $type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 if ( ! is_numeric($n))
176 {
177 // We cache the row data for subsequent uses
178 if ( ! is_array($this->row_data))
179 {
180 $this->row_data = $this->row_array(0);
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // array_key_exists() instead of isset() to allow for MySQL NULL values
184 if (array_key_exists($n, $this->row_data))
185 {
186 return $this->row_data[$n];
187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188 // reset the $n variable if the result was not achieved
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 $n = 0;
190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Andrey Andreev24276a32012-01-08 02:44:38 +0200192 if ($type === 'object') return $this->row_object($n);
193 elseif ($type === 'array') return $this->row_array($n);
Greg Akere70e92b2011-04-25 10:50:53 -0500194 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Assigns an item into a particular column slot
201 *
Andrey Andreev24276a32012-01-08 02:44:38 +0200202 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200203 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300204 public function set_row($key, $value = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 // We cache the row data for subsequent uses
207 if ( ! is_array($this->row_data))
208 {
209 $this->row_data = $this->row_array(0);
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 if (is_array($key))
213 {
214 foreach ($key as $k => $v)
215 {
216 $this->row_data[$k] = $v;
217 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 return;
219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 if ($key != '' AND ! is_null($value))
222 {
223 $this->row_data[$key] = $value;
224 }
225 }
226
227 // --------------------------------------------------------------------
228
Greg Akere70e92b2011-04-25 10:50:53 -0500229 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500230 * Returns a single result row - custom object version
231 *
John Crepezzi7b474302011-01-15 18:17:01 -0500232 * @return object
233 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300234 public function custom_row_object($n, $type)
John Crepezzi7b474302011-01-15 18:17:01 -0500235 {
236 $result = $this->custom_result_object($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200237 if (count($result) === 0)
John Crepezzi7b474302011-01-15 18:17:01 -0500238 {
239 return $result;
240 }
241
242 if ($n != $this->current_row AND isset($result[$n]))
243 {
244 $this->current_row = $n;
245 }
246
247 return $result[$this->current_row];
248 }
249
Greg Akere70e92b2011-04-25 10:50:53 -0500250 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 * Returns a single result row - object version
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200254 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300255 public function row_object($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
257 $result = $this->result_object();
Andrey Andreev24276a32012-01-08 02:44:38 +0200258 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 return $result;
261 }
262
263 if ($n != $this->current_row AND isset($result[$n]))
264 {
265 $this->current_row = $n;
266 }
267
268 return $result[$this->current_row];
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Returns a single result row - array version
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200277 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300278 public function row_array($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $result = $this->result_array();
Andrey Andreev24276a32012-01-08 02:44:38 +0200281 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 return $result;
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($n != $this->current_row AND isset($result[$n]))
287 {
288 $this->current_row = $n;
289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 return $result[$this->current_row];
292 }
293
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // --------------------------------------------------------------------
296
297 /**
298 * Returns the "first" row
299 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200301 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300302 public function first_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200305 return (count($result) === 0) ? $result : $result[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // --------------------------------------------------------------------
309
310 /**
311 * Returns the "last" row
312 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300315 public function last_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200318 return (count($result) === 0) ? $result : $result[count($result) - 1];
Barry Mienydd671972010-10-04 16:33:58 +0200319 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000320
321 // --------------------------------------------------------------------
322
323 /**
324 * Returns the "next" row
325 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200327 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300328 public function next_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200331 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 return $result;
334 }
335
336 if (isset($result[$this->current_row + 1]))
337 {
338 ++$this->current_row;
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 return $result[$this->current_row];
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // --------------------------------------------------------------------
345
346 /**
347 * Returns the "previous" row
348 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200350 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300351 public function previous_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200354 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 return $result;
357 }
358
359 if (isset($result[$this->current_row - 1]))
360 {
361 --$this->current_row;
362 }
363 return $result[$this->current_row];
364 }
365
366 // --------------------------------------------------------------------
367
368 /**
369 * The following functions are normally overloaded by the identically named
370 * methods in the platform-specific driver -- except when query caching
Derek Jones37f4b9c2011-07-01 17:56:50 -0500371 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * These functions are primarily here to prevent undefined function errors
Derek Jones37f4b9c2011-07-01 17:56:50 -0500373 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * operational due to the unavailability of the database resource IDs with
375 * cached results.
376 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300377 public function num_rows() { return $this->num_rows; }
378 public function num_fields() { return 0; }
379 public function list_fields() { return array(); }
380 public function field_data() { return array(); }
381 public function free_result() { return TRUE; }
382 protected function _data_seek() { return TRUE; }
383 protected function _fetch_assoc() { return array(); }
384 protected function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386}
Derek Allard2067d1a2008-11-13 22:59:24 +0000387
388/* End of file DB_result.php */
John Crepezzi7b474302011-01-15 18:17:01 -0500389/* Location: ./system/database/DB_result.php */