blob: 0ec8b53b8bc5018d09c1bb43267ec0c8d89a1132 [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
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 Mienydd671972010-10-04 16:33:58 +020019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
Barry Mienydd671972010-10-04 16:33:58 +020036 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050037 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42
43/**
44 * oci8 Database Adapter Class
45 *
46 * This is a modification of the DB_driver class to
47 * permit access to oracle databases
48 *
Derek Jones4b9c6292011-07-01 17:40:48 -050049 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051class CI_DB_oci8_driver extends CI_DB {
52
Andrey Andreeva24e52e2012-11-02 03:54:12 +020053 /**
54 * Database driver
55 *
56 * @var string
57 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020058 public $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020059
Andrey Andreeva24e52e2012-11-02 03:54:12 +020060 /**
61 * Statement ID
62 *
63 * @var resource
64 */
65 public $stmt_id;
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020068 * Cursor ID
69 *
70 * @var resource
Derek Allard2067d1a2008-11-13 22:59:24 +000071 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020072 public $curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +000073
Andrey Andreeva24e52e2012-11-02 03:54:12 +020074 /**
75 * Commit mode flag
76 *
77 * @var int
78 */
79 public $commit_mode = OCI_COMMIT_ON_SUCCESS;
Derek Allard2067d1a2008-11-13 22:59:24 +000080
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030081 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020082 * Limit used flag
83 *
84 * If we use LIMIT, we'll add a field that will
85 * throw off num_fields later.
86 *
87 * @var bool
88 */
89 public $limit_used;
90
91 // --------------------------------------------------------------------
92
93 /**
94 * List of reserved identifiers
95 *
96 * Identifiers that must NOT be escaped.
97 *
98 * @var string[]
99 */
100 protected $_reserved_identifiers = array('*', 'rownum');
101
102 /**
103 * ORDER BY random keyword
104 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200105 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200106 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200107 protected $_random_keyword = array('ASC', 'ASC'); // not currently supported
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200108
109 /**
110 * COUNT string
111 *
112 * @used-by CI_DB_driver::count_all()
113 * @used-by CI_DB_query_builder::count_all_results()
114 *
115 * @var string
116 */
117 protected $_count_string = 'SELECT COUNT(1) AS ';
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Class constructor
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300123 *
124 * @param array $params
125 * @return void
126 */
Andrey Andreevdad61c22012-02-13 01:08:06 +0200127 public function __construct($params)
128 {
129 parent::__construct($params);
130
131 $valid_dsns = array(
Andrey Andreevdad61c22012-02-13 01:08:06 +0200132 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS
Andrey Andreevfcd1f472012-02-13 09:30:16 +0200133 // Easy Connect string (Oracle 10g+)
134 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i',
Andrey Andreevdad61c22012-02-13 01:08:06 +0200135 'in' => '/^[a-z0-9$_]+$/i' // Instance name (defined in tnsnames.ora)
136 );
137
138 /* Space characters don't have any effect when actually
139 * connecting, but can be a hassle while validating the DSN.
140 */
141 $this->dsn = str_replace(array("\n", "\r", "\t", ' '), '', $this->dsn);
142
143 if ($this->dsn !== '')
144 {
145 foreach ($valid_dsns as $regexp)
146 {
147 if (preg_match($regexp, $this->dsn))
148 {
149 return;
150 }
151 }
152 }
153
154 // Legacy support for TNS in the hostname configuration field
155 $this->hostname = str_replace(array("\n", "\r", "\t", ' '), '', $this->hostname);
156 if (preg_match($valid_dsns['tns'], $this->hostname))
157 {
158 $this->dsn = $this->hostname;
159 return;
160 }
161 elseif ($this->hostname !== '' && strpos($this->hostname, '/') === FALSE && strpos($this->hostname, ':') === FALSE
162 && (( ! empty($this->port) && ctype_digit($this->port)) OR $this->database !== ''))
163 {
164 /* If the hostname field isn't empty, doesn't contain
165 * ':' and/or '/' and if port and/or database aren't
166 * empty, then the hostname field is most likely indeed
167 * just a hostname. Therefore we'll try and build an
168 * Easy Connect string from these 3 settings, assuming
169 * that the database field is a service name.
170 */
171 $this->dsn = $this->hostname
172 .(( ! empty($this->port) && ctype_digit($this->port)) ? ':'.$this->port : '')
173 .($this->database !== '' ? '/'.ltrim($this->database, '/') : '');
174
175 if (preg_match($valid_dsns['ec'], $this->dsn))
176 {
177 return;
178 }
179 }
180
181 /* At this point, we can only try and validate the hostname and
182 * database fields separately as DSNs.
183 */
184 if (preg_match($valid_dsns['ec'], $this->hostname) OR preg_match($valid_dsns['in'], $this->hostname))
185 {
186 $this->dsn = $this->hostname;
187 return;
188 }
189
190 $this->database = str_replace(array("\n", "\r", "\t", ' '), '', $this->database);
191 foreach ($valid_dsns as $regexp)
192 {
193 if (preg_match($regexp, $this->database))
194 {
195 return;
196 }
197 }
198
199 /* Well - OK, an empty string should work as well.
200 * PHP will try to use environment variables to
201 * determine which Oracle instance to connect to.
202 */
203 $this->dsn = '';
204 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
Andrey Andreevd5809992012-06-28 14:06:54 +0300206 // --------------------------------------------------------------------
207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 /**
209 * Non-persistent database connection
210 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200211 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300213 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
Andrey Andreevdad61c22012-02-13 01:08:06 +0200215 return ( ! empty($this->char_set))
216 ? @oci_connect($this->username, $this->password, $this->dsn, $this->char_set)
217 : @oci_connect($this->username, $this->password, $this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Persistent database connection
224 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200225 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300227 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Andrey Andreevd5809992012-06-28 14:06:54 +0300229 return empty($this->char_set)
230 ? @oci_pconnect($this->username, $this->password, $this->dsn)
231 : @oci_pconnect($this->username, $this->password, $this->dsn, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233
234 // --------------------------------------------------------------------
235
236 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200237 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200239 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200241 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200243 if (isset($this->data_cache['version']))
244 {
245 return $this->data_cache['version'];
246 }
247 elseif ( ! $this->conn_id)
248 {
249 $this->initialize();
250 }
251
252 if ( ! $this->conn_id OR ($version = oci_server_version($this->conn_id)) === FALSE)
253 {
254 return FALSE;
255 }
256
257 return $this->data_cache['version'] = $version;
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Execute the query
264 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200265 * @param string $sql an SQL query
Andrey Andreevaa786c92012-01-16 12:14:45 +0200266 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300268 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200270 /* Oracle must parse the query before it is run. All of the actions with
271 * the query are based on the statement id returned by oci_parse().
272 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 $this->stmt_id = FALSE;
274 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300275 oci_set_prefetch($this->stmt_id, 1000);
Andrey Andreev99013ed2012-03-05 16:17:32 +0200276 return @oci_execute($this->stmt_id, $this->commit_mode);
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278
Andrey Andreevd5809992012-06-28 14:06:54 +0300279 // --------------------------------------------------------------------
280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 /**
282 * Generate a statement ID
283 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200284 * @param string $sql an SQL query
Andrey Andreevaa786c92012-01-16 12:14:45 +0200285 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 */
Andrey Andreevda123732012-03-20 22:27:40 +0200287 protected function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 if ( ! is_resource($this->stmt_id))
290 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400291 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293 }
294
295 // --------------------------------------------------------------------
296
297 /**
Andrey Andreevda123732012-03-20 22:27:40 +0200298 * Get cursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 *
Andrey Andreevd5809992012-06-28 14:06:54 +0300300 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300302 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200304 return $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
306
307 // --------------------------------------------------------------------
308
309 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500310 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200312 * @param string package name in which the stored procedure is in
313 * @param string stored procedure name to execute
314 * @param array parameters
315 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 *
317 * params array keys
318 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500319 * KEY OPTIONAL NOTES
Andrey Andreevaa786c92012-01-16 12:14:45 +0200320 * name no the name of the parameter should be in :<param_name> format
321 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
322 * this should be a reference to a variable
323 * type yes the type of the parameter
324 * length yes the max size of the parameter
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300326 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100328 if ($package === '' OR $procedure === '' OR ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 if ($this->db_debug)
331 {
332 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000333 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
335 return FALSE;
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // build the query string
Andrey Andreeve35d7782012-01-19 15:56:20 +0200339 $sql = 'BEGIN '.$package.'.'.$procedure.'(';
Derek Allard2067d1a2008-11-13 22:59:24 +0000340
341 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500342 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200344 $sql .= $param['name'].',';
Barry Mienydd671972010-10-04 16:33:58 +0200345
Andrey Andreev85facfa2012-01-26 14:12:14 +0200346 if (isset($param['type']) && $param['type'] === OCI_B_CURSOR)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 $have_cursor = TRUE;
349 }
350 }
Andrey Andreevaa786c92012-01-16 12:14:45 +0200351 $sql = trim($sql, ',') . '); END;';
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 $this->stmt_id = FALSE;
354 $this->_set_stmt_id($sql);
355 $this->_bind_params($params);
Andrey Andreevaa786c92012-01-16 12:14:45 +0200356 return $this->query($sql, FALSE, $have_cursor);
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // --------------------------------------------------------------------
360
361 /**
362 * Bind parameters
363 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200364 * @param array $params
Andrey Andreevaa786c92012-01-16 12:14:45 +0200365 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 */
Andrey Andreev1b5a8562012-03-13 13:13:43 +0200367 protected function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
369 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
370 {
371 return;
372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 foreach ($params as $param)
375 {
Barry Mienydd671972010-10-04 16:33:58 +0200376 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 if ( ! isset($param[$val]))
379 {
380 $param[$val] = '';
381 }
382 }
383
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300384 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Begin Transaction
392 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200393 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200394 * @return bool
395 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300396 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 if ( ! $this->trans_enabled)
399 {
400 return TRUE;
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // When transactions are nested we only begin/commit/rollback the outermost ones
404 if ($this->_trans_depth > 0)
405 {
406 return TRUE;
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // Reset the transaction failure flag.
410 // If the $test_mode flag is set to TRUE transactions will be rolled back
411 // even if the queries produce a successful result.
412 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200413
Andrey Andreev99013ed2012-03-05 16:17:32 +0200414 $this->commit_mode = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 return TRUE;
416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Commit Transaction
422 *
Barry Mienydd671972010-10-04 16:33:58 +0200423 * @return bool
424 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300425 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 if ( ! $this->trans_enabled)
428 {
429 return TRUE;
430 }
431
432 // When transactions are nested we only begin/commit/rollback the outermost ones
433 if ($this->_trans_depth > 0)
434 {
435 return TRUE;
436 }
437
Andrey Andreev99013ed2012-03-05 16:17:32 +0200438 $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200439 return oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Rollback Transaction
446 *
Barry Mienydd671972010-10-04 16:33:58 +0200447 * @return bool
448 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300449 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeve35d7782012-01-19 15:56:20 +0200452 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
454 return TRUE;
455 }
456
Andrey Andreev99013ed2012-03-05 16:17:32 +0200457 $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200458 return oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460
461 // --------------------------------------------------------------------
462
463 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * Affected Rows
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 affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300470 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472
473 // --------------------------------------------------------------------
474
475 /**
476 * Insert ID
477 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200478 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300480 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000483 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485
486 // --------------------------------------------------------------------
487
488 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * Show table query
490 *
491 * Generates a platform-specific query string so that the table names can be fetched
492 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200493 * @param bool $prefix_limit
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300494 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300496 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300498 $sql = 'SELECT "TABLE_NAME" FROM "ALL_TABLES"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100500 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300502 return $sql.' WHERE "TABLE_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
503 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 return $sql;
507 }
508
509 // --------------------------------------------------------------------
510
511 /**
512 * Show column query
513 *
514 * Generates a platform-specific query string so that the column names can be fetched
515 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200516 * @param string $table
Andrey Andreevaa786c92012-01-16 12:14:45 +0200517 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300519 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200521 if (strpos($table, '.') !== FALSE)
522 {
523 sscanf($table, '%[^.].%s', $owner, $table);
524 }
525 else
526 {
527 $owner = $this->username;
528 }
529
530 return 'SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS
531 WHERE UPPER(OWNER) = '.$this->escape(strtoupper($owner)).'
532 AND UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 }
534
535 // --------------------------------------------------------------------
536
537 /**
Andrey Andreeve1580572012-11-16 16:17:54 +0200538 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200540 * @param string $table
Andrey Andreeve1580572012-11-16 16:17:54 +0200541 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 */
Andrey Andreeve1580572012-11-16 16:17:54 +0200543 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
Andrey Andreeve1580572012-11-16 16:17:54 +0200545 if ($table === '')
546 {
547 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
548 }
549 elseif (strpos($table, '.') !== FALSE)
550 {
551 sscanf($table, '%[^.].%s', $owner, $table);
552 }
553 else
554 {
555 $owner = $this->username;
556 }
557
558 $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_LENGTH, DATA_DEFAULT, NULLABLE
559 FROM ALL_TAB_COLUMNS
560 WHERE UPPER(OWNER) = '.$this->escape(strtoupper($owner)).'
561 AND UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
562
563 if (($query = $this->query($sql)) === FALSE)
564 {
565 return FALSE;
566 }
567 $query = $query->result_object();
568
569 $retval = array();
570 for ($i = 0, $c = count($query); $i < $c; $i++)
571 {
572 $retval[$i] = new stdClass();
573 $retval[$i]->name = $query[$i]->COLUMN_NAME;
574 $retval[$i]->type = $query[$i]->DATA_TYPE;
575
576 $length = ($query[$i]->CHAR_LENGTH > 0)
577 ? $query[$i]->CHAR_LENGTH : $query[$i]->DATA_PRECISION;
578 if ($length === NULL)
579 {
580 $length = $query[$i]->DATA_LENGTH;
581 }
582 $retval[$i]->max_length = $length;
583
584 $default = $query[$i]->DATA_DEFAULT;
585 if ($default === NULL && $query[$i]->NULLABLE === 'N')
586 {
587 $default = '';
588 }
589 $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
590 }
591
592 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
594
595 // --------------------------------------------------------------------
596
597 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200598 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200600 * Returns an array containing code and message of the last
601 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 *
Andrey Andreev601f8b22012-03-01 20:11:15 +0200603 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200605 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200607 /* oci_error() returns an array that already contains the
608 * 'code' and 'message' keys, so we can just return it.
609 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200610 if (is_resource($this->curs_id))
611 {
612 return oci_error($this->curs_id);
613 }
614 elseif (is_resource($this->stmt_id))
615 {
616 return oci_error($this->stmt_id);
617 }
618 elseif (is_resource($this->conn_id))
619 {
620 return oci_error($this->conn_id);
621 }
622
623 return oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // --------------------------------------------------------------------
627
628 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +0200629 * Insert batch statement
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300630 *
631 * Generates a platform-specific insert string from the supplied data
632 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200633 * @param string $table Table name
634 * @param array $keys INSERT keys
635 * @param array $values INSERT values
Andrey Andreevda123732012-03-20 22:27:40 +0200636 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300637 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300638 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300639 {
640 $keys = implode(', ', $keys);
641 $sql = "INSERT ALL\n";
642
643 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300644 {
Andrey Andreev97f36972012-04-05 12:44:36 +0300645 $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i]."\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300646 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300647
Andrey Andreevaa786c92012-01-16 12:14:45 +0200648 return $sql.'SELECT * FROM dual';
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
650
651 // --------------------------------------------------------------------
652
653 /**
654 * Truncate statement
655 *
656 * Generates a platform-specific truncate string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200658 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300659 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200661 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200663 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300664 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200666 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 // --------------------------------------------------------------------
670
671 /**
672 * Delete statement
673 *
674 * Generates a platform-specific delete string from the supplied data
675 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200676 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200678 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300679 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300681 if ($this->qb_limit)
682 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300683 $this->where('rownum <= ',$this->qb_limit, FALSE);
Andrey Andreevb0478652012-07-18 15:34:46 +0300684 $this->qb_limit = FALSE;
685 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000686
Andrey Andreevb0478652012-07-18 15:34:46 +0300687 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
689
690 // --------------------------------------------------------------------
691
692 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200693 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 *
695 * Generates a platform-specific LIMIT clause
696 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200697 * @param string $sql SQL Query
Andrey Andreevaa786c92012-01-16 12:14:45 +0200698 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300700 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 $this->limit_used = TRUE;
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300703 return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($this->qb_offset + $this->qb_limit + 1).')'
704 .($this->qb_offset ? ' WHERE rnum >= '.($this->qb_offset + 1): '');
Barry Mienydd671972010-10-04 16:33:58 +0200705 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000706
707 // --------------------------------------------------------------------
708
709 /**
710 * Close DB Connection
711 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200712 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300714 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300716 @oci_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 }
718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719}
720
Derek Allard2067d1a2008-11-13 22:59:24 +0000721/* End of file oci8_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300722/* Location: ./system/database/drivers/oci8/oci8_driver.php */