blob: e7cbc7af51606d441d04376d2b9f5fb367345388 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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/
36 */
37class CI_DB_odbc_result extends CI_DB_result {
Barry Mienydd671972010-10-04 16:33:58 +020038
Derek Allard2067d1a2008-11-13 22:59:24 +000039 /**
40 * Number of rows in the result set
41 *
Andrey Andreevb76029d2012-01-26 15:13:19 +020042 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000043 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020044 public function num_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +000045 {
Andrey Andreev78b24bf2012-02-29 16:43:57 +020046 if ($this->num_rows > 0)
47 {
48 return $this->num_rows;
49 }
50
51 // Work-around for ODBC subdrivers that don't support num_rows()
52 if (($this->num_rows = @odbc_num_rows($this->result_id)) === -1)
53 {
54 return $this->num_rows = count($this->result_array());
55 }
56
57 return $this->num_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +000058 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 /**
61 * Number of fields in the result set
62 *
Andrey Andreevb76029d2012-01-26 15:13:19 +020063 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000064 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020065 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
67 return @odbc_num_fields($this->result_id);
68 }
69
70 // --------------------------------------------------------------------
71
72 /**
73 * Fetch Field Names
74 *
75 * Generates an array of column names
76 *
Derek Allard2067d1a2008-11-13 22:59:24 +000077 * @return array
78 */
Andrey Andreevb76029d2012-01-26 15:13:19 +020079 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
Andrey Andreevbb8a6832012-02-14 12:17:13 +020081 $num_fields = $this->num_fields();
82 if ($num_fields > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
Andrey Andreevbb8a6832012-02-14 12:17:13 +020084 $field_names = array();
85 for ($i = 1; $i <= $num_fields; $i++)
86 {
87 $field_names[] = odbc_field_name($this->result_id, $i);
88 }
89
90 return $field_names;
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Andrey Andreevbb8a6832012-02-14 12:17:13 +020093 return array();
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * Field data
100 *
101 * Generates an array of objects containing field meta-data
102 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 * @return array
104 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200105 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
107 $retval = array();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200108 for ($i = 0, $odbc_index = 1, $c = $this->num_fields(); $i < $c; $i++, $odbc_index++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200110 $retval[$i] = new stdClass();
Andrey Andreevbb8a6832012-02-14 12:17:13 +0200111 $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index);
112 $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index);
113 $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200114 $retval[$i]->primary_key = 0;
115 $retval[$i]->default = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 return $retval;
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Free the result
125 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200126 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200128 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 if (is_resource($this->result_id))
131 {
132 odbc_free_result($this->result_id);
133 $this->result_id = FALSE;
134 }
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Data Seek
141 *
Andrey Andreevb76029d2012-01-26 15:13:19 +0200142 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * this internally before fetching results to make sure the
144 * result set starts at zero
145 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @return array
147 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200148 protected function _data_seek($n = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200150 // Not supported
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 return FALSE;
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Result - associative array
158 *
159 * Returns the result set as an array
160 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 * @return array
162 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200163 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Andrey Andreev7e934892012-02-12 03:29:06 +0200165 return function_exists('odbc_fetch_array')
Andrey Andreevb76029d2012-01-26 15:13:19 +0200166 ? odbc_fetch_array($this->result_id)
167 : $this->_odbc_fetch_array($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Result - object
174 *
175 * Returns the result set as an object
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @return object
178 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200179 protected function _fetch_object()
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreevb76029d2012-01-26 15:13:19 +0200181 return function_exists('odbc_fetch_object')
182 ? odbc_fetch_object($this->result_id)
183 : $this->_odbc_fetch_object($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 /**
187 * Result - object
188 *
189 * subsititutes the odbc_fetch_object function when
190 * not available (odbc_fetch_object requires unixODBC)
191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @return object
193 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200194 private function _odbc_fetch_object(& $odbc_result)
195 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 $rs = array();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200197 if ( ! odbc_fetch_into($odbc_result, $rs))
198 {
199 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
Andrey Andreevb76029d2012-01-26 15:13:19 +0200201
202 $rs_obj = new stdClass();
203 foreach ($rs as $k => $v)
204 {
Andrey Andreevd0b256f2012-02-13 12:20:11 +0200205 $field_name = odbc_field_name($odbc_result, $k+1);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200206 $rs_obj->$field_name = $v;
207 }
208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 return $rs_obj;
210 }
211
212
213 /**
214 * Result - array
215 *
216 * subsititutes the odbc_fetch_array function when
217 * not available (odbc_fetch_array requires unixODBC)
218 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * @return array
220 */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200221 private function _odbc_fetch_array(& $odbc_result)
222 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $rs = array();
Andrey Andreevb76029d2012-01-26 15:13:19 +0200224 if ( ! odbc_fetch_into($odbc_result, $rs))
225 {
226 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
Andrey Andreevb76029d2012-01-26 15:13:19 +0200228
229 $rs_assoc = array();
230 foreach ($rs as $k => $v)
231 {
Andrey Andreevd0b256f2012-02-13 12:20:11 +0200232 $field_name = odbc_field_name($odbc_result, $k+1);
Andrey Andreevb76029d2012-01-26 15:13:19 +0200233 $rs_assoc[$field_name] = $v;
234 }
235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 return $rs_assoc;
237 }
238
239}
240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241/* End of file odbc_result.php */
Andrey Andreevb76029d2012-01-26 15:13:19 +0200242/* Location: ./system/database/drivers/odbc/odbc_result.php */