blob: 44f166d218d5c977b75abb60ae32d15c29dee2bd [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
admin7b613c72006-09-24 18:05:17 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
admin7b613c72006-09-24 18:05:17 +000016// ------------------------------------------------------------------------
17
18/**
19 * Database Result Class
admine334c472006-10-21 19:44:22 +000020 *
admin7b613c72006-09-24 18:05:17 +000021 * 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.
admine334c472006-10-21 19:44:22 +000041 *
admin7b613c72006-09-24 18:05:17 +000042 * @access public
43 * @param string can be "object" or "array"
admine334c472006-10-21 19:44:22 +000044 * @return mixed either a result object or array
admin7b613c72006-09-24 18:05:17 +000045 */
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.
admine334c472006-10-21 19:44:22 +000055 *
admin7b613c72006-09-24 18:05:17 +000056 * @access public
admine334c472006-10-21 19:44:22 +000057 * @return object
admin7b613c72006-09-24 18:05:17 +000058 */
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
adminfafe28b2006-10-21 19:08:17 +000066 if ($this->numerous == 0)
admin31eeb052006-10-20 05:11:33 +000067 {
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())
admine334c472006-10-21 19:44:22 +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.
admine334c472006-10-21 19:44:22 +000084 *
admin7b613c72006-09-24 18:05:17 +000085 * @access public
admine334c472006-10-21 19:44:22 +000086 * @return array
admin7b613c72006-09-24 18:05:17 +000087 */
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.
admine334c472006-10-21 19:44:22 +0000113 *
admin7b613c72006-09-24 18:05:17 +0000114 * @access public
115 * @param string can be "object" or "array"
admine334c472006-10-21 19:44:22 +0000116 * @return mixed either a result object or array
admin7b613c72006-09-24 18:05:17 +0000117 */
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
admine334c472006-10-21 19:44:22 +0000127 *
admin7b613c72006-09-24 18:05:17 +0000128 * @access public
admine334c472006-10-21 19:44:22 +0000129 * @return object
admin7b613c72006-09-24 18:05:17 +0000130 */
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
admine334c472006-10-21 19:44:22 +0000152 *
admin7b613c72006-09-24 18:05:17 +0000153 * @access public
admine334c472006-10-21 19:44:22 +0000154 * @return array
admin7b613c72006-09-24 18:05:17 +0000155 */
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
admine334c472006-10-21 19:44:22 +0000178 *
admin7b613c72006-09-24 18:05:17 +0000179 * @access public
admine334c472006-10-21 19:44:22 +0000180 * @return object
admin7b613c72006-09-24 18:05:17 +0000181 */
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
admine334c472006-10-21 19:44:22 +0000197 *
admin7b613c72006-09-24 18:05:17 +0000198 * @access public
admine334c472006-10-21 19:44:22 +0000199 * @return object
admin7b613c72006-09-24 18:05:17 +0000200 */
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
admine334c472006-10-21 19:44:22 +0000216 *
admin7b613c72006-09-24 18:05:17 +0000217 * @access public
admine334c472006-10-21 19:44:22 +0000218 * @return object
admin7b613c72006-09-24 18:05:17 +0000219 */
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
admine334c472006-10-21 19:44:22 +0000241 *
admin7b613c72006-09-24 18:05:17 +0000242 * @access public
admine334c472006-10-21 19:44:22 +0000243 * @return object
admin7b613c72006-09-24 18:05:17 +0000244 */
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 /**
admine334c472006-10-21 19:44:22 +0000264 * The following functions are normally overloaded by the identically named
admin7da94762006-10-05 22:18:31 +0000265 * 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
admine334c472006-10-21 19:44:22 +0000268 * 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?>