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