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