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