blob: 9d19075bad57087aac784ee1d46529258b9d0660 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Database Result Class
31 *
32 * This is the platform-independent result class.
33 * This class will not be called directly. Rather, the adapter
34 * class for the specific database will extend and instantiate it.
35 *
36 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/database/
39 */
Andrey Andreev6dd4aff2012-03-20 15:54:23 +020040class CI_DB_result {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
Andrey Andreevae85eb42012-11-02 01:42:31 +020042 /**
43 * Connection ID
44 *
45 * @var resource|object
46 */
Andrey Andreev5ca05132012-07-05 12:06:34 +030047 public $conn_id;
Andrey Andreevae85eb42012-11-02 01:42:31 +020048
49 /**
50 * Result ID
51 *
52 * @var resource|object
53 */
Andrey Andreev5ca05132012-07-05 12:06:34 +030054 public $result_id;
Andrey Andreevae85eb42012-11-02 01:42:31 +020055
56 /**
57 * Result Array
58 *
59 * @var array[]
60 */
Andrey Andreev24276a32012-01-08 02:44:38 +020061 public $result_array = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020062
63 /**
64 * Result Object
65 *
66 * @var object[]
67 */
Andrey Andreev24276a32012-01-08 02:44:38 +020068 public $result_object = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020069
70 /**
71 * Custom Result Object
72 *
73 * @var object[]
74 */
Andrey Andreev24276a32012-01-08 02:44:38 +020075 public $custom_result_object = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020076
77 /**
78 * Current Row index
79 *
80 * @var int
81 */
Andrey Andreev24276a32012-01-08 02:44:38 +020082 public $current_row = 0;
Andrey Andreevae85eb42012-11-02 01:42:31 +020083
84 /**
85 * Number of rows
86 *
87 * @var int
88 */
Andrey Andreev5ca05132012-07-05 12:06:34 +030089 public $num_rows;
Andrey Andreevae85eb42012-11-02 01:42:31 +020090
91 /**
92 * Row data
93 *
94 * @var array
95 */
Andrey Andreev5ca05132012-07-05 12:06:34 +030096 public $row_data;
Derek Allard2067d1a2008-11-13 22:59:24 +000097
Andrey Andreevae85eb42012-11-02 01:42:31 +020098 // --------------------------------------------------------------------
99
Andrey Andreev5ca05132012-07-05 12:06:34 +0300100 /**
101 * Constructor
102 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200103 * @param object $driver_object
Andrey Andreev5ca05132012-07-05 12:06:34 +0300104 * @return void
105 */
Andrey Andreev57bdeb62012-03-05 15:59:16 +0200106 public function __construct(&$driver_object)
107 {
108 $this->conn_id = $driver_object->conn_id;
109 $this->result_id = $driver_object->result_id;
110 }
111
Andrey Andreev5ca05132012-07-05 12:06:34 +0300112 // --------------------------------------------------------------------
113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 /**
Andrey Andreev5ca05132012-07-05 12:06:34 +0300115 * Number of rows in the result set
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 *
Andrey Andreev5ca05132012-07-05 12:06:34 +0300117 * @return int
118 */
119 public function num_rows()
120 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +0300121 if (is_int($this->num_rows))
122 {
123 return $this->num_rows;
124 }
125 elseif (count($this->result_array) > 0)
126 {
127 return $this->num_rows = count($this->result_array);
128 }
129 elseif (count($this->result_object) > 0)
130 {
131 return $this->num_rows = count($this->result_object);
132 }
133
134 return $this->num_rows = count($this->result_array());
Andrey Andreev5ca05132012-07-05 12:06:34 +0300135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Query result. Acts as a wrapper function for the following functions.
141 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200142 * @param string $type 'object', 'array' or a custom class name
Andrey Andreev5ca05132012-07-05 12:06:34 +0300143 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200144 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300145 public function result($type = 'object')
Barry Mienydd671972010-10-04 16:33:58 +0200146 {
Andrey Andreev5ca05132012-07-05 12:06:34 +0300147 if ($type === 'array')
148 {
149 return $this->result_array();
150 }
151 elseif ($type === 'object')
152 {
153 return $this->result_object();
154 }
155 else
156 {
157 return $this->custom_result_object($type);
158 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160
161 // --------------------------------------------------------------------
162
Greg Akere70e92b2011-04-25 10:50:53 -0500163 /**
164 * Custom query result.
165 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200166 * @param string $class_name
167 * @return array
Greg Akere70e92b2011-04-25 10:50:53 -0500168 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300169 public function custom_result_object($class_name)
Greg Akere70e92b2011-04-25 10:50:53 -0500170 {
Andrey Andreev4763c132012-07-05 13:58:18 +0300171 if (isset($this->custom_result_object[$class_name]))
Greg Akere70e92b2011-04-25 10:50:53 -0500172 {
173 return $this->custom_result_object[$class_name];
174 }
Andrey Andreev4763c132012-07-05 13:58:18 +0300175 elseif ( ! $this->result_id OR $this->num_rows === 0)
Greg Akere70e92b2011-04-25 10:50:53 -0500176 {
177 return array();
178 }
Razican114ab092011-04-25 17:26:45 +0200179
Andrey Andreev4763c132012-07-05 13:58:18 +0300180 // Don't fetch the result set again if we already have it
181 $_data = NULL;
182 if (($c = count($this->result_array)) > 0)
183 {
184 $_data = 'result_array';
185 }
186 elseif (($c = count($this->result_object)) > 0)
187 {
188 $_data = 'result_object';
189 }
190
191 if ($_data !== NULL)
192 {
193 for ($i = 0; $i < $c; $i++)
194 {
195 $this->custom_result_object[$class_name][$i] = new $class_name();
196
Andrey Andreev0db81b62012-07-08 22:00:17 +0300197 foreach ($this->{$_data}[$i] as $key => $value)
Andrey Andreev4763c132012-07-05 13:58:18 +0300198 {
199 $this->custom_result_object[$class_name][$i]->$key = $value;
200 }
201 }
202
203 return $this->custom_result_object[$class_name];
204 }
205
Greg Akere70e92b2011-04-25 10:50:53 -0500206 $this->_data_seek(0);
Andrey Andreev4763c132012-07-05 13:58:18 +0300207 $this->custom_result_object[$class_name] = array();
Greg Akere70e92b2011-04-25 10:50:53 -0500208
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300209 while ($row = $this->_fetch_object($class_name))
Greg Akere70e92b2011-04-25 10:50:53 -0500210 {
Jon Ellis-Jonesc4efbdc2012-07-07 10:30:29 +0100211 $this->custom_result_object[$class_name][] = $row;
John Crepezzi7b474302011-01-15 18:17:01 -0500212 }
213
Andrey Andreev4763c132012-07-05 13:58:18 +0300214 return $this->custom_result_object[$class_name];
Greg Akere70e92b2011-04-25 10:50:53 -0500215 }
216
217 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -0500218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 /**
Andrey Andreev38b2a252012-03-29 11:35:32 +0300220 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 *
Andrey Andreev38b2a252012-03-29 11:35:32 +0300222 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200223 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300224 public function result_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 if (count($this->result_object) > 0)
227 {
228 return $this->result_object;
229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Andrey Andreev4763c132012-07-05 13:58:18 +0300231 // In the event that query caching is on, the result_id variable
232 // will not be a valid resource so we'll simply return an empty
233 // array.
234 if ( ! $this->result_id OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 return array();
237 }
238
Andrey Andreev4763c132012-07-05 13:58:18 +0300239 if (($c = count($this->result_array)) > 0)
240 {
241 for ($i = 0; $i < $c; $i++)
242 {
243 $this->result_object[$i] = (object) $this->result_array[$i];
244 }
245
246 return $this->result_object;
247 }
248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 $this->_data_seek(0);
250 while ($row = $this->_fetch_object())
251 {
252 $this->result_object[] = $row;
253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 return $this->result_object;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
259
260 /**
Andrey Andreev5ca05132012-07-05 12:06:34 +0300261 * Query result. "array" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200264 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300265 public function result_array()
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
267 if (count($this->result_array) > 0)
268 {
269 return $this->result_array;
270 }
271
Andrey Andreev4763c132012-07-05 13:58:18 +0300272 // In the event that query caching is on, the result_id variable
273 // will not be a valid resource so we'll simply return an empty
274 // array.
275 if ( ! $this->result_id OR $this->num_rows === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 return array();
278 }
279
Andrey Andreev4763c132012-07-05 13:58:18 +0300280 if (($c = count($this->result_object)) > 0)
281 {
282 for ($i = 0; $i < $c; $i++)
283 {
284 $this->result_array[$i] = (array) $this->result_object[$i];
285 }
286
287 return $this->result_array;
288 }
289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 $this->_data_seek(0);
291 while ($row = $this->_fetch_assoc())
292 {
293 $this->result_array[] = $row;
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 return $this->result_array;
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200302 * Row
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200304 * A wrapper method.
305 *
306 * @param mixed $n
307 * @param string $type 'object' or 'array'
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300308 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200309 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300310 public function row($n = 0, $type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
312 if ( ! is_numeric($n))
313 {
314 // We cache the row data for subsequent uses
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300315 is_array($this->row_data) OR $this->row_data = $this->row_array(0);
316
317 // array_key_exists() instead of isset() to allow for NULL values
318 if (empty($this->row_data) OR ! array_key_exists($n, $this->row_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300320 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Andrey Andreev9f6bdc02012-10-22 23:31:10 +0300323 return $this->row_data[$n];
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Andrey Andreev24276a32012-01-08 02:44:38 +0200326 if ($type === 'object') return $this->row_object($n);
327 elseif ($type === 'array') return $this->row_array($n);
Greg Akere70e92b2011-04-25 10:50:53 -0500328 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * Assigns an item into a particular column slot
335 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300336 * @param mixed $key
337 * @param mixed $value
Andrey Andreev24276a32012-01-08 02:44:38 +0200338 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300340 public function set_row($key, $value = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 // We cache the row data for subsequent uses
343 if ( ! is_array($this->row_data))
344 {
345 $this->row_data = $this->row_array(0);
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 if (is_array($key))
349 {
350 foreach ($key as $k => $v)
351 {
352 $this->row_data[$k] = $v;
353 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 return;
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100357 if ($key !== '' && ! is_null($value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 $this->row_data[$key] = $value;
360 }
361 }
362
363 // --------------------------------------------------------------------
364
Greg Akere70e92b2011-04-25 10:50:53 -0500365 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500366 * Returns a single result row - custom object version
367 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300368 * @param int $n
369 * @param string $type
John Crepezzi7b474302011-01-15 18:17:01 -0500370 * @return object
371 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300372 public function custom_row_object($n, $type)
John Crepezzi7b474302011-01-15 18:17:01 -0500373 {
Andrey Andreev34d67992012-07-05 14:37:36 +0300374 isset($this->custom_result_object[$type]) OR $this->custom_result_object($type);
375
376 if (count($this->custom_result_object[$type]) === 0)
John Crepezzi7b474302011-01-15 18:17:01 -0500377 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300378 return NULL;
John Crepezzi7b474302011-01-15 18:17:01 -0500379 }
380
Andrey Andreev34d67992012-07-05 14:37:36 +0300381 if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n]))
John Crepezzi7b474302011-01-15 18:17:01 -0500382 {
383 $this->current_row = $n;
384 }
385
Andrey Andreev34d67992012-07-05 14:37:36 +0300386 return $this->custom_result_object[$type][$this->current_row];
John Crepezzi7b474302011-01-15 18:17:01 -0500387 }
388
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300389 // --------------------------------------------------------------------
390
Greg Akere70e92b2011-04-25 10:50:53 -0500391 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * Returns a single result row - object version
393 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200394 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200396 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300397 public function row_object($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 $result = $this->result_object();
Andrey Andreev24276a32012-01-08 02:44:38 +0200400 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300402 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100405 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 $this->current_row = $n;
408 }
409
410 return $result[$this->current_row];
411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Returns a single result row - array version
417 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200418 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200420 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300421 public function row_array($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $result = $this->result_array();
Andrey Andreev24276a32012-01-08 02:44:38 +0200424 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300426 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100429 if ($n !== $this->current_row && isset($result[$n]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 $this->current_row = $n;
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 return $result[$this->current_row];
435 }
436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // --------------------------------------------------------------------
438
439 /**
440 * Returns the "first" row
441 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200442 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300443 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200444 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300445 public function first_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300448 return (count($result) === 0) ? NULL : $result[0];
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // --------------------------------------------------------------------
452
453 /**
454 * Returns the "last" row
455 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200456 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300457 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200458 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300459 public function last_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
461 $result = $this->result($type);
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300462 return (count($result) === 0) ? NULL : $result[count($result) - 1];
Barry Mienydd671972010-10-04 16:33:58 +0200463 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
465 // --------------------------------------------------------------------
466
467 /**
468 * Returns the "next" row
469 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200470 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300471 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200472 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300473 public function next_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200476 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300478 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480
481 if (isset($result[$this->current_row + 1]))
482 {
483 ++$this->current_row;
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 return $result[$this->current_row];
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
490
491 /**
492 * Returns the "previous" row
493 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200494 * @param string $type
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300495 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200496 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300497 public function previous_row($type = 'object')
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
499 $result = $this->result($type);
Andrey Andreev24276a32012-01-08 02:44:38 +0200500 if (count($result) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
Andrey Andreev55d3ad42012-05-24 22:13:06 +0300502 return NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
504
505 if (isset($result[$this->current_row - 1]))
506 {
507 --$this->current_row;
508 }
509 return $result[$this->current_row];
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300515 * Returns an unbuffered row and move pointer to next row
516 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200517 * @param string $type 'array', 'object' or a custom class name
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300518 * @return mixed
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300519 */
520 public function unbuffered_row($type = 'object')
521 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300522 if ($type === 'array')
523 {
524 return $this->_fetch_assoc();
525 }
526 elseif ($type === 'object')
527 {
528 return $this->_fetch_object();
529 }
530
531 return $this->_fetch_object($type);
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300532 }
533
534 // --------------------------------------------------------------------
Andrey Andreev79922c02012-05-23 12:27:17 +0300535
Juan Ignacio Borda3020b242012-05-18 18:27:50 -0300536 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200537 * The following methods are normally overloaded by the identically named
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 * methods in the platform-specific driver -- except when query caching
Andrey Andreev38b2a252012-03-29 11:35:32 +0300539 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * These functions are primarily here to prevent undefined function errors
Andrey Andreev38b2a252012-03-29 11:35:32 +0300541 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * operational due to the unavailability of the database resource IDs with
543 * cached results.
544 */
Andrey Andreevae85eb42012-11-02 01:42:31 +0200545
546 // --------------------------------------------------------------------
547
548 /**
549 * Number of fields in the result set
550 *
551 * Overriden by driver result classes.
552 *
553 * @return int
554 */
555 public function num_fields()
556 {
557 return 0;
558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * Fetch Field Names
564 *
565 * Generates an array of column names.
566 *
567 * Overriden by driver result classes.
568 *
569 * @return array
570 */
571 public function list_fields()
572 {
573 return array();
574 }
575
576 // --------------------------------------------------------------------
577
578 /**
579 * Field data
580 *
581 * Generates an array of objects containing field meta-data.
582 *
583 * Overriden by driver result classes.
584 *
585 * @return array
586 */
587 public function field_data()
588 {
589 return array();
590 }
591
592 // --------------------------------------------------------------------
593
594 /**
595 * Free the result
596 *
597 * Overriden by driver result classes.
598 *
599 * @return void
600 */
601 public function free_result()
602 {
603 $this->result_id = FALSE;
604 }
605
606 // --------------------------------------------------------------------
607
608 /**
609 * Data Seek
610 *
611 * Moves the internal pointer to the desired offset. We call
612 * this internally before fetching results to make sure the
613 * result set starts at zero.
614 *
615 * Overriden by driver result classes.
616 *
617 * @param int $n
618 * @return bool
619 */
620 protected function _data_seek($n = 0)
621 {
622 return FALSE;
623 }
624
625 // --------------------------------------------------------------------
626
627 /**
628 * Result - associative array
629 *
630 * Returns the result set as an array.
631 *
632 * Overriden by driver result classes.
633 *
634 * @return array
635 */
636 protected function _fetch_assoc()
637 {
638 return array();
639 }
640
641 // --------------------------------------------------------------------
642
643 /**
644 * Result - object
645 *
646 * Returns the result set as an object.
647 *
648 * Overriden by driver result classes.
649 *
650 * @param string $class_name
651 * @return object
652 */
653 protected function _fetch_object($class_name = 'stdClass')
654 {
655 return array();
656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658}
Derek Allard2067d1a2008-11-13 22:59:24 +0000659
660/* End of file DB_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400661/* Location: ./system/database/DB_result.php */