Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Database Result Class |
| 20 | * |
| 21 | * This is the platform-independent result class. |
| 22 | * This class will not be called directly. Rather, the adapter |
| 23 | * class for the specific database will extend and instantiate it. |
| 24 | * |
| 25 | * @category Database |
| 26 | * @author ExpressionEngine Dev Team |
| 27 | * @link http://codeigniter.com/user_guide/database/ |
| 28 | */ |
| 29 | class CI_DB_result { |
| 30 | |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame^] | 31 | var $conn_id = NULL; |
| 32 | var $result_id = NULL; |
| 33 | var $result_array = array(); |
| 34 | var $result_object = array(); |
| 35 | var $custom_result_object = array(); |
| 36 | var $current_row = 0; |
| 37 | var $num_rows = 0; |
| 38 | var $row_data = NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /** |
| 42 | * Query result. Acts as a wrapper function for the following functions. |
| 43 | * |
| 44 | * @access public |
| 45 | * @param string can be "object" or "array" |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | * @return mixed either a result object or array |
| 47 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | function result($type = 'object') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 49 | { |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame^] | 50 | if ($type == 'array') return $this->result_array(); |
| 51 | else if ($type == 'object') return $this->result_object(); |
| 52 | else return $this->custom_result_object($type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // -------------------------------------------------------------------- |
| 56 | |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame^] | 57 | /** |
| 58 | * Custom query result. |
| 59 | * |
| 60 | * @param class_name A string that represents the type of object you want back |
| 61 | * @return array of objects |
| 62 | */ |
| 63 | function custom_result_object($class_name) |
| 64 | { |
| 65 | if (array_key_exists($class_name, $this->custom_result_object)) |
| 66 | { |
| 67 | return $this->custom_result_object[$class_name]; |
| 68 | } |
| 69 | |
| 70 | if ($this->result_id === FALSE OR $this->num_rows() == 0) |
| 71 | { |
| 72 | return array(); |
| 73 | } |
| 74 | |
| 75 | // add the data to the object |
| 76 | $this->_data_seek(0); |
| 77 | $result_object = array(); |
| 78 | while ($row = $this->_fetch_object()) |
| 79 | { |
| 80 | $object = new $class_name(); |
| 81 | foreach($row as $key => $value) |
| 82 | { |
| 83 | $object->$key = $value; |
| 84 | } |
| 85 | $result_object[] = $object; |
| 86 | } |
| 87 | |
| 88 | // return the array |
| 89 | return $this->custom_result_object[$class_name] = $result_object; |
| 90 | } |
| 91 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | /** |
| 93 | * Query result. "object" version. |
| 94 | * |
| 95 | * @access public |
| 96 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | function result_object() |
| 99 | { |
| 100 | if (count($this->result_object) > 0) |
| 101 | { |
| 102 | return $this->result_object; |
| 103 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | |
| 105 | // In the event that query caching is on the result_id variable |
| 106 | // will return FALSE since there isn't a valid SQL resource so |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | // we'll simply return an empty array. |
| 108 | if ($this->result_id === FALSE OR $this->num_rows() == 0) |
| 109 | { |
| 110 | return array(); |
| 111 | } |
| 112 | |
| 113 | $this->_data_seek(0); |
| 114 | while ($row = $this->_fetch_object()) |
| 115 | { |
| 116 | $this->result_object[] = $row; |
| 117 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 118 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | return $this->result_object; |
| 120 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | // -------------------------------------------------------------------- |
| 123 | |
| 124 | /** |
| 125 | * Query result. "array" version. |
| 126 | * |
| 127 | * @access public |
| 128 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | function result_array() |
| 131 | { |
| 132 | if (count($this->result_array) > 0) |
| 133 | { |
| 134 | return $this->result_array; |
| 135 | } |
| 136 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | // In the event that query caching is on the result_id variable |
| 138 | // will return FALSE since there isn't a valid SQL resource so |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | // we'll simply return an empty array. |
| 140 | if ($this->result_id === FALSE OR $this->num_rows() == 0) |
| 141 | { |
| 142 | return array(); |
| 143 | } |
| 144 | |
| 145 | $this->_data_seek(0); |
| 146 | while ($row = $this->_fetch_assoc()) |
| 147 | { |
| 148 | $this->result_array[] = $row; |
| 149 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | return $this->result_array; |
| 152 | } |
| 153 | |
| 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
| 157 | * Query result. Acts as a wrapper function for the following functions. |
| 158 | * |
| 159 | * @access public |
| 160 | * @param string |
| 161 | * @param string can be "object" or "array" |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | * @return mixed either a result object or array |
| 163 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | function row($n = 0, $type = 'object') |
| 165 | { |
| 166 | if ( ! is_numeric($n)) |
| 167 | { |
| 168 | // We cache the row data for subsequent uses |
| 169 | if ( ! is_array($this->row_data)) |
| 170 | { |
| 171 | $this->row_data = $this->row_array(0); |
| 172 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | // array_key_exists() instead of isset() to allow for MySQL NULL values |
| 175 | if (array_key_exists($n, $this->row_data)) |
| 176 | { |
| 177 | return $this->row_data[$n]; |
| 178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | // reset the $n variable if the result was not achieved |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | $n = 0; |
| 181 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame^] | 183 | if ($type == 'object') return $this->row_object($n); |
| 184 | else if ($type == 'array') return $this->row_array($n); |
| 185 | else return $this->custom_row_object($n, $type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // -------------------------------------------------------------------- |
| 189 | |
| 190 | /** |
| 191 | * Assigns an item into a particular column slot |
| 192 | * |
| 193 | * @access public |
| 194 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | function set_row($key, $value = NULL) |
| 197 | { |
| 198 | // We cache the row data for subsequent uses |
| 199 | if ( ! is_array($this->row_data)) |
| 200 | { |
| 201 | $this->row_data = $this->row_array(0); |
| 202 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | if (is_array($key)) |
| 205 | { |
| 206 | foreach ($key as $k => $v) |
| 207 | { |
| 208 | $this->row_data[$k] = $v; |
| 209 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | if ($key != '' AND ! is_null($value)) |
| 215 | { |
| 216 | $this->row_data[$key] = $value; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame^] | 222 | /** |
| 223 | * Returns a single result row - custom object version |
| 224 | * |
| 225 | * @access public |
| 226 | * @return object |
| 227 | */ |
| 228 | function custom_row_object($n, $type) |
| 229 | { |
| 230 | $result = $this->custom_result_object($type); |
| 231 | |
| 232 | if (count($result) == 0) |
| 233 | { |
| 234 | return $result; |
| 235 | } |
| 236 | |
| 237 | if ($n != $this->current_row AND isset($result[$n])) |
| 238 | { |
| 239 | $this->current_row = $n; |
| 240 | } |
| 241 | |
| 242 | return $result[$this->current_row]; |
| 243 | } |
| 244 | |
| 245 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | * Returns a single result row - object version |
| 247 | * |
| 248 | * @access public |
| 249 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | function row_object($n = 0) |
| 252 | { |
| 253 | $result = $this->result_object(); |
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 | if (count($result) == 0) |
| 256 | { |
| 257 | return $result; |
| 258 | } |
| 259 | |
| 260 | if ($n != $this->current_row AND isset($result[$n])) |
| 261 | { |
| 262 | $this->current_row = $n; |
| 263 | } |
| 264 | |
| 265 | return $result[$this->current_row]; |
| 266 | } |
| 267 | |
| 268 | // -------------------------------------------------------------------- |
| 269 | |
| 270 | /** |
| 271 | * Returns a single result row - array version |
| 272 | * |
| 273 | * @access public |
| 274 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | function row_array($n = 0) |
| 277 | { |
| 278 | $result = $this->result_array(); |
| 279 | |
| 280 | if (count($result) == 0) |
| 281 | { |
| 282 | return $result; |
| 283 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | if ($n != $this->current_row AND isset($result[$n])) |
| 286 | { |
| 287 | $this->current_row = $n; |
| 288 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | return $result[$this->current_row]; |
| 291 | } |
| 292 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 293 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | // -------------------------------------------------------------------- |
| 295 | |
| 296 | /** |
| 297 | * Returns the "first" row |
| 298 | * |
| 299 | * @access public |
| 300 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | function first_row($type = 'object') |
| 303 | { |
| 304 | $result = $this->result($type); |
| 305 | |
| 306 | if (count($result) == 0) |
| 307 | { |
| 308 | return $result; |
| 309 | } |
| 310 | return $result[0]; |
| 311 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 313 | // -------------------------------------------------------------------- |
| 314 | |
| 315 | /** |
| 316 | * Returns the "last" row |
| 317 | * |
| 318 | * @access public |
| 319 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | function last_row($type = 'object') |
| 322 | { |
| 323 | $result = $this->result($type); |
| 324 | |
| 325 | if (count($result) == 0) |
| 326 | { |
| 327 | return $result; |
| 328 | } |
| 329 | return $result[count($result) -1]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | |
| 332 | // -------------------------------------------------------------------- |
| 333 | |
| 334 | /** |
| 335 | * Returns the "next" row |
| 336 | * |
| 337 | * @access public |
| 338 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | function next_row($type = 'object') |
| 341 | { |
| 342 | $result = $this->result($type); |
| 343 | |
| 344 | if (count($result) == 0) |
| 345 | { |
| 346 | return $result; |
| 347 | } |
| 348 | |
| 349 | if (isset($result[$this->current_row + 1])) |
| 350 | { |
| 351 | ++$this->current_row; |
| 352 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | return $result[$this->current_row]; |
| 355 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | // -------------------------------------------------------------------- |
| 358 | |
| 359 | /** |
| 360 | * Returns the "previous" row |
| 361 | * |
| 362 | * @access public |
| 363 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 364 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | function previous_row($type = 'object') |
| 366 | { |
| 367 | $result = $this->result($type); |
| 368 | |
| 369 | if (count($result) == 0) |
| 370 | { |
| 371 | return $result; |
| 372 | } |
| 373 | |
| 374 | if (isset($result[$this->current_row - 1])) |
| 375 | { |
| 376 | --$this->current_row; |
| 377 | } |
| 378 | return $result[$this->current_row]; |
| 379 | } |
| 380 | |
| 381 | // -------------------------------------------------------------------- |
| 382 | |
| 383 | /** |
| 384 | * The following functions are normally overloaded by the identically named |
| 385 | * methods in the platform-specific driver -- except when query caching |
| 386 | * is used. When caching is enabled we do not load the other driver. |
| 387 | * These functions are primarily here to prevent undefined function errors |
| 388 | * when a cached result object is in use. They are not otherwise fully |
| 389 | * operational due to the unavailability of the database resource IDs with |
| 390 | * cached results. |
| 391 | */ |
| 392 | function num_rows() { return $this->num_rows; } |
| 393 | function num_fields() { return 0; } |
| 394 | function list_fields() { return array(); } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 395 | function field_data() { return array(); } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | function free_result() { return TRUE; } |
| 397 | function _data_seek() { return TRUE; } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 398 | function _fetch_assoc() { return array(); } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | function _fetch_object() { return array(); } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | } |
| 402 | // END DB_result class |
| 403 | |
| 404 | /* End of file DB_result.php */ |
John Crepezzi | 7b47430 | 2011-01-15 18:17:01 -0500 | [diff] [blame^] | 405 | /* Location: ./system/database/DB_result.php */ |