blob: 40b8fbb74ff0959774cdde291dd8c0e087e1d109 [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 *
Master Yodada60e9b2016-12-31 08:46:18 -08009 * Copyright (c) 2014 - 2017, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Master Yodada60e9b2016-12-31 08:46:18 -080032 * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
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 }
Andrey Andreevfbe4d792017-12-27 19:49:03 +0200166
167 return $this->custom_result_object($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 // --------------------------------------------------------------------
171
Greg Akere70e92b2011-04-25 10:50:53 -0500172 /**
173 * Custom query result.
174 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200175 * @param string $class_name
176 * @return array
Greg Akere70e92b2011-04-25 10:50:53 -0500177 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300178 public function custom_result_object($class_name)
Greg Akere70e92b2011-04-25 10:50:53 -0500179 {
Andrey Andreev4763c132012-07-05 13:58:18 +0300180 if (isset($this->custom_result_object[$class_name]))
Greg Akere70e92b2011-04-25 10:50:53 -0500181 {
182 return $this->custom_result_object[$class_name];
183 }
Andrey Andreev4763c132012-07-05 13:58:18 +0300184 elseif ( ! $this->result_id OR $this->num_rows === 0)
Greg Akere70e92b2011-04-25 10:50:53 -0500185 {
186 return array();
187 }
Razican114ab092011-04-25 17:26:45 +0200188
Andrey Andreev4763c132012-07-05 13:58:18 +0300189 // Don't fetch the result set again if we already have it
190 $_data = NULL;
191 if (($c = count($this->result_array)) > 0)
192 {
193 $_data = 'result_array';
194 }
195 elseif (($c = count($this->result_object)) > 0)
196 {
197 $_data = 'result_object';
198 }
199
200 if ($_data !== NULL)
201 {
202 for ($i = 0; $i < $c; $i++)
203 {
204 $this->custom_result_object[$class_name][$i] = new $class_name();
205
Andrey Andreev0db81b62012-07-08 22:00:17 +0300206 foreach ($this->{$_data}[$i] as $key => $value)
Andrey Andreev4763c132012-07-05 13:58:18 +0300207 {
208 $this->custom_result_object[$class_name][$i]->$key = $value;
209 }
210 }
211
212 return $this->custom_result_object[$class_name];
213 }
214
Andrey Andreev08ae84f2014-05-09 10:46:11 +0300215 is_null($this->row_data) OR $this->data_seek(0);
Andrey Andreev4763c132012-07-05 13:58:18 +0300216 $this->custom_result_object[$class_name] = array();
Greg Akere70e92b2011-04-25 10:50:53 -0500217
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300218 while ($row = $this->_fetch_object($class_name))
Greg Akere70e92b2011-04-25 10:50:53 -0500219 {
Jon Ellis-Jonesc4efbdc2012-07-07 10:30:29 +0100220 $this->custom_result_object[$class_name][] = $row;
John Crepezzi7b474302011-01-15 18:17:01 -0500221 }
222
Andrey Andreev4763c132012-07-05 13:58:18 +0300223 return $this->custom_result_object[$class_name];
Greg Akere70e92b2011-04-25 10:50:53 -0500224 }
225
226 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -0500227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 /**
Andrey Andreev38b2a252012-03-29 11:35:32 +0300229 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 *
Andrey Andreev38b2a252012-03-29 11:35:32 +0300231 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200232 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300233 public function result_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
235 if (count($this->result_object) > 0)
236 {
237 return $this->result_object;
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Andrey Andreev4763c132012-07-05 13:58:18 +0300240 // In the event that query caching is on, the result_id variable
241 // will not be a valid resource so we'll simply return an empty
242 // array.
243 if ( ! $this->result_id OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
245 return array();
246 }
247
Andrey Andreev4763c132012-07-05 13:58:18 +0300248 if (($c = count($this->result_array)) > 0)
249 {
250 for ($i = 0; $i < $c; $i++)
251 {
252 $this->result_object[$i] = (object) $this->result_array[$i];
253 }
254
255 return $this->result_object;
256 }
257
Andrey Andreev08ae84f2014-05-09 10:46:11 +0300258 is_null($this->row_data) OR $this->data_seek(0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 while ($row = $this->_fetch_object())
260 {
261 $this->result_object[] = $row;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 return $this->result_object;
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // --------------------------------------------------------------------
268
269 /**
Andrey Andreev5ca05132012-07-05 12:06:34 +0300270 * Query result. "array" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200273 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300274 public function result_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 if (count($this->result_array) > 0)
277 {
278 return $this->result_array;
279 }
280
Andrey Andreev4763c132012-07-05 13:58:18 +0300281 // In the event that query caching is on, the result_id variable
282 // will not be a valid resource so we'll simply return an empty
283 // array.
284 if ( ! $this->result_id OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 return array();
287 }
288
Andrey Andreev4763c132012-07-05 13:58:18 +0300289 if (($c = count($this->result_object)) > 0)
290 {
291 for ($i = 0; $i < $c; $i++)
292 {
293 $this->result_array[$i] = (array) $this->result_object[$i];
294 }
295
296 return $this->result_array;
297 }
298
Andrey Andreev08ae84f2014-05-09 10:46:11 +0300299 is_null($this->row_data) OR $this->data_seek(0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 while ($row = $this->_fetch_assoc())
301 {
302 $this->result_array[] = $row;
303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 return $this->result_array;
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200311 * Row
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200313 * A wrapper method.
314 *
315 * @param mixed $n
316 * @param string $type 'object' or 'array'
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300317 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200318 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300319 public function row($n = 0, $type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 if ( ! is_numeric($n))
322 {
323 // We cache the row data for subsequent uses
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300324 is_array($this->row_data) OR $this->row_data = $this->row_array(0);
325
326 // array_key_exists() instead of isset() to allow for NULL values
327 if (empty($this->row_data) OR ! array_key_exists($n, $this->row_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300329 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300332 return $this->row_data[$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Andrey Andreev24276a32012-01-08 02:44:38 +0200335 if ($type === 'object') return $this->row_object($n);
336 elseif ($type === 'array') return $this->row_array($n);
Andrey Andreevfbe4d792017-12-27 19:49:03 +0200337
338 return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * Assigns an item into a particular column slot
345 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300346 * @param mixed $key
347 * @param mixed $value
Andrey Andreev24276a32012-01-08 02:44:38 +0200348 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200349 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300350 public function set_row($key, $value = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
352 // We cache the row data for subsequent uses
353 if ( ! is_array($this->row_data))
354 {
355 $this->row_data = $this->row_array(0);
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 if (is_array($key))
359 {
360 foreach ($key as $k => $v)
361 {
362 $this->row_data[$k] = $v;
363 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 return;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
vlakoff1228fe22013-01-14 01:30:09 +0100367 if ($key !== '' && $value !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
369 $this->row_data[$key] = $value;
370 }
371 }
372
373 // --------------------------------------------------------------------
374
Greg Akere70e92b2011-04-25 10:50:53 -0500375 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500376 * Returns a single result row - custom object version
377 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300378 * @param int $n
379 * @param string $type
John Crepezzi7b474302011-01-15 18:17:01 -0500380 * @return object
381 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300382 public function custom_row_object($n, $type)
John Crepezzi7b474302011-01-15 18:17:01 -0500383 {
Andrey Andreev34d67992012-07-05 14:37:36 +0300384 isset($this->custom_result_object[$type]) OR $this->custom_result_object($type);
385
386 if (count($this->custom_result_object[$type]) === 0)
John Crepezzi7b474302011-01-15 18:17:01 -0500387 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300388 return NULL;
John Crepezzi7b474302011-01-15 18:17:01 -0500389 }
390
Andrey Andreev34d67992012-07-05 14:37:36 +0300391 if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n]))
John Crepezzi7b474302011-01-15 18:17:01 -0500392 {
393 $this->current_row = $n;
394 }
395
Andrey Andreev34d67992012-07-05 14:37:36 +0300396 return $this->custom_result_object[$type][$this->current_row];
John Crepezzi7b474302011-01-15 18:17:01 -0500397 }
398
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300399 // --------------------------------------------------------------------
400
Greg Akere70e92b2011-04-25 10:50:53 -0500401 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * Returns a single result row - object version
403 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200404 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200406 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300407 public function row_object($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 $result = $this->result_object();
Andrey Andreev24276a32012-01-08 02:44:38 +0200410 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300412 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100415 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 $this->current_row = $n;
418 }
419
420 return $result[$this->current_row];
421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Returns a single result row - array version
427 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200428 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200430 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300431 public function row_array($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 $result = $this->result_array();
Andrey Andreev24276a32012-01-08 02:44:38 +0200434 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300436 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100439 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 $this->current_row = $n;
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 return $result[$this->current_row];
445 }
446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // --------------------------------------------------------------------
448
449 /**
450 * Returns the "first" row
451 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200452 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300453 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200454 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300455 public function first_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300458 return (count($result) === 0) ? NULL : $result[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
462
463 /**
464 * Returns the "last" row
465 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200466 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300467 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200468 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300469 public function last_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300472 return (count($result) === 0) ? NULL : $result[count($result) - 1];
Barry Mienydd671972010-10-04 16:33:58 +0200473 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000474
475 // --------------------------------------------------------------------
476
477 /**
478 * Returns the "next" row
479 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200480 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300481 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200482 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300483 public function next_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
485 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200486 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300488 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490
Andrey Andreev5780d8b2013-03-04 07:38:16 +0200491 return isset($result[$this->current_row + 1])
492 ? $result[++$this->current_row]
493 : NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
497
498 /**
499 * Returns the "previous" row
500 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200501 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300502 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200503 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300504 public function previous_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
506 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200507 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300509 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512 if (isset($result[$this->current_row - 1]))
513 {
514 --$this->current_row;
515 }
516 return $result[$this->current_row];
517 }
518
519 // --------------------------------------------------------------------
520
521 /**
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300522 * Returns an unbuffered row and move pointer to next row
523 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200524 * @param string $type 'array', 'object' or a custom class name
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300525 * @return mixed
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300526 */
527 public function unbuffered_row($type = 'object')
528 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300529 if ($type === 'array')
530 {
531 return $this->_fetch_assoc();
532 }
533 elseif ($type === 'object')
534 {
535 return $this->_fetch_object();
536 }
537
538 return $this->_fetch_object($type);
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300539 }
540
541 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300542
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300543 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200544 * The following methods are normally overloaded by the identically named
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * methods in the platform-specific driver -- except when query caching
Andrey Andreev38b2a252012-03-29 11:35:32 +0300546 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * These functions are primarily here to prevent undefined function errors
Andrey Andreev38b2a252012-03-29 11:35:32 +0300548 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * operational due to the unavailability of the database resource IDs with
550 * cached results.
551 */
Andrey Andreevae85eb42012-11-02 01:42:31 +0200552
553 // --------------------------------------------------------------------
554
555 /**
556 * Number of fields in the result set
557 *
Claudio Galdioloc9982e42015-01-29 11:46:05 -0500558 * Overridden by driver result classes.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200559 *
560 * @return int
561 */
562 public function num_fields()
563 {
564 return 0;
565 }
566
567 // --------------------------------------------------------------------
568
569 /**
570 * Fetch Field Names
571 *
572 * Generates an array of column names.
573 *
Claudio Galdioloc9982e42015-01-29 11:46:05 -0500574 * Overridden by driver result classes.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200575 *
576 * @return array
577 */
578 public function list_fields()
579 {
580 return array();
581 }
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Field data
587 *
588 * Generates an array of objects containing field meta-data.
589 *
Claudio Galdioloc9982e42015-01-29 11:46:05 -0500590 * Overridden by driver result classes.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200591 *
592 * @return array
593 */
594 public function field_data()
595 {
596 return array();
597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Free the result
603 *
Claudio Galdioloc9982e42015-01-29 11:46:05 -0500604 * Overridden by driver result classes.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200605 *
606 * @return void
607 */
608 public function free_result()
609 {
610 $this->result_id = FALSE;
611 }
612
613 // --------------------------------------------------------------------
614
615 /**
616 * Data Seek
617 *
618 * Moves the internal pointer to the desired offset. We call
619 * this internally before fetching results to make sure the
620 * result set starts at zero.
621 *
Claudio Galdioloc9982e42015-01-29 11:46:05 -0500622 * Overridden by driver result classes.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200623 *
624 * @param int $n
625 * @return bool
626 */
Andrey Andreev69edc432012-12-04 13:32:16 +0200627 public function data_seek($n = 0)
Andrey Andreevae85eb42012-11-02 01:42:31 +0200628 {
629 return FALSE;
630 }
631
632 // --------------------------------------------------------------------
633
634 /**
635 * Result - associative array
636 *
637 * Returns the result set as an array.
638 *
Claudio Galdioloc9982e42015-01-29 11:46:05 -0500639 * Overridden by driver result classes.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200640 *
641 * @return array
642 */
643 protected function _fetch_assoc()
644 {
645 return array();
646 }
647
648 // --------------------------------------------------------------------
649
650 /**
651 * Result - object
652 *
653 * Returns the result set as an object.
654 *
Claudio Galdioloc9982e42015-01-29 11:46:05 -0500655 * Overridden by driver result classes.
Andrey Andreevae85eb42012-11-02 01:42:31 +0200656 *
657 * @param string $class_name
658 * @return object
659 */
660 protected function _fetch_object($class_name = 'stdClass')
661 {
Andrey Andreevac8541b2016-08-01 11:37:04 +0300662 return new $class_name();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200663 }
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665}