blob: 9784561d40c9323c9776530875f764e9498f1023 [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
admin7591faf2006-10-22 03:13:22 +000066 // In the event that query caching is on the result_id variable
67 // will return FALSE since there isn't a valid SQL resource so
68 // we'll simply return an empty array.
admin1716cb82006-10-25 05:12:34 +000069 if ($this->result_id === FALSE OR $this->num_rows() == 0)
admin31eeb052006-10-20 05:11:33 +000070 {
71 return array();
72 }
admin1716cb82006-10-25 05:12:34 +000073
adminaf644692006-10-05 04:22:41 +000074 $this->_data_seek(0);
admin7b613c72006-09-24 18:05:17 +000075 while ($row = $this->_fetch_object())
admine334c472006-10-21 19:44:22 +000076 {
admin7b613c72006-09-24 18:05:17 +000077 $this->result_object[] = $row;
78 }
79
admin7b613c72006-09-24 18:05:17 +000080 return $this->result_object;
81 }
adminaf644692006-10-05 04:22:41 +000082
admin7b613c72006-09-24 18:05:17 +000083 // --------------------------------------------------------------------
84
85 /**
86 * Query result. "array" version.
admine334c472006-10-21 19:44:22 +000087 *
admin7b613c72006-09-24 18:05:17 +000088 * @access public
admine334c472006-10-21 19:44:22 +000089 * @return array
admin7b613c72006-09-24 18:05:17 +000090 */
91 function result_array()
92 {
93 if (count($this->result_array) > 0)
94 {
95 return $this->result_array;
96 }
adminaf644692006-10-05 04:22:41 +000097
admin7591faf2006-10-22 03:13:22 +000098 // In the event that query caching is on the result_id variable
99 // will return FALSE since there isn't a valid SQL resource so
100 // we'll simply return an empty array.
admin1716cb82006-10-25 05:12:34 +0000101 if ($this->result_id === FALSE OR $this->num_rows() == 0)
admin31eeb052006-10-20 05:11:33 +0000102 {
103 return array();
104 }
105
adminaf644692006-10-05 04:22:41 +0000106 $this->_data_seek(0);
admin7b613c72006-09-24 18:05:17 +0000107 while ($row = $this->_fetch_assoc())
108 {
109 $this->result_array[] = $row;
110 }
111
admin7b613c72006-09-24 18:05:17 +0000112 return $this->result_array;
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Query result. Acts as a wrapper function for the following functions.
admine334c472006-10-21 19:44:22 +0000119 *
admin7b613c72006-09-24 18:05:17 +0000120 * @access public
121 * @param string can be "object" or "array"
admine334c472006-10-21 19:44:22 +0000122 * @return mixed either a result object or array
admin7b613c72006-09-24 18:05:17 +0000123 */
124 function row($n = 0, $type = 'object')
125 {
126 return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Returns a single result row - object version
admine334c472006-10-21 19:44:22 +0000133 *
admin7b613c72006-09-24 18:05:17 +0000134 * @access public
admine334c472006-10-21 19:44:22 +0000135 * @return object
admin7b613c72006-09-24 18:05:17 +0000136 */
137 function row_object($n = 0)
138 {
adminaf644692006-10-05 04:22:41 +0000139 $result = $this->result_object();
140
141 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000142 {
adminaf644692006-10-05 04:22:41 +0000143 return $result;
admin7b613c72006-09-24 18:05:17 +0000144 }
adminaf644692006-10-05 04:22:41 +0000145
admin7b613c72006-09-24 18:05:17 +0000146 if ($n != $this->current_row AND isset($result[$n]))
147 {
148 $this->current_row = $n;
149 }
adminaf644692006-10-05 04:22:41 +0000150
admin7b613c72006-09-24 18:05:17 +0000151 return $result[$this->current_row];
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Returns a single result row - array version
admine334c472006-10-21 19:44:22 +0000158 *
admin7b613c72006-09-24 18:05:17 +0000159 * @access public
admine334c472006-10-21 19:44:22 +0000160 * @return array
admin7b613c72006-09-24 18:05:17 +0000161 */
162 function row_array($n = 0)
163 {
adminaf644692006-10-05 04:22:41 +0000164 $result = $this->result_array();
165
166 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000167 {
adminaf644692006-10-05 04:22:41 +0000168 return $result;
admin7b613c72006-09-24 18:05:17 +0000169 }
170
171 if ($n != $this->current_row AND isset($result[$n]))
172 {
173 $this->current_row = $n;
174 }
175
176 return $result[$this->current_row];
177 }
178
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Returns the "first" row
admine334c472006-10-21 19:44:22 +0000184 *
admin7b613c72006-09-24 18:05:17 +0000185 * @access public
admine334c472006-10-21 19:44:22 +0000186 * @return object
admin7b613c72006-09-24 18:05:17 +0000187 */
188 function first_row($type = 'object')
189 {
adminaf644692006-10-05 04:22:41 +0000190 $result = $this->result($type);
191
192 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000193 {
adminaf644692006-10-05 04:22:41 +0000194 return $result;
admin7b613c72006-09-24 18:05:17 +0000195 }
196 return $result[0];
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * Returns the "last" row
admine334c472006-10-21 19:44:22 +0000203 *
admin7b613c72006-09-24 18:05:17 +0000204 * @access public
admine334c472006-10-21 19:44:22 +0000205 * @return object
admin7b613c72006-09-24 18:05:17 +0000206 */
207 function last_row($type = 'object')
208 {
adminaf644692006-10-05 04:22:41 +0000209 $result = $this->result($type);
210
211 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000212 {
adminaf644692006-10-05 04:22:41 +0000213 return $result;
admin7b613c72006-09-24 18:05:17 +0000214 }
215 return $result[count($result) -1];
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Returns the "next" row
admine334c472006-10-21 19:44:22 +0000222 *
admin7b613c72006-09-24 18:05:17 +0000223 * @access public
admine334c472006-10-21 19:44:22 +0000224 * @return object
admin7b613c72006-09-24 18:05:17 +0000225 */
226 function next_row($type = 'object')
227 {
adminaf644692006-10-05 04:22:41 +0000228 $result = $this->result($type);
229
230 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000231 {
adminaf644692006-10-05 04:22:41 +0000232 return $result;
admin7b613c72006-09-24 18:05:17 +0000233 }
234
235 if (isset($result[$this->current_row + 1]))
236 {
237 ++$this->current_row;
238 }
239
240 return $result[$this->current_row];
241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Returns the "previous" row
admine334c472006-10-21 19:44:22 +0000247 *
admin7b613c72006-09-24 18:05:17 +0000248 * @access public
admine334c472006-10-21 19:44:22 +0000249 * @return object
admin7b613c72006-09-24 18:05:17 +0000250 */
251 function previous_row($type = 'object')
252 {
adminaf644692006-10-05 04:22:41 +0000253 $result = $this->result($type);
254
255 if (count($result) == 0)
admin7b613c72006-09-24 18:05:17 +0000256 {
adminaf644692006-10-05 04:22:41 +0000257 return $result;
admin7b613c72006-09-24 18:05:17 +0000258 }
259
260 if (isset($result[$this->current_row - 1]))
261 {
262 --$this->current_row;
263 }
264 return $result[$this->current_row];
265 }
266
admine1063182006-09-25 22:12:16 +0000267 // --------------------------------------------------------------------
268
269 /**
admine334c472006-10-21 19:44:22 +0000270 * The following functions are normally overloaded by the identically named
admin7da94762006-10-05 22:18:31 +0000271 * methods in the platform-specific driver -- except when query caching
admin386ec182006-10-05 21:52:20 +0000272 * is used. When caching is enabled we do not load the other driver.
admin7da94762006-10-05 22:18:31 +0000273 * These functions are primarily here to prevent undefined function errors
admine334c472006-10-21 19:44:22 +0000274 * when a cached result object is in use. They are not otherwise fully
admin10c3f412006-10-08 07:21:12 +0000275 * operational due to the unavailability of the database resource IDs with
admin7da94762006-10-05 22:18:31 +0000276 * cached results.
admine1063182006-09-25 22:12:16 +0000277 */
admin7da94762006-10-05 22:18:31 +0000278 function num_rows() { return $this->num_rows; }
279 function num_fields() { return 0; }
admin606f99c2006-10-11 23:48:41 +0000280 function list_fields() { return array(); }
281 function field_names() { return array(); } // Deprecated
admin7da94762006-10-05 22:18:31 +0000282 function field_data() { return array(); }
283 function free_result() { return TRUE; }
284 function _data_seek() { return TRUE; }
285 function _fetch_assoc() { return array(); }
286 function _fetch_object() { return array(); }
admin606f99c2006-10-11 23:48:41 +0000287
admin7b613c72006-09-24 18:05:17 +0000288}
admin7b9d4722006-10-05 04:46:47 +0000289// END DB_result class
admin7b613c72006-09-24 18:05:17 +0000290?>