blob: ecba5977ea5ae0b8c4d76c296adfa90907c53db3 [file] [log] [blame]
Andrey Andreev78ef5a52012-03-20 14:43:34 +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 Andreev78ef5a52012-03-20 14:43:34 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev78ef5a52012-03-20 14:43:34 +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 Andreevef795ac2012-03-01 15:15:31 +020044 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000045 */
Andrey Andreevef795ac2012-03-01 15:15:31 +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))
49 {
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 {
56 $this->num_rows = count($this->result_array());
57 }
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
64 /**
65 * Number of fields in the result set
66 *
Andrey Andreev41e46a92012-03-01 14:58:17 +020067 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000068 */
Andrey Andreev41e46a92012-03-01 14:58:17 +020069 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
71 return @odbc_num_fields($this->result_id);
72 }
73
74 // --------------------------------------------------------------------
75
76 /**
77 * Fetch Field Names
78 *
79 * Generates an array of column names
80 *
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @return array
82 */
Andrey Andreev41e46a92012-03-01 14:58:17 +020083 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
85 $field_names = array();
Andrey Andreev41e46a92012-03-01 14:58:17 +020086 $num_fields = $this->num_fields();
87
88 if ($num_fields > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Andrey Andreev41e46a92012-03-01 14:58:17 +020090 for ($i = 1; $i <= $num_fields; $i++)
91 {
92 $field_names[] = odbc_field_name($this->result_id, $i);
93 }
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 return $field_names;
97 }
98
99 // --------------------------------------------------------------------
100
101 /**
102 * Field data
103 *
104 * Generates an array of objects containing field meta-data
105 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * @return array
107 */
Andrey Andreev41e46a92012-03-01 14:58:17 +0200108 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
110 $retval = array();
Andrey Andreev41e46a92012-03-01 14:58:17 +0200111 for ($i = 0, $odbc_index = 1, $c = $this->num_fields(); $i < $c; $i++, $odbc_index++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Andrey Andreev41e46a92012-03-01 14:58:17 +0200113 $retval[$i] = new stdClass();
114 $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index);
115 $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index);
116 $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index);
117 $retval[$i]->primary_key = 0;
118 $retval[$i]->default = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 return $retval;
122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Free the result
128 *
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200129 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200130 */
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200131 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
133 if (is_resource($this->result_id))
134 {
135 odbc_free_result($this->result_id);
136 $this->result_id = FALSE;
137 }
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * Result - associative array
144 *
145 * Returns the result set as an array
146 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 * @return array
148 */
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200149 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200151 if (function_exists('odbc_fetch_array'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
153 return odbc_fetch_array($this->result_id);
154 }
155 else
156 {
157 return $this->_odbc_fetch_array($this->result_id);
158 }
159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Result - object
165 *
166 * Returns the result set as an object
167 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * @return object
169 */
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200170 protected function _fetch_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
172 if (function_exists('odbc_fetch_object'))
173 {
174 return odbc_fetch_object($this->result_id);
175 }
176 else
177 {
178 return $this->_odbc_fetch_object($this->result_id);
179 }
180 }
181
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200182 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000183
184 /**
185 * Result - object
186 *
187 * subsititutes the odbc_fetch_object function when
188 * not available (odbc_fetch_object requires unixODBC)
189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @return object
191 */
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200192 protected function _odbc_fetch_object(& $odbc_result)
193 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $rs = array();
Pascal Kriete8761ef52011-02-14 13:13:52 -0500195 $rs_obj = FALSE;
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200196 if (odbc_fetch_into($odbc_result, $rs))
197 {
198 foreach ($rs as $k => $v)
199 {
200 $field_name = odbc_field_name($odbc_result, $k+1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $rs_obj->$field_name = $v;
202 }
203 }
204 return $rs_obj;
205 }
206
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200207 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000208
209 /**
210 * Result - array
211 *
212 * subsititutes the odbc_fetch_array function when
213 * not available (odbc_fetch_array requires unixODBC)
214 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * @return array
216 */
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200217 protected function _odbc_fetch_array(& $odbc_result)
218 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 $rs = array();
Pascal Kriete8761ef52011-02-14 13:13:52 -0500220 $rs_assoc = FALSE;
Andrey Andreev78ef5a52012-03-20 14:43:34 +0200221 if (odbc_fetch_into($odbc_result, $rs))
222 {
223 $rs_assoc = array();
224 foreach ($rs as $k => $v)
225 {
226 $field_name = odbc_field_name($odbc_result, $k+1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 $rs_assoc[$field_name] = $v;
228 }
229 }
230 return $rs_assoc;
231 }
232
Andrey Andreevcc5af532012-03-05 14:02:15 +0200233 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000234
Andrey Andreevcc5af532012-03-05 14:02:15 +0200235 /**
236 * Query result. Array version.
237 *
238 * @return array
239 */
240 public function result_array()
241 {
242 if (count($this->result_array) > 0)
243 {
244 return $this->result_array;
245 }
246 elseif (($c = count($this->result_object)) > 0)
247 {
248 for ($i = 0; $i < $c; $i++)
249 {
250 $this->result_array[$i] = (array) $this->result_object[$i];
251 }
252 }
253 elseif ($this->result_id === FALSE)
254 {
255 return array();
256 }
257 else
258 {
259 while ($row = $this->_fetch_assoc())
260 {
261 $this->result_array[] = $row;
262 }
263 }
264
265 return $this->result_array;
266 }
267
Andrey Andreev9c68c312012-03-06 10:34:58 +0200268 // --------------------------------------------------------------------
269
270 /**
271 * Query result. Object version.
272 *
273 * @return array
274 */
275 public function result_object()
276 {
277 if (count($this->result_object) > 0)
278 {
279 return $this->result_object;
280 }
281 elseif (($c = count($this->result_array)) > 0)
282 {
283 for ($i = 0; $i < $c; $i++)
284 {
285 $this->result_object[$i] = (object) $this->result_array[$i];
286 }
287 }
288 elseif ($this->result_id === FALSE)
289 {
290 return array();
291 }
292 else
293 {
294 while ($row = $this->_fetch_object())
295 {
296 $this->result_object[] = $row;
297 }
298 }
299
300 return $this->result_object;
301 }
302
Andrey Andreevcc5af532012-03-05 14:02:15 +0200303}
Derek Allard2067d1a2008-11-13 22:59:24 +0000304
305/* End of file odbc_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400306/* Location: ./system/database/drivers/odbc/odbc_result.php */