Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * ODBC Result Class |
| 31 | * |
| 32 | * This class extends the parent result class: CI_DB_result |
| 33 | * |
| 34 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/database/ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 37 | * @since 1.3 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | */ |
| 39 | class CI_DB_odbc_result extends CI_DB_result { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 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 | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 44 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +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)) |
Andrey Andreev | 78b24bf | 2012-02-29 16:43:57 +0200 | [diff] [blame] | 49 | { |
| 50 | return $this->num_rows; |
| 51 | } |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 52 | elseif (($this->num_rows = @odbc_num_rows($this->result_id)) !== -1) |
Andrey Andreev | 78b24bf | 2012-02-29 16:43:57 +0200 | [diff] [blame] | 53 | { |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 54 | return $this->num_rows; |
Andrey Andreev | 78b24bf | 2012-02-29 16:43:57 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Andrey Andreev | c7db6bb | 2012-07-05 15:11:20 +0300 | [diff] [blame] | 57 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 70 | // -------------------------------------------------------------------- |
| 71 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Number of fields in the result set |
| 74 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 75 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 77 | public function num_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | * @return array |
| 90 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 91 | public function list_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | { |
| 93 | $field_names = array(); |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 94 | $num_fields = $this->num_fields(); |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 95 | |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 96 | if ($num_fields > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | { |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 98 | for ($i = 1; $i <= $num_fields; $i++) |
| 99 | { |
Andrey Andreev | 41e46a9 | 2012-03-01 14:58:17 +0200 | [diff] [blame] | 100 | $field_names[] = odbc_field_name($this->result_id, $i); |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 101 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | return $field_names; |
| 105 | } |
| 106 | |
| 107 | // -------------------------------------------------------------------- |
| 108 | |
| 109 | /** |
| 110 | * Field data |
| 111 | * |
| 112 | * Generates an array of objects containing field meta-data |
| 113 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | * @return array |
| 115 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 116 | public function field_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | { |
| 118 | $retval = array(); |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 119 | 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] | 120 | { |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 121 | $retval[$i] = new stdClass(); |
Andrey Andreev | bb8a683 | 2012-02-14 12:17:13 +0200 | [diff] [blame] | 122 | $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 Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 125 | $retval[$i]->primary_key = 0; |
| 126 | $retval[$i]->default = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | return $retval; |
| 130 | } |
| 131 | |
| 132 | // -------------------------------------------------------------------- |
| 133 | |
| 134 | /** |
| 135 | * Free the result |
| 136 | * |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 137 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 139 | public function free_result() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | * Result - associative array |
| 152 | * |
| 153 | * Returns the result set as an array |
| 154 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | * @return array |
| 156 | */ |
Andrey Andreev | b76029d | 2012-01-26 15:13:19 +0200 | [diff] [blame] | 157 | protected function _fetch_assoc() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | { |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 159 | return odbc_fetch_array($this->result_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Result - object |
| 166 | * |
| 167 | * Returns the result set as an object |
| 168 | * |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame^] | 169 | * @param string $class_name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | * @return object |
| 171 | */ |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 172 | protected function _fetch_object($class_name = 'stdClass') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 174 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } |
| 191 | |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 192 | // -------------------------------------------------------------------- |
| 193 | |
| 194 | if ( ! 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 Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame^] | 202 | * @param resource &$result |
| 203 | * @param int $rownumber |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 204 | * @return array |
| 205 | */ |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame^] | 206 | function odbc_fetch_array(&$result, $rownumber = 1) |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 207 | { |
| 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 | |
| 227 | if ( ! 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 Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame^] | 235 | * @param resource &$result |
| 236 | * @param int $rownumber |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 237 | * @return object |
| 238 | */ |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame^] | 239 | function odbc_fetch_object(&$result, $rownumber = 1) |
Andrey Andreev | 08364ea | 2012-07-04 23:10:46 +0300 | [diff] [blame] | 240 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | /* End of file odbc_result.php */ |
Andrey Andreev | 19aee03 | 2012-03-20 15:31:42 +0200 | [diff] [blame] | 259 | /* Location: ./system/database/drivers/odbc/odbc_result.php */ |