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 | |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 31 | var $conn_id = NULL; |
| 32 | var $result_id = NULL; |
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 | |
admin | fafe28b | 2006-10-21 19:08:17 +0000 | [diff] [blame^] | 66 | if ($this->numerous == 0) |
admin | 31eeb05 | 2006-10-20 05:11:33 +0000 | [diff] [blame] | 67 | { |
| 68 | return array(); |
| 69 | } |
| 70 | |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 71 | $this->_data_seek(0); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 72 | while ($row = $this->_fetch_object()) |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 73 | { |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 74 | $this->result_object[] = $row; |
| 75 | } |
| 76 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 77 | return $this->result_object; |
| 78 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 79 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
| 83 | * Query result. "array" version. |
| 84 | * |
| 85 | * @access public |
| 86 | * @return array |
| 87 | */ |
| 88 | function result_array() |
| 89 | { |
| 90 | if (count($this->result_array) > 0) |
| 91 | { |
| 92 | return $this->result_array; |
| 93 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 94 | |
admin | 31eeb05 | 2006-10-20 05:11:33 +0000 | [diff] [blame] | 95 | if ($this->num_rows == 0) |
| 96 | { |
| 97 | return array(); |
| 98 | } |
| 99 | |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 100 | $this->_data_seek(0); |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 101 | while ($row = $this->_fetch_assoc()) |
| 102 | { |
| 103 | $this->result_array[] = $row; |
| 104 | } |
| 105 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 106 | return $this->result_array; |
| 107 | } |
| 108 | |
| 109 | // -------------------------------------------------------------------- |
| 110 | |
| 111 | /** |
| 112 | * Query result. Acts as a wrapper function for the following functions. |
| 113 | * |
| 114 | * @access public |
| 115 | * @param string can be "object" or "array" |
| 116 | * @return mixed either a result object or array |
| 117 | */ |
| 118 | function row($n = 0, $type = 'object') |
| 119 | { |
| 120 | return ($type == 'object') ? $this->row_object($n) : $this->row_array($n); |
| 121 | } |
| 122 | |
| 123 | // -------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
| 126 | * Returns a single result row - object version |
| 127 | * |
| 128 | * @access public |
| 129 | * @return object |
| 130 | */ |
| 131 | function row_object($n = 0) |
| 132 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 133 | $result = $this->result_object(); |
| 134 | |
| 135 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 136 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 137 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 138 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 139 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 140 | if ($n != $this->current_row AND isset($result[$n])) |
| 141 | { |
| 142 | $this->current_row = $n; |
| 143 | } |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 144 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 145 | return $result[$this->current_row]; |
| 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | /** |
| 151 | * Returns a single result row - array version |
| 152 | * |
| 153 | * @access public |
| 154 | * @return array |
| 155 | */ |
| 156 | function row_array($n = 0) |
| 157 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 158 | $result = $this->result_array(); |
| 159 | |
| 160 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 161 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 162 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if ($n != $this->current_row AND isset($result[$n])) |
| 166 | { |
| 167 | $this->current_row = $n; |
| 168 | } |
| 169 | |
| 170 | return $result[$this->current_row]; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
| 175 | |
| 176 | /** |
| 177 | * Returns the "first" row |
| 178 | * |
| 179 | * @access public |
| 180 | * @return object |
| 181 | */ |
| 182 | function first_row($type = 'object') |
| 183 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 184 | $result = $this->result($type); |
| 185 | |
| 186 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 187 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 188 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 189 | } |
| 190 | return $result[0]; |
| 191 | } |
| 192 | |
| 193 | // -------------------------------------------------------------------- |
| 194 | |
| 195 | /** |
| 196 | * Returns the "last" row |
| 197 | * |
| 198 | * @access public |
| 199 | * @return object |
| 200 | */ |
| 201 | function last_row($type = 'object') |
| 202 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 203 | $result = $this->result($type); |
| 204 | |
| 205 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 206 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 207 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 208 | } |
| 209 | return $result[count($result) -1]; |
| 210 | } |
| 211 | |
| 212 | // -------------------------------------------------------------------- |
| 213 | |
| 214 | /** |
| 215 | * Returns the "next" row |
| 216 | * |
| 217 | * @access public |
| 218 | * @return object |
| 219 | */ |
| 220 | function next_row($type = 'object') |
| 221 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 222 | $result = $this->result($type); |
| 223 | |
| 224 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 225 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 226 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if (isset($result[$this->current_row + 1])) |
| 230 | { |
| 231 | ++$this->current_row; |
| 232 | } |
| 233 | |
| 234 | return $result[$this->current_row]; |
| 235 | } |
| 236 | |
| 237 | // -------------------------------------------------------------------- |
| 238 | |
| 239 | /** |
| 240 | * Returns the "previous" row |
| 241 | * |
| 242 | * @access public |
| 243 | * @return object |
| 244 | */ |
| 245 | function previous_row($type = 'object') |
| 246 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 247 | $result = $this->result($type); |
| 248 | |
| 249 | if (count($result) == 0) |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 250 | { |
admin | af64469 | 2006-10-05 04:22:41 +0000 | [diff] [blame] | 251 | return $result; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (isset($result[$this->current_row - 1])) |
| 255 | { |
| 256 | --$this->current_row; |
| 257 | } |
| 258 | return $result[$this->current_row]; |
| 259 | } |
| 260 | |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 261 | // -------------------------------------------------------------------- |
| 262 | |
| 263 | /** |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 264 | * The following functions are normally overloaded by the identically named |
| 265 | * methods in the platform-specific driver -- except when query caching |
admin | 386ec18 | 2006-10-05 21:52:20 +0000 | [diff] [blame] | 266 | * is used. When caching is enabled we do not load the other driver. |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 267 | * These functions are primarily here to prevent undefined function errors |
| 268 | * when a cached result object is in use. They are not otherwise fully |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 269 | * operational due to the unavailability of the database resource IDs with |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 270 | * cached results. |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 271 | */ |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 272 | function num_rows() { return $this->num_rows; } |
| 273 | function num_fields() { return 0; } |
admin | 606f99c | 2006-10-11 23:48:41 +0000 | [diff] [blame] | 274 | function list_fields() { return array(); } |
| 275 | function field_names() { return array(); } // Deprecated |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 276 | function field_data() { return array(); } |
| 277 | function free_result() { return TRUE; } |
| 278 | function _data_seek() { return TRUE; } |
| 279 | function _fetch_assoc() { return array(); } |
| 280 | function _fetch_object() { return array(); } |
admin | 606f99c | 2006-10-11 23:48:41 +0000 | [diff] [blame] | 281 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 282 | } |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame] | 283 | // END DB_result class |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 284 | ?> |