blob: f2e40da9b763034a56cc0ab0047499916bf4eacc [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev24abcb92012-01-05 20:40:15 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev24abcb92012-01-05 20:40:15 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.4.1
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * oci8 Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
Barry Mienydd671972010-10-04 16:33:58 +020047 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050048 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
53
54/**
55 * oci8 Database Adapter Class
56 *
57 * This is a modification of the DB_driver class to
58 * permit access to oracle databases
59 *
Derek Jones4b9c6292011-07-01 17:40:48 -050060 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000061 */
Derek Allard2067d1a2008-11-13 22:59:24 +000062class CI_DB_oci8_driver extends CI_DB {
63
Andrey Andreeva24e52e2012-11-02 03:54:12 +020064 /**
65 * Database driver
66 *
67 * @var string
68 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020069 public $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020070
Andrey Andreeva24e52e2012-11-02 03:54:12 +020071 /**
72 * Statement ID
73 *
74 * @var resource
75 */
76 public $stmt_id;
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020079 * Cursor ID
80 *
81 * @var resource
Derek Allard2067d1a2008-11-13 22:59:24 +000082 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020083 public $curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +000084
Andrey Andreeva24e52e2012-11-02 03:54:12 +020085 /**
86 * Commit mode flag
87 *
88 * @var int
89 */
90 public $commit_mode = OCI_COMMIT_ON_SUCCESS;
Derek Allard2067d1a2008-11-13 22:59:24 +000091
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030092 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020093 * Limit used flag
94 *
95 * If we use LIMIT, we'll add a field that will
96 * throw off num_fields later.
97 *
98 * @var bool
99 */
100 public $limit_used;
101
102 // --------------------------------------------------------------------
103
104 /**
Andrey Andreev611e1fd2015-07-17 12:24:29 +0300105 * Reset $stmt_id flag
106 *
107 * Used by stored_procedure() to prevent _execute() from
108 * re-setting the statement ID.
109 */
110 protected $_reset_stmt_id = TRUE;
111
112 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200113 * List of reserved identifiers
114 *
115 * Identifiers that must NOT be escaped.
116 *
117 * @var string[]
118 */
119 protected $_reserved_identifiers = array('*', 'rownum');
120
121 /**
122 * ORDER BY random keyword
123 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200124 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200125 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200126 protected $_random_keyword = array('ASC', 'ASC'); // not currently supported
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200127
128 /**
129 * COUNT string
130 *
131 * @used-by CI_DB_driver::count_all()
132 * @used-by CI_DB_query_builder::count_all_results()
133 *
134 * @var string
135 */
136 protected $_count_string = 'SELECT COUNT(1) AS ';
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300142 *
143 * @param array $params
144 * @return void
145 */
Andrey Andreevdad61c22012-02-13 01:08:06 +0200146 public function __construct($params)
147 {
148 parent::__construct($params);
149
150 $valid_dsns = array(
Andrey Andreev1a61ba42014-03-24 16:53:16 +0200151 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS
152 // Easy Connect string (Oracle 10g+)
153 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i',
154 'in' => '/^[a-z0-9$_]+$/i' // Instance name (defined in tnsnames.ora)
155 );
Andrey Andreevdad61c22012-02-13 01:08:06 +0200156
157 /* Space characters don't have any effect when actually
158 * connecting, but can be a hassle while validating the DSN.
159 */
160 $this->dsn = str_replace(array("\n", "\r", "\t", ' '), '', $this->dsn);
161
162 if ($this->dsn !== '')
163 {
164 foreach ($valid_dsns as $regexp)
165 {
166 if (preg_match($regexp, $this->dsn))
167 {
168 return;
169 }
170 }
171 }
172
173 // Legacy support for TNS in the hostname configuration field
174 $this->hostname = str_replace(array("\n", "\r", "\t", ' '), '', $this->hostname);
175 if (preg_match($valid_dsns['tns'], $this->hostname))
176 {
177 $this->dsn = $this->hostname;
178 return;
179 }
180 elseif ($this->hostname !== '' && strpos($this->hostname, '/') === FALSE && strpos($this->hostname, ':') === FALSE
181 && (( ! empty($this->port) && ctype_digit($this->port)) OR $this->database !== ''))
182 {
183 /* If the hostname field isn't empty, doesn't contain
184 * ':' and/or '/' and if port and/or database aren't
185 * empty, then the hostname field is most likely indeed
186 * just a hostname. Therefore we'll try and build an
187 * Easy Connect string from these 3 settings, assuming
188 * that the database field is a service name.
189 */
190 $this->dsn = $this->hostname
Andrey Andreev1a61ba42014-03-24 16:53:16 +0200191 .(( ! empty($this->port) && ctype_digit($this->port)) ? ':'.$this->port : '')
192 .($this->database !== '' ? '/'.ltrim($this->database, '/') : '');
Andrey Andreevdad61c22012-02-13 01:08:06 +0200193
194 if (preg_match($valid_dsns['ec'], $this->dsn))
195 {
196 return;
197 }
198 }
199
200 /* At this point, we can only try and validate the hostname and
201 * database fields separately as DSNs.
202 */
203 if (preg_match($valid_dsns['ec'], $this->hostname) OR preg_match($valid_dsns['in'], $this->hostname))
204 {
205 $this->dsn = $this->hostname;
206 return;
207 }
208
209 $this->database = str_replace(array("\n", "\r", "\t", ' '), '', $this->database);
210 foreach ($valid_dsns as $regexp)
211 {
212 if (preg_match($regexp, $this->database))
213 {
214 return;
215 }
216 }
217
218 /* Well - OK, an empty string should work as well.
219 * PHP will try to use environment variables to
220 * determine which Oracle instance to connect to.
221 */
222 $this->dsn = '';
223 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000224
Andrey Andreevd5809992012-06-28 14:06:54 +0300225 // --------------------------------------------------------------------
226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 /**
228 * Non-persistent database connection
229 *
Andrey Andreev2e171022014-02-25 15:21:41 +0200230 * @param bool $persistent
Andrey Andreevaa786c92012-01-16 12:14:45 +0200231 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 */
Andrey Andreev2e171022014-02-25 15:21:41 +0200233 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Andrey Andreev2e171022014-02-25 15:21:41 +0200235 $func = ($persistent === TRUE) ? 'oci_pconnect' : 'oci_connect';
Andrey Andreevd5809992012-06-28 14:06:54 +0300236 return empty($this->char_set)
Andrey Andreev2e171022014-02-25 15:21:41 +0200237 ? $func($this->username, $this->password, $this->dsn)
238 : $func($this->username, $this->password, $this->dsn, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
240
241 // --------------------------------------------------------------------
242
243 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200244 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200246 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200248 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200250 if (isset($this->data_cache['version']))
251 {
252 return $this->data_cache['version'];
253 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200254
255 if ( ! $this->conn_id OR ($version = oci_server_version($this->conn_id)) === FALSE)
256 {
257 return FALSE;
258 }
259
260 return $this->data_cache['version'] = $version;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Execute the query
267 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200268 * @param string $sql an SQL query
Andrey Andreevaa786c92012-01-16 12:14:45 +0200269 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300271 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200273 /* Oracle must parse the query before it is run. All of the actions with
274 * the query are based on the statement id returned by oci_parse().
275 */
Andrey Andreev611e1fd2015-07-17 12:24:29 +0300276 if ($this->_reset_stmt_id === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400278 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
Andrey Andreev611e1fd2015-07-17 12:24:29 +0300280
281 oci_set_prefetch($this->stmt_id, 1000);
282 return oci_execute($this->stmt_id, $this->commit_mode);
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284
285 // --------------------------------------------------------------------
286
287 /**
Andrey Andreevda123732012-03-20 22:27:40 +0200288 * Get cursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 *
Andrey Andreevd5809992012-06-28 14:06:54 +0300290 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300292 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200294 return $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296
297 // --------------------------------------------------------------------
298
299 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500300 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200302 * @param string package name in which the stored procedure is in
303 * @param string stored procedure name to execute
304 * @param array parameters
305 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 *
307 * params array keys
308 *
Andrey Andreev10fb7d12015-08-03 10:05:29 +0300309 * KEY OPTIONAL NOTES
310 * name no the name of the parameter should be in :<param_name> format
311 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
312 * this should be a reference to a variable
313 * type yes the type of the parameter
314 * length yes the max size of the parameter
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 */
Andrey Andreev611e1fd2015-07-17 12:24:29 +0300316 public function stored_procedure($package, $procedure, array $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
Andrey Andreev611e1fd2015-07-17 12:24:29 +0300318 if ($package === '' OR $procedure === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300320 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
321 return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Andrey Andreev611e1fd2015-07-17 12:24:29 +0300324 // Build the query string
Andrey Andreeve35d7782012-01-19 15:56:20 +0200325 $sql = 'BEGIN '.$package.'.'.$procedure.'(';
Derek Allard2067d1a2008-11-13 22:59:24 +0000326
327 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500328 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200330 $sql .= $param['name'].',';
Barry Mienydd671972010-10-04 16:33:58 +0200331
Andrey Andreev85facfa2012-01-26 14:12:14 +0200332 if (isset($param['type']) && $param['type'] === OCI_B_CURSOR)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $have_cursor = TRUE;
335 }
336 }
Andrey Andreev3ca060a2013-11-27 16:30:31 +0200337 $sql = trim($sql, ',').'); END;';
Barry Mienydd671972010-10-04 16:33:58 +0200338
Andrey Andreev611e1fd2015-07-17 12:24:29 +0300339 $this->_reset_stmt_id = FALSE;
340 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 $this->_bind_params($params);
Andrey Andreev611e1fd2015-07-17 12:24:29 +0300342 $result = $this->query($sql, FALSE, $have_cursor);
343 $this->_reset_stmt_id = TRUE;
344 return $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // --------------------------------------------------------------------
348
349 /**
350 * Bind parameters
351 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200352 * @param array $params
Andrey Andreevaa786c92012-01-16 12:14:45 +0200353 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 */
Andrey Andreev1b5a8562012-03-13 13:13:43 +0200355 protected function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
358 {
359 return;
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 foreach ($params as $param)
363 {
Barry Mienydd671972010-10-04 16:33:58 +0200364 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
366 if ( ! isset($param[$val]))
367 {
368 $param[$val] = '';
369 }
370 }
371
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300372 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Begin Transaction
380 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200381 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200382 * @return bool
383 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300384 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 if ( ! $this->trans_enabled)
387 {
388 return TRUE;
389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // When transactions are nested we only begin/commit/rollback the outermost ones
392 if ($this->_trans_depth > 0)
393 {
394 return TRUE;
395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 // Reset the transaction failure flag.
398 // If the $test_mode flag is set to TRUE transactions will be rolled back
399 // even if the queries produce a successful result.
Andrey Andreev1a61ba42014-03-24 16:53:16 +0200400 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200401
Andrey Andreev1a61ba42014-03-24 16:53:16 +0200402 $this->commit_mode = is_php('5.3.2') ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return TRUE;
404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Commit Transaction
410 *
Barry Mienydd671972010-10-04 16:33:58 +0200411 * @return bool
412 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300413 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 if ( ! $this->trans_enabled)
416 {
417 return TRUE;
418 }
419
420 // When transactions are nested we only begin/commit/rollback the outermost ones
421 if ($this->_trans_depth > 0)
422 {
423 return TRUE;
424 }
425
Andrey Andreev99013ed2012-03-05 16:17:32 +0200426 $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200427 return oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Rollback Transaction
434 *
Barry Mienydd671972010-10-04 16:33:58 +0200435 * @return bool
436 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300437 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeve35d7782012-01-19 15:56:20 +0200440 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 return TRUE;
443 }
444
Andrey Andreev99013ed2012-03-05 16:17:32 +0200445 $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200446 return oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
448
449 // --------------------------------------------------------------------
450
451 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * Affected Rows
453 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200454 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300456 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300458 return oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460
461 // --------------------------------------------------------------------
462
463 /**
464 * Insert ID
465 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200466 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300468 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
470 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000471 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473
474 // --------------------------------------------------------------------
475
476 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * Show table query
478 *
479 * Generates a platform-specific query string so that the table names can be fetched
480 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200481 * @param bool $prefix_limit
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300482 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300484 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300486 $sql = 'SELECT "TABLE_NAME" FROM "ALL_TABLES"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100488 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300490 return $sql.' WHERE "TABLE_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
491 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 return $sql;
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Show column query
501 *
502 * Generates a platform-specific query string so that the column names can be fetched
503 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200504 * @param string $table
Andrey Andreevaa786c92012-01-16 12:14:45 +0200505 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300507 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200509 if (strpos($table, '.') !== FALSE)
510 {
511 sscanf($table, '%[^.].%s', $owner, $table);
512 }
513 else
514 {
515 $owner = $this->username;
516 }
517
518 return 'SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS
519 WHERE UPPER(OWNER) = '.$this->escape(strtoupper($owner)).'
520 AND UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
522
523 // --------------------------------------------------------------------
524
525 /**
Andrey Andreeve1580572012-11-16 16:17:54 +0200526 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200528 * @param string $table
Andrey Andreeve1580572012-11-16 16:17:54 +0200529 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200531 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreev5350f052015-01-12 12:33:37 +0200533 if (strpos($table, '.') !== FALSE)
Andrey Andreeve1580572012-11-16 16:17:54 +0200534 {
535 sscanf($table, '%[^.].%s', $owner, $table);
536 }
537 else
538 {
539 $owner = $this->username;
540 }
541
542 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_LENGTH, DATA_DEFAULT, NULLABLE
543 FROM ALL_TAB_COLUMNS
544 WHERE UPPER(OWNER) = '.$this->escape(strtoupper($owner)).'
545 AND UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
546
547 if (($query = $this->query($sql)) === FALSE)
548 {
549 return FALSE;
550 }
551 $query = $query->result_object();
552
553 $retval = array();
554 for ($i = 0, $c = count($query); $i < $c; $i++)
555 {
556 $retval[$i] = new stdClass();
557 $retval[$i]->name = $query[$i]->COLUMN_NAME;
558 $retval[$i]->type = $query[$i]->DATA_TYPE;
559
560 $length = ($query[$i]->CHAR_LENGTH > 0)
561 ? $query[$i]->CHAR_LENGTH : $query[$i]->DATA_PRECISION;
562 if ($length === NULL)
563 {
564 $length = $query[$i]->DATA_LENGTH;
565 }
566 $retval[$i]->max_length = $length;
567
568 $default = $query[$i]->DATA_DEFAULT;
569 if ($default === NULL && $query[$i]->NULLABLE === 'N')
570 {
571 $default = '';
572 }
Leandro Mangini Antunes4c08ea92015-05-13 11:15:01 -0300573 $retval[$i]->default = $default;
Andrey Andreeve1580572012-11-16 16:17:54 +0200574 }
575
576 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
578
579 // --------------------------------------------------------------------
580
581 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200582 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200584 * Returns an array containing code and message of the last
585 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 *
Andrey Andreev601f8b22012-03-01 20:11:15 +0200587 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200589 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200591 /* oci_error() returns an array that already contains the
592 * 'code' and 'message' keys, so we can just return it.
593 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200594 if (is_resource($this->curs_id))
595 {
596 return oci_error($this->curs_id);
597 }
598 elseif (is_resource($this->stmt_id))
599 {
600 return oci_error($this->stmt_id);
601 }
602 elseif (is_resource($this->conn_id))
603 {
604 return oci_error($this->conn_id);
605 }
606
607 return oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 // --------------------------------------------------------------------
611
612 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200613 * Insert batch statement
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300614 *
615 * Generates a platform-specific insert string from the supplied data
616 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200617 * @param string $table Table name
618 * @param array $keys INSERT keys
619 * @param array $values INSERT values
Andrey Andreevda123732012-03-20 22:27:40 +0200620 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300621 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300622 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300623 {
624 $keys = implode(', ', $keys);
625 $sql = "INSERT ALL\n";
626
627 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300628 {
Andrey Andreev97f36972012-04-05 12:44:36 +0300629 $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i]."\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300630 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300631
Andrey Andreevaa786c92012-01-16 12:14:45 +0200632 return $sql.'SELECT * FROM dual';
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
634
635 // --------------------------------------------------------------------
636
637 /**
638 * Truncate statement
639 *
640 * Generates a platform-specific truncate string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200642 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300643 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200645 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200647 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300648 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200650 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
656 * Delete statement
657 *
658 * Generates a platform-specific delete string from the supplied data
659 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200660 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200662 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300663 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300665 if ($this->qb_limit)
666 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300667 $this->where('rownum <= ',$this->qb_limit, FALSE);
Andrey Andreevb0478652012-07-18 15:34:46 +0300668 $this->qb_limit = FALSE;
669 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000670
Andrey Andreevb0478652012-07-18 15:34:46 +0300671 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 }
673
674 // --------------------------------------------------------------------
675
676 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200677 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 *
679 * Generates a platform-specific LIMIT clause
680 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200681 * @param string $sql SQL Query
Andrey Andreevaa786c92012-01-16 12:14:45 +0200682 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300684 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 $this->limit_used = TRUE;
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300687 return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($this->qb_offset + $this->qb_limit + 1).')'
Andrey Andreev1a61ba42014-03-24 16:53:16 +0200688 .($this->qb_offset ? ' WHERE rnum >= '.($this->qb_offset + 1) : '');
Barry Mienydd671972010-10-04 16:33:58 +0200689 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000690
691 // --------------------------------------------------------------------
692
693 /**
694 * Close DB Connection
695 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200696 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300698 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300700 oci_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 }
702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703}