blob: 90c8523a38b5c907d303420ad79474af0def274b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * oci8 Result Class
31 *
32 * This class extends the parent result class: CI_DB_result
33 *
34 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/database/
Andrey Andreev5ca05132012-07-05 12:06:34 +030037 * @since 1.4.1
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
39class CI_DB_oci8_result extends CI_DB_result {
40
Andrey Andreev24abcb92012-01-05 20:40:15 +020041 public $stmt_id;
42 public $curs_id;
43 public $limit_used;
Andrey Andreev99013ed2012-03-05 16:17:32 +020044 public $commit_mode;
Andrey Andreev24abcb92012-01-05 20:40:15 +020045
Andrey Andreev5ca05132012-07-05 12:06:34 +030046 /**
47 * Constructor
48 *
49 * @param object
50 * @return void
Andrey Andreev24abcb92012-01-05 20:40:15 +020051 */
Andrey Andreev57bdeb62012-03-05 15:59:16 +020052 public function __construct(&$driver_object)
53 {
54 parent::__construct($driver_object);
Andrey Andreev5ca05132012-07-05 12:06:34 +030055
Andrey Andreev57bdeb62012-03-05 15:59:16 +020056 $this->stmt_id = $driver_object->stmt_id;
57 $this->curs_id = $driver_object->curs_id;
58 $this->limit_used = $driver_object->limit_used;
Andrey Andreev99013ed2012-03-05 16:17:32 +020059 $this->commit_mode =& $driver_object->commit_mode;
Andrey Andreev57bdeb62012-03-05 15:59:16 +020060 $driver_object->stmt_id = FALSE;
61 }
Derek Allard2067d1a2008-11-13 22:59:24 +000062
Andrey Andreev5ca05132012-07-05 12:06:34 +030063 // --------------------------------------------------------------------
64
Derek Allard2067d1a2008-11-13 22:59:24 +000065 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000066 * Number of fields in the result set
67 *
Andrey Andreevaa786c92012-01-16 12:14:45 +020068 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +000069 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030070 public function num_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030072 $count = @oci_num_fields($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +000073
74 // if we used a limit we subtract it
Andrey Andreevaa786c92012-01-16 12:14:45 +020075 return ($this->limit_used) ? $count - 1 : $count;
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Fetch Field Names
82 *
83 * Generates an array of column names
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @return array
86 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030087 public function list_fields()
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
89 $field_names = array();
Andrey Andreev5c3a2022011-10-07 21:04:58 +030090 for ($c = 1, $fieldCount = $this->num_fields(); $c <= $fieldCount; $c++)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030092 $field_names[] = oci_field_name($this->stmt_id, $c);
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
94 return $field_names;
95 }
96
97 // --------------------------------------------------------------------
98
99 /**
100 * Field data
101 *
102 * Generates an array of objects containing field meta-data
103 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200104 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300106 public function field_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
108 $retval = array();
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300109 for ($c = 1, $fieldCount = $this->num_fields(); $c <= $fieldCount; $c++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200111 $F = new stdClass();
112 $F->name = oci_field_name($this->stmt_id, $c);
113 $F->type = oci_field_type($this->stmt_id, $c);
114 $F->max_length = oci_field_size($this->stmt_id, $c);
Derek Allard2067d1a2008-11-13 22:59:24 +0000115
116 $retval[] = $F;
117 }
118
119 return $retval;
120 }
121
122 // --------------------------------------------------------------------
123
124 /**
125 * Free the result
126 *
Andrey Andreev24abcb92012-01-05 20:40:15 +0200127 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200128 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300129 public function free_result()
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
131 if (is_resource($this->result_id))
132 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300133 oci_free_statement($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 $this->result_id = FALSE;
135 }
Andrey Andreev24abcb92012-01-05 20:40:15 +0200136
137 if (is_resource($this->stmt_id))
138 {
139 oci_free_statement($this->stmt_id);
140 }
141
142 if (is_resource($this->curs_id))
143 {
144 oci_cancel($this->curs_id);
145 $this->curs_id = NULL;
146 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Result - associative array
153 *
154 * Returns the result set as an array
155 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200156 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300158 protected function _fetch_assoc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
160 $id = ($this->curs_id) ? $this->curs_id : $this->stmt_id;
Yannick Lyn Fatt642e3132012-08-13 14:41:17 -0500161 return @oci_fetch_assoc($id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Result - object
168 *
169 * Returns the result set as an object
170 *
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300171 * @param string
Andrey Andreevaa786c92012-01-16 12:14:45 +0200172 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300174 protected function _fetch_object($class_name = 'stdClass')
Barry Mienydd671972010-10-04 16:33:58 +0200175 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300176 $row = ($this->curs_id)
177 ? oci_fetch_object($this->curs_id)
178 : oci_fetch_object($this->stmt_id);
179
180 if ($class_name === 'stdClass' OR ! $row)
181 {
182 return $row;
183 }
184
185 $class_name = new $class_name();
186 foreach ($row as $key => $value)
187 {
188 $class_name->$key = $value;
189 }
190
191 return $class_name;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200192 }
193
194 // --------------------------------------------------------------------
195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * Data Seek
198 *
Andrey Andreev24abcb92012-01-05 20:40:15 +0200199 * Moves the internal pointer to the desired offset. We call
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * this internally before fetching results to make sure the
Andrey Andreev24abcb92012-01-05 20:40:15 +0200201 * result set starts at zero.
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 *
Andrey Andreev24abcb92012-01-05 20:40:15 +0200203 * Oracle's PHP extension doesn't have an easy way of doing this
204 * and the only workaround is to (re)execute the statement or cursor
205 * in order to go to the first (zero) index of the result set.
206 * Then, we would need to "dummy" fetch ($n - 1) rows to get to the
207 * right one.
208 *
209 * This is as ridiculous as it sounds and it's the reason why every
210 * other method that is fetching data tries to use an already "cached"
211 * result set. Keeping this just in case it becomes needed at
212 * some point in the future, but it will only work for resetting the
213 * pointer to zero.
214 *
215 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 */
Andrey Andreev24abcb92012-01-05 20:40:15 +0200217 protected function _data_seek()
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200219 /* The PHP manual says that if OCI_NO_AUTO_COMMIT mode
220 * is used, and oci_rollback() and/or oci_commit() are
221 * not subsequently called - this will cause an unnecessary
222 * rollback to be triggered at the end of the script execution.
223 *
224 * Therefore we'll try to avoid using that mode flag
225 * if we're not currently in the middle of a transaction.
226 */
227 if ($this->commit_mode !== OCI_COMMIT_ON_SUCCESS)
228 {
229 $result = @oci_execute($this->stmt_id, $this->commit_mode);
230 }
231 else
232 {
233 $result = @oci_execute($this->stmt_id);
234 }
235
236 if ($result && $this->curs_id)
237 {
238 if ($this->commit_mode !== OCI_COMMIT_ON_SUCCESS)
239 {
240 return @oci_execute($this->curs_id, $this->commit_mode);
241 }
242 else
243 {
244 return @oci_execute($this->curs_id);
245 }
246 }
247
248 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
250
251}
252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253/* End of file oci8_result.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400254/* Location: ./system/database/drivers/oci8/oci8_result.php */