admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.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 Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/database/ |
| 28 | */ |
| 29 | class CI_DB_result { |
| 30 | |
| 31 | var $conn_id = FALSE; |
| 32 | var $result_id = FALSE; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 33 | var $result_array = array(); |
| 34 | var $result_object = array(); |
| 35 | var $current_row = 0; |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 36 | var $num_rows = 0; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 37 | |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 38 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Query result. Acts as a wrapper function for the following functions. |
| 41 | * |
| 42 | * @access public |
| 43 | * @param string can be "object" or "array" |
| 44 | * @return mixed either a result object or array |
| 45 | */ |
| 46 | function result($type = 'object') |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 47 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 48 | return ($type == 'object') ? $this->result_object() : $this->result_array(); |
| 49 | } |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 50 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 51 | // -------------------------------------------------------------------- |
| 52 | |
| 53 | /** |
| 54 | * Query result. "object" version. |
| 55 | * |
| 56 | * @access public |
| 57 | * @return object |
| 58 | */ |
| 59 | function result_object() |
| 60 | { |
| 61 | if (count($this->result_object) > 0) |
| 62 | { |
| 63 | return $this->result_object; |
| 64 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 65 | |
| 66 | $this->_data_seek(0); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 67 | while ($row = $this->_fetch_object()) |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 68 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 69 | $this->result_object[] = $row; |
| 70 | } |
| 71 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 72 | return $this->result_object; |
| 73 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 74 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 75 | // -------------------------------------------------------------------- |
| 76 | |
| 77 | /** |
| 78 | * Query result. "array" version. |
| 79 | * |
| 80 | * @access public |
| 81 | * @return array |
| 82 | */ |
| 83 | function result_array() |
| 84 | { |
| 85 | if (count($this->result_array) > 0) |
| 86 | { |
| 87 | return $this->result_array; |
| 88 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 89 | |
| 90 | $this->_data_seek(0); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 91 | while ($row = $this->_fetch_assoc()) |
| 92 | { |
| 93 | $this->result_array[] = $row; |
| 94 | } |
| 95 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 96 | return $this->result_array; |
| 97 | } |
| 98 | |
| 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * Query result. Acts as a wrapper function for the following functions. |
| 103 | * |
| 104 | * @access public |
| 105 | * @param string can be "object" or "array" |
| 106 | * @return mixed either a result object or array |
| 107 | */ |
| 108 | function row($n = 0, $type = 'object') |
| 109 | { |
| 110 | return ($type == 'object') ? $this->row_object($n) : $this->row_array($n); |
| 111 | } |
| 112 | |
| 113 | // -------------------------------------------------------------------- |
| 114 | |
| 115 | /** |
| 116 | * Returns a single result row - object version |
| 117 | * |
| 118 | * @access public |
| 119 | * @return object |
| 120 | */ |
| 121 | function row_object($n = 0) |
| 122 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 123 | $result = $this->result_object(); |
| 124 | |
| 125 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 126 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 127 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 128 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 129 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 130 | if ($n != $this->current_row AND isset($result[$n])) |
| 131 | { |
| 132 | $this->current_row = $n; |
| 133 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 134 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 135 | return $result[$this->current_row]; |
| 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
| 139 | |
| 140 | /** |
| 141 | * Returns a single result row - array version |
| 142 | * |
| 143 | * @access public |
| 144 | * @return array |
| 145 | */ |
| 146 | function row_array($n = 0) |
| 147 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 148 | $result = $this->result_array(); |
| 149 | |
| 150 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 151 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 152 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if ($n != $this->current_row AND isset($result[$n])) |
| 156 | { |
| 157 | $this->current_row = $n; |
| 158 | } |
| 159 | |
| 160 | return $result[$this->current_row]; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | // -------------------------------------------------------------------- |
| 165 | |
| 166 | /** |
| 167 | * Returns the "first" row |
| 168 | * |
| 169 | * @access public |
| 170 | * @return object |
| 171 | */ |
| 172 | function first_row($type = 'object') |
| 173 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 174 | $result = $this->result($type); |
| 175 | |
| 176 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 177 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 178 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 179 | } |
| 180 | return $result[0]; |
| 181 | } |
| 182 | |
| 183 | // -------------------------------------------------------------------- |
| 184 | |
| 185 | /** |
| 186 | * Returns the "last" row |
| 187 | * |
| 188 | * @access public |
| 189 | * @return object |
| 190 | */ |
| 191 | function last_row($type = 'object') |
| 192 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 193 | $result = $this->result($type); |
| 194 | |
| 195 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 196 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 197 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 198 | } |
| 199 | return $result[count($result) -1]; |
| 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
| 205 | * Returns the "next" row |
| 206 | * |
| 207 | * @access public |
| 208 | * @return object |
| 209 | */ |
| 210 | function next_row($type = 'object') |
| 211 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 212 | $result = $this->result($type); |
| 213 | |
| 214 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 215 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 216 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (isset($result[$this->current_row + 1])) |
| 220 | { |
| 221 | ++$this->current_row; |
| 222 | } |
| 223 | |
| 224 | return $result[$this->current_row]; |
| 225 | } |
| 226 | |
| 227 | // -------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
| 230 | * Returns the "previous" row |
| 231 | * |
| 232 | * @access public |
| 233 | * @return object |
| 234 | */ |
| 235 | function previous_row($type = 'object') |
| 236 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 237 | $result = $this->result($type); |
| 238 | |
| 239 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 240 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 241 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if (isset($result[$this->current_row - 1])) |
| 245 | { |
| 246 | --$this->current_row; |
| 247 | } |
| 248 | return $result[$this->current_row]; |
| 249 | } |
| 250 | |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 251 | // -------------------------------------------------------------------- |
| 252 | |
| 253 | /** |
| 254 | * Number of rows in the result set |
| 255 | * |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 256 | * Note: This function is normally overloaded by the identically named |
| 257 | * method in the platform-specific driver -- except when query caching |
| 258 | * is used. When caching is enabled we do not load the other driver, |
| 259 | * so this function is here primarily to prevent undefined function errors. |
| 260 | * |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 261 | * @access public |
| 262 | * @return integer |
| 263 | */ |
| 264 | function num_rows() |
| 265 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 266 | return $this->num_rows; |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
| 272 | * Number of fields in the result set |
| 273 | * |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 274 | * Note: This function is normally overloaded by the identically named |
| 275 | * method in the platform-specific driver -- except when query caching |
| 276 | * is used. When caching is enabled we do not load the other driver, |
| 277 | * so this function is here primarily to prevent undefined function errors. |
| 278 | * |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 279 | * @access public |
| 280 | * @return integer |
| 281 | */ |
| 282 | function num_fields() |
| 283 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 284 | return 0; |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 285 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 286 | |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 287 | // -------------------------------------------------------------------- |
| 288 | |
| 289 | /** |
| 290 | * Fetch Field Names |
| 291 | * |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 292 | * Note: This function is normally overloaded by the identically named |
| 293 | * method in the platform-specific driver -- except when query caching |
| 294 | * is used. When caching is enabled we do not load the other driver, |
| 295 | * so this function is here primarily to prevent undefined function errors. |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 296 | * |
| 297 | * @access public |
| 298 | * @return array |
| 299 | */ |
| 300 | function field_names() |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 301 | { |
| 302 | return array(); |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // -------------------------------------------------------------------- |
| 306 | |
| 307 | /** |
| 308 | * Field data |
| 309 | * |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 310 | * Note: This function is normally overloaded by the identically named |
| 311 | * method in the platform-specific driver -- except when query caching |
| 312 | * is used. When caching is enabled we do not load the other driver, |
| 313 | * so this function is here primarily to prevent undefined function errors. |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 314 | * |
| 315 | * @access public |
| 316 | * @return array |
| 317 | */ |
| 318 | function field_data() |
| 319 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 320 | $F = new stdClass(); |
| 321 | $F->name = NULL; |
| 322 | $F->type = NULL; |
| 323 | $F->default = NULL; |
| 324 | $F->max_length = NULL; |
| 325 | $F->primary_key = NULL; |
| 326 | |
| 327 | return $retval[] = $F; |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 328 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 329 | |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 330 | // -------------------------------------------------------------------- |
| 331 | |
| 332 | /** |
| 333 | * Free the result |
| 334 | * |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 335 | * Note: This function is normally overloaded by the identically named |
| 336 | * method in the platform-specific driver -- except when query caching |
| 337 | * is used. When caching is enabled we do not load the other driver, |
| 338 | * so this function is here primarily to prevent undefined function errors. |
| 339 | * |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 340 | * @return null |
| 341 | */ |
| 342 | function free_result() |
| 343 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 344 | return TRUE; |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 345 | } |
| 346 | |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 347 | // -------------------------------------------------------------------- |
| 348 | |
| 349 | /** |
| 350 | * Data Seek |
| 351 | * |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 352 | * Note: This function is normally overloaded by the identically named |
| 353 | * method in the platform-specific driver -- except when query caching |
| 354 | * is used. When caching is enabled we do not load the other driver, |
| 355 | * so this function is here primarily to prevent undefined function errors. |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 356 | * |
| 357 | * @access private |
| 358 | * @return array |
| 359 | */ |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 360 | function _data_seek() |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 361 | { |
| 362 | return TRUE; |
| 363 | } |
| 364 | |
| 365 | // -------------------------------------------------------------------- |
| 366 | |
| 367 | /** |
| 368 | * Result - associative array |
| 369 | * |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 370 | * Note: This function is normally overloaded by the identically named |
| 371 | * method in the platform-specific driver -- except when query caching |
| 372 | * is used. When caching is enabled we do not load the other driver, |
| 373 | * so this function is here primarily to prevent undefined function errors. |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 374 | * |
| 375 | * @access private |
| 376 | * @return array |
| 377 | */ |
| 378 | function _fetch_assoc() |
| 379 | { |
| 380 | return array(); |
| 381 | } |
| 382 | |
| 383 | // -------------------------------------------------------------------- |
| 384 | |
| 385 | /** |
| 386 | * Result - object |
| 387 | * |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 388 | * Note: This function is normally overloaded by the identically named |
| 389 | * method in the platform-specific driver -- except when query caching |
| 390 | * is used. When caching is enabled we do not load the other driver, |
| 391 | * so this function is here primarily to prevent undefined function errors. |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 392 | * |
| 393 | * @access private |
| 394 | * @return object |
| 395 | */ |
| 396 | function _fetch_object() |
| 397 | { |
| 398 | return array(); |
| 399 | } |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 400 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 401 | } |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame^] | 402 | // END DB_result class |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 403 | ?> |