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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 26 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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 | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 128 | if (! is_numeric($n))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 129 | {
|
| 130 | // We cache the row data for subsequent uses
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 131 | if (! is_array($this->row_data))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 132 | {
|
| 133 | $this->row_data = $this->row_array(0);
|
| 134 | }
|
| 135 |
|
Derek Jones | e97b136 | 2008-04-03 21:23:13 +0000 | [diff] [blame] | 136 | // array_key_exists() instead of isset() to allow for MySQL NULL values
|
Derek Jones | 43cbdaa | 2008-04-03 21:54:15 +0000 | [diff] [blame] | 137 | if (array_key_exists($n, $this->row_data))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 138 | {
|
| 139 | return $this->row_data[$n];
|
| 140 | }
|
| 141 | // reset the $n variable if the result was not achieved
|
| 142 | $n = 0;
|
| 143 | }
|
| 144 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 145 | return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
|
| 146 | }
|
| 147 |
|
| 148 | // --------------------------------------------------------------------
|
| 149 |
|
| 150 | /**
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 151 | * Assigns an item into a particular column slot
|
| 152 | *
|
| 153 | * @access public
|
| 154 | * @return object
|
| 155 | */
|
| 156 | function set_row($key, $value = NULL)
|
| 157 | {
|
| 158 | // We cache the row data for subsequent uses
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 159 | if (! is_array($this->row_data))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 160 | {
|
| 161 | $this->row_data = $this->row_array(0);
|
| 162 | }
|
| 163 |
|
| 164 | if (is_array($key))
|
| 165 | {
|
| 166 | foreach ($key as $k => $v)
|
| 167 | {
|
| 168 | $this->row_data[$k] = $v;
|
| 169 | }
|
| 170 |
|
| 171 | return;
|
| 172 | }
|
| 173 |
|
| 174 | if ($key != '' AND ! is_null($value))
|
| 175 | {
|
| 176 | $this->row_data[$key] = $value;
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | // --------------------------------------------------------------------
|
| 181 |
|
| 182 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 183 | * Returns a single result row - object version
|
| 184 | *
|
| 185 | * @access public
|
| 186 | * @return object
|
| 187 | */
|
| 188 | function row_object($n = 0)
|
| 189 | {
|
| 190 | $result = $this->result_object();
|
| 191 |
|
| 192 | if (count($result) == 0)
|
| 193 | {
|
| 194 | return $result;
|
| 195 | }
|
| 196 |
|
| 197 | if ($n != $this->current_row AND isset($result[$n]))
|
| 198 | {
|
| 199 | $this->current_row = $n;
|
| 200 | }
|
| 201 |
|
| 202 | return $result[$this->current_row];
|
| 203 | }
|
| 204 |
|
| 205 | // --------------------------------------------------------------------
|
| 206 |
|
| 207 | /**
|
| 208 | * Returns a single result row - array version
|
| 209 | *
|
| 210 | * @access public
|
| 211 | * @return array
|
| 212 | */
|
| 213 | function row_array($n = 0)
|
| 214 | {
|
| 215 | $result = $this->result_array();
|
| 216 |
|
| 217 | if (count($result) == 0)
|
| 218 | {
|
| 219 | return $result;
|
| 220 | }
|
| 221 |
|
| 222 | if ($n != $this->current_row AND isset($result[$n]))
|
| 223 | {
|
| 224 | $this->current_row = $n;
|
| 225 | }
|
| 226 |
|
| 227 | return $result[$this->current_row];
|
| 228 | }
|
| 229 |
|
| 230 |
|
| 231 | // --------------------------------------------------------------------
|
| 232 |
|
| 233 | /**
|
| 234 | * Returns the "first" row
|
| 235 | *
|
| 236 | * @access public
|
| 237 | * @return object
|
| 238 | */
|
| 239 | function first_row($type = 'object')
|
| 240 | {
|
| 241 | $result = $this->result($type);
|
| 242 |
|
| 243 | if (count($result) == 0)
|
| 244 | {
|
| 245 | return $result;
|
| 246 | }
|
| 247 | return $result[0];
|
| 248 | }
|
| 249 |
|
| 250 | // --------------------------------------------------------------------
|
| 251 |
|
| 252 | /**
|
| 253 | * Returns the "last" row
|
| 254 | *
|
| 255 | * @access public
|
| 256 | * @return object
|
| 257 | */
|
| 258 | function last_row($type = 'object')
|
| 259 | {
|
| 260 | $result = $this->result($type);
|
| 261 |
|
| 262 | if (count($result) == 0)
|
| 263 | {
|
| 264 | return $result;
|
| 265 | }
|
| 266 | return $result[count($result) -1];
|
| 267 | }
|
| 268 |
|
| 269 | // --------------------------------------------------------------------
|
| 270 |
|
| 271 | /**
|
| 272 | * Returns the "next" row
|
| 273 | *
|
| 274 | * @access public
|
| 275 | * @return object
|
| 276 | */
|
| 277 | function next_row($type = 'object')
|
| 278 | {
|
| 279 | $result = $this->result($type);
|
| 280 |
|
| 281 | if (count($result) == 0)
|
| 282 | {
|
| 283 | return $result;
|
| 284 | }
|
| 285 |
|
| 286 | if (isset($result[$this->current_row + 1]))
|
| 287 | {
|
| 288 | ++$this->current_row;
|
| 289 | }
|
| 290 |
|
| 291 | return $result[$this->current_row];
|
| 292 | }
|
| 293 |
|
| 294 | // --------------------------------------------------------------------
|
| 295 |
|
| 296 | /**
|
| 297 | * Returns the "previous" row
|
| 298 | *
|
| 299 | * @access public
|
| 300 | * @return object
|
| 301 | */
|
| 302 | function previous_row($type = 'object')
|
| 303 | {
|
| 304 | $result = $this->result($type);
|
| 305 |
|
| 306 | if (count($result) == 0)
|
| 307 | {
|
| 308 | return $result;
|
| 309 | }
|
| 310 |
|
| 311 | if (isset($result[$this->current_row - 1]))
|
| 312 | {
|
| 313 | --$this->current_row;
|
| 314 | }
|
| 315 | return $result[$this->current_row];
|
| 316 | }
|
| 317 |
|
| 318 | // --------------------------------------------------------------------
|
| 319 |
|
| 320 | /**
|
| 321 | * The following functions are normally overloaded by the identically named
|
| 322 | * methods in the platform-specific driver -- except when query caching
|
| 323 | * is used. When caching is enabled we do not load the other driver.
|
| 324 | * These functions are primarily here to prevent undefined function errors
|
| 325 | * when a cached result object is in use. They are not otherwise fully
|
| 326 | * operational due to the unavailability of the database resource IDs with
|
| 327 | * cached results.
|
| 328 | */
|
| 329 | function num_rows() { return $this->num_rows; }
|
| 330 | function num_fields() { return 0; }
|
| 331 | function list_fields() { return array(); }
|
| 332 | function field_names() { return array(); } // Deprecated
|
| 333 | function field_data() { return array(); }
|
| 334 | function free_result() { return TRUE; }
|
| 335 | function _data_seek() { return TRUE; }
|
| 336 | function _fetch_assoc() { return array(); }
|
| 337 | function _fetch_object() { return array(); }
|
| 338 |
|
| 339 | }
|
| 340 | // END DB_result class
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 341 | |
| 342 | /* End of file DB_result.php */ |
| 343 | /* Location: ./system/database/DB_result.php */ |