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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 8 | * |
Master Yoda | da60e9b | 2016-12-31 08:46:18 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2017, British Columbia Institute of Technology |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Master Yoda | da60e9b | 2016-12-31 08:46:18 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Database Result Class |
| 42 | * |
| 43 | * This is the platform-independent result class. |
| 44 | * This class will not be called directly. Rather, the adapter |
| 45 | * class for the specific database will extend and instantiate it. |
| 46 | * |
| 47 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/database/ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
Andrey Andreev | 6dd4aff | 2012-03-20 15:54:23 +0200 | [diff] [blame] | 51 | class CI_DB_result { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 53 | /** |
| 54 | * Connection ID |
| 55 | * |
| 56 | * @var resource|object |
| 57 | */ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 58 | public $conn_id; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Result ID |
| 62 | * |
| 63 | * @var resource|object |
| 64 | */ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 65 | public $result_id; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Result Array |
| 69 | * |
| 70 | * @var array[] |
| 71 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 72 | public $result_array = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Result Object |
| 76 | * |
| 77 | * @var object[] |
| 78 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 79 | public $result_object = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Custom Result Object |
| 83 | * |
| 84 | * @var object[] |
| 85 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 86 | public $custom_result_object = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Current Row index |
| 90 | * |
| 91 | * @var int |
| 92 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 93 | public $current_row = 0; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Number of rows |
| 97 | * |
| 98 | * @var int |
| 99 | */ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 100 | public $num_rows; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Row data |
| 104 | * |
| 105 | * @var array |
| 106 | */ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 107 | public $row_data; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 109 | // -------------------------------------------------------------------- |
| 110 | |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 111 | /** |
| 112 | * Constructor |
| 113 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 114 | * @param object $driver_object |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 115 | * @return void |
| 116 | */ |
Andrey Andreev | 57bdeb6 | 2012-03-05 15:59:16 +0200 | [diff] [blame] | 117 | public function __construct(&$driver_object) |
| 118 | { |
| 119 | $this->conn_id = $driver_object->conn_id; |
| 120 | $this->result_id = $driver_object->result_id; |
| 121 | } |
| 122 | |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 123 | // -------------------------------------------------------------------- |
| 124 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | /** |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 126 | * Number of rows in the result set |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | * |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 128 | * @return int |
| 129 | */ |
| 130 | public function num_rows() |
| 131 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 132 | if (is_int($this->num_rows)) |
| 133 | { |
| 134 | return $this->num_rows; |
| 135 | } |
| 136 | elseif (count($this->result_array) > 0) |
| 137 | { |
| 138 | return $this->num_rows = count($this->result_array); |
| 139 | } |
| 140 | elseif (count($this->result_object) > 0) |
| 141 | { |
| 142 | return $this->num_rows = count($this->result_object); |
| 143 | } |
| 144 | |
| 145 | return $this->num_rows = count($this->result_array()); |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | /** |
| 151 | * Query result. Acts as a wrapper function for the following functions. |
| 152 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 153 | * @param string $type 'object', 'array' or a custom class name |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 154 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 156 | public function result($type = 'object') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | { |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 158 | if ($type === 'array') |
| 159 | { |
| 160 | return $this->result_array(); |
| 161 | } |
| 162 | elseif ($type === 'object') |
| 163 | { |
| 164 | return $this->result_object(); |
| 165 | } |
Andrey Andreev | fbe4d79 | 2017-12-27 19:49:03 +0200 | [diff] [blame^] | 166 | |
| 167 | return $this->custom_result_object($type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 172 | /** |
| 173 | * Custom query result. |
| 174 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 175 | * @param string $class_name |
| 176 | * @return array |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 177 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 178 | public function custom_result_object($class_name) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 179 | { |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 180 | if (isset($this->custom_result_object[$class_name])) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 181 | { |
| 182 | return $this->custom_result_object[$class_name]; |
| 183 | } |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 184 | elseif ( ! $this->result_id OR $this->num_rows === 0) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 185 | { |
| 186 | return array(); |
| 187 | } |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 188 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 189 | // Don't fetch the result set again if we already have it |
| 190 | $_data = NULL; |
| 191 | if (($c = count($this->result_array)) > 0) |
| 192 | { |
| 193 | $_data = 'result_array'; |
| 194 | } |
| 195 | elseif (($c = count($this->result_object)) > 0) |
| 196 | { |
| 197 | $_data = 'result_object'; |
| 198 | } |
| 199 | |
| 200 | if ($_data !== NULL) |
| 201 | { |
| 202 | for ($i = 0; $i < $c; $i++) |
| 203 | { |
| 204 | $this->custom_result_object[$class_name][$i] = new $class_name(); |
| 205 | |
Andrey Andreev | 0db81b6 | 2012-07-08 22:00:17 +0300 | [diff] [blame] | 206 | foreach ($this->{$_data}[$i] as $key => $value) |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 207 | { |
| 208 | $this->custom_result_object[$class_name][$i]->$key = $value; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return $this->custom_result_object[$class_name]; |
| 213 | } |
| 214 | |
Andrey Andreev | 08ae84f | 2014-05-09 10:46:11 +0300 | [diff] [blame] | 215 | is_null($this->row_data) OR $this->data_seek(0); |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 216 | $this->custom_result_object[$class_name] = array(); |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 217 | |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 218 | while ($row = $this->_fetch_object($class_name)) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 219 | { |
Jon Ellis-Jones | c4efbdc | 2012-07-07 10:30:29 +0100 | [diff] [blame] | 220 | $this->custom_result_object[$class_name][] = $row; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 223 | return $this->custom_result_object[$class_name]; |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // -------------------------------------------------------------------- |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 227 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | /** |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 229 | * Query result. "object" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | * |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 231 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 233 | public function result_object() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | { |
| 235 | if (count($this->result_object) > 0) |
| 236 | { |
| 237 | return $this->result_object; |
| 238 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 240 | // In the event that query caching is on, the result_id variable |
| 241 | // will not be a valid resource so we'll simply return an empty |
| 242 | // array. |
| 243 | if ( ! $this->result_id OR $this->num_rows === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | { |
| 245 | return array(); |
| 246 | } |
| 247 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 248 | if (($c = count($this->result_array)) > 0) |
| 249 | { |
| 250 | for ($i = 0; $i < $c; $i++) |
| 251 | { |
| 252 | $this->result_object[$i] = (object) $this->result_array[$i]; |
| 253 | } |
| 254 | |
| 255 | return $this->result_object; |
| 256 | } |
| 257 | |
Andrey Andreev | 08ae84f | 2014-05-09 10:46:11 +0300 | [diff] [blame] | 258 | is_null($this->row_data) OR $this->data_seek(0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | while ($row = $this->_fetch_object()) |
| 260 | { |
| 261 | $this->result_object[] = $row; |
| 262 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 263 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | return $this->result_object; |
| 265 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | // -------------------------------------------------------------------- |
| 268 | |
| 269 | /** |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 270 | * Query result. "array" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 273 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 274 | public function result_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | { |
| 276 | if (count($this->result_array) > 0) |
| 277 | { |
| 278 | return $this->result_array; |
| 279 | } |
| 280 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 281 | // In the event that query caching is on, the result_id variable |
| 282 | // will not be a valid resource so we'll simply return an empty |
| 283 | // array. |
| 284 | if ( ! $this->result_id OR $this->num_rows === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
| 286 | return array(); |
| 287 | } |
| 288 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 289 | if (($c = count($this->result_object)) > 0) |
| 290 | { |
| 291 | for ($i = 0; $i < $c; $i++) |
| 292 | { |
| 293 | $this->result_array[$i] = (array) $this->result_object[$i]; |
| 294 | } |
| 295 | |
| 296 | return $this->result_array; |
| 297 | } |
| 298 | |
Andrey Andreev | 08ae84f | 2014-05-09 10:46:11 +0300 | [diff] [blame] | 299 | is_null($this->row_data) OR $this->data_seek(0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | while ($row = $this->_fetch_assoc()) |
| 301 | { |
| 302 | $this->result_array[] = $row; |
| 303 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 304 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | return $this->result_array; |
| 306 | } |
| 307 | |
| 308 | // -------------------------------------------------------------------- |
| 309 | |
| 310 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 311 | * Row |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 313 | * A wrapper method. |
| 314 | * |
| 315 | * @param mixed $n |
| 316 | * @param string $type 'object' or 'array' |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 317 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 319 | public function row($n = 0, $type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | { |
| 321 | if ( ! is_numeric($n)) |
| 322 | { |
| 323 | // We cache the row data for subsequent uses |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 324 | is_array($this->row_data) OR $this->row_data = $this->row_array(0); |
| 325 | |
| 326 | // array_key_exists() instead of isset() to allow for NULL values |
| 327 | if (empty($this->row_data) OR ! array_key_exists($n, $this->row_data)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | { |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 329 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 332 | return $this->row_data[$n]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 334 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 335 | if ($type === 'object') return $this->row_object($n); |
| 336 | elseif ($type === 'array') return $this->row_array($n); |
Andrey Andreev | fbe4d79 | 2017-12-27 19:49:03 +0200 | [diff] [blame^] | 337 | |
| 338 | return $this->custom_row_object($n, $type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // -------------------------------------------------------------------- |
| 342 | |
| 343 | /** |
| 344 | * Assigns an item into a particular column slot |
| 345 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 346 | * @param mixed $key |
| 347 | * @param mixed $value |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 348 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 349 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 350 | public function set_row($key, $value = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | { |
| 352 | // We cache the row data for subsequent uses |
| 353 | if ( ! is_array($this->row_data)) |
| 354 | { |
| 355 | $this->row_data = $this->row_array(0); |
| 356 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 357 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | if (is_array($key)) |
| 359 | { |
| 360 | foreach ($key as $k => $v) |
| 361 | { |
| 362 | $this->row_data[$k] = $v; |
| 363 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | return; |
| 365 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 366 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 367 | if ($key !== '' && $value !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | { |
| 369 | $this->row_data[$key] = $value; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // -------------------------------------------------------------------- |
| 374 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 375 | /** |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 376 | * Returns a single result row - custom object version |
| 377 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 378 | * @param int $n |
| 379 | * @param string $type |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 380 | * @return object |
| 381 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 382 | public function custom_row_object($n, $type) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 383 | { |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 384 | isset($this->custom_result_object[$type]) OR $this->custom_result_object($type); |
| 385 | |
| 386 | if (count($this->custom_result_object[$type]) === 0) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 387 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 388 | return NULL; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 389 | } |
| 390 | |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 391 | if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n])) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 392 | { |
| 393 | $this->current_row = $n; |
| 394 | } |
| 395 | |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 396 | return $this->custom_result_object[$type][$this->current_row]; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 397 | } |
| 398 | |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 399 | // -------------------------------------------------------------------- |
| 400 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 401 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | * Returns a single result row - object version |
| 403 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 404 | * @param int $n |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 407 | public function row_object($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | { |
| 409 | $result = $this->result_object(); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 410 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 412 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 415 | if ($n !== $this->current_row && isset($result[$n])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | { |
| 417 | $this->current_row = $n; |
| 418 | } |
| 419 | |
| 420 | return $result[$this->current_row]; |
| 421 | } |
| 422 | |
| 423 | // -------------------------------------------------------------------- |
| 424 | |
| 425 | /** |
| 426 | * Returns a single result row - array version |
| 427 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 428 | * @param int $n |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 431 | public function row_array($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | { |
| 433 | $result = $this->result_array(); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 434 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 436 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 439 | if ($n !== $this->current_row && isset($result[$n])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | { |
| 441 | $this->current_row = $n; |
| 442 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 443 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | return $result[$this->current_row]; |
| 445 | } |
| 446 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | // -------------------------------------------------------------------- |
| 448 | |
| 449 | /** |
| 450 | * Returns the "first" row |
| 451 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 452 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 453 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 455 | public function first_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | { |
| 457 | $result = $this->result($type); |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 458 | return (count($result) === 0) ? NULL : $result[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 460 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | // -------------------------------------------------------------------- |
| 462 | |
| 463 | /** |
| 464 | * Returns the "last" row |
| 465 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 466 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 467 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 468 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 469 | public function last_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | { |
| 471 | $result = $this->result($type); |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 472 | return (count($result) === 0) ? NULL : $result[count($result) - 1]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | |
| 475 | // -------------------------------------------------------------------- |
| 476 | |
| 477 | /** |
| 478 | * Returns the "next" row |
| 479 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 480 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 481 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 483 | public function next_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | { |
| 485 | $result = $this->result($type); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 486 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 488 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Andrey Andreev | 5780d8b | 2013-03-04 07:38:16 +0200 | [diff] [blame] | 491 | return isset($result[$this->current_row + 1]) |
| 492 | ? $result[++$this->current_row] |
| 493 | : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | // -------------------------------------------------------------------- |
| 497 | |
| 498 | /** |
| 499 | * Returns the "previous" row |
| 500 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 501 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 502 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 504 | public function previous_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | { |
| 506 | $result = $this->result($type); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 507 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 509 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | if (isset($result[$this->current_row - 1])) |
| 513 | { |
| 514 | --$this->current_row; |
| 515 | } |
| 516 | return $result[$this->current_row]; |
| 517 | } |
| 518 | |
| 519 | // -------------------------------------------------------------------- |
| 520 | |
| 521 | /** |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 522 | * Returns an unbuffered row and move pointer to next row |
| 523 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 524 | * @param string $type 'array', 'object' or a custom class name |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 525 | * @return mixed |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 526 | */ |
| 527 | public function unbuffered_row($type = 'object') |
| 528 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 529 | if ($type === 'array') |
| 530 | { |
| 531 | return $this->_fetch_assoc(); |
| 532 | } |
| 533 | elseif ($type === 'object') |
| 534 | { |
| 535 | return $this->_fetch_object(); |
| 536 | } |
| 537 | |
| 538 | return $this->_fetch_object($type); |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | // -------------------------------------------------------------------- |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 542 | |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 543 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 544 | * The following methods are normally overloaded by the identically named |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | * methods in the platform-specific driver -- except when query caching |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 546 | * 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] | 547 | * These functions are primarily here to prevent undefined function errors |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 548 | * 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] | 549 | * operational due to the unavailability of the database resource IDs with |
| 550 | * cached results. |
| 551 | */ |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 552 | |
| 553 | // -------------------------------------------------------------------- |
| 554 | |
| 555 | /** |
| 556 | * Number of fields in the result set |
| 557 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 558 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 559 | * |
| 560 | * @return int |
| 561 | */ |
| 562 | public function num_fields() |
| 563 | { |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | // -------------------------------------------------------------------- |
| 568 | |
| 569 | /** |
| 570 | * Fetch Field Names |
| 571 | * |
| 572 | * Generates an array of column names. |
| 573 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 574 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 575 | * |
| 576 | * @return array |
| 577 | */ |
| 578 | public function list_fields() |
| 579 | { |
| 580 | return array(); |
| 581 | } |
| 582 | |
| 583 | // -------------------------------------------------------------------- |
| 584 | |
| 585 | /** |
| 586 | * Field data |
| 587 | * |
| 588 | * Generates an array of objects containing field meta-data. |
| 589 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 590 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 591 | * |
| 592 | * @return array |
| 593 | */ |
| 594 | public function field_data() |
| 595 | { |
| 596 | return array(); |
| 597 | } |
| 598 | |
| 599 | // -------------------------------------------------------------------- |
| 600 | |
| 601 | /** |
| 602 | * Free the result |
| 603 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 604 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 605 | * |
| 606 | * @return void |
| 607 | */ |
| 608 | public function free_result() |
| 609 | { |
| 610 | $this->result_id = FALSE; |
| 611 | } |
| 612 | |
| 613 | // -------------------------------------------------------------------- |
| 614 | |
| 615 | /** |
| 616 | * Data Seek |
| 617 | * |
| 618 | * Moves the internal pointer to the desired offset. We call |
| 619 | * this internally before fetching results to make sure the |
| 620 | * result set starts at zero. |
| 621 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 622 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 623 | * |
| 624 | * @param int $n |
| 625 | * @return bool |
| 626 | */ |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 627 | public function data_seek($n = 0) |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 628 | { |
| 629 | return FALSE; |
| 630 | } |
| 631 | |
| 632 | // -------------------------------------------------------------------- |
| 633 | |
| 634 | /** |
| 635 | * Result - associative array |
| 636 | * |
| 637 | * Returns the result set as an array. |
| 638 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 639 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 640 | * |
| 641 | * @return array |
| 642 | */ |
| 643 | protected function _fetch_assoc() |
| 644 | { |
| 645 | return array(); |
| 646 | } |
| 647 | |
| 648 | // -------------------------------------------------------------------- |
| 649 | |
| 650 | /** |
| 651 | * Result - object |
| 652 | * |
| 653 | * Returns the result set as an object. |
| 654 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 655 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 656 | * |
| 657 | * @param string $class_name |
| 658 | * @return object |
| 659 | */ |
| 660 | protected function _fetch_object($class_name = 'stdClass') |
| 661 | { |
Andrey Andreev | ac8541b | 2016-08-01 11:37:04 +0300 | [diff] [blame] | 662 | return new $class_name(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 663 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 664 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 665 | } |