blob: 238a08ff89b2a2064834fc1e719df68478670279 [file] [log] [blame]
Andrey Andreevda123732012-03-20 22:27:40 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 Andreevda123732012-03-20 22:27:40 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevda123732012-03-20 22:27:40 +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 */
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 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051class CI_DB_oci8_driver extends CI_DB {
52
Andrey Andreevda123732012-03-20 22:27:40 +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 Andreevda123732012-03-20 22:27:40 +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 Andreevda123732012-03-20 22:27:40 +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 Andreevda123732012-03-20 22:27:40 +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 Andreevda123732012-03-20 22:27:40 +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 Andreevda123732012-03-20 22:27:40 +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 Andreevda123732012-03-20 22:27:40 +020079 public $limit_used;
Derek Allard2067d1a2008-11-13 22:59:24 +000080
81 /**
82 * Non-persistent database connection
83 *
Andrey Andreevda123732012-03-20 22:27:40 +020084 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000085 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030086 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030088 return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Persistent database connection
95 *
Andrey Andreevda123732012-03-20 22:27:40 +020096 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000097 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030098 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300100 return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
102
103 // --------------------------------------------------------------------
104
105 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000106 * Reconnect
107 *
108 * Keep / reestablish the db connection if no queries have been
109 * sent for a length of time exceeding the server's idle timeout
110 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000111 * @return void
112 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300113 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000114 {
115 // not implemented in oracle
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300116 return;
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 }
118
119 // --------------------------------------------------------------------
120
121 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 * Select the database
123 *
Andrey Andreevda123732012-03-20 22:27:40 +0200124 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300126 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300128 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 return TRUE;
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200135 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200137 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200139 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200141 return isset($this->data_cache['version'])
142 ? $this->data_cache['version']
143 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145
146 // --------------------------------------------------------------------
147
148 /**
149 * Execute the query
150 *
Andrey Andreevda123732012-03-20 22:27:40 +0200151 * @param string an SQL query
152 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300154 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 // oracle must parse the query before it is run. All of the actions with
157 // the query are based on the statement id returned by ociparse
158 $this->stmt_id = FALSE;
159 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300160 oci_set_prefetch($this->stmt_id, 1000);
161 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
164 /**
165 * Generate a statement ID
166 *
Andrey Andreevda123732012-03-20 22:27:40 +0200167 * @param string an SQL query
168 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 */
Andrey Andreevda123732012-03-20 22:27:40 +0200170 protected function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
172 if ( ! is_resource($this->stmt_id))
173 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400174 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176 }
Andrey Andreevda123732012-03-20 22:27:40 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 // --------------------------------------------------------------------
179
180 /**
Andrey Andreevda123732012-03-20 22:27:40 +0200181 * Get cursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 *
Andrey Andreevda123732012-03-20 22:27:40 +0200183 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300185 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300187 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 return $this->curs_id;
189 }
190
191 // --------------------------------------------------------------------
192
193 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500194 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 *
Andrey Andreevda123732012-03-20 22:27:40 +0200196 * @param string package stored procedure is in
197 * @param string stored procedure to execute
198 * @param array parameters
199 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 *
201 * params array keys
202 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500203 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500205 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Timothy Warren48e1d2e2012-03-19 19:07:07 -0400206 * this should be a reference to a variable
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 * type yes the type of the parameter
208 * length yes the max size of the parameter
209 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300210 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 if ($package == '' OR $procedure == '' OR ! is_array($params))
213 {
214 if ($this->db_debug)
215 {
216 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000217 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219 return FALSE;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // build the query string
223 $sql = "begin $package.$procedure(";
224
225 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500226 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
228 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200229
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300230 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
232 $have_cursor = TRUE;
233 }
234 }
235 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 $this->stmt_id = FALSE;
238 $this->_set_stmt_id($sql);
239 $this->_bind_params($params);
240 $this->query($sql, FALSE, $have_cursor);
241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // --------------------------------------------------------------------
244
245 /**
246 * Bind parameters
247 *
Andrey Andreevda123732012-03-20 22:27:40 +0200248 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 */
Andrey Andreevda123732012-03-20 22:27:40 +0200250 protected function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
253 {
254 return;
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 foreach ($params as $param)
258 {
Barry Mienydd671972010-10-04 16:33:58 +0200259 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 if ( ! isset($param[$val]))
262 {
263 $param[$val] = '';
264 }
265 }
266
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300267 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Begin Transaction
275 *
Barry Mienydd671972010-10-04 16:33:58 +0200276 * @return bool
277 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300278 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if ( ! $this->trans_enabled)
281 {
282 return TRUE;
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // When transactions are nested we only begin/commit/rollback the outermost ones
286 if ($this->_trans_depth > 0)
287 {
288 return TRUE;
289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // Reset the transaction failure flag.
292 // If the $test_mode flag is set to TRUE transactions will be rolled back
293 // even if the queries produce a successful result.
294 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 $this->_commit = OCI_DEFAULT;
297 return TRUE;
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Commit Transaction
304 *
Barry Mienydd671972010-10-04 16:33:58 +0200305 * @return bool
306 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300307 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 if ( ! $this->trans_enabled)
310 {
311 return TRUE;
312 }
313
314 // When transactions are nested we only begin/commit/rollback the outermost ones
315 if ($this->_trans_depth > 0)
316 {
317 return TRUE;
318 }
319
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300320 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 $this->_commit = OCI_COMMIT_ON_SUCCESS;
322 return $ret;
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Rollback Transaction
329 *
Barry Mienydd671972010-10-04 16:33:58 +0200330 * @return bool
331 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300332 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 if ( ! $this->trans_enabled)
335 {
336 return TRUE;
337 }
338
339 // When transactions are nested we only begin/commit/rollback the outermost ones
340 if ($this->_trans_depth > 0)
341 {
342 return TRUE;
343 }
344
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300345 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 $this->_commit = OCI_COMMIT_ON_SUCCESS;
347 return $ret;
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Escape String
354 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200355 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000356 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200357 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300359 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000361 if (is_array($str))
362 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500363 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200364 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000365 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200366 }
367
368 return $str;
369 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000370
Andrey Andreevc2905f52012-03-01 14:39:26 +0200371 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Jonese4ed5832009-02-20 21:44:59 +0000373 // escape LIKE condition wildcards
374 if ($like === TRUE)
375 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200376 return str_replace(array($this->_like_escape_chr, '%', '_'),
377 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
378 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Jonese4ed5832009-02-20 21:44:59 +0000381 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Affected Rows
388 *
Andrey Andreevda123732012-03-20 22:27:40 +0200389 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300391 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300393 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Insert ID
400 *
Andrey Andreevda123732012-03-20 22:27:40 +0200401 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300403 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000406 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408
409 // --------------------------------------------------------------------
410
411 /**
412 * "Count All" query
413 *
414 * Generates a platform-specific query string that counts all records in
415 * the specified database
416 *
Andrey Andreevda123732012-03-20 22:27:40 +0200417 * @param string
418 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300420 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000423 {
424 return 0;
425 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000426
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200427 $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 +0000428 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000429 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000431 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000432
433 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500434 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000435 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Show table query
442 *
443 * Generates a platform-specific query string so that the table names can be fetched
444 *
Andrey Andreevda123732012-03-20 22:27:40 +0200445 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300446 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300448 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
450 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
451
452 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
453 {
Greg Aker0d424892010-01-26 02:14:44 +0000454 $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 +0000455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 return $sql;
458 }
459
460 // --------------------------------------------------------------------
461
462 /**
463 * Show column query
464 *
465 * Generates a platform-specific query string so that the column names can be fetched
466 *
Andrey Andreevda123732012-03-20 22:27:40 +0200467 * @param string the table name
468 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300470 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
473 }
474
475 // --------------------------------------------------------------------
476
477 /**
478 * Field data query
479 *
480 * Generates a platform-specific query so that the column data can be retrieved
481 *
Andrey Andreevda123732012-03-20 22:27:40 +0200482 * @param string the table name
483 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300485 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 return "SELECT * FROM ".$table." where rownum = 1";
488 }
489
490 // --------------------------------------------------------------------
491
492 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200493 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200495 * Returns an array containing code and message of the last
496 * database error that has occured.
Andrey Andreev601f8b22012-03-01 20:11:15 +0200497 *
498 * @return array
499 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200500 public function error()
Andrey Andreev601f8b22012-03-01 20:11:15 +0200501 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200502 /* oci_error() returns an array that already contains the
503 * 'code' and 'message' keys, so we can just return it.
504 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200505 if (is_resource($this->curs_id))
506 {
507 return oci_error($this->curs_id);
508 }
509 elseif (is_resource($this->stmt_id))
510 {
511 return oci_error($this->stmt_id);
512 }
513 elseif (is_resource($this->conn_id))
514 {
515 return oci_error($this->conn_id);
516 }
517
518 return oci_error();
519 }
520
521 // --------------------------------------------------------------------
522
523 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 * Escape the SQL Identifiers
525 *
526 * This function escapes column and table names
527 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @param string
529 * @return string
530 */
Andrey Andreevda123732012-03-20 22:27:40 +0200531 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
533 if ($this->_escape_char == '')
534 {
535 return $item;
536 }
537
538 foreach ($this->_reserved_identifiers as $id)
539 {
540 if (strpos($item, '.'.$id) !== FALSE)
541 {
Barry Mienydd671972010-10-04 16:33:58 +0200542 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // remove duplicates if the user already included the escape
545 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200546 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 if (strpos($item, '.') !== FALSE)
550 {
Barry Mienydd671972010-10-04 16:33:58 +0200551 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553 else
554 {
555 $str = $this->_escape_char.$item.$this->_escape_char;
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 // remove duplicates if the user already included the escape
559 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 // --------------------------------------------------------------------
563
564 /**
565 * From Tables
566 *
567 * This function implicitly groups FROM tables so there is no confusion
568 * about operator precedence in harmony with SQL standards
569 *
Andrey Andreevda123732012-03-20 22:27:40 +0200570 * @param array
571 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300573 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
575 if ( ! is_array($tables))
576 {
577 $tables = array($tables);
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 return implode(', ', $tables);
581 }
582
583 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 /**
586 * Insert statement
587 *
588 * Generates a platform-specific insert string from the supplied data
589 *
Andrey Andreevda123732012-03-20 22:27:40 +0200590 * @param string the table name
591 * @param array the insert keys
592 * @param array the insert values
593 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300595 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300597 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 }
599
600 // --------------------------------------------------------------------
601
602 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300603 * Insert_batch statement
604 *
605 * Generates a platform-specific insert string from the supplied data
606 *
Andrey Andreevda123732012-03-20 22:27:40 +0200607 * @param string the table name
608 * @param array the insert keys
609 * @param array the insert values
610 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300611 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300612 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300613 {
614 $keys = implode(', ', $keys);
615 $sql = "INSERT ALL\n";
616
617 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300618 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300619 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300620 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300621
622 $sql .= 'SELECT * FROM dual';
623
624 return $sql;
625 }
626
627 // --------------------------------------------------------------------
628
629 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 * Update statement
631 *
632 * Generates a platform-specific update string from the supplied data
633 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @param string the table name
635 * @param array the update data
636 * @param array the where clause
637 * @param array the orderby clause
638 * @param array the limit clause
639 * @return string
640 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300641 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500643 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 $valstr[] = $key." = ".$val;
646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
653
654 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
655
656 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 return $sql;
659 }
660
661 // --------------------------------------------------------------------
662
663 /**
664 * Truncate statement
665 *
666 * Generates a platform-specific truncate string from the supplied data
667 * If the database does not support the truncate() command
668 * This function maps to "DELETE FROM table"
669 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * @param string the table name
671 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200672 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300673 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
675 return "TRUNCATE TABLE ".$table;
676 }
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 // --------------------------------------------------------------------
679
680 /**
681 * Delete statement
682 *
683 * Generates a platform-specific delete string from the supplied data
684 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 * @param string the table name
686 * @param array the where clause
687 * @param string the limit clause
688 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200689 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300690 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 $conditions = '';
693
694 if (count($where) > 0 OR count($like) > 0)
695 {
696 $conditions = "\nWHERE ";
697 $conditions .= implode("\n", $this->ar_where);
698
699 if (count($where) > 0 && count($like) > 0)
700 {
701 $conditions .= " AND ";
702 }
703 $conditions .= implode("\n", $like);
704 }
705
706 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 return "DELETE FROM ".$table.$conditions.$limit;
709 }
710
711 // --------------------------------------------------------------------
712
713 /**
714 * Limit string
715 *
716 * Generates a platform-specific LIMIT clause
717 *
Andrey Andreevda123732012-03-20 22:27:40 +0200718 * @param string the sql query string
719 * @param int the number of rows to limit the query to
720 * @param int the offset value
721 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300723 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
725 $limit = $offset + $limit;
726 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
727
728 if ($offset != 0)
729 {
730 $newsql .= " WHERE rnum >= $offset";
731 }
732
733 // remember that we used limits
734 $this->limit_used = TRUE;
735
736 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200737 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000738
739 // --------------------------------------------------------------------
740
741 /**
742 * Close DB Connection
743 *
Andrey Andreevda123732012-03-20 22:27:40 +0200744 * @param resource
745 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300747 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300749 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 }
751
Derek Allard2067d1a2008-11-13 22:59:24 +0000752}
753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754/* End of file oci8_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400755/* Location: ./system/database/drivers/oci8/oci8_driver.php */