Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/database/ |
| 39 | */ |
Andrey Andreev | 6dd4aff | 2012-03-20 15:54:23 +0200 | [diff] [blame] | 40 | class CI_DB_result { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 42 | /** |
| 43 | * Connection ID |
| 44 | * |
| 45 | * @var resource|object |
| 46 | */ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 47 | public $conn_id; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Result ID |
| 51 | * |
| 52 | * @var resource|object |
| 53 | */ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 54 | public $result_id; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Result Array |
| 58 | * |
| 59 | * @var array[] |
| 60 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 61 | public $result_array = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Result Object |
| 65 | * |
| 66 | * @var object[] |
| 67 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 68 | public $result_object = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Custom Result Object |
| 72 | * |
| 73 | * @var object[] |
| 74 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 75 | public $custom_result_object = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Current Row index |
| 79 | * |
| 80 | * @var int |
| 81 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 82 | public $current_row = 0; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Number of rows |
| 86 | * |
| 87 | * @var int |
| 88 | */ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 89 | public $num_rows; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Row data |
| 93 | * |
| 94 | * @var array |
| 95 | */ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 96 | public $row_data; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 98 | // -------------------------------------------------------------------- |
| 99 | |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 100 | /** |
| 101 | * Constructor |
| 102 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 103 | * @param object $driver_object |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 104 | * @return void |
| 105 | */ |
Andrey Andreev | 57bdeb6 | 2012-03-05 15:59:16 +0200 | [diff] [blame] | 106 | 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 Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 112 | // -------------------------------------------------------------------- |
| 113 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | /** |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 115 | * Number of rows in the result set |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | * |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 117 | * @return int |
| 118 | */ |
| 119 | public function num_rows() |
| 120 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 121 | 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 Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // -------------------------------------------------------------------- |
| 138 | |
| 139 | /** |
| 140 | * Query result. Acts as a wrapper function for the following functions. |
| 141 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 142 | * @param string $type 'object', 'array' or a custom class name |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 143 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 145 | public function result($type = 'object') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | { |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 147 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // -------------------------------------------------------------------- |
| 162 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 163 | /** |
| 164 | * Custom query result. |
| 165 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 166 | * @param string $class_name |
| 167 | * @return array |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 168 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 169 | public function custom_result_object($class_name) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 170 | { |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 171 | if (isset($this->custom_result_object[$class_name])) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 172 | { |
| 173 | return $this->custom_result_object[$class_name]; |
| 174 | } |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 175 | elseif ( ! $this->result_id OR $this->num_rows === 0) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 176 | { |
| 177 | return array(); |
| 178 | } |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 179 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 180 | // 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 Andreev | 0db81b6 | 2012-07-08 22:00:17 +0300 | [diff] [blame] | 197 | foreach ($this->{$_data}[$i] as $key => $value) |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 198 | { |
| 199 | $this->custom_result_object[$class_name][$i]->$key = $value; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return $this->custom_result_object[$class_name]; |
| 204 | } |
| 205 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 206 | $this->data_seek(0); |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 207 | $this->custom_result_object[$class_name] = array(); |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 208 | |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 209 | while ($row = $this->_fetch_object($class_name)) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 210 | { |
Jon Ellis-Jones | c4efbdc | 2012-07-07 10:30:29 +0100 | [diff] [blame] | 211 | $this->custom_result_object[$class_name][] = $row; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 214 | return $this->custom_result_object[$class_name]; |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // -------------------------------------------------------------------- |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 218 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | /** |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 220 | * Query result. "object" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | * |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 222 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 224 | public function result_object() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
| 226 | if (count($this->result_object) > 0) |
| 227 | { |
| 228 | return $this->result_object; |
| 229 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 231 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | { |
| 236 | return array(); |
| 237 | } |
| 238 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 239 | 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 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 249 | $this->data_seek(0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | while ($row = $this->_fetch_object()) |
| 251 | { |
| 252 | $this->result_object[] = $row; |
| 253 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 254 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | return $this->result_object; |
| 256 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 261 | * Query result. "array" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 265 | public function result_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | { |
| 267 | if (count($this->result_array) > 0) |
| 268 | { |
| 269 | return $this->result_array; |
| 270 | } |
| 271 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 272 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
| 277 | return array(); |
| 278 | } |
| 279 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 280 | 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 | |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 290 | $this->data_seek(0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | while ($row = $this->_fetch_assoc()) |
| 292 | { |
| 293 | $this->result_array[] = $row; |
| 294 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | return $this->result_array; |
| 297 | } |
| 298 | |
| 299 | // -------------------------------------------------------------------- |
| 300 | |
| 301 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 302 | * Row |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 304 | * A wrapper method. |
| 305 | * |
| 306 | * @param mixed $n |
| 307 | * @param string $type 'object' or 'array' |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 308 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 310 | public function row($n = 0, $type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | { |
| 312 | if ( ! is_numeric($n)) |
| 313 | { |
| 314 | // We cache the row data for subsequent uses |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 315 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | { |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 320 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 323 | return $this->row_data[$n]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 325 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 326 | if ($type === 'object') return $this->row_object($n); |
| 327 | elseif ($type === 'array') return $this->row_array($n); |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 328 | else return $this->custom_row_object($n, $type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // -------------------------------------------------------------------- |
| 332 | |
| 333 | /** |
| 334 | * Assigns an item into a particular column slot |
| 335 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 336 | * @param mixed $key |
| 337 | * @param mixed $value |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 338 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 340 | public function set_row($key, $value = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 347 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | if (is_array($key)) |
| 349 | { |
| 350 | foreach ($key as $k => $v) |
| 351 | { |
| 352 | $this->row_data[$k] = $v; |
| 353 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | return; |
| 355 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame^] | 357 | if ($key !== '' && $value !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | { |
| 359 | $this->row_data[$key] = $value; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 365 | /** |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 366 | * Returns a single result row - custom object version |
| 367 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 368 | * @param int $n |
| 369 | * @param string $type |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 370 | * @return object |
| 371 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 372 | public function custom_row_object($n, $type) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 373 | { |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 374 | isset($this->custom_result_object[$type]) OR $this->custom_result_object($type); |
| 375 | |
| 376 | if (count($this->custom_result_object[$type]) === 0) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 377 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 378 | return NULL; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 379 | } |
| 380 | |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 381 | if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n])) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 382 | { |
| 383 | $this->current_row = $n; |
| 384 | } |
| 385 | |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 386 | return $this->custom_result_object[$type][$this->current_row]; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 389 | // -------------------------------------------------------------------- |
| 390 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 391 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | * Returns a single result row - object version |
| 393 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 394 | * @param int $n |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 396 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 397 | public function row_object($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | { |
| 399 | $result = $this->result_object(); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 400 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 402 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 405 | if ($n !== $this->current_row && isset($result[$n])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | { |
| 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 Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 418 | * @param int $n |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 420 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 421 | public function row_array($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | { |
| 423 | $result = $this->result_array(); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 424 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 426 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 429 | if ($n !== $this->current_row && isset($result[$n])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | { |
| 431 | $this->current_row = $n; |
| 432 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | return $result[$this->current_row]; |
| 435 | } |
| 436 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | // -------------------------------------------------------------------- |
| 438 | |
| 439 | /** |
| 440 | * Returns the "first" row |
| 441 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 442 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 443 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 444 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 445 | public function first_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | { |
| 447 | $result = $this->result($type); |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 448 | return (count($result) === 0) ? NULL : $result[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 450 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | // -------------------------------------------------------------------- |
| 452 | |
| 453 | /** |
| 454 | * Returns the "last" row |
| 455 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 456 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 457 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 458 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 459 | public function last_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | { |
| 461 | $result = $this->result($type); |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 462 | return (count($result) === 0) ? NULL : $result[count($result) - 1]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 463 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | |
| 465 | // -------------------------------------------------------------------- |
| 466 | |
| 467 | /** |
| 468 | * Returns the "next" row |
| 469 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 470 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 471 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 472 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 473 | public function next_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | { |
| 475 | $result = $this->result($type); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 476 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 478 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | if (isset($result[$this->current_row + 1])) |
| 482 | { |
| 483 | ++$this->current_row; |
| 484 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | return $result[$this->current_row]; |
| 487 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 488 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | // -------------------------------------------------------------------- |
| 490 | |
| 491 | /** |
| 492 | * Returns the "previous" row |
| 493 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 494 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 495 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 496 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 497 | public function previous_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | { |
| 499 | $result = $this->result($type); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 500 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 502 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | } |
| 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 Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 515 | * Returns an unbuffered row and move pointer to next row |
| 516 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 517 | * @param string $type 'array', 'object' or a custom class name |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 518 | * @return mixed |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 519 | */ |
| 520 | public function unbuffered_row($type = 'object') |
| 521 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 522 | 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 Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // -------------------------------------------------------------------- |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 535 | |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 536 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 537 | * The following methods are normally overloaded by the identically named |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | * methods in the platform-specific driver -- except when query caching |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 539 | * 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] | 540 | * These functions are primarily here to prevent undefined function errors |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 541 | * 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] | 542 | * operational due to the unavailability of the database resource IDs with |
| 543 | * cached results. |
| 544 | */ |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 545 | |
| 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 | */ |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 620 | public function data_seek($n = 0) |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 657 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 658 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | |
| 660 | /* End of file DB_result.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 661 | /* Location: ./system/database/DB_result.php */ |