blob: c9e791d639c1b31e7980b0d4190d04eb7d1d845b [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 /**
106 * Select the database
107 *
Andrey Andreevda123732012-03-20 22:27:40 +0200108 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300110 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300112 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return TRUE;
114 }
115
116 // --------------------------------------------------------------------
117
118 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200119 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200121 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200123 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200125 return isset($this->data_cache['version'])
126 ? $this->data_cache['version']
127 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Execute the query
134 *
Andrey Andreevda123732012-03-20 22:27:40 +0200135 * @param string an SQL query
136 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300138 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
140 // oracle must parse the query before it is run. All of the actions with
141 // the query are based on the statement id returned by ociparse
142 $this->stmt_id = FALSE;
143 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300144 oci_set_prefetch($this->stmt_id, 1000);
145 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
147
148 /**
149 * Generate a statement ID
150 *
Andrey Andreevda123732012-03-20 22:27:40 +0200151 * @param string an SQL query
152 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 */
Andrey Andreevda123732012-03-20 22:27:40 +0200154 protected function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 if ( ! is_resource($this->stmt_id))
157 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400158 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160 }
Andrey Andreevda123732012-03-20 22:27:40 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // --------------------------------------------------------------------
163
164 /**
Andrey Andreevda123732012-03-20 22:27:40 +0200165 * Get cursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 *
Andrey Andreevda123732012-03-20 22:27:40 +0200167 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300169 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300171 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 return $this->curs_id;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500178 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 *
Andrey Andreevda123732012-03-20 22:27:40 +0200180 * @param string package stored procedure is in
181 * @param string stored procedure to execute
182 * @param array parameters
183 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 *
185 * params array keys
186 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500187 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500189 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Timothy Warren48e1d2e2012-03-19 19:07:07 -0400190 * this should be a reference to a variable
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * type yes the type of the parameter
192 * length yes the max size of the parameter
193 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300194 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
196 if ($package == '' OR $procedure == '' OR ! is_array($params))
197 {
198 if ($this->db_debug)
199 {
200 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000201 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203 return FALSE;
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // build the query string
207 $sql = "begin $package.$procedure(";
208
209 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500210 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200213
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300214 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 $have_cursor = TRUE;
217 }
218 }
219 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $this->stmt_id = FALSE;
222 $this->_set_stmt_id($sql);
223 $this->_bind_params($params);
224 $this->query($sql, FALSE, $have_cursor);
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // --------------------------------------------------------------------
228
229 /**
230 * Bind parameters
231 *
Andrey Andreevda123732012-03-20 22:27:40 +0200232 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 */
Andrey Andreevda123732012-03-20 22:27:40 +0200234 protected function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
237 {
238 return;
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 foreach ($params as $param)
242 {
Barry Mienydd671972010-10-04 16:33:58 +0200243 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
245 if ( ! isset($param[$val]))
246 {
247 $param[$val] = '';
248 }
249 }
250
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300251 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Begin Transaction
259 *
Barry Mienydd671972010-10-04 16:33:58 +0200260 * @return bool
261 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300262 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 if ( ! $this->trans_enabled)
265 {
266 return TRUE;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // When transactions are nested we only begin/commit/rollback the outermost ones
270 if ($this->_trans_depth > 0)
271 {
272 return TRUE;
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // Reset the transaction failure flag.
276 // If the $test_mode flag is set to TRUE transactions will be rolled back
277 // even if the queries produce a successful result.
278 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 $this->_commit = OCI_DEFAULT;
281 return TRUE;
282 }
283
284 // --------------------------------------------------------------------
285
286 /**
287 * Commit Transaction
288 *
Barry Mienydd671972010-10-04 16:33:58 +0200289 * @return bool
290 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300291 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
293 if ( ! $this->trans_enabled)
294 {
295 return TRUE;
296 }
297
298 // When transactions are nested we only begin/commit/rollback the outermost ones
299 if ($this->_trans_depth > 0)
300 {
301 return TRUE;
302 }
303
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300304 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 $this->_commit = OCI_COMMIT_ON_SUCCESS;
306 return $ret;
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Rollback Transaction
313 *
Barry Mienydd671972010-10-04 16:33:58 +0200314 * @return bool
315 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300316 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 if ( ! $this->trans_enabled)
319 {
320 return TRUE;
321 }
322
323 // When transactions are nested we only begin/commit/rollback the outermost ones
324 if ($this->_trans_depth > 0)
325 {
326 return TRUE;
327 }
328
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300329 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 $this->_commit = OCI_COMMIT_ON_SUCCESS;
331 return $ret;
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Escape String
338 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200339 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000340 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200341 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300343 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000345 if (is_array($str))
346 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500347 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200348 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000349 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200350 }
351
352 return $str;
353 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000354
Andrey Andreevc2905f52012-03-01 14:39:26 +0200355 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Jonese4ed5832009-02-20 21:44:59 +0000357 // escape LIKE condition wildcards
358 if ($like === TRUE)
359 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200360 return str_replace(array($this->_like_escape_chr, '%', '_'),
361 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
362 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Jonese4ed5832009-02-20 21:44:59 +0000365 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Affected Rows
372 *
Andrey Andreevda123732012-03-20 22:27:40 +0200373 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300375 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300377 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Insert ID
384 *
Andrey Andreevda123732012-03-20 22:27:40 +0200385 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300387 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000390 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
392
393 // --------------------------------------------------------------------
394
395 /**
396 * "Count All" query
397 *
398 * Generates a platform-specific query string that counts all records in
399 * the specified database
400 *
Andrey Andreevda123732012-03-20 22:27:40 +0200401 * @param string
402 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300404 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000407 {
408 return 0;
409 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000410
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200411 $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 +0000412 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000413 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000415 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000416
417 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500418 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000419 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Show table query
426 *
427 * Generates a platform-specific query string so that the table names can be fetched
428 *
Andrey Andreevda123732012-03-20 22:27:40 +0200429 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300430 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300432 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
435
436 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
437 {
Greg Aker0d424892010-01-26 02:14:44 +0000438 $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 +0000439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 return $sql;
442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Show column query
448 *
449 * Generates a platform-specific query string so that the column names can be fetched
450 *
Andrey Andreevda123732012-03-20 22:27:40 +0200451 * @param string the table name
452 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300454 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
457 }
458
459 // --------------------------------------------------------------------
460
461 /**
462 * Field data query
463 *
464 * Generates a platform-specific query so that the column data can be retrieved
465 *
Andrey Andreevda123732012-03-20 22:27:40 +0200466 * @param string the table name
467 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300469 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 return "SELECT * FROM ".$table." where rownum = 1";
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200477 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200479 * Returns an array containing code and message of the last
480 * database error that has occured.
Andrey Andreev601f8b22012-03-01 20:11:15 +0200481 *
482 * @return array
483 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200484 public function error()
Andrey Andreev601f8b22012-03-01 20:11:15 +0200485 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200486 /* oci_error() returns an array that already contains the
487 * 'code' and 'message' keys, so we can just return it.
488 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200489 if (is_resource($this->curs_id))
490 {
491 return oci_error($this->curs_id);
492 }
493 elseif (is_resource($this->stmt_id))
494 {
495 return oci_error($this->stmt_id);
496 }
497 elseif (is_resource($this->conn_id))
498 {
499 return oci_error($this->conn_id);
500 }
501
502 return oci_error();
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * Escape the SQL Identifiers
509 *
510 * This function escapes column and table names
511 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @param string
513 * @return string
514 */
Andrey Andreevda123732012-03-20 22:27:40 +0200515 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 if ($this->_escape_char == '')
518 {
519 return $item;
520 }
521
522 foreach ($this->_reserved_identifiers as $id)
523 {
524 if (strpos($item, '.'.$id) !== FALSE)
525 {
Barry Mienydd671972010-10-04 16:33:58 +0200526 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // remove duplicates if the user already included the escape
529 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200530 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 if (strpos($item, '.') !== FALSE)
534 {
Barry Mienydd671972010-10-04 16:33:58 +0200535 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
537 else
538 {
539 $str = $this->_escape_char.$item.$this->_escape_char;
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // remove duplicates if the user already included the escape
543 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
544 }
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 // --------------------------------------------------------------------
547
548 /**
549 * From Tables
550 *
551 * This function implicitly groups FROM tables so there is no confusion
552 * about operator precedence in harmony with SQL standards
553 *
Andrey Andreevda123732012-03-20 22:27:40 +0200554 * @param array
555 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300557 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
559 if ( ! is_array($tables))
560 {
561 $tables = array($tables);
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 return implode(', ', $tables);
565 }
566
567 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 /**
570 * Insert statement
571 *
572 * Generates a platform-specific insert string from the supplied data
573 *
Andrey Andreevda123732012-03-20 22:27:40 +0200574 * @param string the table name
575 * @param array the insert keys
576 * @param array the insert values
577 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300579 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300581 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
583
584 // --------------------------------------------------------------------
585
586 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300587 * Insert_batch statement
588 *
589 * Generates a platform-specific insert string from the supplied data
590 *
Andrey Andreevda123732012-03-20 22:27:40 +0200591 * @param string the table name
592 * @param array the insert keys
593 * @param array the insert values
594 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300595 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300596 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300597 {
598 $keys = implode(', ', $keys);
599 $sql = "INSERT ALL\n";
600
601 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300602 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300603 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300604 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300605
606 $sql .= 'SELECT * FROM dual';
607
608 return $sql;
609 }
610
611 // --------------------------------------------------------------------
612
613 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * Update statement
615 *
616 * Generates a platform-specific update string from the supplied data
617 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 * @param string the table name
619 * @param array the update data
620 * @param array the where clause
621 * @param array the orderby clause
622 * @param array the limit clause
623 * @return string
624 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300625 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500627 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
629 $valstr[] = $key." = ".$val;
630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
637
638 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
639
640 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 return $sql;
643 }
644
645 // --------------------------------------------------------------------
646
647 /**
648 * Truncate statement
649 *
650 * Generates a platform-specific truncate string from the supplied data
651 * If the database does not support the truncate() command
652 * This function maps to "DELETE FROM table"
653 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * @param string the table name
655 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200656 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300657 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
659 return "TRUNCATE TABLE ".$table;
660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 // --------------------------------------------------------------------
663
664 /**
665 * Delete statement
666 *
667 * Generates a platform-specific delete string from the supplied data
668 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 * @param string the table name
670 * @param array the where clause
671 * @param string the limit clause
672 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200673 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300674 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 {
676 $conditions = '';
677
678 if (count($where) > 0 OR count($like) > 0)
679 {
680 $conditions = "\nWHERE ";
681 $conditions .= implode("\n", $this->ar_where);
682
683 if (count($where) > 0 && count($like) > 0)
684 {
685 $conditions .= " AND ";
686 }
687 $conditions .= implode("\n", $like);
688 }
689
690 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 return "DELETE FROM ".$table.$conditions.$limit;
693 }
694
695 // --------------------------------------------------------------------
696
697 /**
698 * Limit string
699 *
700 * Generates a platform-specific LIMIT clause
701 *
Andrey Andreevda123732012-03-20 22:27:40 +0200702 * @param string the sql query string
703 * @param int the number of rows to limit the query to
704 * @param int the offset value
705 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300707 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
709 $limit = $offset + $limit;
710 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
711
712 if ($offset != 0)
713 {
714 $newsql .= " WHERE rnum >= $offset";
715 }
716
717 // remember that we used limits
718 $this->limit_used = TRUE;
719
720 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200721 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000722
723 // --------------------------------------------------------------------
724
725 /**
726 * Close DB Connection
727 *
Andrey Andreevda123732012-03-20 22:27:40 +0200728 * @param resource
729 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300731 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300733 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 }
735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736}
737
Derek Allard2067d1a2008-11-13 22:59:24 +0000738/* End of file oci8_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400739/* Location: ./system/database/drivers/oci8/oci8_driver.php */