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 | * |
| 256 | * @access public |
| 257 | * @return integer |
| 258 | */ |
| 259 | function num_rows() |
| 260 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 261 | return $this->num_rows; |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // -------------------------------------------------------------------- |
| 265 | |
| 266 | /** |
| 267 | * Number of fields in the result set |
| 268 | * |
| 269 | * @access public |
| 270 | * @return integer |
| 271 | */ |
| 272 | function num_fields() |
| 273 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 274 | return 0; |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 275 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 276 | |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 277 | // -------------------------------------------------------------------- |
| 278 | |
| 279 | /** |
| 280 | * Fetch Field Names |
| 281 | * |
| 282 | * Generates an array of column names |
| 283 | * |
| 284 | * @access public |
| 285 | * @return array |
| 286 | */ |
| 287 | function field_names() |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 288 | { |
| 289 | return array(); |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // -------------------------------------------------------------------- |
| 293 | |
| 294 | /** |
| 295 | * Field data |
| 296 | * |
| 297 | * Generates an array of objects containing field meta-data |
| 298 | * |
| 299 | * @access public |
| 300 | * @return array |
| 301 | */ |
| 302 | function field_data() |
| 303 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 304 | $F = new stdClass(); |
| 305 | $F->name = NULL; |
| 306 | $F->type = NULL; |
| 307 | $F->default = NULL; |
| 308 | $F->max_length = NULL; |
| 309 | $F->primary_key = NULL; |
| 310 | |
| 311 | return $retval[] = $F; |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 312 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 313 | |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 314 | // -------------------------------------------------------------------- |
| 315 | |
| 316 | /** |
| 317 | * Free the result |
| 318 | * |
| 319 | * @return null |
| 320 | */ |
| 321 | function free_result() |
| 322 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 323 | return TRUE; |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 324 | } |
| 325 | |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 326 | // -------------------------------------------------------------------- |
| 327 | |
| 328 | /** |
| 329 | * Data Seek |
| 330 | * |
| 331 | * Moves the internal pointer to the desired offset. We call |
| 332 | * this internally before fetching results to make sure the |
| 333 | * result set starts at zero |
| 334 | * |
| 335 | * @access private |
| 336 | * @return array |
| 337 | */ |
| 338 | function _data_seek($n = 0) |
| 339 | { |
| 340 | return TRUE; |
| 341 | } |
| 342 | |
| 343 | // -------------------------------------------------------------------- |
| 344 | |
| 345 | /** |
| 346 | * Result - associative array |
| 347 | * |
| 348 | * Returns the result set as an array |
| 349 | * |
| 350 | * @access private |
| 351 | * @return array |
| 352 | */ |
| 353 | function _fetch_assoc() |
| 354 | { |
| 355 | return array(); |
| 356 | } |
| 357 | |
| 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * Result - object |
| 362 | * |
| 363 | * Returns the result set as an object |
| 364 | * |
| 365 | * @access private |
| 366 | * @return object |
| 367 | */ |
| 368 | function _fetch_object() |
| 369 | { |
| 370 | return array(); |
| 371 | } |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 372 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | ?> |