blob: 7d85ebb928009e685f3a4789907255af47435455 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
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, EllisLab, 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 = NULL;
32 var $result_id = NULL;
33 var $result_array = array();
34 var $result_object = array();
35 var $current_row = 0;
36 var $num_rows = 0;
37
38
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')
47 {
48 return ($type == 'object') ? $this->result_object() : $this->result_array();
49 }
50
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 }
65
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.
69 if ($this->result_id === FALSE OR $this->num_rows() == 0)
70 {
71 return array();
72 }
73
74 $this->_data_seek(0);
75 while ($row = $this->_fetch_object())
76 {
77 $this->result_object[] = $row;
78 }
79
80 return $this->result_object;
81 }
82
83 // --------------------------------------------------------------------
84
85 /**
86 * Query result. "array" version.
87 *
88 * @access public
89 * @return array
90 */
91 function result_array()
92 {
93 if (count($this->result_array) > 0)
94 {
95 return $this->result_array;
96 }
97
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.
101 if ($this->result_id === FALSE OR $this->num_rows() == 0)
102 {
103 return array();
104 }
105
106 $this->_data_seek(0);
107 while ($row = $this->_fetch_assoc())
108 {
109 $this->result_array[] = $row;
110 }
111
112 return $this->result_array;
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Query result. Acts as a wrapper function for the following functions.
119 *
120 * @access public
121 * @param string can be "object" or "array"
122 * @return mixed either a result object or array
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
133 *
134 * @access public
135 * @return object
136 */
137 function row_object($n = 0)
138 {
139 $result = $this->result_object();
140
141 if (count($result) == 0)
142 {
143 return $result;
144 }
145
146 if ($n != $this->current_row AND isset($result[$n]))
147 {
148 $this->current_row = $n;
149 }
150
151 return $result[$this->current_row];
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Returns a single result row - array version
158 *
159 * @access public
160 * @return array
161 */
162 function row_array($n = 0)
163 {
164 $result = $this->result_array();
165
166 if (count($result) == 0)
167 {
168 return $result;
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
184 *
185 * @access public
186 * @return object
187 */
188 function first_row($type = 'object')
189 {
190 $result = $this->result($type);
191
192 if (count($result) == 0)
193 {
194 return $result;
195 }
196 return $result[0];
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * Returns the "last" row
203 *
204 * @access public
205 * @return object
206 */
207 function last_row($type = 'object')
208 {
209 $result = $this->result($type);
210
211 if (count($result) == 0)
212 {
213 return $result;
214 }
215 return $result[count($result) -1];
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Returns the "next" row
222 *
223 * @access public
224 * @return object
225 */
226 function next_row($type = 'object')
227 {
228 $result = $this->result($type);
229
230 if (count($result) == 0)
231 {
232 return $result;
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
247 *
248 * @access public
249 * @return object
250 */
251 function previous_row($type = 'object')
252 {
253 $result = $this->result($type);
254
255 if (count($result) == 0)
256 {
257 return $result;
258 }
259
260 if (isset($result[$this->current_row - 1]))
261 {
262 --$this->current_row;
263 }
264 return $result[$this->current_row];
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * The following functions are normally overloaded by the identically named
271 * methods in the platform-specific driver -- except when query caching
272 * is used. When caching is enabled we do not load the other driver.
273 * These functions are primarily here to prevent undefined function errors
274 * when a cached result object is in use. They are not otherwise fully
275 * operational due to the unavailability of the database resource IDs with
276 * cached results.
277 */
278 function num_rows() { return $this->num_rows; }
279 function num_fields() { return 0; }
280 function list_fields() { return array(); }
281 function field_names() { return array(); } // Deprecated
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(); }
287
288}
289// END DB_result class
admin7b613c72006-09-24 18:05:17 +0000290?>