Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +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 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 | 24abcb9 | 2012-01-05 20:40:15 +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 | 24abcb9 | 2012-01-05 20:40:15 +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 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 24 | * @since Version 1.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * oci8 Result Class |
| 32 | * |
| 33 | * This class extends the parent result class: CI_DB_result |
| 34 | * |
| 35 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/database/ |
| 38 | */ |
| 39 | class CI_DB_oci8_result extends CI_DB_result { |
| 40 | |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 41 | public $stmt_id; |
| 42 | public $curs_id; |
| 43 | public $limit_used; |
| 44 | |
| 45 | // This will be changed by CI_DB_driver, but it's good to have a default: |
| 46 | public $commit_mode = OCI_DEFAULT; |
| 47 | |
| 48 | /* Overwriting the parent here, so we have a way to know if it's |
| 49 | * already called or not: |
| 50 | */ |
| 51 | public $num_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Number of rows in the result set. |
| 55 | * |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 56 | * Oracle doesn't have a graceful way to return the number of rows |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | * so we have to use what amounts to a hack. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 58 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 59 | * @return integer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 61 | public function num_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | { |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 63 | if ( ! is_int($this->num_rows)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | { |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 65 | if (count($this->result_array) > 0) |
Andrey Andreev | ef3e240 | 2011-09-21 14:39:29 +0300 | [diff] [blame] | 66 | { |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 67 | return $this->num_rows = count($this->result_array); |
Andrey Andreev | ef3e240 | 2011-09-21 14:39:29 +0300 | [diff] [blame] | 68 | } |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 69 | elseif (count($this->result_object) > 0) |
| 70 | { |
| 71 | return $this->num_rows = count($this->result_array); |
| 72 | } |
| 73 | |
| 74 | return $this->num_rows = count($this->result_array()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Andrey Andreev | ef3e240 | 2011-09-21 14:39:29 +0300 | [diff] [blame] | 77 | return $this->num_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
| 83 | * Number of fields in the result set |
| 84 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 85 | * @return integer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 87 | public function num_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 89 | $count = @oci_num_fields($this->stmt_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | |
| 91 | // if we used a limit we subtract it |
| 92 | if ($this->limit_used) |
| 93 | { |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 94 | return $count - 1; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | return $count; |
| 98 | } |
| 99 | |
| 100 | // -------------------------------------------------------------------- |
| 101 | |
| 102 | /** |
| 103 | * Fetch Field Names |
| 104 | * |
| 105 | * Generates an array of column names |
| 106 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | * @return array |
| 108 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 109 | public function list_fields() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | { |
| 111 | $field_names = array(); |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 112 | for ($c = 1, $fieldCount = $this->num_fields(); $c <= $fieldCount; $c++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 114 | $field_names[] = oci_field_name($this->stmt_id, $c); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | } |
| 116 | return $field_names; |
| 117 | } |
| 118 | |
| 119 | // -------------------------------------------------------------------- |
| 120 | |
| 121 | /** |
| 122 | * Field data |
| 123 | * |
| 124 | * Generates an array of objects containing field meta-data |
| 125 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 126 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 128 | public function field_data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | { |
| 130 | $retval = array(); |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 131 | for ($c = 1, $fieldCount = $this->num_fields(); $c <= $fieldCount; $c++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 133 | $F = new stdClass(); |
| 134 | $F->name = oci_field_name($this->stmt_id, $c); |
| 135 | $F->type = oci_field_type($this->stmt_id, $c); |
| 136 | $F->max_length = oci_field_size($this->stmt_id, $c); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | |
| 138 | $retval[] = $F; |
| 139 | } |
| 140 | |
| 141 | return $retval; |
| 142 | } |
| 143 | |
| 144 | // -------------------------------------------------------------------- |
| 145 | |
| 146 | /** |
| 147 | * Free the result |
| 148 | * |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 149 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 151 | public function free_result() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | { |
| 153 | if (is_resource($this->result_id)) |
| 154 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 155 | oci_free_statement($this->result_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | $this->result_id = FALSE; |
| 157 | } |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 158 | |
| 159 | if (is_resource($this->stmt_id)) |
| 160 | { |
| 161 | oci_free_statement($this->stmt_id); |
| 162 | } |
| 163 | |
| 164 | if (is_resource($this->curs_id)) |
| 165 | { |
| 166 | oci_cancel($this->curs_id); |
| 167 | $this->curs_id = NULL; |
| 168 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
| 174 | * Result - associative array |
| 175 | * |
| 176 | * Returns the result set as an array |
| 177 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 178 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 180 | protected function _fetch_assoc() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | { |
| 182 | $id = ($this->curs_id) ? $this->curs_id : $this->stmt_id; |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 183 | return oci_fetch_assoc($id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
| 189 | * Result - object |
| 190 | * |
| 191 | * Returns the result set as an object |
| 192 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 193 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 195 | protected function _fetch_object() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 197 | $id = ($this->curs_id) ? $this->curs_id : $this->stmt_id; |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 198 | return oci_fetch_object($id); |
| 199 | } |
| 200 | |
| 201 | // -------------------------------------------------------------------- |
| 202 | |
| 203 | /* Query result |
| 204 | * |
| 205 | * Acts as a wrapper for result_array(), result_object() |
| 206 | * and custom_result_object(). |
| 207 | * |
| 208 | * @param string ('object', 'array' or a custom class name) |
| 209 | * @return array |
| 210 | */ |
| 211 | |
| 212 | public function result($type = 'object') |
| 213 | { |
| 214 | if ($type === 'object') |
| 215 | { |
| 216 | return $this->result_object(); |
| 217 | } |
| 218 | elseif ($type === 'array') |
| 219 | { |
| 220 | return $this->result_array(); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | return $this->custom_result_object($type); |
| 225 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // -------------------------------------------------------------------- |
| 229 | |
| 230 | /** |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 231 | * Query result. "array" version. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | * |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 233 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 235 | public function result_array() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | { |
| 237 | if (count($this->result_array) > 0) |
| 238 | { |
| 239 | return $this->result_array; |
| 240 | } |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 241 | elseif (count($this->result_object) > 0) |
| 242 | { |
| 243 | for ($i = 0, $c = count($this->result_object); $i < $c; $i++) |
| 244 | { |
| 245 | $this->result_array[$i] = (array) $this->result_object[$i]; |
| 246 | } |
| 247 | |
| 248 | return $this->result_array; |
| 249 | } |
| 250 | elseif (is_array($this->row_data)) |
| 251 | { |
| 252 | if (count($this->row_data) === 0) |
| 253 | { |
| 254 | return $this->result_array; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | $row_index = count($this->row_data); |
| 259 | } |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | $row_index = 0; |
| 264 | $this->row_data = array(); |
| 265 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | $row = NULL; |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 268 | while ($row = $this->_fetch_assoc()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | { |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 270 | $this->row_data[$row_index++] = $row; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 273 | // Un-comment the following line, in case it becomes needed |
| 274 | // $this->_data_seek(); |
| 275 | return $this->result_array = $this->row_data; |
| 276 | } |
| 277 | |
| 278 | // -------------------------------------------------------------------- |
| 279 | |
| 280 | /** |
| 281 | * Query result. "object" version. |
| 282 | * |
| 283 | * @return array |
| 284 | */ |
| 285 | public function result_object() |
| 286 | { |
| 287 | if (count($this->result_object) > 0) |
| 288 | { |
| 289 | return $this->result_object; |
| 290 | } |
| 291 | elseif (count($this->result_array) > 0) |
| 292 | { |
| 293 | for ($i = 0, $c = count($this->result_array); $i < $c; $i++) |
| 294 | { |
| 295 | $this->result_object[] = (object) $this->result_array[$i]; |
| 296 | } |
| 297 | |
| 298 | return $this->result_object; |
| 299 | } |
| 300 | elseif (is_array($this->row_data)) |
| 301 | { |
| 302 | if (count($this->row_data) === 0) |
| 303 | { |
| 304 | return $this->result_object; |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | $row_index = count($this->row_data); |
| 309 | for ($i = 0; $i < $row_index; $i++) |
| 310 | { |
| 311 | $this->result_object[$i] = (object) $this->row_data[$i]; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | $row_index = 0; |
| 318 | $this->row_data = array(); |
| 319 | } |
| 320 | |
| 321 | $row = NULL; |
| 322 | while ($row = $this->_fetch_object()) |
| 323 | { |
| 324 | $this->row_data[$row_index] = (array) $row; |
| 325 | $this->result_object[$row_index++] = $row; |
| 326 | } |
| 327 | |
| 328 | // Un-comment the following line, in case it becomes needed |
| 329 | // $this->_data_seek(); |
| 330 | return $this->result_object; |
| 331 | } |
| 332 | |
| 333 | // -------------------------------------------------------------------- |
| 334 | |
| 335 | /** |
| 336 | * Query result. Custom object version. |
| 337 | * |
| 338 | * @param string class name used to instantiate rows to |
| 339 | * @return array |
| 340 | */ |
| 341 | public function custom_result_object($class_name) |
| 342 | { |
| 343 | if (array_key_exists($class_name, $this->custom_result_object)) |
| 344 | { |
| 345 | return $this->custom_result_object[$class_name]; |
| 346 | } |
| 347 | |
| 348 | if ( ! class_exists($class_name) OR $this->result_id === FALSE OR $this->num_rows() === 0) |
| 349 | { |
| 350 | return array(); |
| 351 | } |
| 352 | |
| 353 | // add the data to the object |
| 354 | $result_object = array(); |
| 355 | $data = NULL; |
| 356 | |
| 357 | /* First check if we don't already have the data. |
| 358 | * If so - pass by reference, as we don't know how |
| 359 | * large it might be and we don't want 1000 row |
| 360 | * sets being copied. |
| 361 | */ |
| 362 | if (count($this->result_array) > 0) |
| 363 | { |
| 364 | $data = &$this->result_array; |
| 365 | } |
| 366 | elseif (count($this->result_object) > 0) |
| 367 | { |
| 368 | $data = &$this->result_object; |
| 369 | } |
| 370 | |
| 371 | if ( ! is_null($data)) |
| 372 | { |
| 373 | for ($i = 0, $c = count($data); $i < $c; $i++) |
| 374 | { |
| 375 | $result_object[$i] = new $class_name(); |
| 376 | foreach ($data[$i] as $key => $value) |
| 377 | { |
| 378 | $result_object[$i]->$key = $value; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | // No prefetched result set |
| 385 | if (is_array($this->row_data)) |
| 386 | { |
| 387 | $row_index = count($this->row_data); |
| 388 | if ($row_index === 0) |
| 389 | { |
| 390 | return array(); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | for ($i = 0; $i < $row_index; $i++) |
| 395 | { |
| 396 | $result_object[$i] = new $class_name(); |
| 397 | foreach ($this->row_data[$i] as $key => $value) |
| 398 | { |
| 399 | $result_object[$i]->$key = $value; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | $row_index = 0; |
| 407 | $this->row_data = array(); |
| 408 | } |
| 409 | |
| 410 | while ($row = $this->_fetch_assoc()) |
| 411 | { |
| 412 | $data = new $class_name(); |
| 413 | foreach ($row as $key => $value) |
| 414 | { |
| 415 | $data->$key = $value; |
| 416 | } |
| 417 | |
| 418 | $this->row_data[$row_index] = $row; |
| 419 | $result_object[$row_index++] = $data; |
| 420 | } |
| 421 | // Un-comment the following line, in case it becomes needed |
| 422 | // $this->_data_seek(); |
| 423 | } |
| 424 | |
| 425 | /* As described for the num_rows() method - there's no easy |
| 426 | * way to get the number of rows selected. Our work-around |
| 427 | * solution (as in here as well) first checks if result_array |
| 428 | * or result_object exist and returns their count. It doesn't |
| 429 | * however check for a custom_object_result, so - do it here. |
| 430 | */ |
| 431 | if ( ! is_int($this->num_rows)) |
| 432 | { |
| 433 | $this->num_rows = count($result_object); |
| 434 | } |
| 435 | |
| 436 | // Cache and return the array |
| 437 | return $this->custom_result_object[$class_name] = $result_object; |
| 438 | } |
| 439 | |
| 440 | // -------------------------------------------------------------------- |
| 441 | |
| 442 | /* Single row result. |
| 443 | * |
| 444 | * Acts as a wrapper for row_object(), row_array() |
| 445 | * and custom_row_object(). Also used by first_row(), next_row() |
| 446 | * and previous_row(). |
| 447 | * |
| 448 | * @param int row index |
| 449 | * @param string ('object', 'array' or a custom class name) |
| 450 | * @return mixed whatever was passed to the second parameter |
| 451 | */ |
| 452 | |
| 453 | public function row($n = 0, $type = 'object') |
| 454 | { |
| 455 | if ($type === 'object') return $this->row_object($n); |
| 456 | elseif ($type === 'array') return $this->row_array($n); |
| 457 | else return $this->custom_row_object($n, $type); |
| 458 | } |
| 459 | |
| 460 | // -------------------------------------------------------------------- |
| 461 | |
| 462 | /* Single row result. Array version. |
| 463 | * |
| 464 | * @param int row index |
| 465 | * @return array |
| 466 | */ |
| 467 | |
| 468 | public function row_array($n = 0) |
| 469 | { |
| 470 | // Make sure $n is not a string |
| 471 | if ( ! is_int($n)) |
| 472 | { |
| 473 | $n = (int) $n; |
| 474 | } |
| 475 | |
| 476 | /* If row_data is initialized, it means that we've already tried |
| 477 | * (at least) to fetch some data, so ... check if we already have |
| 478 | * this row. |
| 479 | */ |
| 480 | if (is_array($this->row_data)) |
| 481 | { |
| 482 | /* If we already have row_data[$n] - return it. |
| 483 | * |
| 484 | * If we enter the elseif, there's a number of reasons to |
| 485 | * return an empty array: |
| 486 | * |
| 487 | * - count($this->row_data) === 0 means there are no results |
| 488 | * - num_rows being set, result_array and/or result_object |
| 489 | * having count() > 0 means that we've already fetched all |
| 490 | * data and $n is greater than our highest row index available |
| 491 | * - $n < $this->current_row means that if such row existed, |
| 492 | * we would've already returned it, therefore $n is an |
| 493 | * invalid index |
| 494 | */ |
| 495 | if (isset($this->row_data[$n])) // We already have this row |
| 496 | { |
| 497 | $this->current_row = $n; |
| 498 | return $this->row_data[$n]; |
| 499 | } |
| 500 | elseif (count($this->row_data) === 0 OR is_int($this->num_rows) |
| 501 | OR count($this->result_array) > 0 OR count($this->result_object) > 0 |
| 502 | OR $n < $this->current_row) |
| 503 | { |
| 504 | // No such row exists |
| 505 | return array(); |
| 506 | } |
| 507 | |
| 508 | // Get the next row index that would actually need to be fetched |
| 509 | $current_row = ($this->current_row < count($this->row_data)) ? count($this->row_data) : $this->current_row + 1; |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | $current_row = $this->current_row = 0; |
| 514 | $this->row_data = array(); |
| 515 | } |
| 516 | |
| 517 | /* Fetch more data, if available |
| 518 | * |
| 519 | * NOTE: Operator precedence is important here, if you change |
| 520 | * 'AND' with '&&' - it WILL BREAK the results, as |
| 521 | * $row will be assigned the scalar value of both |
| 522 | * expressions! |
| 523 | */ |
| 524 | while ($row = $this->_fetch_assoc() AND $current_row <= $n) |
| 525 | { |
| 526 | $this->row_data[$current_row++] = $row; |
| 527 | } |
| 528 | |
| 529 | // This would mean that there's no (more) data to fetch |
| 530 | if ( ! is_array($this->row_data) OR ! isset($this->row_data[$n])) |
| 531 | { |
| 532 | // Cache what we already have |
| 533 | if (is_array($this->row_data)) |
| 534 | { |
| 535 | $this->num_rows = count($this->row_data); |
| 536 | /* Usually, row_data could have less elements than result_array, |
| 537 | * but at this point - they should be exactly the same. |
| 538 | */ |
| 539 | $this->result_array = $this->row_data; |
| 540 | } |
| 541 | else |
| 542 | { |
| 543 | $this->num_rows = 0; |
| 544 | } |
| 545 | |
| 546 | return array(); |
| 547 | } |
| 548 | |
| 549 | $this->current_row = $n; |
| 550 | return $this->row_data[$n]; |
| 551 | } |
| 552 | |
| 553 | // -------------------------------------------------------------------- |
| 554 | |
| 555 | /* Single row result. Object version. |
| 556 | * |
| 557 | * @param int row index |
| 558 | * @return mixed object if row found; empty array if not |
| 559 | */ |
| 560 | public function row_object($n = 0) |
| 561 | { |
| 562 | // Make sure $n is not a string |
| 563 | if ( ! is_int($n)) |
| 564 | { |
| 565 | $n = (int) $n; |
| 566 | } |
| 567 | /* Logic here is exactly the same as in row_array, |
| 568 | * except we have to cast row_data[$n] to an object. |
| 569 | * |
| 570 | * If we already have result_object though - we can |
| 571 | * directly return from it. |
| 572 | */ |
| 573 | if (isset($this->result_object[$n])) |
| 574 | { |
| 575 | $this->current_row = $n; |
| 576 | // Set this, if not already done. |
| 577 | if ( ! is_int($this->num_rows)) |
| 578 | { |
| 579 | $this->num_rows = count($this->result_object); |
| 580 | } |
| 581 | |
| 582 | return $this->result_object[$n]; |
| 583 | } |
| 584 | |
| 585 | $row = $this->row_array($n); |
| 586 | // Cast only if the row exists |
| 587 | if (count($row) > 0) |
| 588 | { |
| 589 | $this->current_row = $n; |
| 590 | return (object) $row; |
| 591 | } |
| 592 | |
| 593 | return array(); |
| 594 | } |
| 595 | |
| 596 | // -------------------------------------------------------------------- |
| 597 | |
| 598 | /* Single row result. Custom object version. |
| 599 | * |
| 600 | * @param int row index |
| 601 | * @param string custom class name |
| 602 | * @return mixed custom object if row found; empty array otherwise |
| 603 | */ |
| 604 | |
| 605 | public function custom_row_object($n = 0, $class_name) |
| 606 | { |
| 607 | // Make sure $n is not a string |
| 608 | if ( ! is_int($n)) |
| 609 | { |
| 610 | $n = (int) $n; |
| 611 | } |
| 612 | |
| 613 | if (array_key_exists($class_name, $this->custom_result_object)) |
| 614 | { |
| 615 | /* We already have a the whole result set with this class_name, |
| 616 | * return the specified row if it exists, and an empty array if |
| 617 | * it doesn't. |
| 618 | */ |
| 619 | if (isset($this->custom_result_object[$class_name][$n])) |
| 620 | { |
| 621 | $this->current_row = $n; |
| 622 | return $this->custom_result_object[$class_name][$n]; |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | return array(); |
| 627 | } |
| 628 | } |
| 629 | elseif ( ! class_exists($class_name)) // No such class exists |
| 630 | { |
| 631 | return array(); |
| 632 | } |
| 633 | |
| 634 | $row = $this->row_array($n); |
| 635 | // An array would mean that the row doesn't exist |
| 636 | if (is_array($row)) |
| 637 | { |
| 638 | return $row; |
| 639 | } |
| 640 | |
| 641 | // Convert to the desired class and return |
| 642 | $row_object = new $class_name(); |
| 643 | foreach ($row as $key => $value) |
| 644 | { |
| 645 | $row_object->$key = $value; |
| 646 | } |
| 647 | |
| 648 | $this->current_row = $n; |
| 649 | return $row_object; |
| 650 | } |
| 651 | |
| 652 | // -------------------------------------------------------------------- |
| 653 | |
| 654 | /* First row result. |
| 655 | * |
| 656 | * @param string ('object', 'array' or a custom class name) |
| 657 | * @return mixed whatever was passed to the second parameter |
| 658 | */ |
| 659 | |
| 660 | public function first_row($type = 'object') |
| 661 | { |
| 662 | return $this->row(0, $type); |
| 663 | } |
| 664 | |
| 665 | // -------------------------------------------------------------------- |
| 666 | |
| 667 | /* Last row result. |
| 668 | * |
| 669 | * @param string ('object', 'array' or a custom class name) |
| 670 | * @return mixed whatever was passed to the second parameter |
| 671 | */ |
| 672 | public function last_row($type = 'object') |
| 673 | { |
| 674 | $result = &$this->result($type); |
| 675 | if ( ! isset($this->num_rows)) |
| 676 | { |
| 677 | $this->num_rows = count($result); |
| 678 | } |
| 679 | $this->current_row = $this->num_rows - 1; |
| 680 | return $result[$this->current_row]; |
| 681 | } |
| 682 | |
| 683 | // -------------------------------------------------------------------- |
| 684 | |
| 685 | /* Next row result. |
| 686 | * |
| 687 | * @param string ('object', 'array' or a custom class name) |
| 688 | * @return mixed whatever was passed to the second parameter |
| 689 | */ |
| 690 | public function next_row($type = 'object') |
| 691 | { |
| 692 | if (is_array($this->row_data)) |
| 693 | { |
| 694 | $count = count($this->row_data); |
| 695 | if ($this->current_row > $count OR ($this->current_row === 0 && $count === 0)) |
| 696 | { |
| 697 | $n = $count; |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | $n = $this->current_row + 1; |
| 702 | } |
| 703 | } |
| 704 | else |
| 705 | { |
| 706 | $n = 0; |
| 707 | } |
| 708 | |
| 709 | return $this->row($n, $type); |
| 710 | } |
| 711 | |
| 712 | // -------------------------------------------------------------------- |
| 713 | |
| 714 | /* Previous row result. |
| 715 | * |
| 716 | * @param string ('object', 'array' or a custom class name) |
| 717 | * @return mixed whatever was passed to the second parameter |
| 718 | */ |
| 719 | public function previous_row($type = 'object') |
| 720 | { |
| 721 | $n = ($this->current_row !== 0) ? $this->current_row - 1 : 0; |
| 722 | return $this->row($n, $type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // -------------------------------------------------------------------- |
| 726 | |
| 727 | /** |
| 728 | * Data Seek |
| 729 | * |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 730 | * Moves the internal pointer to the desired offset. We call |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 731 | * this internally before fetching results to make sure the |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 732 | * result set starts at zero. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 733 | * |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 734 | * Oracle's PHP extension doesn't have an easy way of doing this |
| 735 | * and the only workaround is to (re)execute the statement or cursor |
| 736 | * in order to go to the first (zero) index of the result set. |
| 737 | * Then, we would need to "dummy" fetch ($n - 1) rows to get to the |
| 738 | * right one. |
| 739 | * |
| 740 | * This is as ridiculous as it sounds and it's the reason why every |
| 741 | * other method that is fetching data tries to use an already "cached" |
| 742 | * result set. Keeping this just in case it becomes needed at |
| 743 | * some point in the future, but it will only work for resetting the |
| 744 | * pointer to zero. |
| 745 | * |
| 746 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 747 | */ |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 748 | protected function _data_seek() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 749 | { |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame^] | 750 | /* The PHP manual says that if OCI_NO_AUTO_COMMIT mode |
| 751 | * is used, and oci_rollback() and/or oci_commit() are |
| 752 | * not subsequently called - this will cause an unnecessary |
| 753 | * rollback to be triggered at the end of the script execution. |
| 754 | * |
| 755 | * Therefore we'll try to avoid using that mode flag |
| 756 | * if we're not currently in the middle of a transaction. |
| 757 | */ |
| 758 | if ($this->commit_mode !== OCI_COMMIT_ON_SUCCESS) |
| 759 | { |
| 760 | $result = @oci_execute($this->stmt_id, $this->commit_mode); |
| 761 | } |
| 762 | else |
| 763 | { |
| 764 | $result = @oci_execute($this->stmt_id); |
| 765 | } |
| 766 | |
| 767 | if ($result && $this->curs_id) |
| 768 | { |
| 769 | if ($this->commit_mode !== OCI_COMMIT_ON_SUCCESS) |
| 770 | { |
| 771 | return @oci_execute($this->curs_id, $this->commit_mode); |
| 772 | } |
| 773 | else |
| 774 | { |
| 775 | return @oci_execute($this->curs_id); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | return $result; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | } |
| 783 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 784 | /* End of file oci8_result.php */ |
Andrey Andreev | ef3e240 | 2011-09-21 14:39:29 +0300 | [diff] [blame] | 785 | /* Location: ./system/database/drivers/oci8/oci8_result.php */ |