blob: 0085ef664b2f9590a052cb001619c1adfaff9201 [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/
Andrey Andreev5ca05132012-07-05 12:06:34 +030036 * @since 1.3
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
38class CI_DB_odbc_result extends CI_DB_result {
Barry Mienydd671972010-10-04 16:33:58 +020039
Derek Allard2067d1a2008-11-13 22:59:24 +000040 /**
41 * Number of rows in the result set
42 *
Andrey Andreevb76029d2012-01-26 15:13:19 +020043 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000044 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020045 public function num_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +000046 {
Andrey Andreevef795ac2012-03-01 15:15:31 +020047 if (is_int($this->num_rows))
Andrey Andreev78b24bf2012-02-29 16:43:57 +020048 {
49 return $this->num_rows;
50 }
51
52 // Work-around for ODBC subdrivers that don't support num_rows()
53 if (($this->num_rows = @odbc_num_rows($this->result_id)) === -1)
54 {
Andrey Andreevef795ac2012-03-01 15:15:31 +020055 $this->num_rows = count($this->result_array());
Andrey Andreev78b24bf2012-02-29 16:43:57 +020056 }
57
58 return $this->num_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +000059 }
Barry Mienydd671972010-10-04 16:33:58 +020060
Andrey Andreev5ca05132012-07-05 12:06:34 +030061 // --------------------------------------------------------------------
62
Derek Allard2067d1a2008-11-13 22:59:24 +000063 /**
64 * Number of fields in the result set
65 *
Andrey Andreevb76029d2012-01-26 15:13:19 +020066 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000067 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020068 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
70 return @odbc_num_fields($this->result_id);
71 }
72
73 // --------------------------------------------------------------------
74
75 /**
76 * Fetch Field Names
77 *
78 * Generates an array of column names
79 *
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * @return array
81 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020082 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
84 $field_names = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +020085 $num_fields = $this->num_fields();
Andrey Andreev41e46a92012-03-01 14:58:17 +020086
Andrey Andreevbb8a6832012-02-14 12:17:13 +020087 if ($num_fields > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreevbb8a6832012-02-14 12:17:13 +020089 for ($i = 1; $i <= $num_fields; $i++)
90 {
Andrey Andreev41e46a92012-03-01 14:58:17 +020091 $field_names[] = odbc_field_name($this->result_id, $i);
Andrey Andreevbb8a6832012-02-14 12:17:13 +020092 }
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 return $field_names;
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
101 * Field data
102 *
103 * Generates an array of objects containing field meta-data
104 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 * @return array
106 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200107 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
109 $retval = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200110 for ($i = 0, $odbc_index = 1, $c = $this->num_fields(); $i < $c; $i++, $odbc_index++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200112 $retval[$i] = new stdClass();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200113 $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index);
114 $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index);
115 $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200116 $retval[$i]->primary_key = 0;
117 $retval[$i]->default = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 return $retval;
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Free the result
127 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200128 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200129 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200130 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
132 if (is_resource($this->result_id))
133 {
134 odbc_free_result($this->result_id);
135 $this->result_id = FALSE;
136 }
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * Result - associative array
143 *
144 * Returns the result set as an array
145 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @return array
147 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200148 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Andrey Andreev08364ea2012-07-04 23:10:46 +0300150 return odbc_fetch_array($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Result - object
157 *
158 * Returns the result set as an object
159 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 * @return object
161 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200162 protected function _fetch_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreev08364ea2012-07-04 23:10:46 +0300164 return odbc_fetch_object($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166
Andrey Andreevcc5af532012-03-05 14:02:15 +0200167 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000168
Andrey Andreevcc5af532012-03-05 14:02:15 +0200169 /**
170 * Query result. Array version.
171 *
172 * @return array
173 */
174 public function result_array()
175 {
176 if (count($this->result_array) > 0)
177 {
178 return $this->result_array;
179 }
180 elseif (($c = count($this->result_object)) > 0)
181 {
182 for ($i = 0; $i < $c; $i++)
183 {
184 $this->result_array[$i] = (array) $this->result_object[$i];
185 }
186 }
187 elseif ($this->result_id === FALSE)
188 {
189 return array();
190 }
191 else
192 {
193 while ($row = $this->_fetch_assoc())
194 {
195 $this->result_array[] = $row;
196 }
197 }
198
199 return $this->result_array;
200 }
201
Andrey Andreev9c68c312012-03-06 10:34:58 +0200202 // --------------------------------------------------------------------
203
204 /**
205 * Query result. Object version.
206 *
207 * @return array
208 */
209 public function result_object()
210 {
211 if (count($this->result_object) > 0)
212 {
213 return $this->result_object;
214 }
215 elseif (($c = count($this->result_array)) > 0)
216 {
217 for ($i = 0; $i < $c; $i++)
218 {
219 $this->result_object[$i] = (object) $this->result_array[$i];
220 }
221 }
222 elseif ($this->result_id === FALSE)
223 {
224 return array();
225 }
226 else
227 {
228 while ($row = $this->_fetch_object())
229 {
230 $this->result_object[] = $row;
231 }
232 }
233
234 return $this->result_object;
235 }
236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237}
238
Andrey Andreev08364ea2012-07-04 23:10:46 +0300239// --------------------------------------------------------------------
240
241if ( ! function_exists('odbc_fetch_array'))
242{
243 /**
244 * ODBC Fetch array
245 *
246 * Emulates the native odbc_fetch_array() function when
247 * it is not available (odbc_fetch_array() requires unixODBC)
248 *
249 * @param resource
250 * @param int
251 * @return array
252 */
253 function odbc_fetch_array(& $result, $rownumber = 1)
254 {
255 $rs = array();
256 if ( ! odbc_fetch_into($result, $rs, $rownumber))
257 {
258 return FALSE;
259 }
260
261 $rs_assoc = array();
262 foreach ($rs as $k => $v)
263 {
264 $field_name = odbc_field_name($result, $k+1);
265 $rs_assoc[$field_name] = $v;
266 }
267
268 return $rs_assoc;
269 }
270}
271
272// --------------------------------------------------------------------
273
274if ( ! function_exists('odbc_fetch_object'))
275{
276 /**
277 * ODBC Fetch object
278 *
279 * Emulates the native odbc_fetch_object() function when
280 * it is not available.
281 *
282 * @param resource
283 * @param int
284 * @return object
285 */
286 function odbc_fetch_object(& $result, $rownumber = 1)
287 {
288 $rs = array();
289 if ( ! odbc_fetch_into($result, $rs, $rownumber))
290 {
291 return FALSE;
292 }
293
294 $rs_object = new stdClass();
295 foreach ($rs as $k => $v)
296 {
297 $field_name = odbc_field_name($result, $k+1);
298 $rs_object->$field_name = $v;
299 }
300
301 return $rs_object;
302 }
303}
304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305/* End of file odbc_result.php */
Andrey Andreev19aee032012-03-20 15:31:42 +0200306/* Location: ./system/database/drivers/odbc/odbc_result.php */