blob: 2c50c255b6cdb4e8b20971397e4fabad442a1e2f [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * ODBC Result Class
31 *
32 * This class extends the parent result class: CI_DB_result
33 *
34 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/database/
Andrey Andreev5ca05132012-07-05 12:06:34 +030037 * @since 1.3
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
39class CI_DB_odbc_result extends CI_DB_result {
Barry Mienydd671972010-10-04 16:33:58 +020040
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 }
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030052 elseif (($this->num_rows = @odbc_num_rows($this->result_id)) !== -1)
Andrey Andreev78b24bf2012-02-29 16:43:57 +020053 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030054 return $this->num_rows;
Andrey Andreev78b24bf2012-02-29 16:43:57 +020055 }
56
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030057 // Work-around for ODBC subdrivers that don't support num_rows()
58 if (count($this->result_array) > 0)
59 {
60 return $this->num_rows = count($this->result_array);
61 }
62 elseif (count($this->result_object) > 0)
63 {
64 return $this->num_rows = count($this->result_object);
65 }
66
67 return $this->num_rows = count($this->result_array());
Derek Allard2067d1a2008-11-13 22:59:24 +000068 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Andrey Andreev5ca05132012-07-05 12:06:34 +030070 // --------------------------------------------------------------------
71
Derek Allard2067d1a2008-11-13 22:59:24 +000072 /**
73 * Number of fields in the result set
74 *
Andrey Andreevb76029d2012-01-26 15:13:19 +020075 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000076 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020077 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000078 {
79 return @odbc_num_fields($this->result_id);
80 }
81
82 // --------------------------------------------------------------------
83
84 /**
85 * Fetch Field Names
86 *
87 * Generates an array of column names
88 *
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * @return array
90 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020091 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
93 $field_names = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +020094 $num_fields = $this->num_fields();
Andrey Andreev41e46a92012-03-01 14:58:17 +020095
Andrey Andreevbb8a6832012-02-14 12:17:13 +020096 if ($num_fields > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Andrey Andreevbb8a6832012-02-14 12:17:13 +020098 for ($i = 1; $i <= $num_fields; $i++)
99 {
Andrey Andreev41e46a92012-03-01 14:58:17 +0200100 $field_names[] = odbc_field_name($this->result_id, $i);
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200101 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 return $field_names;
105 }
106
107 // --------------------------------------------------------------------
108
109 /**
110 * Field data
111 *
112 * Generates an array of objects containing field meta-data
113 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @return array
115 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200116 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
118 $retval = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200119 for ($i = 0, $odbc_index = 1, $c = $this->num_fields(); $i < $c; $i++, $odbc_index++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200121 $retval[$i] = new stdClass();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200122 $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index);
123 $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index);
124 $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200125 $retval[$i]->primary_key = 0;
126 $retval[$i]->default = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 return $retval;
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Free the result
136 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200137 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200138 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200139 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
141 if (is_resource($this->result_id))
142 {
143 odbc_free_result($this->result_id);
144 $this->result_id = FALSE;
145 }
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * Result - associative array
152 *
153 * Returns the result set as an array
154 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @return array
156 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200157 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreev08364ea2012-07-04 23:10:46 +0300159 return odbc_fetch_array($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Result - object
166 *
167 * Returns the result set as an object
168 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200169 * @param string $class_name
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @return object
171 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300172 protected function _fetch_object($class_name = 'stdClass')
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300174 $row = odbc_fetch_object($this->result_id);
175
176 if ($class_name === 'stdClass' OR ! $row)
177 {
178 return $row;
179 }
180
181 $class_name = new $class_name();
182 foreach ($row as $key => $value)
183 {
184 $class_name->$key = $value;
185 }
186
187 return $class_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
190}
191
Andrey Andreev08364ea2012-07-04 23:10:46 +0300192// --------------------------------------------------------------------
193
194if ( ! function_exists('odbc_fetch_array'))
195{
196 /**
197 * ODBC Fetch array
198 *
199 * Emulates the native odbc_fetch_array() function when
200 * it is not available (odbc_fetch_array() requires unixODBC)
201 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200202 * @param resource &$result
203 * @param int $rownumber
Andrey Andreev08364ea2012-07-04 23:10:46 +0300204 * @return array
205 */
Andrey Andreev8463b912012-11-02 02:16:28 +0200206 function odbc_fetch_array(&$result, $rownumber = 1)
Andrey Andreev08364ea2012-07-04 23:10:46 +0300207 {
208 $rs = array();
209 if ( ! odbc_fetch_into($result, $rs, $rownumber))
210 {
211 return FALSE;
212 }
213
214 $rs_assoc = array();
215 foreach ($rs as $k => $v)
216 {
217 $field_name = odbc_field_name($result, $k+1);
218 $rs_assoc[$field_name] = $v;
219 }
220
221 return $rs_assoc;
222 }
223}
224
225// --------------------------------------------------------------------
226
227if ( ! function_exists('odbc_fetch_object'))
228{
229 /**
230 * ODBC Fetch object
231 *
232 * Emulates the native odbc_fetch_object() function when
233 * it is not available.
234 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200235 * @param resource &$result
236 * @param int $rownumber
Andrey Andreev08364ea2012-07-04 23:10:46 +0300237 * @return object
238 */
Andrey Andreev8463b912012-11-02 02:16:28 +0200239 function odbc_fetch_object(&$result, $rownumber = 1)
Andrey Andreev08364ea2012-07-04 23:10:46 +0300240 {
241 $rs = array();
242 if ( ! odbc_fetch_into($result, $rs, $rownumber))
243 {
244 return FALSE;
245 }
246
247 $rs_object = new stdClass();
248 foreach ($rs as $k => $v)
249 {
250 $field_name = odbc_field_name($result, $k+1);
251 $rs_object->$field_name = $v;
252 }
253
254 return $rs_object;
255 }
256}
257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258/* End of file odbc_result.php */
Andrey Andreev19aee032012-03-20 15:31:42 +0200259/* Location: ./system/database/drivers/odbc/odbc_result.php */