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