blob: e83228386cdfdd2da02f09f2b92e8700dbb4ea17 [file] [log] [blame]
Razican114ab092011-04-25 17:26:45 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, 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
Razican114ab092011-04-25 17:26:45 +020031 var $conn_id = NULL;
32 var $result_id = NULL;
33 var $result_array = array();
34 var $result_object = array();
Greg Akere70e92b2011-04-25 10:50:53 -050035 var $custom_result_object = array();
Razican114ab092011-04-25 17:26:45 +020036 var $current_row = 0;
37 var $num_rows = 0;
38 var $row_data = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +000039
40
41 /**
Razican114ab092011-04-25 17:26:45 +020042 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +000043 *
44 * @access public
45 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +020046 * @return mixed either a result object or array
47 */
Derek Allard2067d1a2008-11-13 22:59:24 +000048 function result($type = 'object')
Barry Mienydd671972010-10-04 16:33:58 +020049 {
Greg Akere70e92b2011-04-25 10:50:53 -050050 if ($type == 'array') return $this->result_array();
51 else if ($type == 'object') return $this->result_object();
52 else return $this->custom_result_object($type);
Derek Allard2067d1a2008-11-13 22:59:24 +000053 }
54
55 // --------------------------------------------------------------------
56
Greg Akere70e92b2011-04-25 10:50:53 -050057 /**
58 * Custom query result.
59 *
60 * @param class_name A string that represents the type of object you want back
61 * @return array of objects
62 */
63 function custom_result_object($class_name)
64 {
65 if (array_key_exists($class_name, $this->custom_result_object))
66 {
67 return $this->custom_result_object[$class_name];
68 }
John Crepezzi7b474302011-01-15 18:17:01 -050069
Greg Akere70e92b2011-04-25 10:50:53 -050070 if ($this->result_id === FALSE OR $this->num_rows() == 0)
71 {
72 return array();
73 }
Razican114ab092011-04-25 17:26:45 +020074
Greg Akere70e92b2011-04-25 10:50:53 -050075 // add the data to the object
76 $this->_data_seek(0);
77 $result_object = array();
78
John Crepezzi7b474302011-01-15 18:17:01 -050079 while ($row = $this->_fetch_object())
Greg Akere70e92b2011-04-25 10:50:53 -050080 {
81 $object = new $class_name();
82
83 foreach ($row as $key => $value)
84 {
85 $object->$key = $value;
86 }
87
John Crepezzi7b474302011-01-15 18:17:01 -050088 $result_object[] = $object;
89 }
90
Greg Akere70e92b2011-04-25 10:50:53 -050091 // return the array
92 return $this->custom_result_object[$class_name] = $result_object;
93 }
94
95 // --------------------------------------------------------------------
John Crepezzi7b474302011-01-15 18:17:01 -050096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 /**
Razican114ab092011-04-25 17:26:45 +020098 * Query result. "object" version.
Derek Allard2067d1a2008-11-13 22:59:24 +000099 *
100 * @access public
101 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 function result_object()
104 {
105 if (count($this->result_object) > 0)
106 {
107 return $this->result_object;
108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
110 // In the event that query caching is on the result_id variable
111 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // we'll simply return an empty array.
113 if ($this->result_id === FALSE OR $this->num_rows() == 0)
114 {
115 return array();
116 }
117
118 $this->_data_seek(0);
119 while ($row = $this->_fetch_object())
120 {
121 $this->result_object[] = $row;
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 return $this->result_object;
125 }
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // --------------------------------------------------------------------
128
129 /**
Razican114ab092011-04-25 17:26:45 +0200130 * Query result. "array" version.
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 *
132 * @access public
133 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200134 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 function result_array()
136 {
137 if (count($this->result_array) > 0)
138 {
139 return $this->result_array;
140 }
141
Barry Mienydd671972010-10-04 16:33:58 +0200142 // In the event that query caching is on the result_id variable
143 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // we'll simply return an empty array.
145 if ($this->result_id === FALSE OR $this->num_rows() == 0)
146 {
147 return array();
148 }
149
150 $this->_data_seek(0);
151 while ($row = $this->_fetch_assoc())
152 {
153 $this->result_array[] = $row;
154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 return $this->result_array;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
Razican114ab092011-04-25 17:26:45 +0200162 * Query result. Acts as a wrapper function for the following functions.
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 *
164 * @access public
165 * @param string
166 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +0200167 * @return mixed either a result object or array
168 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 function row($n = 0, $type = 'object')
170 {
171 if ( ! is_numeric($n))
172 {
173 // We cache the row data for subsequent uses
174 if ( ! is_array($this->row_data))
175 {
176 $this->row_data = $this->row_array(0);
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 // array_key_exists() instead of isset() to allow for MySQL NULL values
180 if (array_key_exists($n, $this->row_data))
181 {
182 return $this->row_data[$n];
183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184 // reset the $n variable if the result was not achieved
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 $n = 0;
186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Greg Akere70e92b2011-04-25 10:50:53 -0500188 if ($type == 'object') return $this->row_object($n);
189 else if ($type == 'array') return $this->row_array($n);
190 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Assigns an item into a particular column slot
197 *
198 * @access public
199 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200200 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 function set_row($key, $value = NULL)
202 {
203 // We cache the row data for subsequent uses
204 if ( ! is_array($this->row_data))
205 {
206 $this->row_data = $this->row_array(0);
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 if (is_array($key))
210 {
211 foreach ($key as $k => $v)
212 {
213 $this->row_data[$k] = $v;
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 return;
217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 if ($key != '' AND ! is_null($value))
220 {
221 $this->row_data[$key] = $value;
222 }
223 }
224
225 // --------------------------------------------------------------------
226
Greg Akere70e92b2011-04-25 10:50:53 -0500227 /**
John Crepezzi7b474302011-01-15 18:17:01 -0500228 * Returns a single result row - custom object version
229 *
230 * @access public
231 * @return object
232 */
233 function custom_row_object($n, $type)
234 {
235 $result = $this->custom_result_object($type);
236
237 if (count($result) == 0)
238 {
239 return $result;
240 }
241
242 if ($n != $this->current_row AND isset($result[$n]))
243 {
244 $this->current_row = $n;
245 }
246
247 return $result[$this->current_row];
248 }
249
Greg Akere70e92b2011-04-25 10:50:53 -0500250 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 * Returns a single result row - object version
252 *
253 * @access public
254 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200255 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 function row_object($n = 0)
257 {
258 $result = $this->result_object();
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 if (count($result) == 0)
261 {
262 return $result;
263 }
264
265 if ($n != $this->current_row AND isset($result[$n]))
266 {
267 $this->current_row = $n;
268 }
269
270 return $result[$this->current_row];
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Returns a single result row - array version
277 *
278 * @access public
279 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200280 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 function row_array($n = 0)
282 {
283 $result = $this->result_array();
284
285 if (count($result) == 0)
286 {
287 return $result;
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 if ($n != $this->current_row AND isset($result[$n]))
291 {
292 $this->current_row = $n;
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 return $result[$this->current_row];
296 }
297
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // --------------------------------------------------------------------
300
301 /**
302 * Returns the "first" row
303 *
304 * @access public
305 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200306 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 function first_row($type = 'object')
308 {
309 $result = $this->result($type);
310
311 if (count($result) == 0)
312 {
313 return $result;
314 }
315 return $result[0];
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
319
320 /**
321 * Returns the "last" row
322 *
323 * @access public
324 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200325 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 function last_row($type = 'object')
327 {
328 $result = $this->result($type);
329
330 if (count($result) == 0)
331 {
332 return $result;
333 }
334 return $result[count($result) -1];
Barry Mienydd671972010-10-04 16:33:58 +0200335 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
337 // --------------------------------------------------------------------
338
339 /**
340 * Returns the "next" row
341 *
342 * @access public
343 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200344 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 function next_row($type = 'object')
346 {
347 $result = $this->result($type);
348
349 if (count($result) == 0)
350 {
351 return $result;
352 }
353
354 if (isset($result[$this->current_row + 1]))
355 {
356 ++$this->current_row;
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 return $result[$this->current_row];
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // --------------------------------------------------------------------
363
364 /**
365 * Returns the "previous" row
366 *
367 * @access public
368 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200369 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 function previous_row($type = 'object')
371 {
372 $result = $this->result($type);
373
374 if (count($result) == 0)
375 {
376 return $result;
377 }
378
379 if (isset($result[$this->current_row - 1]))
380 {
381 --$this->current_row;
382 }
383 return $result[$this->current_row];
384 }
385
386 // --------------------------------------------------------------------
387
388 /**
389 * The following functions are normally overloaded by the identically named
390 * methods in the platform-specific driver -- except when query caching
Razican114ab092011-04-25 17:26:45 +0200391 * is used. When caching is enabled we do not load the other driver.
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * These functions are primarily here to prevent undefined function errors
Razican114ab092011-04-25 17:26:45 +0200393 * when a cached result object is in use. They are not otherwise fully
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * operational due to the unavailability of the database resource IDs with
395 * cached results.
396 */
397 function num_rows() { return $this->num_rows; }
398 function num_fields() { return 0; }
399 function list_fields() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200400 function field_data() { return array(); }
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 function free_result() { return TRUE; }
402 function _data_seek() { return TRUE; }
Barry Mienydd671972010-10-04 16:33:58 +0200403 function _fetch_assoc() { return array(); }
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406}
407// END DB_result class
408
409/* End of file DB_result.php */
John Crepezzi7b474302011-01-15 18:17:01 -0500410/* Location: ./system/database/DB_result.php */