blob: 3bd43bac6eee70ca2250a8dd2b1ed63e8ec8d740 [file] [log] [blame]
admin7b613c72006-09-24 18:05:17 +00001<?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 */
29class CI_DB_result {
30
31 var $conn_id = FALSE;
32 var $result_id = FALSE;
33 var $db_debug = FALSE;
34 var $result_array = array();
35 var $result_object = array();
36 var $current_row = 0;
37
admine1063182006-09-25 22:12:16 +000038
admin7b613c72006-09-24 18:05:17 +000039 /**
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')
47 {
48 return ($type == 'object') ? $this->result_object() : $this->result_array();
49 }
admine1063182006-09-25 22:12:16 +000050
admin7b613c72006-09-24 18:05:17 +000051 // --------------------------------------------------------------------
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 }
65
66 while ($row = $this->_fetch_object())
67 {
68 $this->result_object[] = $row;
69 }
70
71 if (count($this->result_object) == 0)
72 {
admin07ad6662006-10-03 06:12:52 +000073 return array();
admin7b613c72006-09-24 18:05:17 +000074 }
75
76 return $this->result_object;
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Query result. "array" version.
83 *
84 * @access public
85 * @return array
86 */
87 function result_array()
88 {
89 if (count($this->result_array) > 0)
90 {
91 return $this->result_array;
92 }
93
94 while ($row = $this->_fetch_assoc())
95 {
96 $this->result_array[] = $row;
97 }
98
99 if (count($this->result_array) == 0)
100 {
admin07ad6662006-10-03 06:12:52 +0000101 return array();
admin7b613c72006-09-24 18:05:17 +0000102 }
103
104 return $this->result_array;
105 }
106
107 // --------------------------------------------------------------------
108
109 /**
110 * Query result. Acts as a wrapper function for the following functions.
111 *
112 * @access public
113 * @param string can be "object" or "array"
114 * @return mixed either a result object or array
115 */
116 function row($n = 0, $type = 'object')
117 {
118 return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Returns a single result row - object version
125 *
126 * @access public
127 * @return object
128 */
129 function row_object($n = 0)
130 {
131 if (FALSE === ($result = $this->result_object()))
132 {
133 return FALSE;
134 }
135
136 if ($n != $this->current_row AND isset($result[$n]))
137 {
138 $this->current_row = $n;
139 }
140
141 return $result[$this->current_row];
142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Returns a single result row - array version
148 *
149 * @access public
150 * @return array
151 */
152 function row_array($n = 0)
153 {
154 if (FALSE === ($result = $this->result_array()))
155 {
156 return FALSE;
157 }
158
159 if ($n != $this->current_row AND isset($result[$n]))
160 {
161 $this->current_row = $n;
162 }
163
164 return $result[$this->current_row];
165 }
166
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Returns the "first" row
172 *
173 * @access public
174 * @return object
175 */
176 function first_row($type = 'object')
177 {
178 if (FALSE === ($result = $this->result($type)))
179 {
180 return FALSE;
181 }
182 return $result[0];
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Returns the "last" row
189 *
190 * @access public
191 * @return object
192 */
193 function last_row($type = 'object')
194 {
195 if (FALSE === ($result = $this->result($type)))
196 {
197 return FALSE;
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 {
212 if (FALSE === ($result = $this->result($type)))
213 {
214 return FALSE;
215 }
216
217 if (isset($result[$this->current_row + 1]))
218 {
219 ++$this->current_row;
220 }
221
222 return $result[$this->current_row];
223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Returns the "previous" row
229 *
230 * @access public
231 * @return object
232 */
233 function previous_row($type = 'object')
234 {
235 if (FALSE === ($result = $this->result($type)))
236 {
237 return FALSE;
238 }
239
240 if (isset($result[$this->current_row - 1]))
241 {
242 --$this->current_row;
243 }
244 return $result[$this->current_row];
245 }
246
admine1063182006-09-25 22:12:16 +0000247 // --------------------------------------------------------------------
248
249 /**
250 * Number of rows in the result set
251 *
252 * @access public
253 * @return integer
254 */
255 function num_rows()
256 {
257 // Implemented in the platform-specific result adapter
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Number of fields in the result set
264 *
265 * @access public
266 * @return integer
267 */
268 function num_fields()
269 {
270 // Implemented in the platform-specific result adapter
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Fetch Field Names
277 *
278 * Generates an array of column names
279 *
280 * @access public
281 * @return array
282 */
283 function field_names()
284 {
285 // Implemented in the platform-specific result adapter
286 }
287
288 // --------------------------------------------------------------------
289
290 /**
291 * Field data
292 *
293 * Generates an array of objects containing field meta-data
294 *
295 * @access public
296 * @return array
297 */
298 function field_data()
299 {
300 // Implemented in the platform-specific result adapter
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Free the result
307 *
308 * @return null
309 */
310 function free_result()
311 {
312 // Implemented in the platform-specific result adapter
313 }
314
315
admin7b613c72006-09-24 18:05:17 +0000316}
317
318?>