blob: 1d998bea868a9de05ef0ad13970d85a9e105bbcb [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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * @return object
169 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200170 protected function _fetch_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Andrey Andreev08364ea2012-07-04 23:10:46 +0300172 return odbc_fetch_object($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174
175}
176
Andrey Andreev08364ea2012-07-04 23:10:46 +0300177// --------------------------------------------------------------------
178
179if ( ! function_exists('odbc_fetch_array'))
180{
181 /**
182 * ODBC Fetch array
183 *
184 * Emulates the native odbc_fetch_array() function when
185 * it is not available (odbc_fetch_array() requires unixODBC)
186 *
187 * @param resource
188 * @param int
189 * @return array
190 */
191 function odbc_fetch_array(& $result, $rownumber = 1)
192 {
193 $rs = array();
194 if ( ! odbc_fetch_into($result, $rs, $rownumber))
195 {
196 return FALSE;
197 }
198
199 $rs_assoc = array();
200 foreach ($rs as $k => $v)
201 {
202 $field_name = odbc_field_name($result, $k+1);
203 $rs_assoc[$field_name] = $v;
204 }
205
206 return $rs_assoc;
207 }
208}
209
210// --------------------------------------------------------------------
211
212if ( ! function_exists('odbc_fetch_object'))
213{
214 /**
215 * ODBC Fetch object
216 *
217 * Emulates the native odbc_fetch_object() function when
218 * it is not available.
219 *
220 * @param resource
221 * @param int
222 * @return object
223 */
224 function odbc_fetch_object(& $result, $rownumber = 1)
225 {
226 $rs = array();
227 if ( ! odbc_fetch_into($result, $rs, $rownumber))
228 {
229 return FALSE;
230 }
231
232 $rs_object = new stdClass();
233 foreach ($rs as $k => $v)
234 {
235 $field_name = odbc_field_name($result, $k+1);
236 $rs_object->$field_name = $v;
237 }
238
239 return $rs_object;
240 }
241}
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243/* End of file odbc_result.php */
Andrey Andreev19aee032012-03-20 15:31:42 +0200244/* Location: ./system/database/drivers/odbc/odbc_result.php */