Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * ODBC Result Class |
| 30 | * |
| 31 | * This class extends the parent result class: CI_DB_result |
| 32 | * |
| 33 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/database/ |
| 36 | */ |
| 37 | class CI_DB_odbc_result extends CI_DB_result { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 38 | |
Andrey Andreev | ef795ac | 2012-03-01 15:15:31 +0200 | [diff] [blame] | 39 | public $num_rows; |
| 40 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Number of rows in the result set |
| 43 | * |
Andrey Andreev | ef795ac | 2012-03-01 15:15:31 +0200 | [diff] [blame] | 44 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | */ |
Andrey Andreev | ef795ac | 2012-03-01 15:15:31 +0200 | [diff] [blame] | 46 | public function num_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | { |
Andrey Andreev | ef795ac | 2012-03-01 15:15:31 +0200 | [diff] [blame] | 48 | if (is_int($this->num_rows)) |
| 49 | { |
| 50 | return $this->num_rows; |
| 51 | } |
| 52 | |
| 53 | // Work-around for ODBC subdrivers that don't support num_rows() |
| 54 | if (($this->num_rows = @odbc_num_rows($this->result_id)) === -1) |
| 55 | { |
| 56 | $this->num_rows = count($this->result_array()); |
| 57 | } |
| 58 | |
| 59 | return $this->num_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | // -------------------------------------------------------------------- |
| 63 | |
| 64 | /** |
| 65 | * Number of fields in the result set |
| 66 | * |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 67 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | */ |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 69 | public function num_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | { |
| 71 | return @odbc_num_fields($this->result_id); |
| 72 | } |
| 73 | |
| 74 | // -------------------------------------------------------------------- |
| 75 | |
| 76 | /** |
| 77 | * Fetch Field Names |
| 78 | * |
| 79 | * Generates an array of column names |
| 80 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | * @return array |
| 82 | */ |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 83 | public function list_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | { |
| 85 | $field_names = array(); |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 86 | $num_fields = $this->num_fields(); |
| 87 | |
| 88 | if ($num_fields > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | { |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 90 | for ($i = 1; $i <= $num_fields; $i++) |
| 91 | { |
| 92 | $field_names[] = odbc_field_name($this->result_id, $i); |
| 93 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | return $field_names; |
| 97 | } |
| 98 | |
| 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * Field data |
| 103 | * |
| 104 | * Generates an array of objects containing field meta-data |
| 105 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | * @return array |
| 107 | */ |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 108 | public function field_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | { |
| 110 | $retval = array(); |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 111 | for ($i = 0, $odbc_index = 1, $c = $this->num_fields(); $i < $c; $i++, $odbc_index++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | { |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 113 | $retval[$i] = new stdClass(); |
| 114 | $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index); |
| 115 | $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index); |
| 116 | $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index); |
| 117 | $retval[$i]->primary_key = 0; |
| 118 | $retval[$i]->default = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | return $retval; |
| 122 | } |
| 123 | |
| 124 | // -------------------------------------------------------------------- |
| 125 | |
| 126 | /** |
| 127 | * Free the result |
| 128 | * |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 129 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | */ |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 131 | public function free_result() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
| 133 | if (is_resource($this->result_id)) |
| 134 | { |
| 135 | odbc_free_result($this->result_id); |
| 136 | $this->result_id = FALSE; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // -------------------------------------------------------------------- |
| 141 | |
| 142 | /** |
| 143 | * Data Seek |
| 144 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 145 | * Moves the internal pointer to the desired offset. We call |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | * this internally before fetching results to make sure the |
| 147 | * result set starts at zero |
| 148 | * |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 149 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | */ |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 151 | protected function _data_seek($n = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | { |
| 153 | return FALSE; |
| 154 | } |
| 155 | |
| 156 | // -------------------------------------------------------------------- |
| 157 | |
| 158 | /** |
| 159 | * Result - associative array |
| 160 | * |
| 161 | * Returns the result set as an array |
| 162 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | * @return array |
| 164 | */ |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 165 | protected function _fetch_assoc() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | { |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 167 | if (function_exists('odbc_fetch_array')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | { |
| 169 | return odbc_fetch_array($this->result_id); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | return $this->_odbc_fetch_array($this->result_id); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // -------------------------------------------------------------------- |
| 178 | |
| 179 | /** |
| 180 | * Result - object |
| 181 | * |
| 182 | * Returns the result set as an object |
| 183 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | * @return object |
| 185 | */ |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 186 | protected function _fetch_object() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | { |
| 188 | if (function_exists('odbc_fetch_object')) |
| 189 | { |
| 190 | return odbc_fetch_object($this->result_id); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | return $this->_odbc_fetch_object($this->result_id); |
| 195 | } |
| 196 | } |
| 197 | |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 198 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * Result - object |
| 202 | * |
| 203 | * subsititutes the odbc_fetch_object function when |
| 204 | * not available (odbc_fetch_object requires unixODBC) |
| 205 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | * @return object |
| 207 | */ |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 208 | protected function _odbc_fetch_object(& $odbc_result) |
| 209 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | $rs = array(); |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 211 | $rs_obj = FALSE; |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 212 | if (odbc_fetch_into($odbc_result, $rs)) |
| 213 | { |
| 214 | foreach ($rs as $k => $v) |
| 215 | { |
| 216 | $field_name = odbc_field_name($odbc_result, $k+1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | $rs_obj->$field_name = $v; |
| 218 | } |
| 219 | } |
| 220 | return $rs_obj; |
| 221 | } |
| 222 | |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 223 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * Result - array |
| 227 | * |
| 228 | * subsititutes the odbc_fetch_array function when |
| 229 | * not available (odbc_fetch_array requires unixODBC) |
| 230 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | * @return array |
| 232 | */ |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 233 | protected function _odbc_fetch_array(& $odbc_result) |
| 234 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | $rs = array(); |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 236 | $rs_assoc = FALSE; |
Andrey Andreev | 78ef5a5 | 2012-03-20 14:43:34 +0200 | [diff] [blame] | 237 | if (odbc_fetch_into($odbc_result, $rs)) |
| 238 | { |
| 239 | $rs_assoc = array(); |
| 240 | foreach ($rs as $k => $v) |
| 241 | { |
| 242 | $field_name = odbc_field_name($odbc_result, $k+1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | $rs_assoc[$field_name] = $v; |
| 244 | } |
| 245 | } |
| 246 | return $rs_assoc; |
| 247 | } |
| 248 | |
Andrey Andreev | cc5af53 | 2012-03-05 14:02:15 +0200 | [diff] [blame] | 249 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | |
Andrey Andreev | cc5af53 | 2012-03-05 14:02:15 +0200 | [diff] [blame] | 251 | /** |
| 252 | * Query result. Array version. |
| 253 | * |
| 254 | * @return array |
| 255 | */ |
| 256 | public function result_array() |
| 257 | { |
| 258 | if (count($this->result_array) > 0) |
| 259 | { |
| 260 | return $this->result_array; |
| 261 | } |
| 262 | elseif (($c = count($this->result_object)) > 0) |
| 263 | { |
| 264 | for ($i = 0; $i < $c; $i++) |
| 265 | { |
| 266 | $this->result_array[$i] = (array) $this->result_object[$i]; |
| 267 | } |
| 268 | } |
| 269 | elseif ($this->result_id === FALSE) |
| 270 | { |
| 271 | return array(); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | while ($row = $this->_fetch_assoc()) |
| 276 | { |
| 277 | $this->result_array[] = $row; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return $this->result_array; |
| 282 | } |
| 283 | |
Andrey Andreev | 9c68c31 | 2012-03-06 10:34:58 +0200 | [diff] [blame] | 284 | // -------------------------------------------------------------------- |
| 285 | |
| 286 | /** |
| 287 | * Query result. Object version. |
| 288 | * |
| 289 | * @return array |
| 290 | */ |
| 291 | public function result_object() |
| 292 | { |
| 293 | if (count($this->result_object) > 0) |
| 294 | { |
| 295 | return $this->result_object; |
| 296 | } |
| 297 | elseif (($c = count($this->result_array)) > 0) |
| 298 | { |
| 299 | for ($i = 0; $i < $c; $i++) |
| 300 | { |
| 301 | $this->result_object[$i] = (object) $this->result_array[$i]; |
| 302 | } |
| 303 | } |
| 304 | elseif ($this->result_id === FALSE) |
| 305 | { |
| 306 | return array(); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | while ($row = $this->_fetch_object()) |
| 311 | { |
| 312 | $this->result_object[] = $row; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return $this->result_object; |
| 317 | } |
| 318 | |
Andrey Andreev | cc5af53 | 2012-03-05 14:02:15 +0200 | [diff] [blame] | 319 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | |
| 321 | /* End of file odbc_result.php */ |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 322 | /* Location: ./system/database/drivers/odbc/odbc_result.php */ |