Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/database/ |
| 38 | */ |
Andrey Andreev | 6dd4aff | 2012-03-20 15:54:23 +0200 | [diff] [blame] | 39 | class CI_DB_result { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 41 | public $conn_id; |
| 42 | public $result_id; |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 43 | public $result_array = array(); |
| 44 | public $result_object = array(); |
| 45 | public $custom_result_object = array(); |
| 46 | public $current_row = 0; |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 47 | public $num_rows; |
| 48 | public $row_data; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 50 | /** |
| 51 | * Constructor |
| 52 | * |
| 53 | * @param object |
| 54 | * @return void |
| 55 | */ |
Andrey Andreev | 57bdeb6 | 2012-03-05 15:59:16 +0200 | [diff] [blame] | 56 | public function __construct(&$driver_object) |
| 57 | { |
| 58 | $this->conn_id = $driver_object->conn_id; |
| 59 | $this->result_id = $driver_object->result_id; |
| 60 | } |
| 61 | |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 62 | // -------------------------------------------------------------------- |
| 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | /** |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 65 | * Number of rows in the result set |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | * |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 67 | * @return int |
| 68 | */ |
| 69 | public function num_rows() |
| 70 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 71 | if (is_int($this->num_rows)) |
| 72 | { |
| 73 | return $this->num_rows; |
| 74 | } |
| 75 | elseif (count($this->result_array) > 0) |
| 76 | { |
| 77 | return $this->num_rows = count($this->result_array); |
| 78 | } |
| 79 | elseif (count($this->result_object) > 0) |
| 80 | { |
| 81 | return $this->num_rows = count($this->result_object); |
| 82 | } |
| 83 | |
| 84 | return $this->num_rows = count($this->result_array()); |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // -------------------------------------------------------------------- |
| 88 | |
| 89 | /** |
| 90 | * Query result. Acts as a wrapper function for the following functions. |
| 91 | * |
| 92 | * @param string 'object', 'array' or a custom class name |
| 93 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 95 | public function result($type = 'object') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | { |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 97 | if ($type === 'array') |
| 98 | { |
| 99 | return $this->result_array(); |
| 100 | } |
| 101 | elseif ($type === 'object') |
| 102 | { |
| 103 | return $this->result_object(); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | return $this->custom_result_object($type); |
| 108 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // -------------------------------------------------------------------- |
| 112 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 113 | /** |
| 114 | * Custom query result. |
| 115 | * |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 116 | * @param string A string that represents the type of object you want back |
| 117 | * @return array of objects |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 118 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 119 | public function custom_result_object($class_name) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 120 | { |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 121 | if (isset($this->custom_result_object[$class_name])) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 122 | { |
| 123 | return $this->custom_result_object[$class_name]; |
| 124 | } |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 125 | elseif ( ! $this->result_id OR $this->num_rows === 0) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 126 | { |
| 127 | return array(); |
| 128 | } |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 129 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 130 | // Don't fetch the result set again if we already have it |
| 131 | $_data = NULL; |
| 132 | if (($c = count($this->result_array)) > 0) |
| 133 | { |
| 134 | $_data = 'result_array'; |
| 135 | } |
| 136 | elseif (($c = count($this->result_object)) > 0) |
| 137 | { |
| 138 | $_data = 'result_object'; |
| 139 | } |
| 140 | |
| 141 | if ($_data !== NULL) |
| 142 | { |
| 143 | for ($i = 0; $i < $c; $i++) |
| 144 | { |
| 145 | $this->custom_result_object[$class_name][$i] = new $class_name(); |
| 146 | |
| 147 | foreach ($this->$_data as $key => $value) |
| 148 | { |
| 149 | $this->custom_result_object[$class_name][$i]->$key = $value; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return $this->custom_result_object[$class_name]; |
| 154 | } |
| 155 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 156 | $this->_data_seek(0); |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 157 | $this->custom_result_object[$class_name] = array(); |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 158 | |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 159 | while ($row = $this->_fetch_object($class_name)) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 160 | { |
Jon Ellis-Jones | c4efbdc | 2012-07-07 10:30:29 +0100 | [diff] [blame^] | 161 | $this->custom_result_object[$class_name][] = $row; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 164 | return $this->custom_result_object[$class_name]; |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // -------------------------------------------------------------------- |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 168 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | /** |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 170 | * Query result. "object" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | * |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 172 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 174 | public function result_object() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | { |
| 176 | if (count($this->result_object) > 0) |
| 177 | { |
| 178 | return $this->result_object; |
| 179 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 181 | // In the event that query caching is on, the result_id variable |
| 182 | // will not be a valid resource so we'll simply return an empty |
| 183 | // array. |
| 184 | if ( ! $this->result_id OR $this->num_rows === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | { |
| 186 | return array(); |
| 187 | } |
| 188 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 189 | if (($c = count($this->result_array)) > 0) |
| 190 | { |
| 191 | for ($i = 0; $i < $c; $i++) |
| 192 | { |
| 193 | $this->result_object[$i] = (object) $this->result_array[$i]; |
| 194 | } |
| 195 | |
| 196 | return $this->result_object; |
| 197 | } |
| 198 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | $this->_data_seek(0); |
| 200 | while ($row = $this->_fetch_object()) |
| 201 | { |
| 202 | $this->result_object[] = $row; |
| 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | return $this->result_object; |
| 206 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | // -------------------------------------------------------------------- |
| 209 | |
| 210 | /** |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 211 | * Query result. "array" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 214 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 215 | public function result_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | { |
| 217 | if (count($this->result_array) > 0) |
| 218 | { |
| 219 | return $this->result_array; |
| 220 | } |
| 221 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 222 | // In the event that query caching is on, the result_id variable |
| 223 | // will not be a valid resource so we'll simply return an empty |
| 224 | // array. |
| 225 | if ( ! $this->result_id OR $this->num_rows === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | { |
| 227 | return array(); |
| 228 | } |
| 229 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 230 | if (($c = count($this->result_object)) > 0) |
| 231 | { |
| 232 | for ($i = 0; $i < $c; $i++) |
| 233 | { |
| 234 | $this->result_array[$i] = (array) $this->result_object[$i]; |
| 235 | } |
| 236 | |
| 237 | return $this->result_array; |
| 238 | } |
| 239 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | $this->_data_seek(0); |
| 241 | while ($row = $this->_fetch_assoc()) |
| 242 | { |
| 243 | $this->result_array[] = $row; |
| 244 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | return $this->result_array; |
| 247 | } |
| 248 | |
| 249 | // -------------------------------------------------------------------- |
| 250 | |
| 251 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 252 | * Query result. Acts as a wrapper function for the following functions. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | * @param string |
| 255 | * @param string can be "object" or "array" |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 256 | * @return mixed either a result object or array |
| 257 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 258 | public function row($n = 0, $type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | { |
| 260 | if ( ! is_numeric($n)) |
| 261 | { |
| 262 | // We cache the row data for subsequent uses |
| 263 | if ( ! is_array($this->row_data)) |
| 264 | { |
| 265 | $this->row_data = $this->row_array(0); |
| 266 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | // array_key_exists() instead of isset() to allow for MySQL NULL values |
| 269 | if (array_key_exists($n, $this->row_data)) |
| 270 | { |
| 271 | return $this->row_data[$n]; |
| 272 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 273 | // reset the $n variable if the result was not achieved |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | $n = 0; |
| 275 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 276 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 277 | if ($type === 'object') return $this->row_object($n); |
| 278 | elseif ($type === 'array') return $this->row_array($n); |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 279 | else return $this->custom_row_object($n, $type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // -------------------------------------------------------------------- |
| 283 | |
| 284 | /** |
| 285 | * Assigns an item into a particular column slot |
| 286 | * |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 287 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 289 | public function set_row($key, $value = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | { |
| 291 | // We cache the row data for subsequent uses |
| 292 | if ( ! is_array($this->row_data)) |
| 293 | { |
| 294 | $this->row_data = $this->row_array(0); |
| 295 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | if (is_array($key)) |
| 298 | { |
| 299 | foreach ($key as $k => $v) |
| 300 | { |
| 301 | $this->row_data[$k] = $v; |
| 302 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | return; |
| 304 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 305 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 306 | if ($key !== '' && ! is_null($value)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | { |
| 308 | $this->row_data[$key] = $value; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // -------------------------------------------------------------------- |
| 313 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 314 | /** |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 315 | * Returns a single result row - custom object version |
| 316 | * |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 317 | * @return object |
| 318 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 319 | public function custom_row_object($n, $type) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 320 | { |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 321 | isset($this->custom_result_object[$type]) OR $this->custom_result_object($type); |
| 322 | |
| 323 | if (count($this->custom_result_object[$type]) === 0) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 324 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 325 | return NULL; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 328 | if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n])) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 329 | { |
| 330 | $this->current_row = $n; |
| 331 | } |
| 332 | |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 333 | return $this->custom_result_object[$type][$this->current_row]; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 336 | // -------------------------------------------------------------------- |
| 337 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 338 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | * Returns a single result row - object version |
| 340 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 342 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 343 | public function row_object($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | { |
| 345 | $result = $this->result_object(); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 346 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 348 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 351 | if ($n !== $this->current_row && isset($result[$n])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | { |
| 353 | $this->current_row = $n; |
| 354 | } |
| 355 | |
| 356 | return $result[$this->current_row]; |
| 357 | } |
| 358 | |
| 359 | // -------------------------------------------------------------------- |
| 360 | |
| 361 | /** |
| 362 | * Returns a single result row - array version |
| 363 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 365 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 366 | public function row_array($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | { |
| 368 | $result = $this->result_array(); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 369 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 371 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 374 | if ($n !== $this->current_row && isset($result[$n])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | { |
| 376 | $this->current_row = $n; |
| 377 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | return $result[$this->current_row]; |
| 380 | } |
| 381 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | // -------------------------------------------------------------------- |
| 383 | |
| 384 | /** |
| 385 | * Returns the "first" row |
| 386 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 389 | public function first_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | { |
| 391 | $result = $this->result($type); |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 392 | return (count($result) === 0) ? NULL : $result[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 394 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | // -------------------------------------------------------------------- |
| 396 | |
| 397 | /** |
| 398 | * Returns the "last" row |
| 399 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 401 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 402 | public function last_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | { |
| 404 | $result = $this->result($type); |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 405 | return (count($result) === 0) ? NULL : $result[count($result) - 1]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | |
| 408 | // -------------------------------------------------------------------- |
| 409 | |
| 410 | /** |
| 411 | * Returns the "next" row |
| 412 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 415 | public function next_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | { |
| 417 | $result = $this->result($type); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 418 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 420 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | if (isset($result[$this->current_row + 1])) |
| 424 | { |
| 425 | ++$this->current_row; |
| 426 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 427 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | return $result[$this->current_row]; |
| 429 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | // -------------------------------------------------------------------- |
| 432 | |
| 433 | /** |
| 434 | * Returns the "previous" row |
| 435 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 438 | public function previous_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | { |
| 440 | $result = $this->result($type); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 441 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 443 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | if (isset($result[$this->current_row - 1])) |
| 447 | { |
| 448 | --$this->current_row; |
| 449 | } |
| 450 | return $result[$this->current_row]; |
| 451 | } |
| 452 | |
| 453 | // -------------------------------------------------------------------- |
| 454 | |
| 455 | /** |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 456 | * Returns an unbuffered row and move pointer to next row |
| 457 | * |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 458 | * @param string 'array', 'object' or a custom class name |
Juan Ignacio Borda | fece884 | 2012-05-19 09:33:07 -0300 | [diff] [blame] | 459 | * @return mixed either a result object or array |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 460 | */ |
| 461 | public function unbuffered_row($type = 'object') |
| 462 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 463 | if ($type === 'array') |
| 464 | { |
| 465 | return $this->_fetch_assoc(); |
| 466 | } |
| 467 | elseif ($type === 'object') |
| 468 | { |
| 469 | return $this->_fetch_object(); |
| 470 | } |
| 471 | |
| 472 | return $this->_fetch_object($type); |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // -------------------------------------------------------------------- |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 476 | |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 477 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | * The following functions are normally overloaded by the identically named |
| 479 | * methods in the platform-specific driver -- except when query caching |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 480 | * is used. When caching is enabled we do not load the other driver. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | * These functions are primarily here to prevent undefined function errors |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 482 | * when a cached result object is in use. They are not otherwise fully |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | * operational due to the unavailability of the database resource IDs with |
| 484 | * cached results. |
| 485 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 486 | public function num_fields() { return 0; } |
| 487 | public function list_fields() { return array(); } |
| 488 | public function field_data() { return array(); } |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 489 | public function free_result() { $this->result_id = FALSE; } |
Andrey Andreev | 8f3566f | 2012-04-12 14:30:05 +0300 | [diff] [blame] | 490 | protected function _data_seek() { return FALSE; } |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 491 | protected function _fetch_assoc() { return array(); } |
| 492 | protected function _fetch_object() { return array(); } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 493 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | |
| 496 | /* End of file DB_result.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 497 | /* Location: ./system/database/DB_result.php */ |