blob: 30e85cb99ff81f7b914e33ea58052b902a67313a [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 {
110 if (array_key_exists($class_name, $this->custom_result_object))
111 {
112 return $this->custom_result_object[$class_name];
113 }
John Crepezzi7b474302011-01-15 18:17:01 -0500114
Andrey Andreev5ca05132012-07-05 12:06:34 +0300115 if ($this->result_id === FALSE OR $this->num_rows === 0)
Greg Akere70e92b2011-04-25 10:50:53 -0500116 {
117 return array();
118 }
Razican114ab092011-04-25 17:26:45 +0200119
Greg Akere70e92b2011-04-25 10:50:53 -0500120 // add the data to the object
121 $this->_data_seek(0);
122 $result_object = array();
123
John Crepezzi7b474302011-01-15 18:17:01 -0500124 while ($row = $this->_fetch_object())
Greg Akere70e92b2011-04-25 10:50:53 -0500125 {
126 $object = new $class_name();
Greg Akere70e92b2011-04-25 10:50:53 -0500127 foreach ($row as $key => $value)
128 {
129 $object->$key = $value;
130 }
Andrey Andreevbc95e472011-10-20 09:44:48 +0300131
John Crepezzi7b474302011-01-15 18:17:01 -0500132 $result_object[] = $object;
133 }
134
Greg Akere70e92b2011-04-25 10:50:53 -0500135 // return the array
136 return $this->custom_result_object[$class_name] = $result_object;
137 }
138
139 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -0500140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 /**
Andrey Andreev38b2a252012-03-29 11:35:32 +0300142 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 *
Andrey Andreev38b2a252012-03-29 11:35:32 +0300144 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200145 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300146 public function result_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 if (count($this->result_object) > 0)
149 {
150 return $this->result_object;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
153 // In the event that query caching is on the result_id variable
154 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 // we'll simply return an empty array.
Andrey Andreev5ca05132012-07-05 12:06:34 +0300156 if ($this->result_id === FALSE OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 return array();
159 }
160
161 $this->_data_seek(0);
162 while ($row = $this->_fetch_object())
163 {
164 $this->result_object[] = $row;
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 return $this->result_object;
168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 // --------------------------------------------------------------------
171
172 /**
Andrey Andreev5ca05132012-07-05 12:06:34 +0300173 * Query result. "array" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200176 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300177 public function result_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 if (count($this->result_array) > 0)
180 {
181 return $this->result_array;
182 }
183
Barry Mienydd671972010-10-04 16:33:58 +0200184 // In the event that query caching is on the result_id variable
185 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // we'll simply return an empty array.
Andrey Andreev5ca05132012-07-05 12:06:34 +0300187 if ($this->result_id === FALSE OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
189 return array();
190 }
191
192 $this->_data_seek(0);
193 while ($row = $this->_fetch_assoc())
194 {
195 $this->result_array[] = $row;
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 return $this->result_array;
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500204 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * @param string
207 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +0200208 * @return mixed either a result object or array
209 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300210 public function row($n = 0, $type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 if ( ! is_numeric($n))
213 {
214 // We cache the row data for subsequent uses
215 if ( ! is_array($this->row_data))
216 {
217 $this->row_data = $this->row_array(0);
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // array_key_exists() instead of isset() to allow for MySQL NULL values
221 if (array_key_exists($n, $this->row_data))
222 {
223 return $this->row_data[$n];
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225 // reset the $n variable if the result was not achieved
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $n = 0;
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Andrey Andreev24276a32012-01-08 02:44:38 +0200229 if ($type === 'object') return $this->row_object($n);
230 elseif ($type === 'array') return $this->row_array($n);
Greg Akere70e92b2011-04-25 10:50:53 -0500231 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Assigns an item into a particular column slot
238 *
Andrey Andreev24276a32012-01-08 02:44:38 +0200239 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200240 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300241 public function set_row($key, $value = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 // We cache the row data for subsequent uses
244 if ( ! is_array($this->row_data))
245 {
246 $this->row_data = $this->row_array(0);
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 if (is_array($key))
250 {
251 foreach ($key as $k => $v)
252 {
253 $this->row_data[$k] = $v;
254 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 return;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100258 if ($key !== '' && ! is_null($value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 $this->row_data[$key] = $value;
261 }
262 }
263
264 // --------------------------------------------------------------------
265
Greg Akere70e92b2011-04-25 10:50:53 -0500266 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500267 * Returns a single result row - custom object version
268 *
John Crepezzi7b474302011-01-15 18:17:01 -0500269 * @return object
270 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300271 public function custom_row_object($n, $type)
John Crepezzi7b474302011-01-15 18:17:01 -0500272 {
273 $result = $this->custom_result_object($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200274 if (count($result) === 0)
John Crepezzi7b474302011-01-15 18:17:01 -0500275 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300276 return NULL;
John Crepezzi7b474302011-01-15 18:17:01 -0500277 }
278
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100279 if ($n !== $this->current_row && isset($result[$n]))
John Crepezzi7b474302011-01-15 18:17:01 -0500280 {
281 $this->current_row = $n;
282 }
283
284 return $result[$this->current_row];
285 }
286
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300287 // --------------------------------------------------------------------
288
Greg Akere70e92b2011-04-25 10:50:53 -0500289 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * Returns a single result row - object version
291 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200293 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300294 public function row_object($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
296 $result = $this->result_object();
Andrey Andreev24276a32012-01-08 02:44:38 +0200297 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300299 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100302 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 $this->current_row = $n;
305 }
306
307 return $result[$this->current_row];
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Returns a single result row - array version
314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200316 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300317 public function row_array($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 $result = $this->result_array();
Andrey Andreev24276a32012-01-08 02:44:38 +0200320 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300322 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100325 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 $this->current_row = $n;
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 return $result[$this->current_row];
331 }
332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 // --------------------------------------------------------------------
334
335 /**
336 * Returns the "first" row
337 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300340 public function first_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300343 return (count($result) === 0) ? NULL : $result[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
349 * Returns the "last" row
350 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200352 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300353 public function last_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300356 return (count($result) === 0) ? NULL : $result[count($result) - 1];
Barry Mienydd671972010-10-04 16:33:58 +0200357 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000358
359 // --------------------------------------------------------------------
360
361 /**
362 * Returns the "next" row
363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200365 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300366 public function next_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200369 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300371 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
373
374 if (isset($result[$this->current_row + 1]))
375 {
376 ++$this->current_row;
377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 return $result[$this->current_row];
380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // --------------------------------------------------------------------
383
384 /**
385 * Returns the "previous" row
386 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200388 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300389 public function previous_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200392 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300394 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
396
397 if (isset($result[$this->current_row - 1]))
398 {
399 --$this->current_row;
400 }
401 return $result[$this->current_row];
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300407 * Returns an unbuffered row and move pointer to next row
408 *
Juan Ignacio Bordafece8842012-05-19 09:33:07 -0300409 * @return mixed either a result object or array
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300410 */
411 public function unbuffered_row($type = 'object')
412 {
Juan Ignacio Bordafece8842012-05-19 09:33:07 -0300413 return ($type !== 'array') ? $this->_fetch_object() : $this->_fetch_assoc();
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300414 }
415
416 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300417
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300418 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * The following functions are normally overloaded by the identically named
420 * methods in the platform-specific driver -- except when query caching
Andrey Andreev38b2a252012-03-29 11:35:32 +0300421 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 * These functions are primarily here to prevent undefined function errors
Andrey Andreev38b2a252012-03-29 11:35:32 +0300423 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * operational due to the unavailability of the database resource IDs with
425 * cached results.
426 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300427 public function num_fields() { return 0; }
428 public function list_fields() { return array(); }
429 public function field_data() { return array(); }
Andrey Andreev38b2a252012-03-29 11:35:32 +0300430 public function free_result() { $this->result_id = FALSE; }
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300431 protected function _data_seek() { return FALSE; }
Andrey Andreevbc95e472011-10-20 09:44:48 +0300432 protected function _fetch_assoc() { return array(); }
433 protected function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435}
Derek Allard2067d1a2008-11-13 22:59:24 +0000436
437/* End of file DB_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400438/* Location: ./system/database/DB_result.php */