blob: 227fe4fac399fa8af4eb2c97683e427333eb36e8 [file] [log] [blame]
Andrey Andreevb76029d2012-01-26 15:13:19 +02001<?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
Andrey Andreevb76029d2012-01-26 15:13:19 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevb76029d2012-01-26 15:13:19 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * ODBC Result Class
30 *
31 * This class extends the parent result class: CI_DB_result
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_odbc_result extends CI_DB_result {
Barry Mienydd671972010-10-04 16:33:58 +020038
Andrey Andreevef795ac2012-03-01 15:15:31 +020039 public $num_rows;
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041 /**
42 * Number of rows in the result set
43 *
Andrey Andreevb76029d2012-01-26 15:13:19 +020044 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000045 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020046 public function num_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +000047 {
Andrey Andreevef795ac2012-03-01 15:15:31 +020048 if (is_int($this->num_rows))
Andrey Andreev78b24bf2012-02-29 16:43:57 +020049 {
50 return $this->num_rows;
51 }
52
53 // Work-around for ODBC subdrivers that don't support num_rows()
54 if (($this->num_rows = @odbc_num_rows($this->result_id)) === -1)
55 {
Andrey Andreevef795ac2012-03-01 15:15:31 +020056 $this->num_rows = count($this->result_array());
Andrey Andreev78b24bf2012-02-29 16:43:57 +020057 }
58
59 return $this->num_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +000060 }
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
63 * Number of fields in the result set
64 *
Andrey Andreevb76029d2012-01-26 15:13:19 +020065 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000066 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020067 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
69 return @odbc_num_fields($this->result_id);
70 }
71
72 // --------------------------------------------------------------------
73
74 /**
75 * Fetch Field Names
76 *
77 * Generates an array of column names
78 *
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * @return array
80 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020081 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
83 $field_names = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +020084 $num_fields = $this->num_fields();
Andrey Andreev41e46a92012-03-01 14:58:17 +020085
Andrey Andreevbb8a6832012-02-14 12:17:13 +020086 if ($num_fields > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreevbb8a6832012-02-14 12:17:13 +020088 for ($i = 1; $i <= $num_fields; $i++)
89 {
Andrey Andreev41e46a92012-03-01 14:58:17 +020090 $field_names[] = odbc_field_name($this->result_id, $i);
Andrey Andreevbb8a6832012-02-14 12:17:13 +020091 }
Derek Allard2067d1a2008-11-13 22:59:24 +000092 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 return $field_names;
95 }
96
97 // --------------------------------------------------------------------
98
99 /**
100 * Field data
101 *
102 * Generates an array of objects containing field meta-data
103 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 * @return array
105 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200106 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
108 $retval = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200109 for ($i = 0, $odbc_index = 1, $c = $this->num_fields(); $i < $c; $i++, $odbc_index++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200111 $retval[$i] = new stdClass();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200112 $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index);
113 $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index);
114 $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200115 $retval[$i]->primary_key = 0;
116 $retval[$i]->default = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 return $retval;
120 }
121
122 // --------------------------------------------------------------------
123
124 /**
125 * Free the result
126 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200127 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200128 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200129 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
131 if (is_resource($this->result_id))
132 {
133 odbc_free_result($this->result_id);
134 $this->result_id = FALSE;
135 }
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * Result - associative array
142 *
143 * Returns the result set as an array
144 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 * @return array
146 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200147 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Andrey Andreev7e934892012-02-12 03:29:06 +0200149 return function_exists('odbc_fetch_array')
Andrey Andreevb76029d2012-01-26 15:13:19 +0200150 ? odbc_fetch_array($this->result_id)
151 : $this->_odbc_fetch_array($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Result - object
158 *
159 * Returns the result set as an object
160 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 * @return object
162 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200163 protected function _fetch_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200165 return function_exists('odbc_fetch_object')
166 ? odbc_fetch_object($this->result_id)
167 : $this->_odbc_fetch_object($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200170 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000171
172 /**
173 * Result - object
174 *
175 * subsititutes the odbc_fetch_object function when
176 * not available (odbc_fetch_object requires unixODBC)
177 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @return object
179 */
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200180 protected function _odbc_fetch_object(& $odbc_result)
Andrey Andreevb76029d2012-01-26 15:13:19 +0200181 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $rs = array();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200183 if ( ! odbc_fetch_into($odbc_result, $rs))
184 {
185 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Andrey Andreevb76029d2012-01-26 15:13:19 +0200187
188 $rs_obj = new stdClass();
189 foreach ($rs as $k => $v)
190 {
Andrey Andreevd0b256f2012-02-13 12:20:11 +0200191 $field_name = odbc_field_name($odbc_result, $k+1);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200192 $rs_obj->$field_name = $v;
193 }
194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 return $rs_obj;
196 }
197
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200198 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
200 /**
201 * Result - array
202 *
203 * subsititutes the odbc_fetch_array function when
204 * not available (odbc_fetch_array requires unixODBC)
205 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * @return array
207 */
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200208 protected function _odbc_fetch_array(& $odbc_result)
Andrey Andreevb76029d2012-01-26 15:13:19 +0200209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $rs = array();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200211 if ( ! odbc_fetch_into($odbc_result, $rs))
212 {
213 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Andrey Andreevb76029d2012-01-26 15:13:19 +0200215
216 $rs_assoc = array();
217 foreach ($rs as $k => $v)
218 {
Andrey Andreevd0b256f2012-02-13 12:20:11 +0200219 $field_name = odbc_field_name($odbc_result, $k+1);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200220 $rs_assoc[$field_name] = $v;
221 }
222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 return $rs_assoc;
224 }
225
Andrey Andreevcc5af532012-03-05 14:02:15 +0200226 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000227
Andrey Andreevcc5af532012-03-05 14:02:15 +0200228 /**
229 * Query result. Array version.
230 *
231 * @return array
232 */
233 public function result_array()
234 {
235 if (count($this->result_array) > 0)
236 {
237 return $this->result_array;
238 }
239 elseif (($c = count($this->result_object)) > 0)
240 {
241 for ($i = 0; $i < $c; $i++)
242 {
243 $this->result_array[$i] = (array) $this->result_object[$i];
244 }
245 }
246 elseif ($this->result_id === FALSE)
247 {
248 return array();
249 }
250 else
251 {
252 while ($row = $this->_fetch_assoc())
253 {
254 $this->result_array[] = $row;
255 }
256 }
257
258 return $this->result_array;
259 }
260
Andrey Andreev9c68c312012-03-06 10:34:58 +0200261 // --------------------------------------------------------------------
262
263 /**
264 * Query result. Object version.
265 *
266 * @return array
267 */
268 public function result_object()
269 {
270 if (count($this->result_object) > 0)
271 {
272 return $this->result_object;
273 }
274 elseif (($c = count($this->result_array)) > 0)
275 {
276 for ($i = 0; $i < $c; $i++)
277 {
278 $this->result_object[$i] = (object) $this->result_array[$i];
279 }
280 }
281 elseif ($this->result_id === FALSE)
282 {
283 return array();
284 }
285 else
286 {
287 while ($row = $this->_fetch_object())
288 {
289 $this->result_object[] = $row;
290 }
291 }
292
293 return $this->result_object;
294 }
295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296}
297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298/* End of file odbc_result.php */
Andrey Andreev19aee032012-03-20 15:31:42 +0200299/* Location: ./system/database/drivers/odbc/odbc_result.php */