blob: 4e8c4fc7601a72a84bf221d18845dbb92e704fe1 [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
admin31eeb052006-10-20 05:11:33 +000066 if ($this->num_rows == 0)
67 {
68 return array();
69 }
70
adminaf644692006-10-05 04:22:41 +000071 $this->_data_seek(0);
admin7b613c72006-09-24 18:05:17 +000072 while ($row = $this->_fetch_object())
adminaf644692006-10-05 04:22:41 +000073 {
admin7b613c72006-09-24 18:05:17 +000074 $this->result_object[] = $row;
75 }
76
admin7b613c72006-09-24 18:05:17 +000077 return $this->result_object;
78 }
adminaf644692006-10-05 04:22:41 +000079
admin7b613c72006-09-24 18:05:17 +000080 // --------------------------------------------------------------------
81
82 /**
83 * Query result. "array" version.
84 *
85 * @access public
86 * @return array
87 */
88 function result_array()
89 {
90 if (count($this->result_array) > 0)
91 {
92 return $this->result_array;
93 }
adminaf644692006-10-05 04:22:41 +000094
admin31eeb052006-10-20 05:11:33 +000095 if ($this->num_rows == 0)
96 {
97 return array();
98 }
99
adminaf644692006-10-05 04:22:41 +0000100 $this->_data_seek(0);
admin7b613c72006-09-24 18:05:17 +0000101 while ($row = $this->_fetch_assoc())
102 {
103 $this->result_array[] = $row;
104 }
105
admin7b613c72006-09-24 18:05:17 +0000106 return $this->result_array;
107 }
108
109 // --------------------------------------------------------------------
110
111 /**
112 * Query result. Acts as a wrapper function for the following functions.
113 *
114 * @access public
115 * @param string can be "object" or "array"
116 * @return mixed either a result object or array
117 */
118 function row($n = 0, $type = 'object')
119 {
120 return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Returns a single result row - object version
127 *
128 * @access public
129 * @return object
130 */
131 function row_object($n = 0)
132 {
adminaf644692006-10-05 04:22:41 +0000133 $result = $this->result_object();
134
135 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000136 {
adminaf644692006-10-05 04:22:41 +0000137 return $result;
admin7b613c72006-09-24 18:05:17 +0000138 }
adminaf644692006-10-05 04:22:41 +0000139
admin7b613c72006-09-24 18:05:17 +0000140 if ($n != $this->current_row AND isset($result[$n]))
141 {
142 $this->current_row = $n;
143 }
adminaf644692006-10-05 04:22:41 +0000144
admin7b613c72006-09-24 18:05:17 +0000145 return $result[$this->current_row];
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Returns a single result row - array version
152 *
153 * @access public
154 * @return array
155 */
156 function row_array($n = 0)
157 {
adminaf644692006-10-05 04:22:41 +0000158 $result = $this->result_array();
159
160 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000161 {
adminaf644692006-10-05 04:22:41 +0000162 return $result;
admin7b613c72006-09-24 18:05:17 +0000163 }
164
165 if ($n != $this->current_row AND isset($result[$n]))
166 {
167 $this->current_row = $n;
168 }
169
170 return $result[$this->current_row];
171 }
172
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Returns the "first" row
178 *
179 * @access public
180 * @return object
181 */
182 function first_row($type = 'object')
183 {
adminaf644692006-10-05 04:22:41 +0000184 $result = $this->result($type);
185
186 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000187 {
adminaf644692006-10-05 04:22:41 +0000188 return $result;
admin7b613c72006-09-24 18:05:17 +0000189 }
190 return $result[0];
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Returns the "last" row
197 *
198 * @access public
199 * @return object
200 */
201 function last_row($type = 'object')
202 {
adminaf644692006-10-05 04:22:41 +0000203 $result = $this->result($type);
204
205 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000206 {
adminaf644692006-10-05 04:22:41 +0000207 return $result;
admin7b613c72006-09-24 18:05:17 +0000208 }
209 return $result[count($result) -1];
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Returns the "next" row
216 *
217 * @access public
218 * @return object
219 */
220 function next_row($type = 'object')
221 {
adminaf644692006-10-05 04:22:41 +0000222 $result = $this->result($type);
223
224 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000225 {
adminaf644692006-10-05 04:22:41 +0000226 return $result;
admin7b613c72006-09-24 18:05:17 +0000227 }
228
229 if (isset($result[$this->current_row + 1]))
230 {
231 ++$this->current_row;
232 }
233
234 return $result[$this->current_row];
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Returns the "previous" row
241 *
242 * @access public
243 * @return object
244 */
245 function previous_row($type = 'object')
246 {
adminaf644692006-10-05 04:22:41 +0000247 $result = $this->result($type);
248
249 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000250 {
adminaf644692006-10-05 04:22:41 +0000251 return $result;
admin7b613c72006-09-24 18:05:17 +0000252 }
253
254 if (isset($result[$this->current_row - 1]))
255 {
256 --$this->current_row;
257 }
258 return $result[$this->current_row];
259 }
260
admine1063182006-09-25 22:12:16 +0000261 // --------------------------------------------------------------------
262
263 /**
admin7da94762006-10-05 22:18:31 +0000264 * The following functions are normally overloaded by the identically named
265 * methods in the platform-specific driver -- except when query caching
admin386ec182006-10-05 21:52:20 +0000266 * is used. When caching is enabled we do not load the other driver.
admin7da94762006-10-05 22:18:31 +0000267 * These functions are primarily here to prevent undefined function errors
268 * when a cached result object is in use. They are not otherwise fully
admin10c3f412006-10-08 07:21:12 +0000269 * operational due to the unavailability of the database resource IDs with
admin7da94762006-10-05 22:18:31 +0000270 * cached results.
admine1063182006-09-25 22:12:16 +0000271 */
admin7da94762006-10-05 22:18:31 +0000272 function num_rows() { return $this->num_rows; }
273 function num_fields() { return 0; }
admin606f99c2006-10-11 23:48:41 +0000274 function list_fields() { return array(); }
275 function field_names() { return array(); } // Deprecated
admin7da94762006-10-05 22:18:31 +0000276 function field_data() { return array(); }
277 function free_result() { return TRUE; }
278 function _data_seek() { return TRUE; }
279 function _fetch_assoc() { return array(); }
280 function _fetch_object() { return array(); }
admin606f99c2006-10-11 23:48:41 +0000281
admin7b613c72006-09-24 18:05:17 +0000282}
admin7b9d4722006-10-05 04:46:47 +0000283// END DB_result class
admin7b613c72006-09-24 18:05:17 +0000284?>