blob: 06acbbb67c1215ae38fccc58f8ffb0c9ca29d64a [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 Andreev24abcb92012-01-05 20:40:15 +020071 protected $_commit = 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(
86 // Easy Connect string - Oracle 10g+
87 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^/])?(\/[a-z0-9$_]+)?$/i',
88 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS
89 '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 /**
218 * Set client character set
219 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * @param string
221 * @param string
222 * @return resource
223 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300224 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
narfbg068e3de2011-09-17 21:38:46 +0300226 // this is done upon connect
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 return TRUE;
228 }
229
230 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 /**
233 * Version number query string
234 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200235 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300237 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300239 return oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Execute the query
246 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200247 * @param string an SQL query
248 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300250 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200252 /* Oracle must parse the query before it is run. All of the actions with
253 * the query are based on the statement id returned by oci_parse().
254 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 $this->stmt_id = FALSE;
256 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300257 oci_set_prefetch($this->stmt_id, 1000);
258 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260
261 /**
262 * Generate a statement ID
263 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200264 * @param string an SQL query
265 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300267 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 if ( ! is_resource($this->stmt_id))
270 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300271 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * Prep the query
279 *
280 * If needed, each database adapter can prep the query string
281 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200282 * @param string an SQL query
283 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300285 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
287 return $sql;
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
Andrey Andreevaa786c92012-01-16 12:14:45 +0200293 * getCursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200295 * @return resource cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300297 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200299 return $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301
302 // --------------------------------------------------------------------
303
304 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500305 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200307 * @param string package name in which the stored procedure is in
308 * @param string stored procedure name to execute
309 * @param array parameters
310 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 *
312 * params array keys
313 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500314 * KEY OPTIONAL NOTES
Andrey Andreevaa786c92012-01-16 12:14:45 +0200315 * name no the name of the parameter should be in :<param_name> format
316 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
317 * this should be a reference to a variable
318 * type yes the type of the parameter
319 * length yes the max size of the parameter
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300321 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 if ($package == '' OR $procedure == '' OR ! is_array($params))
324 {
325 if ($this->db_debug)
326 {
327 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000328 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330 return FALSE;
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 // build the query string
Andrey Andreeve35d7782012-01-19 15:56:20 +0200334 $sql = 'BEGIN '.$package.'.'.$procedure.'(';
Derek Allard2067d1a2008-11-13 22:59:24 +0000335
336 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500337 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200339 $sql .= $param['name'].',';
Barry Mienydd671972010-10-04 16:33:58 +0200340
Andrey Andreev85facfa2012-01-26 14:12:14 +0200341 if (isset($param['type']) && $param['type'] === OCI_B_CURSOR)
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 $have_cursor = TRUE;
344 }
345 }
Andrey Andreevaa786c92012-01-16 12:14:45 +0200346 $sql = trim($sql, ',') . '); END;';
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $this->stmt_id = FALSE;
349 $this->_set_stmt_id($sql);
350 $this->_bind_params($params);
Andrey Andreevaa786c92012-01-16 12:14:45 +0200351 return $this->query($sql, FALSE, $have_cursor);
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // --------------------------------------------------------------------
355
356 /**
357 * Bind parameters
358 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200359 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300361 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
363 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
364 {
365 return;
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 foreach ($params as $param)
369 {
Barry Mienydd671972010-10-04 16:33:58 +0200370 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 if ( ! isset($param[$val]))
373 {
374 $param[$val] = '';
375 }
376 }
377
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300378 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Begin Transaction
386 *
Barry Mienydd671972010-10-04 16:33:58 +0200387 * @return bool
388 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300389 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 if ( ! $this->trans_enabled)
392 {
393 return TRUE;
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // When transactions are nested we only begin/commit/rollback the outermost ones
397 if ($this->_trans_depth > 0)
398 {
399 return TRUE;
400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 // Reset the transaction failure flag.
403 // If the $test_mode flag is set to TRUE transactions will be rolled back
404 // even if the queries produce a successful result.
405 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200406
Andrey Andreev24abcb92012-01-05 20:40:15 +0200407 $this->_commit = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 return TRUE;
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Commit Transaction
415 *
Barry Mienydd671972010-10-04 16:33:58 +0200416 * @return bool
417 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300418 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 if ( ! $this->trans_enabled)
421 {
422 return TRUE;
423 }
424
425 // When transactions are nested we only begin/commit/rollback the outermost ones
426 if ($this->_trans_depth > 0)
427 {
428 return TRUE;
429 }
430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 $this->_commit = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200432 return oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Rollback Transaction
439 *
Barry Mienydd671972010-10-04 16:33:58 +0200440 * @return bool
441 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300442 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeve35d7782012-01-19 15:56:20 +0200445 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 return TRUE;
448 }
449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 $this->_commit = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200451 return oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
453
454 // --------------------------------------------------------------------
455
456 /**
457 * Escape String
458 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200459 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000460 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevaa786c92012-01-16 12:14:45 +0200461 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300463 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000465 if (is_array($str))
466 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500467 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200468 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000469 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200470 }
471
472 return $str;
473 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000474
Andrey Andreevaa786c92012-01-16 12:14:45 +0200475 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Jonese4ed5832009-02-20 21:44:59 +0000477 // escape LIKE condition wildcards
478 if ($like === TRUE)
479 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200480 return str_replace(array('%', '_', $this->_like_escape_chr),
481 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
482 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Jonese4ed5832009-02-20 21:44:59 +0000485 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487
488 // --------------------------------------------------------------------
489
490 /**
491 * Affected Rows
492 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200493 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300495 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300497 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Insert ID
504 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200505 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300507 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000510 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * "Count All" query
517 *
518 * Generates a platform-specific query string that counts all records in
519 * the specified database
520 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200521 * @param string
522 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300524 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000527 {
528 return 0;
529 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000530
Andrey Andreevaa786c92012-01-16 12:14:45 +0200531 $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 +0000532
533 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000534 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000536 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000537
538 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500539 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000540 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542
543 // --------------------------------------------------------------------
544
545 /**
546 * Show table query
547 *
548 * Generates a platform-specific query string so that the table names can be fetched
549 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200550 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300551 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300553 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200555 $sql = 'SELECT TABLE_NAME FROM ALL_TABLES';
Derek Allard2067d1a2008-11-13 22:59:24 +0000556
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200557 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200559 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 +0000560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 return $sql;
563 }
564
565 // --------------------------------------------------------------------
566
567 /**
568 * Show column query
569 *
570 * Generates a platform-specific query string so that the column names can be fetched
571 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200572 * @param string the table name
573 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300575 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200577 return 'SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = \''.$table.'\'';
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 }
579
580 // --------------------------------------------------------------------
581
582 /**
583 * Field data query
584 *
585 * Generates a platform-specific query so that the column data can be retrieved
586 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200587 * @param string the table name
588 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300590 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200592 return 'SELECT * FROM '.$table.' WHERE rownum = 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
594
595 // --------------------------------------------------------------------
596
597 /**
598 * The error message string
599 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200600 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300602 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200604 $error = self::_get_error_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 return $error['message'];
606 }
607
608 // --------------------------------------------------------------------
609
610 /**
611 * The error message number
612 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200613 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300615 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200617 $error = self::_get_error_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 return $error['code'];
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
622
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200623 /* Get error data
624 *
625 * Used by _error_message() and _error_number()
626 *
627 * @return array
628 */
629 private function _get_error_data()
630 {
631 $res = NULL;
632 foreach (array('curs_id', 'stmt_id', 'conn_id') as $key)
633 {
634 if (is_resource($this->$key))
635 {
Andrey Andreev6684ce52012-01-05 22:05:14 +0200636 $res = $this->$key;
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200637 break;
638 }
639 }
640
641 return ( ! is_null($res)) ? oci_error($res) : oci_error();
642 }
643
644 // --------------------------------------------------------------------
645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 /**
647 * Escape the SQL Identifiers
648 *
649 * This function escapes column and table names
650 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 * @param string
652 * @return string
653 */
Andrey Andreev4a315682012-01-26 02:03:10 +0200654 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 if ($this->_escape_char == '')
657 {
658 return $item;
659 }
660
661 foreach ($this->_reserved_identifiers as $id)
662 {
663 if (strpos($item, '.'.$id) !== FALSE)
664 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200665 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // remove duplicates if the user already included the escape
Andrey Andreevaa786c92012-01-16 12:14:45 +0200668 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200669 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 if (strpos($item, '.') !== FALSE)
673 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200674 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 // remove duplicates if the user already included the escape
Andrey Andreevaa786c92012-01-16 12:14:45 +0200678 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // --------------------------------------------------------------------
682
683 /**
684 * From Tables
685 *
686 * This function implicitly groups FROM tables so there is no confusion
687 * about operator precedence in harmony with SQL standards
688 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200689 * @param array
690 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300692 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
Andrey Andreeve35d7782012-01-19 15:56:20 +0200694 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 }
696
697 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 /**
700 * Insert statement
701 *
702 * Generates a platform-specific insert string from the supplied data
703 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200704 * @param string the table name
705 * @param array the insert keys
706 * @param array the insert values
707 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300709 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200711 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
713
714 // --------------------------------------------------------------------
715
716 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300717 * Insert_batch statement
718 *
719 * Generates a platform-specific insert string from the supplied data
720 *
Greg Aker03abee32011-12-25 00:31:29 -0600721 * @param string the table name
722 * @param array the insert keys
723 * @param array the insert values
724 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300725 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300726 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300727 {
728 $keys = implode(', ', $keys);
729 $sql = "INSERT ALL\n";
730
731 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300732 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200733 $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i].'\n';
Andrey Andreevb83c4082011-09-23 03:32:45 +0300734 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300735
Andrey Andreevaa786c92012-01-16 12:14:45 +0200736 return $sql.'SELECT * FROM dual';
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300737 }
738
739 // --------------------------------------------------------------------
740
741 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 * Update statement
743 *
744 * Generates a platform-specific update string from the supplied data
745 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 * @param string the table name
747 * @param array the update data
748 * @param array the where clause
749 * @param array the orderby clause
750 * @param array the limit clause
751 * @return string
752 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300753 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500755 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200757 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
Barry Mienydd671972010-10-04 16:33:58 +0200759
Andrey Andreevaa786c92012-01-16 12:14:45 +0200760 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
761 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
762 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
763 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 }
765
766 // --------------------------------------------------------------------
767
768 /**
769 * Truncate statement
770 *
771 * Generates a platform-specific truncate string from the supplied data
772 * If the database does not support the truncate() command
773 * This function maps to "DELETE FROM table"
774 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 * @param string the table name
776 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200777 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300778 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200780 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 }
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 // --------------------------------------------------------------------
784
785 /**
786 * Delete statement
787 *
788 * Generates a platform-specific delete string from the supplied data
789 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 * @param string the table name
791 * @param array the where clause
792 * @param string the limit clause
793 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200794 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300795 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 {
797 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 if (count($where) > 0 OR count($like) > 0)
799 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200800 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000801
802 if (count($where) > 0 && count($like) > 0)
803 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200804 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
806 $conditions .= implode("\n", $like);
807 }
808
Andrey Andreevaa786c92012-01-16 12:14:45 +0200809 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 }
811
812 // --------------------------------------------------------------------
813
814 /**
815 * Limit string
816 *
817 * Generates a platform-specific LIMIT clause
818 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200819 * @param string the sql query string
820 * @param int the number of rows to limit the query to
821 * @param int the offset value
822 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300824 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 $this->limit_used = TRUE;
Andrey Andreevaa786c92012-01-16 12:14:45 +0200827 return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($offset + $limit).')'
828 .($offset != 0 ? ' WHERE rnum >= '.$offset : '');
Barry Mienydd671972010-10-04 16:33:58 +0200829 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000830
831 // --------------------------------------------------------------------
832
833 /**
834 * Close DB Connection
835 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200836 * @param resource
837 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300839 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300841 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 }
843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844}
845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300847/* Location: ./system/database/drivers/oci8/oci8_driver.php */