blob: 38d2395b6fed69531a4b6c3e311fd99b31a076ec [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
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 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 *
105 * @var string
106 */
107 protected $_random_keyword = ' ASC'; // not currently supported
108
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 Andreev08856b82012-03-03 03:19:28 +0200243 return isset($this->data_cache['version'])
244 ? $this->data_cache['version']
245 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * Execute the query
252 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200253 * @param string $sql an SQL query
Andrey Andreevaa786c92012-01-16 12:14:45 +0200254 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300256 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200258 /* Oracle must parse the query before it is run. All of the actions with
259 * the query are based on the statement id returned by oci_parse().
260 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 $this->stmt_id = FALSE;
262 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300263 oci_set_prefetch($this->stmt_id, 1000);
Andrey Andreev99013ed2012-03-05 16:17:32 +0200264 return @oci_execute($this->stmt_id, $this->commit_mode);
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
266
Andrey Andreevd5809992012-06-28 14:06:54 +0300267 // --------------------------------------------------------------------
268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 /**
270 * Generate a statement ID
271 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200272 * @param string $sql an SQL query
Andrey Andreevaa786c92012-01-16 12:14:45 +0200273 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 */
Andrey Andreevda123732012-03-20 22:27:40 +0200275 protected function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 if ( ! is_resource($this->stmt_id))
278 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400279 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
Andrey Andreevda123732012-03-20 22:27:40 +0200286 * Get cursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 *
Andrey Andreevd5809992012-06-28 14:06:54 +0300288 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300290 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200292 return $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294
295 // --------------------------------------------------------------------
296
297 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500298 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200300 * @param string package name in which the stored procedure is in
301 * @param string stored procedure name to execute
302 * @param array parameters
303 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 *
305 * params array keys
306 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500307 * KEY OPTIONAL NOTES
Andrey Andreevaa786c92012-01-16 12:14:45 +0200308 * name no the name of the parameter should be in :<param_name> format
309 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
310 * this should be a reference to a variable
311 * type yes the type of the parameter
312 * length yes the max size of the parameter
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300314 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100316 if ($package === '' OR $procedure === '' OR ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 if ($this->db_debug)
319 {
320 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000321 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323 return FALSE;
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // build the query string
Andrey Andreeve35d7782012-01-19 15:56:20 +0200327 $sql = 'BEGIN '.$package.'.'.$procedure.'(';
Derek Allard2067d1a2008-11-13 22:59:24 +0000328
329 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500330 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200332 $sql .= $param['name'].',';
Barry Mienydd671972010-10-04 16:33:58 +0200333
Andrey Andreev85facfa2012-01-26 14:12:14 +0200334 if (isset($param['type']) && $param['type'] === OCI_B_CURSOR)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $have_cursor = TRUE;
337 }
338 }
Andrey Andreevaa786c92012-01-16 12:14:45 +0200339 $sql = trim($sql, ',') . '); END;';
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 $this->stmt_id = FALSE;
342 $this->_set_stmt_id($sql);
343 $this->_bind_params($params);
Andrey Andreevaa786c92012-01-16 12:14:45 +0200344 return $this->query($sql, FALSE, $have_cursor);
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.
400 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200401
Andrey Andreev99013ed2012-03-05 16:17:32 +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 /**
452 * Escape String
453 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200454 * @param string $str
455 * @param bool $like Whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200456 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300458 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000460 if (is_array($str))
461 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500462 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200463 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000464 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200465 }
466
467 return $str;
468 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000469
Andrey Andreevc2905f52012-03-01 14:39:26 +0200470 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Jonese4ed5832009-02-20 21:44:59 +0000472 // escape LIKE condition wildcards
473 if ($like === TRUE)
474 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200475 return str_replace(array($this->_like_escape_chr, '%', '_'),
476 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
477 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Jonese4ed5832009-02-20 21:44:59 +0000480 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
483 // --------------------------------------------------------------------
484
485 /**
486 * Affected Rows
487 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200488 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300490 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300492 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Insert ID
499 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200500 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300502 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000505 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507
508 // --------------------------------------------------------------------
509
510 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * Show table query
512 *
513 * Generates a platform-specific query string so that the table names can be fetched
514 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200515 * @param bool $prefix_limit
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300516 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300518 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300520 $sql = 'SELECT "TABLE_NAME" FROM "ALL_TABLES"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000521
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100522 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300524 return $sql.' WHERE "TABLE_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
525 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 return $sql;
529 }
530
531 // --------------------------------------------------------------------
532
533 /**
534 * Show column query
535 *
536 * Generates a platform-specific query string so that the column names can be fetched
537 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200538 * @param string $table
Andrey Andreevaa786c92012-01-16 12:14:45 +0200539 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300541 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300543 return 'SELECT "COLUMN_NAME" FROM "all_tab_columns" WHERE "TABLE_NAME" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545
546 // --------------------------------------------------------------------
547
548 /**
549 * Field data query
550 *
551 * Generates a platform-specific query so that the column data can be retrieved
552 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200553 * @param string $table
Andrey Andreevaa786c92012-01-16 12:14:45 +0200554 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300556 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300558 return 'SELECT * FROM '.$this->protect_identifiers($table).' WHERE rownum = 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
560
561 // --------------------------------------------------------------------
562
563 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200564 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200566 * Returns an array containing code and message of the last
567 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 *
Andrey Andreev601f8b22012-03-01 20:11:15 +0200569 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200571 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200573 /* oci_error() returns an array that already contains the
574 * 'code' and 'message' keys, so we can just return it.
575 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200576 if (is_resource($this->curs_id))
577 {
578 return oci_error($this->curs_id);
579 }
580 elseif (is_resource($this->stmt_id))
581 {
582 return oci_error($this->stmt_id);
583 }
584 elseif (is_resource($this->conn_id))
585 {
586 return oci_error($this->conn_id);
587 }
588
589 return oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // --------------------------------------------------------------------
593
594 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300595 * Insert_batch statement
596 *
597 * Generates a platform-specific insert string from the supplied data
598 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200599 * @param string $table Table name
600 * @param array $keys INSERT keys
601 * @param array $values INSERT values
Andrey Andreevda123732012-03-20 22:27:40 +0200602 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300603 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300604 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300605 {
606 $keys = implode(', ', $keys);
607 $sql = "INSERT ALL\n";
608
609 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300610 {
Andrey Andreev97f36972012-04-05 12:44:36 +0300611 $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i]."\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300612 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300613
Andrey Andreevaa786c92012-01-16 12:14:45 +0200614 return $sql.'SELECT * FROM dual';
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 }
616
617 // --------------------------------------------------------------------
618
619 /**
620 * Truncate statement
621 *
622 * Generates a platform-specific truncate string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200624 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300625 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200627 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200629 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300630 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200632 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // --------------------------------------------------------------------
636
637 /**
638 * Delete statement
639 *
640 * Generates a platform-specific delete string from the supplied data
641 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200642 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200644 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300645 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300647 if ($this->qb_limit)
648 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300649 $this->where('rownum <= ',$this->qb_limit, FALSE);
Andrey Andreevb0478652012-07-18 15:34:46 +0300650 $this->qb_limit = FALSE;
651 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000652
Andrey Andreevb0478652012-07-18 15:34:46 +0300653 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 }
655
656 // --------------------------------------------------------------------
657
658 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200659 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 *
661 * Generates a platform-specific LIMIT clause
662 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200663 * @param string $sql SQL Query
Andrey Andreevaa786c92012-01-16 12:14:45 +0200664 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300666 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 $this->limit_used = TRUE;
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300669 return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($this->qb_offset + $this->qb_limit + 1).')'
670 .($this->qb_offset ? ' WHERE rnum >= '.($this->qb_offset + 1): '');
Barry Mienydd671972010-10-04 16:33:58 +0200671 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000672
673 // --------------------------------------------------------------------
674
675 /**
676 * Close DB Connection
677 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200678 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300680 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300682 @oci_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685}
686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687/* End of file oci8_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300688/* Location: ./system/database/drivers/oci8/oci8_driver.php */