blob: 1c8ad6be0e72a30503c2e33942ac839fdd56ee1d [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
admin7da94762006-10-05 22:18:31 +000031 var $conn_id = NULL;
32 var $result_id = NULL;
admin7b613c72006-09-24 18:05:17 +000033 var $result_array = array();
34 var $result_object = array();
35 var $current_row = 0;
adminaf644692006-10-05 04:22:41 +000036 var $num_rows = 0;
admin7b613c72006-09-24 18:05:17 +000037
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')
adminaf644692006-10-05 04:22:41 +000047 {
admin7b613c72006-09-24 18:05:17 +000048 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 }
adminaf644692006-10-05 04:22:41 +000065
66 $this->_data_seek(0);
admin7b613c72006-09-24 18:05:17 +000067 while ($row = $this->_fetch_object())
adminaf644692006-10-05 04:22:41 +000068 {
admin7b613c72006-09-24 18:05:17 +000069 $this->result_object[] = $row;
70 }
71
admin7b613c72006-09-24 18:05:17 +000072 return $this->result_object;
73 }
adminaf644692006-10-05 04:22:41 +000074
admin7b613c72006-09-24 18:05:17 +000075 // --------------------------------------------------------------------
76
77 /**
78 * Query result. "array" version.
79 *
80 * @access public
81 * @return array
82 */
83 function result_array()
84 {
85 if (count($this->result_array) > 0)
86 {
87 return $this->result_array;
88 }
adminaf644692006-10-05 04:22:41 +000089
90 $this->_data_seek(0);
admin7b613c72006-09-24 18:05:17 +000091 while ($row = $this->_fetch_assoc())
92 {
93 $this->result_array[] = $row;
94 }
95
admin7b613c72006-09-24 18:05:17 +000096 return $this->result_array;
97 }
98
99 // --------------------------------------------------------------------
100
101 /**
102 * Query result. Acts as a wrapper function for the following functions.
103 *
104 * @access public
105 * @param string can be "object" or "array"
106 * @return mixed either a result object or array
107 */
108 function row($n = 0, $type = 'object')
109 {
110 return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Returns a single result row - object version
117 *
118 * @access public
119 * @return object
120 */
121 function row_object($n = 0)
122 {
adminaf644692006-10-05 04:22:41 +0000123 $result = $this->result_object();
124
125 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000126 {
adminaf644692006-10-05 04:22:41 +0000127 return $result;
admin7b613c72006-09-24 18:05:17 +0000128 }
adminaf644692006-10-05 04:22:41 +0000129
admin7b613c72006-09-24 18:05:17 +0000130 if ($n != $this->current_row AND isset($result[$n]))
131 {
132 $this->current_row = $n;
133 }
adminaf644692006-10-05 04:22:41 +0000134
admin7b613c72006-09-24 18:05:17 +0000135 return $result[$this->current_row];
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Returns a single result row - array version
142 *
143 * @access public
144 * @return array
145 */
146 function row_array($n = 0)
147 {
adminaf644692006-10-05 04:22:41 +0000148 $result = $this->result_array();
149
150 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000151 {
adminaf644692006-10-05 04:22:41 +0000152 return $result;
admin7b613c72006-09-24 18:05:17 +0000153 }
154
155 if ($n != $this->current_row AND isset($result[$n]))
156 {
157 $this->current_row = $n;
158 }
159
160 return $result[$this->current_row];
161 }
162
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Returns the "first" row
168 *
169 * @access public
170 * @return object
171 */
172 function first_row($type = 'object')
173 {
adminaf644692006-10-05 04:22:41 +0000174 $result = $this->result($type);
175
176 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000177 {
adminaf644692006-10-05 04:22:41 +0000178 return $result;
admin7b613c72006-09-24 18:05:17 +0000179 }
180 return $result[0];
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Returns the "last" row
187 *
188 * @access public
189 * @return object
190 */
191 function last_row($type = 'object')
192 {
adminaf644692006-10-05 04:22:41 +0000193 $result = $this->result($type);
194
195 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000196 {
adminaf644692006-10-05 04:22:41 +0000197 return $result;
admin7b613c72006-09-24 18:05:17 +0000198 }
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 {
adminaf644692006-10-05 04:22:41 +0000212 $result = $this->result($type);
213
214 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000215 {
adminaf644692006-10-05 04:22:41 +0000216 return $result;
admin7b613c72006-09-24 18:05:17 +0000217 }
218
219 if (isset($result[$this->current_row + 1]))
220 {
221 ++$this->current_row;
222 }
223
224 return $result[$this->current_row];
225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Returns the "previous" row
231 *
232 * @access public
233 * @return object
234 */
235 function previous_row($type = 'object')
236 {
adminaf644692006-10-05 04:22:41 +0000237 $result = $this->result($type);
238
239 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000240 {
adminaf644692006-10-05 04:22:41 +0000241 return $result;
admin7b613c72006-09-24 18:05:17 +0000242 }
243
244 if (isset($result[$this->current_row - 1]))
245 {
246 --$this->current_row;
247 }
248 return $result[$this->current_row];
249 }
250
admine1063182006-09-25 22:12:16 +0000251 // --------------------------------------------------------------------
252
253 /**
admin7da94762006-10-05 22:18:31 +0000254 * The following functions are normally overloaded by the identically named
255 * methods in the platform-specific driver -- except when query caching
admin386ec182006-10-05 21:52:20 +0000256 * is used. When caching is enabled we do not load the other driver.
admin7da94762006-10-05 22:18:31 +0000257 * These functions are primarily here to prevent undefined function errors
258 * when a cached result object is in use. They are not otherwise fully
admin10c3f412006-10-08 07:21:12 +0000259 * operational due to the unavailability of the database resource IDs with
admin7da94762006-10-05 22:18:31 +0000260 * cached results.
admine1063182006-09-25 22:12:16 +0000261 */
admin7da94762006-10-05 22:18:31 +0000262 function num_rows() { return $this->num_rows; }
263 function num_fields() { return 0; }
264 function field_names() { return array(); }
265 function field_data() { return array(); }
266 function free_result() { return TRUE; }
267 function _data_seek() { return TRUE; }
268 function _fetch_assoc() { return array(); }
269 function _fetch_object() { return array(); }
admine1063182006-09-25 22:12:16 +0000270
admine1063182006-09-25 22:12:16 +0000271
admin7b613c72006-09-24 18:05:17 +0000272}
admin7b9d4722006-10-05 04:46:47 +0000273// END DB_result class
admin7b613c72006-09-24 18:05:17 +0000274?>