blob: 48dc48dd9cf9e7447e70d905781629fc51eebe34 [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 }
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030051 elseif (($this->num_rows = @odbc_num_rows($this->result_id)) !== -1)
Andrey Andreev78b24bf2012-02-29 16:43:57 +020052 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030053 return $this->num_rows;
Andrey Andreev78b24bf2012-02-29 16:43:57 +020054 }
55
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030056 // Work-around for ODBC subdrivers that don't support num_rows()
57 if (count($this->result_array) > 0)
58 {
59 return $this->num_rows = count($this->result_array);
60 }
61 elseif (count($this->result_object) > 0)
62 {
63 return $this->num_rows = count($this->result_object);
64 }
65
66 return $this->num_rows = count($this->result_array());
Derek Allard2067d1a2008-11-13 22:59:24 +000067 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Andrey Andreev5ca05132012-07-05 12:06:34 +030069 // --------------------------------------------------------------------
70
Derek Allard2067d1a2008-11-13 22:59:24 +000071 /**
72 * Number of fields in the result set
73 *
Andrey Andreevb76029d2012-01-26 15:13:19 +020074 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000075 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020076 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000077 {
78 return @odbc_num_fields($this->result_id);
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Fetch Field Names
85 *
86 * Generates an array of column names
87 *
Derek Allard2067d1a2008-11-13 22:59:24 +000088 * @return array
89 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020090 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
92 $field_names = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +020093 $num_fields = $this->num_fields();
Andrey Andreev41e46a92012-03-01 14:58:17 +020094
Andrey Andreevbb8a6832012-02-14 12:17:13 +020095 if ($num_fields > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
Andrey Andreevbb8a6832012-02-14 12:17:13 +020097 for ($i = 1; $i <= $num_fields; $i++)
98 {
Andrey Andreev41e46a92012-03-01 14:58:17 +020099 $field_names[] = odbc_field_name($this->result_id, $i);
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200100 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 return $field_names;
104 }
105
106 // --------------------------------------------------------------------
107
108 /**
109 * Field data
110 *
111 * Generates an array of objects containing field meta-data
112 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @return array
114 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200115 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 $retval = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200118 for ($i = 0, $odbc_index = 1, $c = $this->num_fields(); $i < $c; $i++, $odbc_index++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200120 $retval[$i] = new stdClass();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200121 $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index);
122 $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index);
123 $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200124 $retval[$i]->primary_key = 0;
125 $retval[$i]->default = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 return $retval;
129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Free the result
135 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200136 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200137 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200138 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
140 if (is_resource($this->result_id))
141 {
142 odbc_free_result($this->result_id);
143 $this->result_id = FALSE;
144 }
145 }
146
147 // --------------------------------------------------------------------
148
149 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * Result - associative array
151 *
152 * Returns the result set as an array
153 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * @return array
155 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200156 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev08364ea2012-07-04 23:10:46 +0300158 return odbc_fetch_array($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Result - object
165 *
166 * Returns the result set as an object
167 *
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300168 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @return object
170 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300171 protected function _fetch_object($class_name = 'stdClass')
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300173 $row = odbc_fetch_object($this->result_id);
174
175 if ($class_name === 'stdClass' OR ! $row)
176 {
177 return $row;
178 }
179
180 $class_name = new $class_name();
181 foreach ($row as $key => $value)
182 {
183 $class_name->$key = $value;
184 }
185
186 return $class_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188
189}
190
Andrey Andreev08364ea2012-07-04 23:10:46 +0300191// --------------------------------------------------------------------
192
193if ( ! function_exists('odbc_fetch_array'))
194{
195 /**
196 * ODBC Fetch array
197 *
198 * Emulates the native odbc_fetch_array() function when
199 * it is not available (odbc_fetch_array() requires unixODBC)
200 *
201 * @param resource
202 * @param int
203 * @return array
204 */
205 function odbc_fetch_array(& $result, $rownumber = 1)
206 {
207 $rs = array();
208 if ( ! odbc_fetch_into($result, $rs, $rownumber))
209 {
210 return FALSE;
211 }
212
213 $rs_assoc = array();
214 foreach ($rs as $k => $v)
215 {
216 $field_name = odbc_field_name($result, $k+1);
217 $rs_assoc[$field_name] = $v;
218 }
219
220 return $rs_assoc;
221 }
222}
223
224// --------------------------------------------------------------------
225
226if ( ! function_exists('odbc_fetch_object'))
227{
228 /**
229 * ODBC Fetch object
230 *
231 * Emulates the native odbc_fetch_object() function when
232 * it is not available.
233 *
234 * @param resource
235 * @param int
236 * @return object
237 */
238 function odbc_fetch_object(& $result, $rownumber = 1)
239 {
240 $rs = array();
241 if ( ! odbc_fetch_into($result, $rs, $rownumber))
242 {
243 return FALSE;
244 }
245
246 $rs_object = new stdClass();
247 foreach ($rs as $k => $v)
248 {
249 $field_name = odbc_field_name($result, $k+1);
250 $rs_object->$field_name = $v;
251 }
252
253 return $rs_object;
254 }
255}
256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257/* End of file odbc_result.php */
Andrey Andreev19aee032012-03-20 15:31:42 +0200258/* Location: ./system/database/drivers/odbc/odbc_result.php */