Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
| 3 | * CodeIgniter
|
| 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, EllisLab, Inc.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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 = NULL;
|
| 32 | var $result_id = NULL;
|
| 33 | var $result_array = array();
|
| 34 | var $result_object = array();
|
| 35 | var $current_row = 0;
|
| 36 | var $num_rows = 0;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 37 | var $row_data = NULL;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 38 |
|
| 39 |
|
| 40 | /**
|
| 41 | * Query result. Acts as a wrapper function for the following functions.
|
| 42 | *
|
| 43 | * @access public
|
| 44 | * @param string can be "object" or "array"
|
| 45 | * @return mixed either a result object or array
|
| 46 | */
|
| 47 | function result($type = 'object')
|
| 48 | {
|
| 49 | return ($type == 'object') ? $this->result_object() : $this->result_array();
|
| 50 | }
|
| 51 |
|
| 52 | // --------------------------------------------------------------------
|
| 53 |
|
| 54 | /**
|
| 55 | * Query result. "object" version.
|
| 56 | *
|
| 57 | * @access public
|
| 58 | * @return object
|
| 59 | */
|
| 60 | function result_object()
|
| 61 | {
|
| 62 | if (count($this->result_object) > 0)
|
| 63 | {
|
| 64 | return $this->result_object;
|
| 65 | }
|
| 66 |
|
| 67 | // In the event that query caching is on the result_id variable
|
| 68 | // will return FALSE since there isn't a valid SQL resource so
|
| 69 | // we'll simply return an empty array.
|
| 70 | if ($this->result_id === FALSE OR $this->num_rows() == 0)
|
| 71 | {
|
| 72 | return array();
|
| 73 | }
|
| 74 |
|
| 75 | $this->_data_seek(0);
|
| 76 | while ($row = $this->_fetch_object())
|
| 77 | {
|
| 78 | $this->result_object[] = $row;
|
| 79 | }
|
| 80 |
|
| 81 | return $this->result_object;
|
| 82 | }
|
| 83 |
|
| 84 | // --------------------------------------------------------------------
|
| 85 |
|
| 86 | /**
|
| 87 | * Query result. "array" version.
|
| 88 | *
|
| 89 | * @access public
|
| 90 | * @return array
|
| 91 | */
|
| 92 | function result_array()
|
| 93 | {
|
| 94 | if (count($this->result_array) > 0)
|
| 95 | {
|
| 96 | return $this->result_array;
|
| 97 | }
|
| 98 |
|
| 99 | // In the event that query caching is on the result_id variable
|
| 100 | // will return FALSE since there isn't a valid SQL resource so
|
| 101 | // we'll simply return an empty array.
|
| 102 | if ($this->result_id === FALSE OR $this->num_rows() == 0)
|
| 103 | {
|
| 104 | return array();
|
| 105 | }
|
| 106 |
|
| 107 | $this->_data_seek(0);
|
| 108 | while ($row = $this->_fetch_assoc())
|
| 109 | {
|
| 110 | $this->result_array[] = $row;
|
| 111 | }
|
| 112 |
|
| 113 | return $this->result_array;
|
| 114 | }
|
| 115 |
|
| 116 | // --------------------------------------------------------------------
|
| 117 |
|
| 118 | /**
|
| 119 | * Query result. Acts as a wrapper function for the following functions.
|
| 120 | *
|
| 121 | * @access public
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 122 | * @param string
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 123 | * @param string can be "object" or "array"
|
| 124 | * @return mixed either a result object or array
|
| 125 | */
|
| 126 | function row($n = 0, $type = 'object')
|
| 127 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 128 | if ( ! is_numeric($n))
|
| 129 | {
|
| 130 | // We cache the row data for subsequent uses
|
| 131 | if ( ! is_array($this->row_data))
|
| 132 | {
|
| 133 | $this->row_data = $this->row_array(0);
|
| 134 | }
|
| 135 |
|
| 136 | if (isset($this->row_data[$n]))
|
| 137 | {
|
| 138 | return $this->row_data[$n];
|
| 139 | }
|
| 140 | // reset the $n variable if the result was not achieved
|
| 141 | $n = 0;
|
| 142 | }
|
| 143 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 144 | return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
|
| 145 | }
|
| 146 |
|
| 147 | // --------------------------------------------------------------------
|
| 148 |
|
| 149 | /**
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 150 | * Assigns an item into a particular column slot
|
| 151 | *
|
| 152 | * @access public
|
| 153 | * @return object
|
| 154 | */
|
| 155 | function set_row($key, $value = NULL)
|
| 156 | {
|
| 157 | // We cache the row data for subsequent uses
|
| 158 | if ( ! is_array($this->row_data))
|
| 159 | {
|
| 160 | $this->row_data = $this->row_array(0);
|
| 161 | }
|
| 162 |
|
| 163 | if (is_array($key))
|
| 164 | {
|
| 165 | foreach ($key as $k => $v)
|
| 166 | {
|
| 167 | $this->row_data[$k] = $v;
|
| 168 | }
|
| 169 |
|
| 170 | return;
|
| 171 | }
|
| 172 |
|
| 173 | if ($key != '' AND ! is_null($value))
|
| 174 | {
|
| 175 | $this->row_data[$key] = $value;
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | // --------------------------------------------------------------------
|
| 180 |
|
| 181 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 182 | * Returns a single result row - object version
|
| 183 | *
|
| 184 | * @access public
|
| 185 | * @return object
|
| 186 | */
|
| 187 | function row_object($n = 0)
|
| 188 | {
|
| 189 | $result = $this->result_object();
|
| 190 |
|
| 191 | if (count($result) == 0)
|
| 192 | {
|
| 193 | return $result;
|
| 194 | }
|
| 195 |
|
| 196 | if ($n != $this->current_row AND isset($result[$n]))
|
| 197 | {
|
| 198 | $this->current_row = $n;
|
| 199 | }
|
| 200 |
|
| 201 | return $result[$this->current_row];
|
| 202 | }
|
| 203 |
|
| 204 | // --------------------------------------------------------------------
|
| 205 |
|
| 206 | /**
|
| 207 | * Returns a single result row - array version
|
| 208 | *
|
| 209 | * @access public
|
| 210 | * @return array
|
| 211 | */
|
| 212 | function row_array($n = 0)
|
| 213 | {
|
| 214 | $result = $this->result_array();
|
| 215 |
|
| 216 | if (count($result) == 0)
|
| 217 | {
|
| 218 | return $result;
|
| 219 | }
|
| 220 |
|
| 221 | if ($n != $this->current_row AND isset($result[$n]))
|
| 222 | {
|
| 223 | $this->current_row = $n;
|
| 224 | }
|
| 225 |
|
| 226 | return $result[$this->current_row];
|
| 227 | }
|
| 228 |
|
| 229 |
|
| 230 | // --------------------------------------------------------------------
|
| 231 |
|
| 232 | /**
|
| 233 | * Returns the "first" row
|
| 234 | *
|
| 235 | * @access public
|
| 236 | * @return object
|
| 237 | */
|
| 238 | function first_row($type = 'object')
|
| 239 | {
|
| 240 | $result = $this->result($type);
|
| 241 |
|
| 242 | if (count($result) == 0)
|
| 243 | {
|
| 244 | return $result;
|
| 245 | }
|
| 246 | return $result[0];
|
| 247 | }
|
| 248 |
|
| 249 | // --------------------------------------------------------------------
|
| 250 |
|
| 251 | /**
|
| 252 | * Returns the "last" row
|
| 253 | *
|
| 254 | * @access public
|
| 255 | * @return object
|
| 256 | */
|
| 257 | function last_row($type = 'object')
|
| 258 | {
|
| 259 | $result = $this->result($type);
|
| 260 |
|
| 261 | if (count($result) == 0)
|
| 262 | {
|
| 263 | return $result;
|
| 264 | }
|
| 265 | return $result[count($result) -1];
|
| 266 | }
|
| 267 |
|
| 268 | // --------------------------------------------------------------------
|
| 269 |
|
| 270 | /**
|
| 271 | * Returns the "next" row
|
| 272 | *
|
| 273 | * @access public
|
| 274 | * @return object
|
| 275 | */
|
| 276 | function next_row($type = 'object')
|
| 277 | {
|
| 278 | $result = $this->result($type);
|
| 279 |
|
| 280 | if (count($result) == 0)
|
| 281 | {
|
| 282 | return $result;
|
| 283 | }
|
| 284 |
|
| 285 | if (isset($result[$this->current_row + 1]))
|
| 286 | {
|
| 287 | ++$this->current_row;
|
| 288 | }
|
| 289 |
|
| 290 | return $result[$this->current_row];
|
| 291 | }
|
| 292 |
|
| 293 | // --------------------------------------------------------------------
|
| 294 |
|
| 295 | /**
|
| 296 | * Returns the "previous" row
|
| 297 | *
|
| 298 | * @access public
|
| 299 | * @return object
|
| 300 | */
|
| 301 | function previous_row($type = 'object')
|
| 302 | {
|
| 303 | $result = $this->result($type);
|
| 304 |
|
| 305 | if (count($result) == 0)
|
| 306 | {
|
| 307 | return $result;
|
| 308 | }
|
| 309 |
|
| 310 | if (isset($result[$this->current_row - 1]))
|
| 311 | {
|
| 312 | --$this->current_row;
|
| 313 | }
|
| 314 | return $result[$this->current_row];
|
| 315 | }
|
| 316 |
|
| 317 | // --------------------------------------------------------------------
|
| 318 |
|
| 319 | /**
|
| 320 | * The following functions are normally overloaded by the identically named
|
| 321 | * methods in the platform-specific driver -- except when query caching
|
| 322 | * is used. When caching is enabled we do not load the other driver.
|
| 323 | * These functions are primarily here to prevent undefined function errors
|
| 324 | * when a cached result object is in use. They are not otherwise fully
|
| 325 | * operational due to the unavailability of the database resource IDs with
|
| 326 | * cached results.
|
| 327 | */
|
| 328 | function num_rows() { return $this->num_rows; }
|
| 329 | function num_fields() { return 0; }
|
| 330 | function list_fields() { return array(); }
|
| 331 | function field_names() { return array(); } // Deprecated
|
| 332 | function field_data() { return array(); }
|
| 333 | function free_result() { return TRUE; }
|
| 334 | function _data_seek() { return TRUE; }
|
| 335 | function _fetch_assoc() { return array(); }
|
| 336 | function _fetch_object() { return array(); }
|
| 337 |
|
| 338 | }
|
| 339 | // END DB_result class
|
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 340 | ?> |