blob: 53a23a8be6d4c6416a9e7471383502b44e2ff43d [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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 */
Andrey Andreev6dd4aff2012-03-20 15:54:23 +020039class CI_DB_result {
Derek Allard2067d1a2008-11-13 22:59:24 +000040
Andrey Andreev5ca05132012-07-05 12:06:34 +030041 public $conn_id;
42 public $result_id;
Andrey Andreev24276a32012-01-08 02:44:38 +020043 public $result_array = array();
44 public $result_object = array();
45 public $custom_result_object = array();
46 public $current_row = 0;
Andrey Andreev5ca05132012-07-05 12:06:34 +030047 public $num_rows;
48 public $row_data;
Derek Allard2067d1a2008-11-13 22:59:24 +000049
Andrey Andreev5ca05132012-07-05 12:06:34 +030050 /**
51 * Constructor
52 *
53 * @param object
54 * @return void
55 */
Andrey Andreev57bdeb62012-03-05 15:59:16 +020056 public function __construct(&$driver_object)
57 {
58 $this->conn_id = $driver_object->conn_id;
59 $this->result_id = $driver_object->result_id;
60 }
61
Andrey Andreev5ca05132012-07-05 12:06:34 +030062 // --------------------------------------------------------------------
63
Derek Allard2067d1a2008-11-13 22:59:24 +000064 /**
Andrey Andreev5ca05132012-07-05 12:06:34 +030065 * Number of rows in the result set
Derek Allard2067d1a2008-11-13 22:59:24 +000066 *
Andrey Andreev5ca05132012-07-05 12:06:34 +030067 * @return int
68 */
69 public function num_rows()
70 {
71 return is_int($this->num_rows)
72 ? $this->num_rows
73 : $this->num_rows = 0;
74 }
75
76 // --------------------------------------------------------------------
77
78 /**
79 * Query result. Acts as a wrapper function for the following functions.
80 *
81 * @param string 'object', 'array' or a custom class name
82 * @return array
Barry Mienydd671972010-10-04 16:33:58 +020083 */
Andrey Andreevbc95e472011-10-20 09:44:48 +030084 public function result($type = 'object')
Barry Mienydd671972010-10-04 16:33:58 +020085 {
Andrey Andreev5ca05132012-07-05 12:06:34 +030086 if ($type === 'array')
87 {
88 return $this->result_array();
89 }
90 elseif ($type === 'object')
91 {
92 return $this->result_object();
93 }
94 else
95 {
96 return $this->custom_result_object($type);
97 }
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
99
100 // --------------------------------------------------------------------
101
Greg Akere70e92b2011-04-25 10:50:53 -0500102 /**
103 * Custom query result.
104 *
Andrey Andreev24276a32012-01-08 02:44:38 +0200105 * @param string A string that represents the type of object you want back
106 * @return array of objects
Greg Akere70e92b2011-04-25 10:50:53 -0500107 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300108 public function custom_result_object($class_name)
Greg Akere70e92b2011-04-25 10:50:53 -0500109 {
Andrey Andreev4763c132012-07-05 13:58:18 +0300110 if (isset($this->custom_result_object[$class_name]))
Greg Akere70e92b2011-04-25 10:50:53 -0500111 {
112 return $this->custom_result_object[$class_name];
113 }
Andrey Andreev4763c132012-07-05 13:58:18 +0300114 elseif ( ! $this->result_id OR $this->num_rows === 0)
Greg Akere70e92b2011-04-25 10:50:53 -0500115 {
116 return array();
117 }
Razican114ab092011-04-25 17:26:45 +0200118
Andrey Andreev4763c132012-07-05 13:58:18 +0300119 // Don't fetch the result set again if we already have it
120 $_data = NULL;
121 if (($c = count($this->result_array)) > 0)
122 {
123 $_data = 'result_array';
124 }
125 elseif (($c = count($this->result_object)) > 0)
126 {
127 $_data = 'result_object';
128 }
129
130 if ($_data !== NULL)
131 {
132 for ($i = 0; $i < $c; $i++)
133 {
134 $this->custom_result_object[$class_name][$i] = new $class_name();
135
136 foreach ($this->$_data as $key => $value)
137 {
138 $this->custom_result_object[$class_name][$i]->$key = $value;
139 }
140 }
141
142 return $this->custom_result_object[$class_name];
143 }
144
Greg Akere70e92b2011-04-25 10:50:53 -0500145 $this->_data_seek(0);
Andrey Andreev4763c132012-07-05 13:58:18 +0300146 $this->custom_result_object[$class_name] = array();
Greg Akere70e92b2011-04-25 10:50:53 -0500147
John Crepezzi7b474302011-01-15 18:17:01 -0500148 while ($row = $this->_fetch_object())
Greg Akere70e92b2011-04-25 10:50:53 -0500149 {
150 $object = new $class_name();
Greg Akere70e92b2011-04-25 10:50:53 -0500151 foreach ($row as $key => $value)
152 {
153 $object->$key = $value;
154 }
Andrey Andreevbc95e472011-10-20 09:44:48 +0300155
Andrey Andreev4763c132012-07-05 13:58:18 +0300156 $custom_result_object[$class_name][] = $object;
John Crepezzi7b474302011-01-15 18:17:01 -0500157 }
158
Andrey Andreev4763c132012-07-05 13:58:18 +0300159 return $this->custom_result_object[$class_name];
Greg Akere70e92b2011-04-25 10:50:53 -0500160 }
161
162 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -0500163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 /**
Andrey Andreev38b2a252012-03-29 11:35:32 +0300165 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 *
Andrey Andreev38b2a252012-03-29 11:35:32 +0300167 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200168 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300169 public function result_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 if (count($this->result_object) > 0)
172 {
173 return $this->result_object;
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Andrey Andreev4763c132012-07-05 13:58:18 +0300176 // In the event that query caching is on, the result_id variable
177 // will not be a valid resource so we'll simply return an empty
178 // array.
179 if ( ! $this->result_id OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 return array();
182 }
183
Andrey Andreev4763c132012-07-05 13:58:18 +0300184 if (($c = count($this->result_array)) > 0)
185 {
186 for ($i = 0; $i < $c; $i++)
187 {
188 $this->result_object[$i] = (object) $this->result_array[$i];
189 }
190
191 return $this->result_object;
192 }
193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $this->_data_seek(0);
195 while ($row = $this->_fetch_object())
196 {
197 $this->result_object[] = $row;
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 return $this->result_object;
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // --------------------------------------------------------------------
204
205 /**
Andrey Andreev5ca05132012-07-05 12:06:34 +0300206 * Query result. "array" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200209 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300210 public function result_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 if (count($this->result_array) > 0)
213 {
214 return $this->result_array;
215 }
216
Andrey Andreev4763c132012-07-05 13:58:18 +0300217 // In the event that query caching is on, the result_id variable
218 // will not be a valid resource so we'll simply return an empty
219 // array.
220 if ( ! $this->result_id OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 return array();
223 }
224
Andrey Andreev4763c132012-07-05 13:58:18 +0300225 if (($c = count($this->result_object)) > 0)
226 {
227 for ($i = 0; $i < $c; $i++)
228 {
229 $this->result_array[$i] = (array) $this->result_object[$i];
230 }
231
232 return $this->result_array;
233 }
234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $this->_data_seek(0);
236 while ($row = $this->_fetch_assoc())
237 {
238 $this->result_array[] = $row;
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 return $this->result_array;
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500247 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * @param string
250 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +0200251 * @return mixed either a result object or array
252 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300253 public function row($n = 0, $type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 if ( ! is_numeric($n))
256 {
257 // We cache the row data for subsequent uses
258 if ( ! is_array($this->row_data))
259 {
260 $this->row_data = $this->row_array(0);
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // array_key_exists() instead of isset() to allow for MySQL NULL values
264 if (array_key_exists($n, $this->row_data))
265 {
266 return $this->row_data[$n];
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268 // reset the $n variable if the result was not achieved
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 $n = 0;
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Andrey Andreev24276a32012-01-08 02:44:38 +0200272 if ($type === 'object') return $this->row_object($n);
273 elseif ($type === 'array') return $this->row_array($n);
Greg Akere70e92b2011-04-25 10:50:53 -0500274 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Assigns an item into a particular column slot
281 *
Andrey Andreev24276a32012-01-08 02:44:38 +0200282 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200283 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300284 public function set_row($key, $value = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 // We cache the row data for subsequent uses
287 if ( ! is_array($this->row_data))
288 {
289 $this->row_data = $this->row_array(0);
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 if (is_array($key))
293 {
294 foreach ($key as $k => $v)
295 {
296 $this->row_data[$k] = $v;
297 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 return;
299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100301 if ($key !== '' && ! is_null($value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
303 $this->row_data[$key] = $value;
304 }
305 }
306
307 // --------------------------------------------------------------------
308
Greg Akere70e92b2011-04-25 10:50:53 -0500309 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500310 * Returns a single result row - custom object version
311 *
John Crepezzi7b474302011-01-15 18:17:01 -0500312 * @return object
313 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300314 public function custom_row_object($n, $type)
John Crepezzi7b474302011-01-15 18:17:01 -0500315 {
316 $result = $this->custom_result_object($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200317 if (count($result) === 0)
John Crepezzi7b474302011-01-15 18:17:01 -0500318 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300319 return NULL;
John Crepezzi7b474302011-01-15 18:17:01 -0500320 }
321
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100322 if ($n !== $this->current_row && isset($result[$n]))
John Crepezzi7b474302011-01-15 18:17:01 -0500323 {
324 $this->current_row = $n;
325 }
326
327 return $result[$this->current_row];
328 }
329
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300330 // --------------------------------------------------------------------
331
Greg Akere70e92b2011-04-25 10:50:53 -0500332 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * Returns a single result row - object version
334 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200336 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300337 public function row_object($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $result = $this->result_object();
Andrey Andreev24276a32012-01-08 02:44:38 +0200340 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300342 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
344
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100345 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 $this->current_row = $n;
348 }
349
350 return $result[$this->current_row];
351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * Returns a single result row - array version
357 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200359 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300360 public function row_array($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 $result = $this->result_array();
Andrey Andreev24276a32012-01-08 02:44:38 +0200363 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300365 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100368 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 $this->current_row = $n;
371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 return $result[$this->current_row];
374 }
375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // --------------------------------------------------------------------
377
378 /**
379 * Returns the "first" row
380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200382 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300383 public function first_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300386 return (count($result) === 0) ? NULL : $result[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // --------------------------------------------------------------------
390
391 /**
392 * Returns the "last" row
393 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200395 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300396 public function last_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300399 return (count($result) === 0) ? NULL : $result[count($result) - 1];
Barry Mienydd671972010-10-04 16:33:58 +0200400 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000401
402 // --------------------------------------------------------------------
403
404 /**
405 * Returns the "next" row
406 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200408 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300409 public function next_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
411 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200412 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300414 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416
417 if (isset($result[$this->current_row + 1]))
418 {
419 ++$this->current_row;
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 return $result[$this->current_row];
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // --------------------------------------------------------------------
426
427 /**
428 * Returns the "previous" row
429 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200431 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300432 public function previous_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200435 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300437 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440 if (isset($result[$this->current_row - 1]))
441 {
442 --$this->current_row;
443 }
444 return $result[$this->current_row];
445 }
446
447 // --------------------------------------------------------------------
448
449 /**
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300450 * Returns an unbuffered row and move pointer to next row
451 *
Juan Ignacio Bordafece8842012-05-19 09:33:07 -0300452 * @return mixed either a result object or array
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300453 */
454 public function unbuffered_row($type = 'object')
455 {
Juan Ignacio Bordafece8842012-05-19 09:33:07 -0300456 return ($type !== 'array') ? $this->_fetch_object() : $this->_fetch_assoc();
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300457 }
458
459 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300460
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300461 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * The following functions are normally overloaded by the identically named
463 * methods in the platform-specific driver -- except when query caching
Andrey Andreev38b2a252012-03-29 11:35:32 +0300464 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 * These functions are primarily here to prevent undefined function errors
Andrey Andreev38b2a252012-03-29 11:35:32 +0300466 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * operational due to the unavailability of the database resource IDs with
468 * cached results.
469 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300470 public function num_fields() { return 0; }
471 public function list_fields() { return array(); }
472 public function field_data() { return array(); }
Andrey Andreev38b2a252012-03-29 11:35:32 +0300473 public function free_result() { $this->result_id = FALSE; }
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300474 protected function _data_seek() { return FALSE; }
Andrey Andreevbc95e472011-10-20 09:44:48 +0300475 protected function _fetch_assoc() { return array(); }
476 protected function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478}
Derek Allard2067d1a2008-11-13 22:59:24 +0000479
480/* End of file DB_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400481/* Location: ./system/database/DB_result.php */