Andrey Andreev | b76029d | 2012-01-26 15:13:19 +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 | b76029d | 2012-01-26 15:13:19 +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 | b76029d | 2012-01-26 15:13:19 +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/ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame^] | 36 | * @since 1.3 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | */ |
| 38 | class CI_DB_odbc_result extends CI_DB_result { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Number of rows in the result set |
| 42 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 43 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 45 | public function num_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | { |
Andrey Andreev | ef795ac | 2012-03-01 15:15:31 +0200 | [diff] [blame] | 47 | if (is_int($this->num_rows)) |
Andrey Andreev | 78b24bf | 2012-02-29 16:43:57 +0200 | [diff] [blame] | 48 | { |
| 49 | return $this->num_rows; |
| 50 | } |
| 51 | |
| 52 | // Work-around for ODBC subdrivers that don't support num_rows() |
| 53 | if (($this->num_rows = @odbc_num_rows($this->result_id)) === -1) |
| 54 | { |
Andrey Andreev | ef795ac | 2012-03-01 15:15:31 +0200 | [diff] [blame] | 55 | $this->num_rows = count($this->result_array()); |
Andrey Andreev | 78b24bf | 2012-02-29 16:43:57 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return $this->num_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame^] | 61 | // -------------------------------------------------------------------- |
| 62 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Number of fields in the result set |
| 65 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 66 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 68 | public function num_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | { |
| 70 | return @odbc_num_fields($this->result_id); |
| 71 | } |
| 72 | |
| 73 | // -------------------------------------------------------------------- |
| 74 | |
| 75 | /** |
| 76 | * Fetch Field Names |
| 77 | * |
| 78 | * Generates an array of column names |
| 79 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | * @return array |
| 81 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 82 | public function list_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | { |
| 84 | $field_names = array(); |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 85 | $num_fields = $this->num_fields(); |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 86 | |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 87 | if ($num_fields > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | { |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 89 | for ($i = 1; $i <= $num_fields; $i++) |
| 90 | { |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 91 | $field_names[] = odbc_field_name($this->result_id, $i); |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 92 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | return $field_names; |
| 96 | } |
| 97 | |
| 98 | // -------------------------------------------------------------------- |
| 99 | |
| 100 | /** |
| 101 | * Field data |
| 102 | * |
| 103 | * Generates an array of objects containing field meta-data |
| 104 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | * @return array |
| 106 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 107 | public function field_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | { |
| 109 | $retval = array(); |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 110 | 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] | 111 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 112 | $retval[$i] = new stdClass(); |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 113 | $retval[$i]->name = odbc_field_name($this->result_id, $odbc_index); |
| 114 | $retval[$i]->type = odbc_field_type($this->result_id, $odbc_index); |
| 115 | $retval[$i]->max_length = odbc_field_len($this->result_id, $odbc_index); |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 116 | $retval[$i]->primary_key = 0; |
| 117 | $retval[$i]->default = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | return $retval; |
| 121 | } |
| 122 | |
| 123 | // -------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
| 126 | * Free the result |
| 127 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 128 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 130 | public function free_result() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | { |
| 132 | if (is_resource($this->result_id)) |
| 133 | { |
| 134 | odbc_free_result($this->result_id); |
| 135 | $this->result_id = FALSE; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // -------------------------------------------------------------------- |
| 140 | |
| 141 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | * Result - associative array |
| 143 | * |
| 144 | * Returns the result set as an array |
| 145 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | * @return array |
| 147 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 148 | protected function _fetch_assoc() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | { |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 150 | return odbc_fetch_array($this->result_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // -------------------------------------------------------------------- |
| 154 | |
| 155 | /** |
| 156 | * Result - object |
| 157 | * |
| 158 | * Returns the result set as an object |
| 159 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | * @return object |
| 161 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 162 | protected function _fetch_object() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | { |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 164 | return odbc_fetch_object($this->result_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Andrey Andreev | cc5af53 | 2012-03-05 14:02:15 +0200 | [diff] [blame] | 167 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | |
Andrey Andreev | cc5af53 | 2012-03-05 14:02:15 +0200 | [diff] [blame] | 169 | /** |
| 170 | * Query result. Array version. |
| 171 | * |
| 172 | * @return array |
| 173 | */ |
| 174 | public function result_array() |
| 175 | { |
| 176 | if (count($this->result_array) > 0) |
| 177 | { |
| 178 | return $this->result_array; |
| 179 | } |
| 180 | elseif (($c = count($this->result_object)) > 0) |
| 181 | { |
| 182 | for ($i = 0; $i < $c; $i++) |
| 183 | { |
| 184 | $this->result_array[$i] = (array) $this->result_object[$i]; |
| 185 | } |
| 186 | } |
| 187 | elseif ($this->result_id === FALSE) |
| 188 | { |
| 189 | return array(); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | while ($row = $this->_fetch_assoc()) |
| 194 | { |
| 195 | $this->result_array[] = $row; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return $this->result_array; |
| 200 | } |
| 201 | |
Andrey Andreev | 9c68c31 | 2012-03-06 10:34:58 +0200 | [diff] [blame] | 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
| 205 | * Query result. Object version. |
| 206 | * |
| 207 | * @return array |
| 208 | */ |
| 209 | public function result_object() |
| 210 | { |
| 211 | if (count($this->result_object) > 0) |
| 212 | { |
| 213 | return $this->result_object; |
| 214 | } |
| 215 | elseif (($c = count($this->result_array)) > 0) |
| 216 | { |
| 217 | for ($i = 0; $i < $c; $i++) |
| 218 | { |
| 219 | $this->result_object[$i] = (object) $this->result_array[$i]; |
| 220 | } |
| 221 | } |
| 222 | elseif ($this->result_id === FALSE) |
| 223 | { |
| 224 | return array(); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | while ($row = $this->_fetch_object()) |
| 229 | { |
| 230 | $this->result_object[] = $row; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return $this->result_object; |
| 235 | } |
| 236 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 239 | // -------------------------------------------------------------------- |
| 240 | |
| 241 | if ( ! function_exists('odbc_fetch_array')) |
| 242 | { |
| 243 | /** |
| 244 | * ODBC Fetch array |
| 245 | * |
| 246 | * Emulates the native odbc_fetch_array() function when |
| 247 | * it is not available (odbc_fetch_array() requires unixODBC) |
| 248 | * |
| 249 | * @param resource |
| 250 | * @param int |
| 251 | * @return array |
| 252 | */ |
| 253 | function odbc_fetch_array(& $result, $rownumber = 1) |
| 254 | { |
| 255 | $rs = array(); |
| 256 | if ( ! odbc_fetch_into($result, $rs, $rownumber)) |
| 257 | { |
| 258 | return FALSE; |
| 259 | } |
| 260 | |
| 261 | $rs_assoc = array(); |
| 262 | foreach ($rs as $k => $v) |
| 263 | { |
| 264 | $field_name = odbc_field_name($result, $k+1); |
| 265 | $rs_assoc[$field_name] = $v; |
| 266 | } |
| 267 | |
| 268 | return $rs_assoc; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // -------------------------------------------------------------------- |
| 273 | |
| 274 | if ( ! function_exists('odbc_fetch_object')) |
| 275 | { |
| 276 | /** |
| 277 | * ODBC Fetch object |
| 278 | * |
| 279 | * Emulates the native odbc_fetch_object() function when |
| 280 | * it is not available. |
| 281 | * |
| 282 | * @param resource |
| 283 | * @param int |
| 284 | * @return object |
| 285 | */ |
| 286 | function odbc_fetch_object(& $result, $rownumber = 1) |
| 287 | { |
| 288 | $rs = array(); |
| 289 | if ( ! odbc_fetch_into($result, $rs, $rownumber)) |
| 290 | { |
| 291 | return FALSE; |
| 292 | } |
| 293 | |
| 294 | $rs_object = new stdClass(); |
| 295 | foreach ($rs as $k => $v) |
| 296 | { |
| 297 | $field_name = odbc_field_name($result, $k+1); |
| 298 | $rs_object->$field_name = $v; |
| 299 | } |
| 300 | |
| 301 | return $rs_object; |
| 302 | } |
| 303 | } |
| 304 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | /* End of file odbc_result.php */ |
Andrey Andreev | 19aee03 | 2012-03-20 15:31:42 +0200 | [diff] [blame] | 306 | /* Location: ./system/database/drivers/odbc/odbc_result.php */ |