blob: 991f6ba94538f8c5f246ccd2db365ea8d309a046 [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 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
Andrey Andreev57bdeb62012-03-05 15:59:16 +020050 public function __construct(&$driver_object)
51 {
52 $this->conn_id = $driver_object->conn_id;
53 $this->result_id = $driver_object->result_id;
54 }
55
Derek Allard2067d1a2008-11-13 22:59:24 +000056 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050057 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +000058 *
Derek Allard2067d1a2008-11-13 22:59:24 +000059 * @param string can be "object" or "array"
Andrey Andreev38b2a252012-03-29 11:35:32 +030060 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020061 */
Andrey Andreevbc95e472011-10-20 09:44:48 +030062 public function result($type = 'object')
Barry Mienydd671972010-10-04 16:33:58 +020063 {
Andrey Andreev24276a32012-01-08 02:44:38 +020064 if ($type === 'array') return $this->result_array();
65 elseif ($type === 'object') return $this->result_object();
Greg Akere70e92b2011-04-25 10:50:53 -050066 else return $this->custom_result_object($type);
Derek Allard2067d1a2008-11-13 22:59:24 +000067 }
68
69 // --------------------------------------------------------------------
70
Greg Akere70e92b2011-04-25 10:50:53 -050071 /**
72 * Custom query result.
73 *
Andrey Andreev24276a32012-01-08 02:44:38 +020074 * @param string A string that represents the type of object you want back
75 * @return array of objects
Greg Akere70e92b2011-04-25 10:50:53 -050076 */
Andrey Andreevbc95e472011-10-20 09:44:48 +030077 public function custom_result_object($class_name)
Greg Akere70e92b2011-04-25 10:50:53 -050078 {
79 if (array_key_exists($class_name, $this->custom_result_object))
80 {
81 return $this->custom_result_object[$class_name];
82 }
John Crepezzi7b474302011-01-15 18:17:01 -050083
Alex Bilbie48a2baf2012-06-02 11:09:54 +010084 if ($this->result_id === FALSE OR $this->num_rows() === 0)
Greg Akere70e92b2011-04-25 10:50:53 -050085 {
86 return array();
87 }
Razican114ab092011-04-25 17:26:45 +020088
Greg Akere70e92b2011-04-25 10:50:53 -050089 // add the data to the object
90 $this->_data_seek(0);
91 $result_object = array();
92
John Crepezzi7b474302011-01-15 18:17:01 -050093 while ($row = $this->_fetch_object())
Greg Akere70e92b2011-04-25 10:50:53 -050094 {
95 $object = new $class_name();
Greg Akere70e92b2011-04-25 10:50:53 -050096 foreach ($row as $key => $value)
97 {
98 $object->$key = $value;
99 }
Andrey Andreevbc95e472011-10-20 09:44:48 +0300100
John Crepezzi7b474302011-01-15 18:17:01 -0500101 $result_object[] = $object;
102 }
103
Greg Akere70e92b2011-04-25 10:50:53 -0500104 // return the array
105 return $this->custom_result_object[$class_name] = $result_object;
106 }
107
108 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -0500109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 /**
Andrey Andreev38b2a252012-03-29 11:35:32 +0300111 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 *
Andrey Andreev38b2a252012-03-29 11:35:32 +0300113 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200114 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300115 public function result_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
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.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100125 if ($this->result_id === FALSE OR $this->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200145 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300146 public function result_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 if (count($this->result_array) > 0)
149 {
150 return $this->result_array;
151 }
152
Barry Mienydd671972010-10-04 16:33:58 +0200153 // 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.
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100156 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_assoc())
163 {
164 $this->result_array[] = $row;
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 return $this->result_array;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500173 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @param string
176 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +0200177 * @return mixed either a result object or array
178 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300179 public function row($n = 0, $type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 if ( ! is_numeric($n))
182 {
183 // We cache the row data for subsequent uses
184 if ( ! is_array($this->row_data))
185 {
186 $this->row_data = $this->row_array(0);
187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // array_key_exists() instead of isset() to allow for MySQL NULL values
190 if (array_key_exists($n, $this->row_data))
191 {
192 return $this->row_data[$n];
193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194 // reset the $n variable if the result was not achieved
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 $n = 0;
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Andrey Andreev24276a32012-01-08 02:44:38 +0200198 if ($type === 'object') return $this->row_object($n);
199 elseif ($type === 'array') return $this->row_array($n);
Greg Akere70e92b2011-04-25 10:50:53 -0500200 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Assigns an item into a particular column slot
207 *
Andrey Andreev24276a32012-01-08 02:44:38 +0200208 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200209 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300210 public function set_row($key, $value = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 // We cache the row data for subsequent uses
213 if ( ! is_array($this->row_data))
214 {
215 $this->row_data = $this->row_array(0);
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 if (is_array($key))
219 {
220 foreach ($key as $k => $v)
221 {
222 $this->row_data[$k] = $v;
223 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 return;
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100227 if ($key !== '' && ! is_null($value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 $this->row_data[$key] = $value;
230 }
231 }
232
233 // --------------------------------------------------------------------
234
Greg Akere70e92b2011-04-25 10:50:53 -0500235 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500236 * Returns a single result row - custom object version
237 *
John Crepezzi7b474302011-01-15 18:17:01 -0500238 * @return object
239 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300240 public function custom_row_object($n, $type)
John Crepezzi7b474302011-01-15 18:17:01 -0500241 {
242 $result = $this->custom_result_object($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200243 if (count($result) === 0)
John Crepezzi7b474302011-01-15 18:17:01 -0500244 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300245 return NULL;
John Crepezzi7b474302011-01-15 18:17:01 -0500246 }
247
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100248 if ($n !== $this->current_row && isset($result[$n]))
John Crepezzi7b474302011-01-15 18:17:01 -0500249 {
250 $this->current_row = $n;
251 }
252
253 return $result[$this->current_row];
254 }
255
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300256 // --------------------------------------------------------------------
257
Greg Akere70e92b2011-04-25 10:50:53 -0500258 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * Returns a single result row - object version
260 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200262 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300263 public function row_object($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 $result = $this->result_object();
Andrey Andreev24276a32012-01-08 02:44:38 +0200266 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300268 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
270
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100271 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 $this->current_row = $n;
274 }
275
276 return $result[$this->current_row];
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Returns a single result row - array version
283 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200285 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300286 public function row_array($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
288 $result = $this->result_array();
Andrey Andreev24276a32012-01-08 02:44:38 +0200289 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300291 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100294 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
296 $this->current_row = $n;
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 return $result[$this->current_row];
300 }
301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // --------------------------------------------------------------------
303
304 /**
305 * Returns the "first" row
306 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200308 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300309 public function first_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
311 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300312 return (count($result) === 0) ? NULL : $result[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 // --------------------------------------------------------------------
316
317 /**
318 * Returns the "last" row
319 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200321 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300322 public function last_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300325 return (count($result) === 0) ? NULL : $result[count($result) - 1];
Barry Mienydd671972010-10-04 16:33:58 +0200326 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000327
328 // --------------------------------------------------------------------
329
330 /**
331 * Returns the "next" row
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200334 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300335 public function next_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200338 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300340 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 }
342
343 if (isset($result[$this->current_row + 1]))
344 {
345 ++$this->current_row;
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 return $result[$this->current_row];
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // --------------------------------------------------------------------
352
353 /**
354 * Returns the "previous" row
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200357 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300358 public function previous_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200361 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300363 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 if (isset($result[$this->current_row - 1]))
367 {
368 --$this->current_row;
369 }
370 return $result[$this->current_row];
371 }
372
373 // --------------------------------------------------------------------
374
375 /**
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300376 * Returns an unbuffered row and move pointer to next row
377 *
Juan Ignacio Bordafece8842012-05-19 09:33:07 -0300378 * @return mixed either a result object or array
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300379 */
380 public function unbuffered_row($type = 'object')
381 {
Juan Ignacio Bordafece8842012-05-19 09:33:07 -0300382 return ($type !== 'array') ? $this->_fetch_object() : $this->_fetch_assoc();
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300383 }
384
385 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300386
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300387 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * The following functions are normally overloaded by the identically named
389 * methods in the platform-specific driver -- except when query caching
Andrey Andreev38b2a252012-03-29 11:35:32 +0300390 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 * These functions are primarily here to prevent undefined function errors
Andrey Andreev38b2a252012-03-29 11:35:32 +0300392 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * operational due to the unavailability of the database resource IDs with
394 * cached results.
395 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300396 public function num_rows() { return $this->num_rows; }
397 public function num_fields() { return 0; }
398 public function list_fields() { return array(); }
399 public function field_data() { return array(); }
Andrey Andreev38b2a252012-03-29 11:35:32 +0300400 public function free_result() { $this->result_id = FALSE; }
Andrey Andreev8f3566f2012-04-12 14:30:05 +0300401 protected function _data_seek() { return FALSE; }
Andrey Andreevbc95e472011-10-20 09:44:48 +0300402 protected function _fetch_assoc() { return array(); }
403 protected function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405}
Derek Allard2067d1a2008-11-13 22:59:24 +0000406
407/* End of file DB_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400408/* Location: ./system/database/DB_result.php */