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 | * |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, 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/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, 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 | } |
| 166 | else |
| 167 | { |
| 168 | return $this->custom_result_object($type); |
| 169 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 174 | /** |
| 175 | * Custom query result. |
| 176 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 177 | * @param string $class_name |
| 178 | * @return array |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 179 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 180 | public function custom_result_object($class_name) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 181 | { |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 182 | if (isset($this->custom_result_object[$class_name])) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 183 | { |
| 184 | return $this->custom_result_object[$class_name]; |
| 185 | } |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 186 | elseif ( ! $this->result_id OR $this->num_rows === 0) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 187 | { |
| 188 | return array(); |
| 189 | } |
Razican | 114ab09 | 2011-04-25 17:26:45 +0200 | [diff] [blame] | 190 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 191 | // Don't fetch the result set again if we already have it |
| 192 | $_data = NULL; |
| 193 | if (($c = count($this->result_array)) > 0) |
| 194 | { |
| 195 | $_data = 'result_array'; |
| 196 | } |
| 197 | elseif (($c = count($this->result_object)) > 0) |
| 198 | { |
| 199 | $_data = 'result_object'; |
| 200 | } |
| 201 | |
| 202 | if ($_data !== NULL) |
| 203 | { |
| 204 | for ($i = 0; $i < $c; $i++) |
| 205 | { |
| 206 | $this->custom_result_object[$class_name][$i] = new $class_name(); |
| 207 | |
Andrey Andreev | 0db81b6 | 2012-07-08 22:00:17 +0300 | [diff] [blame] | 208 | foreach ($this->{$_data}[$i] as $key => $value) |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 209 | { |
| 210 | $this->custom_result_object[$class_name][$i]->$key = $value; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return $this->custom_result_object[$class_name]; |
| 215 | } |
| 216 | |
Andrey Andreev | 08ae84f | 2014-05-09 10:46:11 +0300 | [diff] [blame] | 217 | is_null($this->row_data) OR $this->data_seek(0); |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 218 | $this->custom_result_object[$class_name] = array(); |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 219 | |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 220 | while ($row = $this->_fetch_object($class_name)) |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 221 | { |
Jon Ellis-Jones | c4efbdc | 2012-07-07 10:30:29 +0100 | [diff] [blame] | 222 | $this->custom_result_object[$class_name][] = $row; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 225 | return $this->custom_result_object[$class_name]; |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // -------------------------------------------------------------------- |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 229 | |
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 | * Query result. "object" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | * |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 233 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 235 | public function result_object() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | { |
| 237 | if (count($this->result_object) > 0) |
| 238 | { |
| 239 | return $this->result_object; |
| 240 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 242 | // In the event that query caching is on, the result_id variable |
| 243 | // will not be a valid resource so we'll simply return an empty |
| 244 | // array. |
| 245 | if ( ! $this->result_id OR $this->num_rows === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | { |
| 247 | return array(); |
| 248 | } |
| 249 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 250 | if (($c = count($this->result_array)) > 0) |
| 251 | { |
| 252 | for ($i = 0; $i < $c; $i++) |
| 253 | { |
| 254 | $this->result_object[$i] = (object) $this->result_array[$i]; |
| 255 | } |
| 256 | |
| 257 | return $this->result_object; |
| 258 | } |
| 259 | |
Andrey Andreev | 08ae84f | 2014-05-09 10:46:11 +0300 | [diff] [blame] | 260 | is_null($this->row_data) OR $this->data_seek(0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | while ($row = $this->_fetch_object()) |
| 262 | { |
| 263 | $this->result_object[] = $row; |
| 264 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 265 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | return $this->result_object; |
| 267 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 268 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 272 | * Query result. "array" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 276 | public function result_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | { |
| 278 | if (count($this->result_array) > 0) |
| 279 | { |
| 280 | return $this->result_array; |
| 281 | } |
| 282 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 283 | // In the event that query caching is on, the result_id variable |
| 284 | // will not be a valid resource so we'll simply return an empty |
| 285 | // array. |
| 286 | if ( ! $this->result_id OR $this->num_rows === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | { |
| 288 | return array(); |
| 289 | } |
| 290 | |
Andrey Andreev | 4763c13 | 2012-07-05 13:58:18 +0300 | [diff] [blame] | 291 | if (($c = count($this->result_object)) > 0) |
| 292 | { |
| 293 | for ($i = 0; $i < $c; $i++) |
| 294 | { |
| 295 | $this->result_array[$i] = (array) $this->result_object[$i]; |
| 296 | } |
| 297 | |
| 298 | return $this->result_array; |
| 299 | } |
| 300 | |
Andrey Andreev | 08ae84f | 2014-05-09 10:46:11 +0300 | [diff] [blame] | 301 | is_null($this->row_data) OR $this->data_seek(0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | while ($row = $this->_fetch_assoc()) |
| 303 | { |
| 304 | $this->result_array[] = $row; |
| 305 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 306 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | return $this->result_array; |
| 308 | } |
| 309 | |
| 310 | // -------------------------------------------------------------------- |
| 311 | |
| 312 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 313 | * Row |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 315 | * A wrapper method. |
| 316 | * |
| 317 | * @param mixed $n |
| 318 | * @param string $type 'object' or 'array' |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 319 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 321 | public function row($n = 0, $type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | { |
| 323 | if ( ! is_numeric($n)) |
| 324 | { |
| 325 | // We cache the row data for subsequent uses |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 326 | is_array($this->row_data) OR $this->row_data = $this->row_array(0); |
| 327 | |
| 328 | // array_key_exists() instead of isset() to allow for NULL values |
| 329 | 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] | 330 | { |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 331 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Andrey Andreev | 9f6bdc0 | 2012-10-22 23:31:10 +0300 | [diff] [blame] | 334 | return $this->row_data[$n]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 336 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 337 | if ($type === 'object') return $this->row_object($n); |
| 338 | elseif ($type === 'array') return $this->row_array($n); |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 339 | else return $this->custom_row_object($n, $type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
| 345 | * Assigns an item into a particular column slot |
| 346 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 347 | * @param mixed $key |
| 348 | * @param mixed $value |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 349 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 351 | public function set_row($key, $value = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | { |
| 353 | // We cache the row data for subsequent uses |
| 354 | if ( ! is_array($this->row_data)) |
| 355 | { |
| 356 | $this->row_data = $this->row_array(0); |
| 357 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | if (is_array($key)) |
| 360 | { |
| 361 | foreach ($key as $k => $v) |
| 362 | { |
| 363 | $this->row_data[$k] = $v; |
| 364 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | return; |
| 366 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 367 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 368 | if ($key !== '' && $value !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | { |
| 370 | $this->row_data[$key] = $value; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // -------------------------------------------------------------------- |
| 375 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 376 | /** |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 377 | * Returns a single result row - custom object version |
| 378 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 379 | * @param int $n |
| 380 | * @param string $type |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 381 | * @return object |
| 382 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 383 | public function custom_row_object($n, $type) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 384 | { |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 385 | isset($this->custom_result_object[$type]) OR $this->custom_result_object($type); |
| 386 | |
| 387 | if (count($this->custom_result_object[$type]) === 0) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 388 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 389 | return NULL; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 390 | } |
| 391 | |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 392 | if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n])) |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 393 | { |
| 394 | $this->current_row = $n; |
| 395 | } |
| 396 | |
Andrey Andreev | 34d6799 | 2012-07-05 14:37:36 +0300 | [diff] [blame] | 397 | return $this->custom_result_object[$type][$this->current_row]; |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 400 | // -------------------------------------------------------------------- |
| 401 | |
Greg Aker | e70e92b | 2011-04-25 10:50:53 -0500 | [diff] [blame] | 402 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | * Returns a single result row - object version |
| 404 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 405 | * @param int $n |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 407 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 408 | public function row_object($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | { |
| 410 | $result = $this->result_object(); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 411 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 413 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 416 | if ($n !== $this->current_row && isset($result[$n])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | { |
| 418 | $this->current_row = $n; |
| 419 | } |
| 420 | |
| 421 | return $result[$this->current_row]; |
| 422 | } |
| 423 | |
| 424 | // -------------------------------------------------------------------- |
| 425 | |
| 426 | /** |
| 427 | * Returns a single result row - array version |
| 428 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 429 | * @param int $n |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | * @return array |
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 row_array($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | { |
| 434 | $result = $this->result_array(); |
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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 439 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 440 | if ($n !== $this->current_row && isset($result[$n])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | { |
| 442 | $this->current_row = $n; |
| 443 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 444 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | return $result[$this->current_row]; |
| 446 | } |
| 447 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | // -------------------------------------------------------------------- |
| 449 | |
| 450 | /** |
| 451 | * Returns the "first" row |
| 452 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 453 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 454 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 455 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 456 | public function first_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | { |
| 458 | $result = $this->result($type); |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 459 | return (count($result) === 0) ? NULL : $result[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 461 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | // -------------------------------------------------------------------- |
| 463 | |
| 464 | /** |
| 465 | * Returns the "last" row |
| 466 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 467 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 468 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 470 | public function last_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | { |
| 472 | $result = $this->result($type); |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 473 | return (count($result) === 0) ? NULL : $result[count($result) - 1]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 474 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | |
| 476 | // -------------------------------------------------------------------- |
| 477 | |
| 478 | /** |
| 479 | * Returns the "next" row |
| 480 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 481 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 482 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 484 | public function next_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | { |
| 486 | $result = $this->result($type); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 487 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 489 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Andrey Andreev | 5780d8b | 2013-03-04 07:38:16 +0200 | [diff] [blame] | 492 | return isset($result[$this->current_row + 1]) |
| 493 | ? $result[++$this->current_row] |
| 494 | : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 496 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | // -------------------------------------------------------------------- |
| 498 | |
| 499 | /** |
| 500 | * Returns the "previous" row |
| 501 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 502 | * @param string $type |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 503 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 504 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 505 | public function previous_row($type = 'object') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | { |
| 507 | $result = $this->result($type); |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 508 | if (count($result) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | { |
Andrey Andreev | 55d3ad4 | 2012-05-24 22:13:06 +0300 | [diff] [blame] | 510 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (isset($result[$this->current_row - 1])) |
| 514 | { |
| 515 | --$this->current_row; |
| 516 | } |
| 517 | return $result[$this->current_row]; |
| 518 | } |
| 519 | |
| 520 | // -------------------------------------------------------------------- |
| 521 | |
| 522 | /** |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 523 | * Returns an unbuffered row and move pointer to next row |
| 524 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 525 | * @param string $type 'array', 'object' or a custom class name |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 526 | * @return mixed |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 527 | */ |
| 528 | public function unbuffered_row($type = 'object') |
| 529 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 530 | if ($type === 'array') |
| 531 | { |
| 532 | return $this->_fetch_assoc(); |
| 533 | } |
| 534 | elseif ($type === 'object') |
| 535 | { |
| 536 | return $this->_fetch_object(); |
| 537 | } |
| 538 | |
| 539 | return $this->_fetch_object($type); |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // -------------------------------------------------------------------- |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 543 | |
Juan Ignacio Borda | 3020b24 | 2012-05-18 18:27:50 -0300 | [diff] [blame] | 544 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 545 | * The following methods are normally overloaded by the identically named |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 546 | * methods in the platform-specific driver -- except when query caching |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 547 | * 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] | 548 | * These functions are primarily here to prevent undefined function errors |
Andrey Andreev | 38b2a25 | 2012-03-29 11:35:32 +0300 | [diff] [blame] | 549 | * 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] | 550 | * operational due to the unavailability of the database resource IDs with |
| 551 | * cached results. |
| 552 | */ |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 553 | |
| 554 | // -------------------------------------------------------------------- |
| 555 | |
| 556 | /** |
| 557 | * Number of fields in the result set |
| 558 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 559 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 560 | * |
| 561 | * @return int |
| 562 | */ |
| 563 | public function num_fields() |
| 564 | { |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | // -------------------------------------------------------------------- |
| 569 | |
| 570 | /** |
| 571 | * Fetch Field Names |
| 572 | * |
| 573 | * Generates an array of column names. |
| 574 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 575 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 576 | * |
| 577 | * @return array |
| 578 | */ |
| 579 | public function list_fields() |
| 580 | { |
| 581 | return array(); |
| 582 | } |
| 583 | |
| 584 | // -------------------------------------------------------------------- |
| 585 | |
| 586 | /** |
| 587 | * Field data |
| 588 | * |
| 589 | * Generates an array of objects containing field meta-data. |
| 590 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 591 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 592 | * |
| 593 | * @return array |
| 594 | */ |
| 595 | public function field_data() |
| 596 | { |
| 597 | return array(); |
| 598 | } |
| 599 | |
| 600 | // -------------------------------------------------------------------- |
| 601 | |
| 602 | /** |
| 603 | * Free the result |
| 604 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 605 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 606 | * |
| 607 | * @return void |
| 608 | */ |
| 609 | public function free_result() |
| 610 | { |
| 611 | $this->result_id = FALSE; |
| 612 | } |
| 613 | |
| 614 | // -------------------------------------------------------------------- |
| 615 | |
| 616 | /** |
| 617 | * Data Seek |
| 618 | * |
| 619 | * Moves the internal pointer to the desired offset. We call |
| 620 | * this internally before fetching results to make sure the |
| 621 | * result set starts at zero. |
| 622 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 623 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 624 | * |
| 625 | * @param int $n |
| 626 | * @return bool |
| 627 | */ |
Andrey Andreev | 69edc43 | 2012-12-04 13:32:16 +0200 | [diff] [blame] | 628 | public function data_seek($n = 0) |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 629 | { |
| 630 | return FALSE; |
| 631 | } |
| 632 | |
| 633 | // -------------------------------------------------------------------- |
| 634 | |
| 635 | /** |
| 636 | * Result - associative array |
| 637 | * |
| 638 | * Returns the result set as an array. |
| 639 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 640 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 641 | * |
| 642 | * @return array |
| 643 | */ |
| 644 | protected function _fetch_assoc() |
| 645 | { |
| 646 | return array(); |
| 647 | } |
| 648 | |
| 649 | // -------------------------------------------------------------------- |
| 650 | |
| 651 | /** |
| 652 | * Result - object |
| 653 | * |
| 654 | * Returns the result set as an object. |
| 655 | * |
Claudio Galdiolo | c9982e4 | 2015-01-29 11:46:05 -0500 | [diff] [blame] | 656 | * Overridden by driver result classes. |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 657 | * |
| 658 | * @param string $class_name |
| 659 | * @return object |
| 660 | */ |
| 661 | protected function _fetch_object($class_name = 'stdClass') |
| 662 | { |
Andrey Andreev | ac8541b | 2016-08-01 11:37:04 +0300 | [diff] [blame^] | 663 | return new $class_name(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 664 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 665 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 666 | } |