blob: 0b74871e026c210d8824da413108d32d97481f94 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * ODBC Result Class
32 *
33 * This class extends the parent result class: CI_DB_result
34 *
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
39class CI_DB_odbc_result extends CI_DB_result {
Barry Mienydd671972010-10-04 16:33:58 +020040
Andrey Andreevef795ac2012-03-01 15:15:31 +020041 public $num_rows;
42
Derek Allard2067d1a2008-11-13 22:59:24 +000043 /**
44 * Number of rows in the result set
45 *
Andrey Andreevef795ac2012-03-01 15:15:31 +020046 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000047 */
Andrey Andreevef795ac2012-03-01 15:15:31 +020048 public function num_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +000049 {
Andrey Andreevef795ac2012-03-01 15:15:31 +020050 if (is_int($this->num_rows))
51 {
52 return $this->num_rows;
53 }
54
55 // Work-around for ODBC subdrivers that don't support num_rows()
56 if (($this->num_rows = @odbc_num_rows($this->result_id)) === -1)
57 {
58 $this->num_rows = count($this->result_array());
59 }
60
61 return $this->num_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +000062 }
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 // --------------------------------------------------------------------
65
66 /**
67 * Number of fields in the result set
68 *
Andrey Andreev41e46a92012-03-01 14:58:17 +020069 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000070 */
Andrey Andreev41e46a92012-03-01 14:58:17 +020071 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
73 return @odbc_num_fields($this->result_id);
74 }
75
76 // --------------------------------------------------------------------
77
78 /**
79 * Fetch Field Names
80 *
81 * Generates an array of column names
82 *
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @return array
84 */
Andrey Andreev41e46a92012-03-01 14:58:17 +020085 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 $field_names = array();
Andrey Andreev41e46a92012-03-01 14:58:17 +020088 $num_fields = $this->num_fields();
89
90 if ($num_fields > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreev41e46a92012-03-01 14:58:17 +020092 for ($i = 1; $i <= $num_fields; $i++)
93 {
94 $field_names[] = odbc_field_name($this->result_id, $i);
95 }
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 return $field_names;
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Field data
105 *
106 * Generates an array of objects containing field meta-data
107 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * @return array
109 */
Andrey Andreev41e46a92012-03-01 14:58:17 +0200110 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
112 $retval = array();
Andrey Andreev41e46a92012-03-01 14:58:17 +0200113 for ($i = 0, $odbc_index = 1, $c = $this->num_fields(); $i < $c; $i++, $odbc_index++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Andrey Andreev41e46a92012-03-01 14:58:17 +0200115 $retval[$i] = new stdClass();
116 $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index);
117 $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index);
118 $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index);
119 $retval[$i]->primary_key = 0;
120 $retval[$i]->default = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 return $retval;
124 }
125
126 // --------------------------------------------------------------------
127
128 /**
129 * Free the result
130 *
131 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200132 */
Timothy Warren142ca8e2012-03-19 18:00:35 -0400133 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 if (is_resource($this->result_id))
136 {
137 odbc_free_result($this->result_id);
138 $this->result_id = FALSE;
139 }
140 }
141
142 // --------------------------------------------------------------------
143
144 /**
145 * Data Seek
146 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500147 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * this internally before fetching results to make sure the
149 * result set starts at zero
150 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * @return array
152 */
Timothy Warren142ca8e2012-03-19 18:00:35 -0400153 protected function _data_seek($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
155 return FALSE;
156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Result - associative array
162 *
163 * Returns the result set as an array
164 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * @return array
166 */
Timothy Warren142ca8e2012-03-19 18:00:35 -0400167 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 if (function_exists('odbc_fetch_object'))
170 {
171 return odbc_fetch_array($this->result_id);
172 }
173 else
174 {
175 return $this->_odbc_fetch_array($this->result_id);
176 }
177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Result - object
183 *
184 * Returns the result set as an object
185 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * @return object
187 */
Timothy Warren142ca8e2012-03-19 18:00:35 -0400188 protected function _fetch_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 if (function_exists('odbc_fetch_object'))
191 {
192 return odbc_fetch_object($this->result_id);
193 }
194 else
195 {
196 return $this->_odbc_fetch_object($this->result_id);
197 }
198 }
199
200
201 /**
202 * Result - object
203 *
204 * subsititutes the odbc_fetch_object function when
205 * not available (odbc_fetch_object requires unixODBC)
206 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 * @return object
208 */
Timothy Warren142ca8e2012-03-19 18:00:35 -0400209 protected function _odbc_fetch_object(& $odbc_result) {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $rs = array();
Pascal Kriete8761ef52011-02-14 13:13:52 -0500211 $rs_obj = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 if (odbc_fetch_into($odbc_result, $rs)) {
213 foreach ($rs as $k=>$v) {
214 $field_name= odbc_field_name($odbc_result, $k+1);
215 $rs_obj->$field_name = $v;
216 }
217 }
218 return $rs_obj;
219 }
220
221
222 /**
223 * Result - array
224 *
225 * subsititutes the odbc_fetch_array function when
226 * not available (odbc_fetch_array requires unixODBC)
227 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 * @return array
229 */
Timothy Warren142ca8e2012-03-19 18:00:35 -0400230 protected function _odbc_fetch_array(& $odbc_result) {
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $rs = array();
Pascal Kriete8761ef52011-02-14 13:13:52 -0500232 $rs_assoc = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 if (odbc_fetch_into($odbc_result, $rs)) {
234 $rs_assoc=array();
235 foreach ($rs as $k=>$v) {
236 $field_name= odbc_field_name($odbc_result, $k+1);
237 $rs_assoc[$field_name] = $v;
238 }
239 }
240 return $rs_assoc;
241 }
242
Andrey Andreevcc5af532012-03-05 14:02:15 +0200243 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000244
Andrey Andreevcc5af532012-03-05 14:02:15 +0200245 /**
246 * Query result. Array version.
247 *
248 * @return array
249 */
250 public function result_array()
251 {
252 if (count($this->result_array) > 0)
253 {
254 return $this->result_array;
255 }
256 elseif (($c = count($this->result_object)) > 0)
257 {
258 for ($i = 0; $i < $c; $i++)
259 {
260 $this->result_array[$i] = (array) $this->result_object[$i];
261 }
262 }
263 elseif ($this->result_id === FALSE)
264 {
265 return array();
266 }
267 else
268 {
269 while ($row = $this->_fetch_assoc())
270 {
271 $this->result_array[] = $row;
272 }
273 }
274
275 return $this->result_array;
276 }
277
Andrey Andreev9c68c312012-03-06 10:34:58 +0200278 // --------------------------------------------------------------------
279
280 /**
281 * Query result. Object version.
282 *
283 * @return array
284 */
285 public function result_object()
286 {
287 if (count($this->result_object) > 0)
288 {
289 return $this->result_object;
290 }
291 elseif (($c = count($this->result_array)) > 0)
292 {
293 for ($i = 0; $i < $c; $i++)
294 {
295 $this->result_object[$i] = (object) $this->result_array[$i];
296 }
297 }
298 elseif ($this->result_id === FALSE)
299 {
300 return array();
301 }
302 else
303 {
304 while ($row = $this->_fetch_object())
305 {
306 $this->result_object[] = $row;
307 }
308 }
309
310 return $this->result_object;
311 }
312
Andrey Andreevcc5af532012-03-05 14:02:15 +0200313}
Derek Allard2067d1a2008-11-13 22:59:24 +0000314
315/* End of file odbc_result.php */
Timothy Warren142ca8e2012-03-19 18:00:35 -0400316/* Location: ./system/database/drivers/odbc/odbc_result.php */