blob: 04f964fb1c6cbc465465c8042dc6fe614c85d023 [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"
Barry Mienydd671972010-10-04 16:33:58 +020060 * @return mixed either a result object or array
61 */
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
Greg Akere70e92b2011-04-25 10:50:53 -050084 if ($this->result_id === FALSE OR $this->num_rows() == 0)
85 {
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 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500111 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @return object
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.
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 *
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.
156 if ($this->result_id === FALSE OR $this->num_rows() == 0)
157 {
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 if ($key != '' AND ! is_null($value))
228 {
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 {
245 return $result;
246 }
247
248 if ($n != $this->current_row AND isset($result[$n]))
249 {
250 $this->current_row = $n;
251 }
252
253 return $result[$this->current_row];
254 }
255
Greg Akere70e92b2011-04-25 10:50:53 -0500256 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * Returns a single result row - object version
258 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200260 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300261 public function row_object($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 $result = $this->result_object();
Andrey Andreev24276a32012-01-08 02:44:38 +0200264 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
266 return $result;
267 }
268
269 if ($n != $this->current_row AND isset($result[$n]))
270 {
271 $this->current_row = $n;
272 }
273
274 return $result[$this->current_row];
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Returns a single result row - array version
281 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200283 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300284 public function row_array($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 $result = $this->result_array();
Andrey Andreev24276a32012-01-08 02:44:38 +0200287 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 return $result;
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 if ($n != $this->current_row AND isset($result[$n]))
293 {
294 $this->current_row = $n;
295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 return $result[$this->current_row];
298 }
299
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // --------------------------------------------------------------------
302
303 /**
304 * Returns the "first" row
305 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200307 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300308 public function first_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200311 return (count($result) === 0) ? $result : $result[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
315
316 /**
317 * Returns the "last" row
318 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300321 public function last_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200324 return (count($result) === 0) ? $result : $result[count($result) - 1];
Barry Mienydd671972010-10-04 16:33:58 +0200325 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000326
327 // --------------------------------------------------------------------
328
329 /**
330 * Returns the "next" row
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200333 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300334 public function next_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200337 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 return $result;
340 }
341
342 if (isset($result[$this->current_row + 1]))
343 {
344 ++$this->current_row;
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 return $result[$this->current_row];
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
351
352 /**
353 * Returns the "previous" row
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300357 public function previous_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200360 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 return $result;
363 }
364
365 if (isset($result[$this->current_row - 1]))
366 {
367 --$this->current_row;
368 }
369 return $result[$this->current_row];
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * The following functions are normally overloaded by the identically named
376 * methods in the platform-specific driver -- except when query caching
Derek Jones37f4b9c2011-07-01 17:56:50 -0500377 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * These functions are primarily here to prevent undefined function errors
Derek Jones37f4b9c2011-07-01 17:56:50 -0500379 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * operational due to the unavailability of the database resource IDs with
381 * cached results.
382 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300383 public function num_rows() { return $this->num_rows; }
384 public function num_fields() { return 0; }
385 public function list_fields() { return array(); }
386 public function field_data() { return array(); }
387 public function free_result() { return TRUE; }
388 protected function _data_seek() { return TRUE; }
389 protected function _fetch_assoc() { return array(); }
390 protected function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392}
Derek Allard2067d1a2008-11-13 22:59:24 +0000393
394/* End of file DB_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400395/* Location: ./system/database/DB_result.php */