blob: 7438d325848346b93a5e6ee71cc1cce8870ca278 [file] [log] [blame]
Andrey Andreev24abcb92012-01-05 20:40:15 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev24abcb92012-01-05 20:40:15 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24abcb92012-01-05 20:40:15 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreev342a4662012-01-24 15:24:00 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Barry Mienydd671972010-10-04 16:33:58 +020019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
Barry Mienydd671972010-10-04 16:33:58 +020024 * @since Version 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000025 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * oci8 Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
32 * creates dynamically based on whether the active record
33 * class is being used or not.
34 *
Barry Mienydd671972010-10-04 16:33:58 +020035 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050036 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41
42/**
43 * oci8 Database Adapter Class
44 *
45 * This is a modification of the DB_driver class to
46 * permit access to oracle databases
47 *
Derek Jones4b9c6292011-07-01 17:40:48 -050048 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000049 */
50
51class CI_DB_oci8_driver extends CI_DB {
52
Andrey Andreev24abcb92012-01-05 20:40:15 +020053 public $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Allard2067d1a2008-11-13 22:59:24 +000055 // The character used for excaping
Andrey Andreev24abcb92012-01-05 20:40:15 +020056 protected $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Jonese4ed5832009-02-20 21:44:59 +000058 // clause and character used for LIKE escape sequences
Andrey Andreev24abcb92012-01-05 20:40:15 +020059 protected $_like_escape_str = " escape '%s' ";
60 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
63 * The syntax to count rows is slightly different across different
64 * database engines, so this string appears in each driver and is
65 * used for the count_all() and count_all_results() functions.
66 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020067 protected $_count_string = 'SELECT COUNT(1) AS ';
68 protected $_random_keyword = ' ASC'; // not currently supported
Derek Allard2067d1a2008-11-13 22:59:24 +000069
70 // Set "auto commit" by default
Andrey Andreev99013ed2012-03-05 16:17:32 +020071 public $commit_mode = OCI_COMMIT_ON_SUCCESS;
Derek Allard2067d1a2008-11-13 22:59:24 +000072
73 // need to track statement id and cursor id
Andrey Andreev24abcb92012-01-05 20:40:15 +020074 public $stmt_id;
75 public $curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +000076
77 // if we use a limit, we will add a field that will
78 // throw off num_fields later
Andrey Andreev24abcb92012-01-05 20:40:15 +020079 public $limit_used;
Derek Allard2067d1a2008-11-13 22:59:24 +000080
Andrey Andreevdad61c22012-02-13 01:08:06 +020081 public function __construct($params)
82 {
83 parent::__construct($params);
84
85 $valid_dsns = array(
Andrey Andreevdad61c22012-02-13 01:08:06 +020086 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS
Andrey Andreevfcd1f472012-02-13 09:30:16 +020087 // Easy Connect string (Oracle 10g+)
88 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i',
Andrey Andreevdad61c22012-02-13 01:08:06 +020089 'in' => '/^[a-z0-9$_]+$/i' // Instance name (defined in tnsnames.ora)
90 );
91
92 /* Space characters don't have any effect when actually
93 * connecting, but can be a hassle while validating the DSN.
94 */
95 $this->dsn = str_replace(array("\n", "\r", "\t", ' '), '', $this->dsn);
96
97 if ($this->dsn !== '')
98 {
99 foreach ($valid_dsns as $regexp)
100 {
101 if (preg_match($regexp, $this->dsn))
102 {
103 return;
104 }
105 }
106 }
107
108 // Legacy support for TNS in the hostname configuration field
109 $this->hostname = str_replace(array("\n", "\r", "\t", ' '), '', $this->hostname);
110 if (preg_match($valid_dsns['tns'], $this->hostname))
111 {
112 $this->dsn = $this->hostname;
113 return;
114 }
115 elseif ($this->hostname !== '' && strpos($this->hostname, '/') === FALSE && strpos($this->hostname, ':') === FALSE
116 && (( ! empty($this->port) && ctype_digit($this->port)) OR $this->database !== ''))
117 {
118 /* If the hostname field isn't empty, doesn't contain
119 * ':' and/or '/' and if port and/or database aren't
120 * empty, then the hostname field is most likely indeed
121 * just a hostname. Therefore we'll try and build an
122 * Easy Connect string from these 3 settings, assuming
123 * that the database field is a service name.
124 */
125 $this->dsn = $this->hostname
126 .(( ! empty($this->port) && ctype_digit($this->port)) ? ':'.$this->port : '')
127 .($this->database !== '' ? '/'.ltrim($this->database, '/') : '');
128
129 if (preg_match($valid_dsns['ec'], $this->dsn))
130 {
131 return;
132 }
133 }
134
135 /* At this point, we can only try and validate the hostname and
136 * database fields separately as DSNs.
137 */
138 if (preg_match($valid_dsns['ec'], $this->hostname) OR preg_match($valid_dsns['in'], $this->hostname))
139 {
140 $this->dsn = $this->hostname;
141 return;
142 }
143
144 $this->database = str_replace(array("\n", "\r", "\t", ' '), '', $this->database);
145 foreach ($valid_dsns as $regexp)
146 {
147 if (preg_match($regexp, $this->database))
148 {
149 return;
150 }
151 }
152
153 /* Well - OK, an empty string should work as well.
154 * PHP will try to use environment variables to
155 * determine which Oracle instance to connect to.
156 */
157 $this->dsn = '';
158 }
159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 /**
161 * Non-persistent database connection
162 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200163 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300165 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Andrey Andreevdad61c22012-02-13 01:08:06 +0200167 return ( ! empty($this->char_set))
168 ? @oci_connect($this->username, $this->password, $this->dsn, $this->char_set)
169 : @oci_connect($this->username, $this->password, $this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Persistent database connection
176 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200177 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300179 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreevdad61c22012-02-13 01:08:06 +0200181 return ( ! empty($this->char_set))
182 ? @oci_pconnect($this->username, $this->password, $this->dsn, $this->char_set)
183 : @oci_pconnect($this->username, $this->password, $this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
186 // --------------------------------------------------------------------
187
188 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000189 * Reconnect
190 *
191 * Keep / reestablish the db connection if no queries have been
192 * sent for a length of time exceeding the server's idle timeout
193 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000194 * @return void
195 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300196 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000197 {
198 // not implemented in oracle
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300199 return;
Derek Jones87cbafc2009-02-27 16:29:59 +0000200 }
201
202 // --------------------------------------------------------------------
203
204 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 * Select the database
206 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200207 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300209 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300211 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 return TRUE;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200218 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200220 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200222 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200224 return isset($this->data_cache['version'])
225 ? $this->data_cache['version']
226 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Execute the query
233 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200234 * @param string an SQL query
235 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300237 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200239 /* Oracle must parse the query before it is run. All of the actions with
240 * the query are based on the statement id returned by oci_parse().
241 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 $this->stmt_id = FALSE;
243 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300244 oci_set_prefetch($this->stmt_id, 1000);
Andrey Andreev99013ed2012-03-05 16:17:32 +0200245 return @oci_execute($this->stmt_id, $this->commit_mode);
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248 /**
249 * Generate a statement ID
250 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200251 * @param string an SQL query
252 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300254 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 if ( ! is_resource($this->stmt_id))
257 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300258 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * Prep the query
266 *
267 * If needed, each database adapter can prep the query string
268 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200269 * @param string an SQL query
270 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300272 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 return $sql;
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
Andrey Andreevaa786c92012-01-16 12:14:45 +0200280 * getCursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200282 * @return resource cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300284 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200286 return $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
288
289 // --------------------------------------------------------------------
290
291 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500292 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200294 * @param string package name in which the stored procedure is in
295 * @param string stored procedure name to execute
296 * @param array parameters
297 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 *
299 * params array keys
300 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500301 * KEY OPTIONAL NOTES
Andrey Andreevaa786c92012-01-16 12:14:45 +0200302 * name no the name of the parameter should be in :<param_name> format
303 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
304 * this should be a reference to a variable
305 * type yes the type of the parameter
306 * length yes the max size of the parameter
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300308 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 if ($package == '' OR $procedure == '' OR ! is_array($params))
311 {
312 if ($this->db_debug)
313 {
314 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000315 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317 return FALSE;
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // build the query string
Andrey Andreeve35d7782012-01-19 15:56:20 +0200321 $sql = 'BEGIN '.$package.'.'.$procedure.'(';
Derek Allard2067d1a2008-11-13 22:59:24 +0000322
323 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500324 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200326 $sql .= $param['name'].',';
Barry Mienydd671972010-10-04 16:33:58 +0200327
Andrey Andreev85facfa2012-01-26 14:12:14 +0200328 if (isset($param['type']) && $param['type'] === OCI_B_CURSOR)
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $have_cursor = TRUE;
331 }
332 }
Andrey Andreevaa786c92012-01-16 12:14:45 +0200333 $sql = trim($sql, ',') . '); END;';
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 $this->stmt_id = FALSE;
336 $this->_set_stmt_id($sql);
337 $this->_bind_params($params);
Andrey Andreevaa786c92012-01-16 12:14:45 +0200338 return $this->query($sql, FALSE, $have_cursor);
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
342
343 /**
344 * Bind parameters
345 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200346 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300348 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
351 {
352 return;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 foreach ($params as $param)
356 {
Barry Mienydd671972010-10-04 16:33:58 +0200357 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 if ( ! isset($param[$val]))
360 {
361 $param[$val] = '';
362 }
363 }
364
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300365 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Begin Transaction
373 *
Barry Mienydd671972010-10-04 16:33:58 +0200374 * @return bool
375 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300376 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 if ( ! $this->trans_enabled)
379 {
380 return TRUE;
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // When transactions are nested we only begin/commit/rollback the outermost ones
384 if ($this->_trans_depth > 0)
385 {
386 return TRUE;
387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // Reset the transaction failure flag.
390 // If the $test_mode flag is set to TRUE transactions will be rolled back
391 // even if the queries produce a successful result.
392 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200393
Andrey Andreev99013ed2012-03-05 16:17:32 +0200394 $this->commit_mode = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 return TRUE;
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Commit Transaction
402 *
Barry Mienydd671972010-10-04 16:33:58 +0200403 * @return bool
404 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300405 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 if ( ! $this->trans_enabled)
408 {
409 return TRUE;
410 }
411
412 // When transactions are nested we only begin/commit/rollback the outermost ones
413 if ($this->_trans_depth > 0)
414 {
415 return TRUE;
416 }
417
Andrey Andreev99013ed2012-03-05 16:17:32 +0200418 $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200419 return oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Rollback Transaction
426 *
Barry Mienydd671972010-10-04 16:33:58 +0200427 * @return bool
428 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300429 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeve35d7782012-01-19 15:56:20 +0200432 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 return TRUE;
435 }
436
Andrey Andreev99013ed2012-03-05 16:17:32 +0200437 $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200438 return oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Escape String
445 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200446 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000447 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevaa786c92012-01-16 12:14:45 +0200448 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300450 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000452 if (is_array($str))
453 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500454 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200455 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000456 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200457 }
458
459 return $str;
460 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000461
Andrey Andreevaa786c92012-01-16 12:14:45 +0200462 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Jonese4ed5832009-02-20 21:44:59 +0000464 // escape LIKE condition wildcards
465 if ($like === TRUE)
466 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200467 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev08c7c622012-02-28 13:23:38 +0200468 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevaa786c92012-01-16 12:14:45 +0200469 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Jonese4ed5832009-02-20 21:44:59 +0000472 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474
475 // --------------------------------------------------------------------
476
477 /**
478 * Affected Rows
479 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200480 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300482 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300484 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Insert ID
491 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200492 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300494 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000497 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * "Count All" query
504 *
505 * Generates a platform-specific query string that counts all records in
506 * the specified database
507 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200508 * @param string
509 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300511 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000514 {
515 return 0;
516 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000517
Andrey Andreevaa786c92012-01-16 12:14:45 +0200518 $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000519
520 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000521 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000523 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000524
525 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500526 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000527 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529
530 // --------------------------------------------------------------------
531
532 /**
533 * Show table query
534 *
535 * Generates a platform-specific query string so that the table names can be fetched
536 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200537 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300538 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300540 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200542 $sql = 'SELECT TABLE_NAME FROM ALL_TABLES';
Derek Allard2067d1a2008-11-13 22:59:24 +0000543
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200544 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200546 return $sql." WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 return $sql;
550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Show column query
556 *
557 * Generates a platform-specific query string so that the column names can be fetched
558 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200559 * @param string the table name
560 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300562 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200564 return 'SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = \''.$table.'\'';
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566
567 // --------------------------------------------------------------------
568
569 /**
570 * Field data query
571 *
572 * Generates a platform-specific query so that the column data can be retrieved
573 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200574 * @param string the table name
575 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300577 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200579 return 'SELECT * FROM '.$table.' WHERE rownum = 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
581
582 // --------------------------------------------------------------------
583
584 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200585 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200587 * Returns an array containing code and message of the last
588 * database error that has occured.
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200589 *
590 * @return array
591 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200592 public function error()
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200593 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200594 /* oci_error() returns an array that already contains the
595 * 'code' and 'message' keys, so we can just return it.
596 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200597 if (is_resource($this->curs_id))
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200598 {
Andrey Andreev601f8b22012-03-01 20:11:15 +0200599 return oci_error($this->curs_id);
600 }
601 elseif (is_resource($this->stmt_id))
602 {
603 return oci_error($this->stmt_id);
604 }
605 elseif (is_resource($this->conn_id))
606 {
607 return oci_error($this->conn_id);
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200608 }
609
Andrey Andreev601f8b22012-03-01 20:11:15 +0200610 return oci_error();
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200611 }
612
613 // --------------------------------------------------------------------
614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 /**
616 * Escape the SQL Identifiers
617 *
618 * This function escapes column and table names
619 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * @param string
621 * @return string
622 */
Andrey Andreev4a315682012-01-26 02:03:10 +0200623 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 if ($this->_escape_char == '')
626 {
627 return $item;
628 }
629
630 foreach ($this->_reserved_identifiers as $id)
631 {
632 if (strpos($item, '.'.$id) !== FALSE)
633 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200634 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // remove duplicates if the user already included the escape
Andrey Andreevaa786c92012-01-16 12:14:45 +0200637 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200638 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 if (strpos($item, '.') !== FALSE)
642 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200643 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 }
Barry Mienydd671972010-10-04 16:33:58 +0200645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 // remove duplicates if the user already included the escape
Andrey Andreevaa786c92012-01-16 12:14:45 +0200647 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 // --------------------------------------------------------------------
651
652 /**
653 * From Tables
654 *
655 * This function implicitly groups FROM tables so there is no confusion
656 * about operator precedence in harmony with SQL standards
657 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200658 * @param array
659 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300661 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
Andrey Andreeve35d7782012-01-19 15:56:20 +0200663 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665
666 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 /**
669 * Insert statement
670 *
671 * Generates a platform-specific insert string from the supplied data
672 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200673 * @param string the table name
674 * @param array the insert keys
675 * @param array the insert values
676 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300678 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200680 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682
683 // --------------------------------------------------------------------
684
685 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300686 * Insert_batch statement
687 *
688 * Generates a platform-specific insert string from the supplied data
689 *
Greg Aker03abee32011-12-25 00:31:29 -0600690 * @param string the table name
691 * @param array the insert keys
692 * @param array the insert values
693 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300694 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300695 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300696 {
697 $keys = implode(', ', $keys);
698 $sql = "INSERT ALL\n";
699
700 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300701 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200702 $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i].'\n';
Andrey Andreevb83c4082011-09-23 03:32:45 +0300703 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300704
Andrey Andreevaa786c92012-01-16 12:14:45 +0200705 return $sql.'SELECT * FROM dual';
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300706 }
707
708 // --------------------------------------------------------------------
709
710 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 * Update statement
712 *
713 * Generates a platform-specific update string from the supplied data
714 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 * @param string the table name
716 * @param array the update data
717 * @param array the where clause
718 * @param array the orderby clause
719 * @param array the limit clause
720 * @return string
721 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300722 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500724 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200726 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 }
Barry Mienydd671972010-10-04 16:33:58 +0200728
Andrey Andreevaa786c92012-01-16 12:14:45 +0200729 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
730 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
731 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
732 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
734
735 // --------------------------------------------------------------------
736
737 /**
738 * Truncate statement
739 *
740 * Generates a platform-specific truncate string from the supplied data
741 * If the database does not support the truncate() command
742 * This function maps to "DELETE FROM table"
743 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 * @param string the table name
745 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200746 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300747 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200749 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 }
Barry Mienydd671972010-10-04 16:33:58 +0200751
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 // --------------------------------------------------------------------
753
754 /**
755 * Delete statement
756 *
757 * Generates a platform-specific delete string from the supplied data
758 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 * @param string the table name
760 * @param array the where clause
761 * @param string the limit clause
762 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200763 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300764 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 {
766 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 if (count($where) > 0 OR count($like) > 0)
768 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200769 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000770
771 if (count($where) > 0 && count($like) > 0)
772 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200773 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 }
775 $conditions .= implode("\n", $like);
776 }
777
Andrey Andreevaa786c92012-01-16 12:14:45 +0200778 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 }
780
781 // --------------------------------------------------------------------
782
783 /**
784 * Limit string
785 *
786 * Generates a platform-specific LIMIT clause
787 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200788 * @param string the sql query string
789 * @param int the number of rows to limit the query to
790 * @param int the offset value
791 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300793 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 $this->limit_used = TRUE;
Andrey Andreevaa786c92012-01-16 12:14:45 +0200796 return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($offset + $limit).')'
797 .($offset != 0 ? ' WHERE rnum >= '.$offset : '');
Barry Mienydd671972010-10-04 16:33:58 +0200798 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000799
800 // --------------------------------------------------------------------
801
802 /**
803 * Close DB Connection
804 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200805 * @param resource
806 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300808 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300810 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 }
812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813}
814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300816/* Location: ./system/database/drivers/oci8/oci8_driver.php */