blob: 070d58a340cf6c49c1b105380e9d3828cb8dd7ae [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
28// ------------------------------------------------------------------------
29
30/**
31 * oci8 Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
Barry Mienydd671972010-10-04 16:33:58 +020037 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050038 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43
44/**
45 * oci8 Database Adapter Class
46 *
47 * This is a modification of the DB_driver class to
48 * permit access to oracle databases
49 *
Derek Jones4b9c6292011-07-01 17:40:48 -050050 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000051 *
52 */
53
54class CI_DB_oci8_driver extends CI_DB {
55
56 var $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 // The character used for excaping
59 var $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Jonese4ed5832009-02-20 21:44:59 +000061 // clause and character used for LIKE escape sequences
62 var $_like_escape_str = " escape '%s' ";
63 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 /**
66 * The syntax to count rows is slightly different across different
67 * database engines, so this string appears in each driver and is
68 * used for the count_all() and count_all_results() functions.
69 */
70 var $_count_string = "SELECT COUNT(1) AS ";
71 var $_random_keyword = ' ASC'; // not currently supported
72
73 // Set "auto commit" by default
74 var $_commit = OCI_COMMIT_ON_SUCCESS;
75
76 // need to track statement id and cursor id
77 var $stmt_id;
78 var $curs_id;
79
80 // if we use a limit, we will add a field that will
81 // throw off num_fields later
82 var $limit_used;
83
84 /**
85 * Non-persistent database connection
86 *
Derek Jones4b9c6292011-07-01 17:40:48 -050087 * @access private called by the base class
88 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000089 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030090 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030092 return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Persistent database connection
99 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500100 * @access private called by the base class
101 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300103 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300105 return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
107
108 // --------------------------------------------------------------------
109
110 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000111 * Reconnect
112 *
113 * Keep / reestablish the db connection if no queries have been
114 * sent for a length of time exceeding the server's idle timeout
115 *
116 * @access public
117 * @return void
118 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300119 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000120 {
121 // not implemented in oracle
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300122 return;
Derek Jones87cbafc2009-02-27 16:29:59 +0000123 }
124
125 // --------------------------------------------------------------------
126
127 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * Select the database
129 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500130 * @access private called by the base class
131 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300133 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300135 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 return TRUE;
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200142 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200144 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200146 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200148 return isset($this->data_cache['version'])
149 ? $this->data_cache['version']
150 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Execute the query
157 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300158 * @access protected called by the base class
Derek Jones4b9c6292011-07-01 17:40:48 -0500159 * @param string an SQL query
160 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300162 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 // oracle must parse the query before it is run. All of the actions with
165 // the query are based on the statement id returned by ociparse
166 $this->stmt_id = FALSE;
167 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300168 oci_set_prefetch($this->stmt_id, 1000);
169 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 /**
173 * Generate a statement ID
174 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500175 * @access private
176 * @param string an SQL query
177 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300179 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 if ( ! is_resource($this->stmt_id))
182 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300183 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Prep the query
191 *
192 * If needed, each database adapter can prep the query string
193 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500194 * @access private called by execute()
195 * @param string an SQL query
196 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300198 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 return $sql;
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500206 * getCursor. Returns a cursor from the datbase
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500208 * @access public
209 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300211 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300213 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 return $this->curs_id;
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500220 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500222 * @access public
223 * @param package package stored procedure is in
224 * @param procedure stored procedure to execute
225 * @param params array of parameters
226 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 *
228 * params array keys
229 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500230 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500232 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * this should be a reference to a variable
234 * type yes the type of the parameter
235 * length yes the max size of the parameter
236 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300237 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 if ($package == '' OR $procedure == '' OR ! is_array($params))
240 {
241 if ($this->db_debug)
242 {
243 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000244 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246 return FALSE;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // build the query string
250 $sql = "begin $package.$procedure(";
251
252 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500253 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200256
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300257 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 $have_cursor = TRUE;
260 }
261 }
262 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 $this->stmt_id = FALSE;
265 $this->_set_stmt_id($sql);
266 $this->_bind_params($params);
267 $this->query($sql, FALSE, $have_cursor);
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
271
272 /**
273 * Bind parameters
274 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500275 * @access private
276 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300278 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
281 {
282 return;
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 foreach ($params as $param)
286 {
Barry Mienydd671972010-10-04 16:33:58 +0200287 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 if ( ! isset($param[$val]))
290 {
291 $param[$val] = '';
292 }
293 }
294
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300295 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Begin Transaction
303 *
304 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200305 * @return bool
306 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300307 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 if ( ! $this->trans_enabled)
310 {
311 return TRUE;
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // When transactions are nested we only begin/commit/rollback the outermost ones
315 if ($this->_trans_depth > 0)
316 {
317 return TRUE;
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Reset the transaction failure flag.
321 // If the $test_mode flag is set to TRUE transactions will be rolled back
322 // even if the queries produce a successful result.
323 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 $this->_commit = OCI_DEFAULT;
326 return TRUE;
327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Commit Transaction
333 *
334 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200335 * @return bool
336 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300337 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 if ( ! $this->trans_enabled)
340 {
341 return TRUE;
342 }
343
344 // When transactions are nested we only begin/commit/rollback the outermost ones
345 if ($this->_trans_depth > 0)
346 {
347 return TRUE;
348 }
349
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300350 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 $this->_commit = OCI_COMMIT_ON_SUCCESS;
352 return $ret;
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * Rollback Transaction
359 *
360 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200361 * @return bool
362 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300363 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
365 if ( ! $this->trans_enabled)
366 {
367 return TRUE;
368 }
369
370 // When transactions are nested we only begin/commit/rollback the outermost ones
371 if ($this->_trans_depth > 0)
372 {
373 return TRUE;
374 }
375
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300376 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 $this->_commit = OCI_COMMIT_ON_SUCCESS;
378 return $ret;
379 }
380
381 // --------------------------------------------------------------------
382
383 /**
384 * Escape String
385 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200386 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000387 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200388 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300390 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000392 if (is_array($str))
393 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500394 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200395 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000396 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200397 }
398
399 return $str;
400 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000401
Andrey Andreevc2905f52012-03-01 14:39:26 +0200402 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Jonese4ed5832009-02-20 21:44:59 +0000404 // escape LIKE condition wildcards
405 if ($like === TRUE)
406 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200407 return str_replace(array($this->_like_escape_chr, '%', '_'),
408 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
409 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Jonese4ed5832009-02-20 21:44:59 +0000412 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Affected Rows
419 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500420 * @access public
421 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300423 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300425 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427
428 // --------------------------------------------------------------------
429
430 /**
431 * Insert ID
432 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500433 * @access public
434 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300436 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000439 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * "Count All" query
446 *
447 * Generates a platform-specific query string that counts all records in
448 * the specified database
449 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500450 * @access public
451 * @param string
452 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300454 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000457 {
458 return 0;
459 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000460
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200461 $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 +0000462 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000463 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000465 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000466
467 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500468 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000469 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Show table query
476 *
477 * Generates a platform-specific query string so that the table names can be fetched
478 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300479 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @param boolean
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300481 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300483 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
485 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
486
487 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
488 {
Greg Aker0d424892010-01-26 02:14:44 +0000489 $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 +0000490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 return $sql;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Show column query
499 *
500 * Generates a platform-specific query string so that the column names can be fetched
501 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300502 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500503 * @param string the table name
504 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300506 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Field data query
515 *
516 * Generates a platform-specific query so that the column data can be retrieved
517 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500518 * @access public
519 * @param string the table name
520 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300522 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 return "SELECT * FROM ".$table." where rownum = 1";
525 }
526
527 // --------------------------------------------------------------------
528
529 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200530 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200532 * Returns an array containing code and message of the last
533 * database error that has occured.
Andrey Andreev601f8b22012-03-01 20:11:15 +0200534 *
535 * @return array
536 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200537 public function error()
Andrey Andreev601f8b22012-03-01 20:11:15 +0200538 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200539 /* oci_error() returns an array that already contains the
540 * 'code' and 'message' keys, so we can just return it.
541 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200542 if (is_resource($this->curs_id))
543 {
544 return oci_error($this->curs_id);
545 }
546 elseif (is_resource($this->stmt_id))
547 {
548 return oci_error($this->stmt_id);
549 }
550 elseif (is_resource($this->conn_id))
551 {
552 return oci_error($this->conn_id);
553 }
554
555 return oci_error();
556 }
557
558 // --------------------------------------------------------------------
559
560 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * Escape the SQL Identifiers
562 *
563 * This function escapes column and table names
564 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300565 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * @param string
567 * @return string
568 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300569 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
571 if ($this->_escape_char == '')
572 {
573 return $item;
574 }
575
576 foreach ($this->_reserved_identifiers as $id)
577 {
578 if (strpos($item, '.'.$id) !== FALSE)
579 {
Barry Mienydd671972010-10-04 16:33:58 +0200580 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // remove duplicates if the user already included the escape
583 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200584 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 if (strpos($item, '.') !== FALSE)
588 {
Barry Mienydd671972010-10-04 16:33:58 +0200589 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591 else
592 {
593 $str = $this->_escape_char.$item.$this->_escape_char;
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // remove duplicates if the user already included the escape
597 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // --------------------------------------------------------------------
601
602 /**
603 * From Tables
604 *
605 * This function implicitly groups FROM tables so there is no confusion
606 * about operator precedence in harmony with SQL standards
607 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300608 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @param type
610 * @return type
611 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300612 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
614 if ( ! is_array($tables))
615 {
616 $tables = array($tables);
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 return implode(', ', $tables);
620 }
621
622 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 /**
625 * Insert statement
626 *
627 * Generates a platform-specific insert string from the supplied data
628 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500629 * @access public
630 * @param string the table name
631 * @param array the insert keys
632 * @param array the insert values
633 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300635 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300637 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
639
640 // --------------------------------------------------------------------
641
642 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300643 * Insert_batch statement
644 *
645 * Generates a platform-specific insert string from the supplied data
646 *
Greg Aker03abee32011-12-25 00:31:29 -0600647 * @param string the table name
648 * @param array the insert keys
649 * @param array the insert values
650 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300651 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300652 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300653 {
654 $keys = implode(', ', $keys);
655 $sql = "INSERT ALL\n";
656
657 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300658 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300659 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300660 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300661
662 $sql .= 'SELECT * FROM dual';
663
664 return $sql;
665 }
666
667 // --------------------------------------------------------------------
668
669 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * Update statement
671 *
672 * Generates a platform-specific update string from the supplied data
673 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300674 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 * @param string the table name
676 * @param array the update data
677 * @param array the where clause
678 * @param array the orderby clause
679 * @param array the limit clause
680 * @return string
681 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300682 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500684 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
686 $valstr[] = $key." = ".$val;
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
694
695 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
696
697 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 return $sql;
700 }
701
702 // --------------------------------------------------------------------
703
704 /**
705 * Truncate statement
706 *
707 * Generates a platform-specific truncate string from the supplied data
708 * If the database does not support the truncate() command
709 * This function maps to "DELETE FROM table"
710 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300711 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string the table name
713 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200714 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300715 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
717 return "TRUNCATE TABLE ".$table;
718 }
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 // --------------------------------------------------------------------
721
722 /**
723 * Delete statement
724 *
725 * Generates a platform-specific delete string from the supplied data
726 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300727 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @param string the table name
729 * @param array the where clause
730 * @param string the limit clause
731 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200732 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300733 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
735 $conditions = '';
736
737 if (count($where) > 0 OR count($like) > 0)
738 {
739 $conditions = "\nWHERE ";
740 $conditions .= implode("\n", $this->ar_where);
741
742 if (count($where) > 0 && count($like) > 0)
743 {
744 $conditions .= " AND ";
745 }
746 $conditions .= implode("\n", $like);
747 }
748
749 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 return "DELETE FROM ".$table.$conditions.$limit;
752 }
753
754 // --------------------------------------------------------------------
755
756 /**
757 * Limit string
758 *
759 * Generates a platform-specific LIMIT clause
760 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300761 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500762 * @param string the sql query string
763 * @param integer the number of rows to limit the query to
764 * @param integer the offset value
765 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300767 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
769 $limit = $offset + $limit;
770 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
771
772 if ($offset != 0)
773 {
774 $newsql .= " WHERE rnum >= $offset";
775 }
776
777 // remember that we used limits
778 $this->limit_used = TRUE;
779
780 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200781 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000782
783 // --------------------------------------------------------------------
784
785 /**
786 * Close DB Connection
787 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300788 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500789 * @param resource
790 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300792 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300794 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
796
797
798}
799
800
801
802/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300803/* Location: ./system/database/drivers/oci8/oci8_driver.php */