Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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 ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 35 | var $current_row = 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | var $num_rows = 0; |
| 37 | var $row_data = NULL; |
| 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" |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 45 | * @return mixed either a result object or array |
| 46 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | function result($type = 'object') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 48 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 59 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | function result_object() |
| 61 | { |
| 62 | if (count($this->result_object) > 0) |
| 63 | { |
| 64 | return $this->result_object; |
| 65 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 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 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 80 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | return $this->result_object; |
| 82 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 83 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
| 87 | * Query result. "array" version. |
| 88 | * |
| 89 | * @access public |
| 90 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 91 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | function result_array() |
| 93 | { |
| 94 | if (count($this->result_array) > 0) |
| 95 | { |
| 96 | return $this->result_array; |
| 97 | } |
| 98 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 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 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 112 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
| 122 | * @param string |
| 123 | * @param string can be "object" or "array" |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 124 | * @return mixed either a result object or array |
| 125 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | function row($n = 0, $type = 'object') |
| 127 | { |
| 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | // array_key_exists() instead of isset() to allow for MySQL NULL values |
| 137 | if (array_key_exists($n, $this->row_data)) |
| 138 | { |
| 139 | return $this->row_data[$n]; |
| 140 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 141 | // reset the $n variable if the result was not achieved |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | $n = 0; |
| 143 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 144 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | return ($type == 'object') ? $this->row_object($n) : $this->row_array($n); |
| 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | /** |
| 151 | * Assigns an item into a particular column slot |
| 152 | * |
| 153 | * @access public |
| 154 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 155 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | function set_row($key, $value = NULL) |
| 157 | { |
| 158 | // We cache the row data for subsequent uses |
| 159 | if ( ! is_array($this->row_data)) |
| 160 | { |
| 161 | $this->row_data = $this->row_array(0); |
| 162 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | if (is_array($key)) |
| 165 | { |
| 166 | foreach ($key as $k => $v) |
| 167 | { |
| 168 | $this->row_data[$k] = $v; |
| 169 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 170 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 173 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | if ($key != '' AND ! is_null($value)) |
| 175 | { |
| 176 | $this->row_data[$key] = $value; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // -------------------------------------------------------------------- |
| 181 | |
| 182 | /** |
| 183 | * Returns a single result row - object version |
| 184 | * |
| 185 | * @access public |
| 186 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 187 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | function row_object($n = 0) |
| 189 | { |
| 190 | $result = $this->result_object(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 212 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | function row_array($n = 0) |
| 214 | { |
| 215 | $result = $this->result_array(); |
| 216 | |
| 217 | if (count($result) == 0) |
| 218 | { |
| 219 | return $result; |
| 220 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 221 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | if ($n != $this->current_row AND isset($result[$n])) |
| 223 | { |
| 224 | $this->current_row = $n; |
| 225 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | return $result[$this->current_row]; |
| 228 | } |
| 229 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 230 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | // -------------------------------------------------------------------- |
| 232 | |
| 233 | /** |
| 234 | * Returns the "first" row |
| 235 | * |
| 236 | * @access public |
| 237 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 238 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 249 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | // -------------------------------------------------------------------- |
| 251 | |
| 252 | /** |
| 253 | * Returns the "last" row |
| 254 | * |
| 255 | * @access public |
| 256 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 257 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 267 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | |
| 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
| 272 | * Returns the "next" row |
| 273 | * |
| 274 | * @access public |
| 275 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 276 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 290 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | return $result[$this->current_row]; |
| 292 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 293 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | // -------------------------------------------------------------------- |
| 295 | |
| 296 | /** |
| 297 | * Returns the "previous" row |
| 298 | * |
| 299 | * @access public |
| 300 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 301 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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(); } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 332 | function field_data() { return array(); } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | function free_result() { return TRUE; } |
| 334 | function _data_seek() { return TRUE; } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 335 | function _fetch_assoc() { return array(); } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | function _fetch_object() { return array(); } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 337 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | } |
| 339 | // END DB_result class |
| 340 | |
| 341 | /* End of file DB_result.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 342 | /* Location: ./system/database/DB_result.php */ |