blob: 0cb89b91ab68a78a73f2b5dd65b312a1bc314369 [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 *
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
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
John Crepezzi7b474302011-01-15 18:17:01 -050031 var $conn_id = NULL;
32 var $result_id = NULL;
33 var $result_array = array();
34 var $result_object = array();
35 var $custom_result_object = array();
36 var $current_row = 0;
37 var $num_rows = 0;
38 var $row_data = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +000039
40
41 /**
42 * Query result. Acts as a wrapper function for the following functions.
43 *
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 {
John Crepezzi7b474302011-01-15 18:17:01 -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
John Crepezzi7b474302011-01-15 18:17:01 -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 }
69
70 if ($this->result_id === FALSE OR $this->num_rows() == 0)
71 {
72 return array();
73 }
74
75 // add the data to the object
76 $this->_data_seek(0);
77 $result_object = array();
78 while ($row = $this->_fetch_object())
79 {
80 $object = new $class_name();
81 foreach($row as $key => $value)
82 {
83 $object->$key = $value;
84 }
85 $result_object[] = $object;
86 }
87
88 // return the array
89 return $this->custom_result_object[$class_name] = $result_object;
90 }
91
Derek Allard2067d1a2008-11-13 22:59:24 +000092 /**
93 * Query result. "object" version.
94 *
95 * @access public
96 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020097 */
Derek Allard2067d1a2008-11-13 22:59:24 +000098 function result_object()
99 {
100 if (count($this->result_object) > 0)
101 {
102 return $this->result_object;
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
105 // In the event that query caching is on the result_id variable
106 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // we'll simply return an empty array.
108 if ($this->result_id === FALSE OR $this->num_rows() == 0)
109 {
110 return array();
111 }
112
113 $this->_data_seek(0);
114 while ($row = $this->_fetch_object())
115 {
116 $this->result_object[] = $row;
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 return $this->result_object;
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // --------------------------------------------------------------------
123
124 /**
125 * Query result. "array" version.
126 *
127 * @access public
128 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200129 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 function result_array()
131 {
132 if (count($this->result_array) > 0)
133 {
134 return $this->result_array;
135 }
136
Barry Mienydd671972010-10-04 16:33:58 +0200137 // In the event that query caching is on the result_id variable
138 // will return FALSE since there isn't a valid SQL resource so
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 // we'll simply return an empty array.
140 if ($this->result_id === FALSE OR $this->num_rows() == 0)
141 {
142 return array();
143 }
144
145 $this->_data_seek(0);
146 while ($row = $this->_fetch_assoc())
147 {
148 $this->result_array[] = $row;
149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 return $this->result_array;
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Query result. Acts as a wrapper function for the following functions.
158 *
159 * @access public
160 * @param string
161 * @param string can be "object" or "array"
Barry Mienydd671972010-10-04 16:33:58 +0200162 * @return mixed either a result object or array
163 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 function row($n = 0, $type = 'object')
165 {
166 if ( ! is_numeric($n))
167 {
168 // We cache the row data for subsequent uses
169 if ( ! is_array($this->row_data))
170 {
171 $this->row_data = $this->row_array(0);
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // array_key_exists() instead of isset() to allow for MySQL NULL values
175 if (array_key_exists($n, $this->row_data))
176 {
177 return $this->row_data[$n];
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179 // reset the $n variable if the result was not achieved
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $n = 0;
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
John Crepezzi7b474302011-01-15 18:17:01 -0500183 if ($type == 'object') return $this->row_object($n);
184 else if ($type == 'array') return $this->row_array($n);
185 else return $this->custom_row_object($n, $type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Assigns an item into a particular column slot
192 *
193 * @access public
194 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200195 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 function set_row($key, $value = NULL)
197 {
198 // We cache the row data for subsequent uses
199 if ( ! is_array($this->row_data))
200 {
201 $this->row_data = $this->row_array(0);
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 if (is_array($key))
205 {
206 foreach ($key as $k => $v)
207 {
208 $this->row_data[$k] = $v;
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 return;
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 if ($key != '' AND ! is_null($value))
215 {
216 $this->row_data[$key] = $value;
217 }
218 }
219
220 // --------------------------------------------------------------------
221
John Crepezzi7b474302011-01-15 18:17:01 -0500222 /**
223 * Returns a single result row - custom object version
224 *
225 * @access public
226 * @return object
227 */
228 function custom_row_object($n, $type)
229 {
230 $result = $this->custom_result_object($type);
231
232 if (count($result) == 0)
233 {
234 return $result;
235 }
236
237 if ($n != $this->current_row AND isset($result[$n]))
238 {
239 $this->current_row = $n;
240 }
241
242 return $result[$this->current_row];
243 }
244
245 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * Returns a single result row - object version
247 *
248 * @access public
249 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200250 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 function row_object($n = 0)
252 {
253 $result = $this->result_object();
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 if (count($result) == 0)
256 {
257 return $result;
258 }
259
260 if ($n != $this->current_row AND isset($result[$n]))
261 {
262 $this->current_row = $n;
263 }
264
265 return $result[$this->current_row];
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
271 * Returns a single result row - array version
272 *
273 * @access public
274 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200275 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 function row_array($n = 0)
277 {
278 $result = $this->result_array();
279
280 if (count($result) == 0)
281 {
282 return $result;
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 if ($n != $this->current_row AND isset($result[$n]))
286 {
287 $this->current_row = $n;
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 return $result[$this->current_row];
291 }
292
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 // --------------------------------------------------------------------
295
296 /**
297 * Returns the "first" 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 first_row($type = 'object')
303 {
304 $result = $this->result($type);
305
306 if (count($result) == 0)
307 {
308 return $result;
309 }
310 return $result[0];
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
314
315 /**
316 * Returns the "last" row
317 *
318 * @access public
319 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200320 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 function last_row($type = 'object')
322 {
323 $result = $this->result($type);
324
325 if (count($result) == 0)
326 {
327 return $result;
328 }
329 return $result[count($result) -1];
Barry Mienydd671972010-10-04 16:33:58 +0200330 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
332 // --------------------------------------------------------------------
333
334 /**
335 * Returns the "next" row
336 *
337 * @access public
338 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 function next_row($type = 'object')
341 {
342 $result = $this->result($type);
343
344 if (count($result) == 0)
345 {
346 return $result;
347 }
348
349 if (isset($result[$this->current_row + 1]))
350 {
351 ++$this->current_row;
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 return $result[$this->current_row];
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // --------------------------------------------------------------------
358
359 /**
360 * Returns the "previous" row
361 *
362 * @access public
363 * @return object
Barry Mienydd671972010-10-04 16:33:58 +0200364 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 function previous_row($type = 'object')
366 {
367 $result = $this->result($type);
368
369 if (count($result) == 0)
370 {
371 return $result;
372 }
373
374 if (isset($result[$this->current_row - 1]))
375 {
376 --$this->current_row;
377 }
378 return $result[$this->current_row];
379 }
380
381 // --------------------------------------------------------------------
382
383 /**
384 * The following functions are normally overloaded by the identically named
385 * methods in the platform-specific driver -- except when query caching
386 * is used. When caching is enabled we do not load the other driver.
387 * These functions are primarily here to prevent undefined function errors
388 * when a cached result object is in use. They are not otherwise fully
389 * operational due to the unavailability of the database resource IDs with
390 * cached results.
391 */
392 function num_rows() { return $this->num_rows; }
393 function num_fields() { return 0; }
394 function list_fields() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200395 function field_data() { return array(); }
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 function free_result() { return TRUE; }
397 function _data_seek() { return TRUE; }
Barry Mienydd671972010-10-04 16:33:58 +0200398 function _fetch_assoc() { return array(); }
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 function _fetch_object() { return array(); }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401}
402// END DB_result class
403
404/* End of file DB_result.php */
John Crepezzi7b474302011-01-15 18:17:01 -0500405/* Location: ./system/database/DB_result.php */