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. | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
12 | * @since Version 1.0 | ||||
13 | * @filesource | ||||
14 | */ | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 15 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
17 | |||||
18 | /** | ||||
19 | * Database Result Class | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 20 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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. | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 41 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 42 | * @access public |
43 | * @param string can be "object" or "array" | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 44 | * @return mixed either a result object or array |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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. | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 55 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 56 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 57 | * @return object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | e334c47 | 2006-10-21 19:44:22 +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. | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 84 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 85 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 86 | * @return array |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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. | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 113 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 114 | * @access public |
115 | * @param string can be "object" or "array" | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 116 | * @return mixed either a result object or array |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 127 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 128 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 129 | * @return object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 152 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 153 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 154 | * @return array |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 178 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 179 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 180 | * @return object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 197 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 198 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 199 | * @return object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 216 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 217 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 218 | * @return object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | ||||
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 241 | * |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 242 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 243 | * @return object |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 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 | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 264 | * The following functions are normally overloaded by the identically named |
admin | 7da9476 | 2006-10-05 22:18:31 +0000 | [diff] [blame] | 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 |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame^] | 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 | ?> |