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 | |
| 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 | /** |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 254 | * The following functions are normally overloaded by the identically named |
| 255 | * methods in the platform-specific driver -- except when query caching |
admin | 386ec18 | 2006-10-05 21:52:20 +0000 | [diff] [blame] | 256 | * is used. When caching is enabled we do not load the other driver. |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 257 | * These functions are primarily here to prevent undefined function errors |
| 258 | * when a cached result object is in use. They are not otherwise fully |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 259 | * operational due to the unavailability of the database resource IDs with |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 260 | * cached results. |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 261 | */ |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 262 | function num_rows() { return $this->num_rows; } |
| 263 | function num_fields() { return 0; } |
| 264 | function field_names() { return array(); } |
| 265 | function field_data() { return array(); } |
| 266 | function free_result() { return TRUE; } |
| 267 | function _data_seek() { return TRUE; } |
| 268 | function _fetch_assoc() { return array(); } |
| 269 | function _fetch_object() { return array(); } |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 270 | |
admin | e106318 | 2006-09-25 22:12:16 +0000 | [diff] [blame] | 271 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 272 | } |
admin | 7b9d472 | 2006-10-05 04:46:47 +0000 | [diff] [blame] | 273 | // END DB_result class |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 274 | ?> |