blob: 406afb1b8c710e0748579d031a0659aab8741bf5 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +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 ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://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 ExpressionEngine Dev Team
27 * @link http://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();
Barry Mienydd671972010-10-04 16:33:58 +020035 var $current_row = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +000036 var $num_rows = 0;
37 var $row_data = NULL;
38
39
40 /**
41 * Query result. Acts as a wrapper function for the following functions.
42 *
43 * @access public
44 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +020045 * @return mixed either a result object or array
46 */
Derek Allard2067d1a2008-11-13 22:59:24 +000047 function result($type = 'object')
Barry Mienydd671972010-10-04 16:33:58 +020048 {
Derek Allard2067d1a2008-11-13 22:59:24 +000049 return ($type == 'object') ? $this->result_object() : $this->result_array();
50 }
51
52 // --------------------------------------------------------------------
53
54 /**
55 * Query result. "object" version.
56 *
57 * @access public
58 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020059 */
Derek Allard2067d1a2008-11-13 22:59:24 +000060 function result_object()
61 {
62 if (count($this->result_object) > 0)
63 {
64 return $this->result_object;
65 }
Barry Mienydd671972010-10-04 16:33:58 +020066
67 // In the event that query caching is on the result_id variable
68 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +000069 // we'll simply return an empty array.
70 if ($this->result_id === FALSE OR $this->num_rows() == 0)
71 {
72 return array();
73 }
74
75 $this->_data_seek(0);
76 while ($row = $this->_fetch_object())
77 {
78 $this->result_object[] = $row;
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 return $this->result_object;
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 // --------------------------------------------------------------------
85
86 /**
87 * Query result. "array" version.
88 *
89 * @access public
90 * @return array
Barry Mienydd671972010-10-04 16:33:58 +020091 */
Derek Allard2067d1a2008-11-13 22:59:24 +000092 function result_array()
93 {
94 if (count($this->result_array) > 0)
95 {
96 return $this->result_array;
97 }
98
Barry Mienydd671972010-10-04 16:33:58 +020099 // In the event that query caching is on the result_id variable
100 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 // we'll simply return an empty array.
102 if ($this->result_id === FALSE OR $this->num_rows() == 0)
103 {
104 return array();
105 }
106
107 $this->_data_seek(0);
108 while ($row = $this->_fetch_assoc())
109 {
110 $this->result_array[] = $row;
111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return $this->result_array;
114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Query result. Acts as a wrapper function for the following functions.
120 *
121 * @access public
122 * @param string
123 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +0200124 * @return mixed either a result object or array
125 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 function row($n = 0, $type = 'object')
127 {
128 if ( ! is_numeric($n))
129 {
130 // We cache the row data for subsequent uses
131 if ( ! is_array($this->row_data))
132 {
133 $this->row_data = $this->row_array(0);
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // array_key_exists() instead of isset() to allow for MySQL NULL values
137 if (array_key_exists($n, $this->row_data))
138 {
139 return $this->row_data[$n];
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141 // reset the $n variable if the result was not achieved
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 $n = 0;
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Assigns an item into a particular column slot
152 *
153 * @access public
154 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 function set_row($key, $value = NULL)
157 {
158 // We cache the row data for subsequent uses
159 if ( ! is_array($this->row_data))
160 {
161 $this->row_data = $this->row_array(0);
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 if (is_array($key))
165 {
166 foreach ($key as $k => $v)
167 {
168 $this->row_data[$k] = $v;
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 return;
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 if ($key != '' AND ! is_null($value))
175 {
176 $this->row_data[$key] = $value;
177 }
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Returns a single result row - object version
184 *
185 * @access public
186 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200187 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 function row_object($n = 0)
189 {
190 $result = $this->result_object();
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 if (count($result) == 0)
193 {
194 return $result;
195 }
196
197 if ($n != $this->current_row AND isset($result[$n]))
198 {
199 $this->current_row = $n;
200 }
201
202 return $result[$this->current_row];
203 }
204
205 // --------------------------------------------------------------------
206
207 /**
208 * Returns a single result row - array version
209 *
210 * @access public
211 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200212 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 function row_array($n = 0)
214 {
215 $result = $this->result_array();
216
217 if (count($result) == 0)
218 {
219 return $result;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 if ($n != $this->current_row AND isset($result[$n]))
223 {
224 $this->current_row = $n;
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 return $result[$this->current_row];
228 }
229
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // --------------------------------------------------------------------
232
233 /**
234 * Returns the "first" row
235 *
236 * @access public
237 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200238 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 function first_row($type = 'object')
240 {
241 $result = $this->result($type);
242
243 if (count($result) == 0)
244 {
245 return $result;
246 }
247 return $result[0];
248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // --------------------------------------------------------------------
251
252 /**
253 * Returns the "last" row
254 *
255 * @access public
256 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200257 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 function last_row($type = 'object')
259 {
260 $result = $this->result($type);
261
262 if (count($result) == 0)
263 {
264 return $result;
265 }
266 return $result[count($result) -1];
Barry Mienydd671972010-10-04 16:33:58 +0200267 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000268
269 // --------------------------------------------------------------------
270
271 /**
272 * Returns the "next" row
273 *
274 * @access public
275 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200276 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 function next_row($type = 'object')
278 {
279 $result = $this->result($type);
280
281 if (count($result) == 0)
282 {
283 return $result;
284 }
285
286 if (isset($result[$this->current_row + 1]))
287 {
288 ++$this->current_row;
289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 return $result[$this->current_row];
292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 // --------------------------------------------------------------------
295
296 /**
297 * Returns the "previous" row
298 *
299 * @access public
300 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200301 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 function previous_row($type = 'object')
303 {
304 $result = $this->result($type);
305
306 if (count($result) == 0)
307 {
308 return $result;
309 }
310
311 if (isset($result[$this->current_row - 1]))
312 {
313 --$this->current_row;
314 }
315 return $result[$this->current_row];
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * The following functions are normally overloaded by the identically named
322 * methods in the platform-specific driver -- except when query caching
323 * is used. When caching is enabled we do not load the other driver.
324 * These functions are primarily here to prevent undefined function errors
325 * when a cached result object is in use. They are not otherwise fully
326 * operational due to the unavailability of the database resource IDs with
327 * cached results.
328 */
329 function num_rows() { return $this->num_rows; }
330 function num_fields() { return 0; }
331 function list_fields() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200332 function field_data() { return array(); }
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 function free_result() { return TRUE; }
334 function _data_seek() { return TRUE; }
Barry Mienydd671972010-10-04 16:33:58 +0200335 function _fetch_assoc() { return array(); }
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338}
339// END DB_result class
340
341/* End of file DB_result.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000342/* Location: ./system/database/DB_result.php */