blob: a2b600e6c873bccf242c2063fc4b51b5400cebd8 [file] [log] [blame]
Andrey Andreev24abcb92012-01-05 20:40:15 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev24abcb92012-01-05 20:40:15 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24abcb92012-01-05 20:40:15 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreev342a4662012-01-24 15:24:00 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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 Mienydd671972010-10-04 16:33:58 +020019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
Barry Mienydd671972010-10-04 16:33:58 +020024 * @since Version 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000025 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * oci8 Result Class
30 *
31 * This class extends the parent result class: CI_DB_result
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
Andrey Andreev5ca05132012-07-05 12:06:34 +030036 * @since 1.4.1
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
38class CI_DB_oci8_result extends CI_DB_result {
39
Andrey Andreev24abcb92012-01-05 20:40:15 +020040 public $stmt_id;
41 public $curs_id;
42 public $limit_used;
Andrey Andreev99013ed2012-03-05 16:17:32 +020043 public $commit_mode;
Andrey Andreev24abcb92012-01-05 20:40:15 +020044
Andrey Andreev5ca05132012-07-05 12:06:34 +030045 /**
46 * Constructor
47 *
48 * @param object
49 * @return void
Andrey Andreev24abcb92012-01-05 20:40:15 +020050 */
Andrey Andreev57bdeb62012-03-05 15:59:16 +020051 public function __construct(&$driver_object)
52 {
53 parent::__construct($driver_object);
Andrey Andreev5ca05132012-07-05 12:06:34 +030054
Andrey Andreev57bdeb62012-03-05 15:59:16 +020055 $this->stmt_id = $driver_object->stmt_id;
56 $this->curs_id = $driver_object->curs_id;
57 $this->limit_used = $driver_object->limit_used;
Andrey Andreev99013ed2012-03-05 16:17:32 +020058 $this->commit_mode =& $driver_object->commit_mode;
Andrey Andreev57bdeb62012-03-05 15:59:16 +020059 $driver_object->stmt_id = FALSE;
60 }
Derek Allard2067d1a2008-11-13 22:59:24 +000061
Andrey Andreev5ca05132012-07-05 12:06:34 +030062 // --------------------------------------------------------------------
63
Derek Allard2067d1a2008-11-13 22:59:24 +000064 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000065 * Number of fields in the result set
66 *
Andrey Andreevaa786c92012-01-16 12:14:45 +020067 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000068 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030069 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030071 $count = @oci_num_fields($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +000072
73 // if we used a limit we subtract it
Andrey Andreevaa786c92012-01-16 12:14:45 +020074 return ($this->limit_used) ? $count - 1 : $count;
Derek Allard2067d1a2008-11-13 22:59:24 +000075 }
76
77 // --------------------------------------------------------------------
78
79 /**
80 * Fetch Field Names
81 *
82 * Generates an array of column names
83 *
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * @return array
85 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030086 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 $field_names = array();
Andrey Andreev5c3a2022011-10-07 21:04:58 +030089 for ($c = 1, $fieldCount = $this->num_fields(); $c <= $fieldCount; $c++)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030091 $field_names[] = oci_field_name($this->stmt_id, $c);
Derek Allard2067d1a2008-11-13 22:59:24 +000092 }
93 return $field_names;
94 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * Field data
100 *
101 * Generates an array of objects containing field meta-data
102 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200103 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300105 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
107 $retval = array();
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300108 for ($c = 1, $fieldCount = $this->num_fields(); $c <= $fieldCount; $c++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200110 $F = new stdClass();
111 $F->name = oci_field_name($this->stmt_id, $c);
112 $F->type = oci_field_type($this->stmt_id, $c);
113 $F->max_length = oci_field_size($this->stmt_id, $c);
Derek Allard2067d1a2008-11-13 22:59:24 +0000114
115 $retval[] = $F;
116 }
117
118 return $retval;
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Free the result
125 *
Andrey Andreev24abcb92012-01-05 20:40:15 +0200126 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300128 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 if (is_resource($this->result_id))
131 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300132 oci_free_statement($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 $this->result_id = FALSE;
134 }
Andrey Andreev24abcb92012-01-05 20:40:15 +0200135
136 if (is_resource($this->stmt_id))
137 {
138 oci_free_statement($this->stmt_id);
139 }
140
141 if (is_resource($this->curs_id))
142 {
143 oci_cancel($this->curs_id);
144 $this->curs_id = NULL;
145 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Result - associative array
152 *
153 * Returns the result set as an array
154 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200155 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300157 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
159 $id = ($this->curs_id) ? $this->curs_id : $this->stmt_id;
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300160 return oci_fetch_assoc($id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Result - object
167 *
168 * Returns the result set as an object
169 *
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300170 * @param string
Andrey Andreevaa786c92012-01-16 12:14:45 +0200171 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300173 protected function _fetch_object($class_name = 'stdClass')
Barry Mienydd671972010-10-04 16:33:58 +0200174 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300175 $row = ($this->curs_id)
176 ? oci_fetch_object($this->curs_id)
177 : oci_fetch_object($this->stmt_id);
178
179 if ($class_name === 'stdClass' OR ! $row)
180 {
181 return $row;
182 }
183
184 $class_name = new $class_name();
185 foreach ($row as $key => $value)
186 {
187 $class_name->$key = $value;
188 }
189
190 return $class_name;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200191 }
192
193 // --------------------------------------------------------------------
194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 * Data Seek
197 *
Andrey Andreev24abcb92012-01-05 20:40:15 +0200198 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 * this internally before fetching results to make sure the
Andrey Andreev24abcb92012-01-05 20:40:15 +0200200 * result set starts at zero.
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 *
Andrey Andreev24abcb92012-01-05 20:40:15 +0200202 * Oracle's PHP extension doesn't have an easy way of doing this
203 * and the only workaround is to (re)execute the statement or cursor
204 * in order to go to the first (zero) index of the result set.
205 * Then, we would need to "dummy" fetch ($n - 1) rows to get to the
206 * right one.
207 *
208 * This is as ridiculous as it sounds and it's the reason why every
209 * other method that is fetching data tries to use an already "cached"
210 * result set. Keeping this just in case it becomes needed at
211 * some point in the future, but it will only work for resetting the
212 * pointer to zero.
213 *
214 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200216 protected function _data_seek()
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200218 /* The PHP manual says that if OCI_NO_AUTO_COMMIT mode
219 * is used, and oci_rollback() and/or oci_commit() are
220 * not subsequently called - this will cause an unnecessary
221 * rollback to be triggered at the end of the script execution.
222 *
223 * Therefore we'll try to avoid using that mode flag
224 * if we're not currently in the middle of a transaction.
225 */
226 if ($this->commit_mode !== OCI_COMMIT_ON_SUCCESS)
227 {
228 $result = @oci_execute($this->stmt_id, $this->commit_mode);
229 }
230 else
231 {
232 $result = @oci_execute($this->stmt_id);
233 }
234
235 if ($result && $this->curs_id)
236 {
237 if ($this->commit_mode !== OCI_COMMIT_ON_SUCCESS)
238 {
239 return @oci_execute($this->curs_id, $this->commit_mode);
240 }
241 else
242 {
243 return @oci_execute($this->curs_id);
244 }
245 }
246
247 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249
250}
251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252/* End of file oci8_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400253/* Location: ./system/database/drivers/oci8/oci8_result.php */