blob: 3f26b8aaeac94aff71771ae9a379927a2cafc074 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev24276a32012-01-08 02:44:38 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev24276a32012-01-08 02:44:38 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Database Result Class
42 *
43 * This is the platform-independent result class.
44 * This class will not be called directly. Rather, the adapter
45 * class for the specific database will extend and instantiate it.
46 *
47 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/database/
50 */
Andrey Andreev6dd4aff2012-03-20 15:54:23 +020051class CI_DB_result {
Derek Allard2067d1a2008-11-13 22:59:24 +000052
Andrey Andreevae85eb42012-11-02 01:42:31 +020053 /**
54 * Connection ID
55 *
56 * @var resource|object
57 */
Andrey Andreev5ca05132012-07-05 12:06:34 +030058 public $conn_id;
Andrey Andreevae85eb42012-11-02 01:42:31 +020059
60 /**
61 * Result ID
62 *
63 * @var resource|object
64 */
Andrey Andreev5ca05132012-07-05 12:06:34 +030065 public $result_id;
Andrey Andreevae85eb42012-11-02 01:42:31 +020066
67 /**
68 * Result Array
69 *
70 * @var array[]
71 */
Andrey Andreev24276a32012-01-08 02:44:38 +020072 public $result_array = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020073
74 /**
75 * Result Object
76 *
77 * @var object[]
78 */
Andrey Andreev24276a32012-01-08 02:44:38 +020079 public $result_object = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020080
81 /**
82 * Custom Result Object
83 *
84 * @var object[]
85 */
Andrey Andreev24276a32012-01-08 02:44:38 +020086 public $custom_result_object = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020087
88 /**
89 * Current Row index
90 *
91 * @var int
92 */
Andrey Andreev24276a32012-01-08 02:44:38 +020093 public $current_row = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +020094
95 /**
96 * Number of rows
97 *
98 * @var int
99 */
Andrey Andreev5ca05132012-07-05 12:06:34 +0300100 public $num_rows;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200101
102 /**
103 * Row data
104 *
105 * @var array
106 */
Andrey Andreev5ca05132012-07-05 12:06:34 +0300107 public $row_data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
Andrey Andreevae85eb42012-11-02 01:42:31 +0200109 // --------------------------------------------------------------------
110
Andrey Andreev5ca05132012-07-05 12:06:34 +0300111 /**
112 * Constructor
113 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200114 * @param object $driver_object
Andrey Andreev5ca05132012-07-05 12:06:34 +0300115 * @return void
116 */
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200117 public function __construct(&$driver_object)
118 {
119 $this->conn_id = $driver_object->conn_id;
120 $this->result_id = $driver_object->result_id;
121 }
122
Andrey Andreev5ca05132012-07-05 12:06:34 +0300123 // --------------------------------------------------------------------
124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 /**
Andrey Andreev5ca05132012-07-05 12:06:34 +0300126 * Number of rows in the result set
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 *
Andrey Andreev5ca05132012-07-05 12:06:34 +0300128 * @return int
129 */
130 public function num_rows()
131 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +0300132 if (is_int($this->num_rows))
133 {
134 return $this->num_rows;
135 }
136 elseif (count($this->result_array) > 0)
137 {
138 return $this->num_rows = count($this->result_array);
139 }
140 elseif (count($this->result_object) > 0)
141 {
142 return $this->num_rows = count($this->result_object);
143 }
144
145 return $this->num_rows = count($this->result_array());
Andrey Andreev5ca05132012-07-05 12:06:34 +0300146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Query result. Acts as a wrapper function for the following functions.
152 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200153 * @param string $type 'object', 'array' or a custom class name
Andrey Andreev5ca05132012-07-05 12:06:34 +0300154 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300156 public function result($type = 'object')
Barry Mienydd671972010-10-04 16:33:58 +0200157 {
Andrey Andreev5ca05132012-07-05 12:06:34 +0300158 if ($type === 'array')
159 {
160 return $this->result_array();
161 }
162 elseif ($type === 'object')
163 {
164 return $this->result_object();
165 }
166 else
167 {
168 return $this->custom_result_object($type);
169 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
Greg Akere70e92b2011-04-25 10:50:53 -0500174 /**
175 * Custom query result.
176 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200177 * @param string $class_name
178 * @return array
Greg Akere70e92b2011-04-25 10:50:53 -0500179 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300180 public function custom_result_object($class_name)
Greg Akere70e92b2011-04-25 10:50:53 -0500181 {
Andrey Andreev4763c132012-07-05 13:58:18 +0300182 if (isset($this->custom_result_object[$class_name]))
Greg Akere70e92b2011-04-25 10:50:53 -0500183 {
184 return $this->custom_result_object[$class_name];
185 }
Andrey Andreev4763c132012-07-05 13:58:18 +0300186 elseif ( ! $this->result_id OR $this->num_rows === 0)
Greg Akere70e92b2011-04-25 10:50:53 -0500187 {
188 return array();
189 }
Razican114ab092011-04-25 17:26:45 +0200190
Andrey Andreev4763c132012-07-05 13:58:18 +0300191 // Don't fetch the result set again if we already have it
192 $_data = NULL;
193 if (($c = count($this->result_array)) > 0)
194 {
195 $_data = 'result_array';
196 }
197 elseif (($c = count($this->result_object)) > 0)
198 {
199 $_data = 'result_object';
200 }
201
202 if ($_data !== NULL)
203 {
204 for ($i = 0; $i < $c; $i++)
205 {
206 $this->custom_result_object[$class_name][$i] = new $class_name();
207
Andrey Andreev0db81b62012-07-08 22:00:17 +0300208 foreach ($this->{$_data}[$i] as $key => $value)
Andrey Andreev4763c132012-07-05 13:58:18 +0300209 {
210 $this->custom_result_object[$class_name][$i]->$key = $value;
211 }
212 }
213
214 return $this->custom_result_object[$class_name];
215 }
216
Andrey Andreev08ae84f2014-05-09 10:46:11 +0300217 is_null($this->row_data) OR $this->data_seek(0);
Andrey Andreev4763c132012-07-05 13:58:18 +0300218 $this->custom_result_object[$class_name] = array();
Greg Akere70e92b2011-04-25 10:50:53 -0500219
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300220 while ($row = $this->_fetch_object($class_name))
Greg Akere70e92b2011-04-25 10:50:53 -0500221 {
Jon Ellis-Jonesc4efbdc2012-07-07 10:30:29 +0100222 $this->custom_result_object[$class_name][] = $row;
John Crepezzi7b474302011-01-15 18:17:01 -0500223 }
224
Andrey Andreev4763c132012-07-05 13:58:18 +0300225 return $this->custom_result_object[$class_name];
Greg Akere70e92b2011-04-25 10:50:53 -0500226 }
227
228 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -0500229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 /**
Andrey Andreev38b2a252012-03-29 11:35:32 +0300231 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 *
Andrey Andreev38b2a252012-03-29 11:35:32 +0300233 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200234 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300235 public function result_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 if (count($this->result_object) > 0)
238 {
239 return $this->result_object;
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Andrey Andreev4763c132012-07-05 13:58:18 +0300242 // In the event that query caching is on, the result_id variable
243 // will not be a valid resource so we'll simply return an empty
244 // array.
245 if ( ! $this->result_id OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 return array();
248 }
249
Andrey Andreev4763c132012-07-05 13:58:18 +0300250 if (($c = count($this->result_array)) > 0)
251 {
252 for ($i = 0; $i < $c; $i++)
253 {
254 $this->result_object[$i] = (object) $this->result_array[$i];
255 }
256
257 return $this->result_object;
258 }
259
Andrey Andreev08ae84f2014-05-09 10:46:11 +0300260 is_null($this->row_data) OR $this->data_seek(0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 while ($row = $this->_fetch_object())
262 {
263 $this->result_object[] = $row;
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 return $this->result_object;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
270
271 /**
Andrey Andreev5ca05132012-07-05 12:06:34 +0300272 * Query result. "array" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200275 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300276 public function result_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 if (count($this->result_array) > 0)
279 {
280 return $this->result_array;
281 }
282
Andrey Andreev4763c132012-07-05 13:58:18 +0300283 // In the event that query caching is on, the result_id variable
284 // will not be a valid resource so we'll simply return an empty
285 // array.
286 if ( ! $this->result_id OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
288 return array();
289 }
290
Andrey Andreev4763c132012-07-05 13:58:18 +0300291 if (($c = count($this->result_object)) > 0)
292 {
293 for ($i = 0; $i < $c; $i++)
294 {
295 $this->result_array[$i] = (array) $this->result_object[$i];
296 }
297
298 return $this->result_array;
299 }
300
Andrey Andreev08ae84f2014-05-09 10:46:11 +0300301 is_null($this->row_data) OR $this->data_seek(0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 while ($row = $this->_fetch_assoc())
303 {
304 $this->result_array[] = $row;
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 return $this->result_array;
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200313 * Row
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200315 * A wrapper method.
316 *
317 * @param mixed $n
318 * @param string $type 'object' or 'array'
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300319 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300321 public function row($n = 0, $type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 if ( ! is_numeric($n))
324 {
325 // We cache the row data for subsequent uses
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300326 is_array($this->row_data) OR $this->row_data = $this->row_array(0);
327
328 // array_key_exists() instead of isset() to allow for NULL values
329 if (empty($this->row_data) OR ! array_key_exists($n, $this->row_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300331 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300334 return $this->row_data[$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Andrey Andreev24276a32012-01-08 02:44:38 +0200337 if ($type === 'object') return $this->row_object($n);
338 elseif ($type === 'array') return $this->row_array($n);
Greg Akere70e92b2011-04-25 10:50:53 -0500339 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Assigns an item into a particular column slot
346 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300347 * @param mixed $key
348 * @param mixed $value
Andrey Andreev24276a32012-01-08 02:44:38 +0200349 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200350 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300351 public function set_row($key, $value = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 // We cache the row data for subsequent uses
354 if ( ! is_array($this->row_data))
355 {
356 $this->row_data = $this->row_array(0);
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 if (is_array($key))
360 {
361 foreach ($key as $k => $v)
362 {
363 $this->row_data[$k] = $v;
364 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 return;
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
vlakoff1228fe22013-01-14 01:30:09 +0100368 if ($key !== '' && $value !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 $this->row_data[$key] = $value;
371 }
372 }
373
374 // --------------------------------------------------------------------
375
Greg Akere70e92b2011-04-25 10:50:53 -0500376 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500377 * Returns a single result row - custom object version
378 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300379 * @param int $n
380 * @param string $type
John Crepezzi7b474302011-01-15 18:17:01 -0500381 * @return object
382 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300383 public function custom_row_object($n, $type)
John Crepezzi7b474302011-01-15 18:17:01 -0500384 {
Andrey Andreev34d67992012-07-05 14:37:36 +0300385 isset($this->custom_result_object[$type]) OR $this->custom_result_object($type);
386
387 if (count($this->custom_result_object[$type]) === 0)
John Crepezzi7b474302011-01-15 18:17:01 -0500388 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300389 return NULL;
John Crepezzi7b474302011-01-15 18:17:01 -0500390 }
391
Andrey Andreev34d67992012-07-05 14:37:36 +0300392 if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n]))
John Crepezzi7b474302011-01-15 18:17:01 -0500393 {
394 $this->current_row = $n;
395 }
396
Andrey Andreev34d67992012-07-05 14:37:36 +0300397 return $this->custom_result_object[$type][$this->current_row];
John Crepezzi7b474302011-01-15 18:17:01 -0500398 }
399
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300400 // --------------------------------------------------------------------
401
Greg Akere70e92b2011-04-25 10:50:53 -0500402 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * Returns a single result row - object version
404 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200405 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200407 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300408 public function row_object($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 $result = $this->result_object();
Andrey Andreev24276a32012-01-08 02:44:38 +0200411 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300413 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100416 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 $this->current_row = $n;
419 }
420
421 return $result[$this->current_row];
422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Returns a single result row - array version
428 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200429 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200431 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300432 public function row_array($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 $result = $this->result_array();
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 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100440 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 $this->current_row = $n;
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 return $result[$this->current_row];
446 }
447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
449
450 /**
451 * Returns the "first" row
452 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200453 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300454 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200455 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300456 public function first_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
458 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300459 return (count($result) === 0) ? NULL : $result[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // --------------------------------------------------------------------
463
464 /**
465 * Returns the "last" row
466 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200467 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300468 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200469 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300470 public function last_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300473 return (count($result) === 0) ? NULL : $result[count($result) - 1];
Barry Mienydd671972010-10-04 16:33:58 +0200474 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000475
476 // --------------------------------------------------------------------
477
478 /**
479 * Returns the "next" row
480 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200481 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300482 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200483 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300484 public function next_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
486 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200487 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300489 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491
Andrey Andreev5780d8b2013-03-04 07:38:16 +0200492 return isset($result[$this->current_row + 1])
493 ? $result[++$this->current_row]
494 : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // --------------------------------------------------------------------
498
499 /**
500 * Returns the "previous" row
501 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200502 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300503 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200504 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300505 public function previous_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 {
507 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200508 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300510 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
512
513 if (isset($result[$this->current_row - 1]))
514 {
515 --$this->current_row;
516 }
517 return $result[$this->current_row];
518 }
519
520 // --------------------------------------------------------------------
521
522 /**
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300523 * Returns an unbuffered row and move pointer to next row
524 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200525 * @param string $type 'array', 'object' or a custom class name
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300526 * @return mixed
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300527 */
528 public function unbuffered_row($type = 'object')
529 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300530 if ($type === 'array')
531 {
532 return $this->_fetch_assoc();
533 }
534 elseif ($type === 'object')
535 {
536 return $this->_fetch_object();
537 }
538
539 return $this->_fetch_object($type);
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300540 }
541
542 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300543
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300544 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200545 * The following methods are normally overloaded by the identically named
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 * methods in the platform-specific driver -- except when query caching
Andrey Andreev38b2a252012-03-29 11:35:32 +0300547 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * These functions are primarily here to prevent undefined function errors
Andrey Andreev38b2a252012-03-29 11:35:32 +0300549 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * operational due to the unavailability of the database resource IDs with
551 * cached results.
552 */
Andrey Andreevae85eb42012-11-02 01:42:31 +0200553
554 // --------------------------------------------------------------------
555
556 /**
557 * Number of fields in the result set
558 *
559 * Overriden by driver result classes.
560 *
561 * @return int
562 */
563 public function num_fields()
564 {
565 return 0;
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Fetch Field Names
572 *
573 * Generates an array of column names.
574 *
575 * Overriden by driver result classes.
576 *
577 * @return array
578 */
579 public function list_fields()
580 {
581 return array();
582 }
583
584 // --------------------------------------------------------------------
585
586 /**
587 * Field data
588 *
589 * Generates an array of objects containing field meta-data.
590 *
591 * Overriden by driver result classes.
592 *
593 * @return array
594 */
595 public function field_data()
596 {
597 return array();
598 }
599
600 // --------------------------------------------------------------------
601
602 /**
603 * Free the result
604 *
605 * Overriden by driver result classes.
606 *
607 * @return void
608 */
609 public function free_result()
610 {
611 $this->result_id = FALSE;
612 }
613
614 // --------------------------------------------------------------------
615
616 /**
617 * Data Seek
618 *
619 * Moves the internal pointer to the desired offset. We call
620 * this internally before fetching results to make sure the
621 * result set starts at zero.
622 *
623 * Overriden by driver result classes.
624 *
625 * @param int $n
626 * @return bool
627 */
Andrey Andreev69edc432012-12-04 13:32:16 +0200628 public function data_seek($n = 0)
Andrey Andreevae85eb42012-11-02 01:42:31 +0200629 {
630 return FALSE;
631 }
632
633 // --------------------------------------------------------------------
634
635 /**
636 * Result - associative array
637 *
638 * Returns the result set as an array.
639 *
640 * Overriden by driver result classes.
641 *
642 * @return array
643 */
644 protected function _fetch_assoc()
645 {
646 return array();
647 }
648
649 // --------------------------------------------------------------------
650
651 /**
652 * Result - object
653 *
654 * Returns the result set as an object.
655 *
656 * Overriden by driver result classes.
657 *
658 * @param string $class_name
659 * @return object
660 */
661 protected function _fetch_object($class_name = 'stdClass')
662 {
663 return array();
664 }
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666}
Derek Allard2067d1a2008-11-13 22:59:24 +0000667
668/* End of file DB_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400669/* Location: ./system/database/DB_result.php */