blob: 057095c23b795011a2df00d820d31a208184a6be [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
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 /**
142 * Set client character set
143 *
144 * @access public
145 * @param string
146 * @param string
147 * @return resource
148 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300149 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
narfbg068e3de2011-09-17 21:38:46 +0300151 // this is done upon connect
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 return TRUE;
153 }
154
155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 /**
158 * Version number query string
159 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300160 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500161 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300163 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300165 return oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Execute the query
172 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300173 * @access protected called by the base class
Derek Jones4b9c6292011-07-01 17:40:48 -0500174 * @param string an SQL query
175 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300177 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 // oracle must parse the query before it is run. All of the actions with
180 // the query are based on the statement id returned by ociparse
181 $this->stmt_id = FALSE;
182 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300183 oci_set_prefetch($this->stmt_id, 1000);
184 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
186
187 /**
188 * Generate a statement ID
189 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500190 * @access private
191 * @param string an SQL query
192 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300194 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
196 if ( ! is_resource($this->stmt_id))
197 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300198 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Prep the query
206 *
207 * If needed, each database adapter can prep the query string
208 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500209 * @access private called by execute()
210 * @param string an SQL query
211 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300213 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 return $sql;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500221 * getCursor. Returns a cursor from the datbase
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500223 * @access public
224 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300226 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300228 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 return $this->curs_id;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500235 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500237 * @access public
238 * @param package package stored procedure is in
239 * @param procedure stored procedure to execute
240 * @param params array of parameters
241 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 *
243 * params array keys
244 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500245 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500247 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * this should be a reference to a variable
249 * type yes the type of the parameter
250 * length yes the max size of the parameter
251 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300252 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 if ($package == '' OR $procedure == '' OR ! is_array($params))
255 {
256 if ($this->db_debug)
257 {
258 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000259 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261 return FALSE;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // build the query string
265 $sql = "begin $package.$procedure(";
266
267 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500268 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
270 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200271
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300272 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 $have_cursor = TRUE;
275 }
276 }
277 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 $this->stmt_id = FALSE;
280 $this->_set_stmt_id($sql);
281 $this->_bind_params($params);
282 $this->query($sql, FALSE, $have_cursor);
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // --------------------------------------------------------------------
286
287 /**
288 * Bind parameters
289 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500290 * @access private
291 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300293 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
296 {
297 return;
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 foreach ($params as $param)
301 {
Barry Mienydd671972010-10-04 16:33:58 +0200302 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 if ( ! isset($param[$val]))
305 {
306 $param[$val] = '';
307 }
308 }
309
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300310 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Begin Transaction
318 *
319 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200320 * @return bool
321 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300322 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 if ( ! $this->trans_enabled)
325 {
326 return TRUE;
327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // When transactions are nested we only begin/commit/rollback the outermost ones
330 if ($this->_trans_depth > 0)
331 {
332 return TRUE;
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // Reset the transaction failure flag.
336 // If the $test_mode flag is set to TRUE transactions will be rolled back
337 // even if the queries produce a successful result.
338 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 $this->_commit = OCI_DEFAULT;
341 return TRUE;
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Commit Transaction
348 *
349 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200350 * @return bool
351 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300352 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 if ( ! $this->trans_enabled)
355 {
356 return TRUE;
357 }
358
359 // When transactions are nested we only begin/commit/rollback the outermost ones
360 if ($this->_trans_depth > 0)
361 {
362 return TRUE;
363 }
364
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300365 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 $this->_commit = OCI_COMMIT_ON_SUCCESS;
367 return $ret;
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Rollback Transaction
374 *
375 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200376 * @return bool
377 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300378 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 if ( ! $this->trans_enabled)
381 {
382 return TRUE;
383 }
384
385 // When transactions are nested we only begin/commit/rollback the outermost ones
386 if ($this->_trans_depth > 0)
387 {
388 return TRUE;
389 }
390
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300391 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $this->_commit = OCI_COMMIT_ON_SUCCESS;
393 return $ret;
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Escape String
400 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200401 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000402 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200403 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300405 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000407 if (is_array($str))
408 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500409 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200410 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000411 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200412 }
413
414 return $str;
415 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000416
Andrey Andreevc2905f52012-03-01 14:39:26 +0200417 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Jonese4ed5832009-02-20 21:44:59 +0000419 // escape LIKE condition wildcards
420 if ($like === TRUE)
421 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200422 return str_replace(array($this->_like_escape_chr, '%', '_'),
423 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
424 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Jonese4ed5832009-02-20 21:44:59 +0000427 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Affected Rows
434 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500435 * @access public
436 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300438 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300440 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Insert ID
447 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500448 * @access public
449 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300451 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000454 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * "Count All" query
461 *
462 * Generates a platform-specific query string that counts all records in
463 * the specified database
464 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500465 * @access public
466 * @param string
467 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300469 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000472 {
473 return 0;
474 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000475
Derek Allarde37ab382009-02-03 16:13:57 +0000476 $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 +0000477
478 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000479 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000481 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000482
483 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500484 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000485 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487
488 // --------------------------------------------------------------------
489
490 /**
491 * Show table query
492 *
493 * Generates a platform-specific query string so that the table names can be fetched
494 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300495 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @param boolean
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300497 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300499 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
502
503 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
504 {
Greg Aker0d424892010-01-26 02:14:44 +0000505 $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 +0000506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 return $sql;
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Show column query
515 *
516 * Generates a platform-specific query string so that the column names can be fetched
517 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300518 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500519 * @param string the table name
520 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300522 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
525 }
526
527 // --------------------------------------------------------------------
528
529 /**
530 * Field data query
531 *
532 * Generates a platform-specific query so that the column data can be retrieved
533 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500534 * @access public
535 * @param string the table name
536 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300538 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
540 return "SELECT * FROM ".$table." where rownum = 1";
541 }
542
543 // --------------------------------------------------------------------
544
545 /**
546 * The error message string
547 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300548 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500549 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300551 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300553 // If the error was during connection, no conn_id should be passed
554 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 return $error['message'];
556 }
557
558 // --------------------------------------------------------------------
559
560 /**
561 * The error message number
562 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300563 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500564 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300566 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300568 // Same as _error_message()
569 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 return $error['code'];
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 // --------------------------------------------------------------------
574
575 /**
576 * Escape the SQL Identifiers
577 *
578 * This function escapes column and table names
579 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300580 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @param string
582 * @return string
583 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300584 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 if ($this->_escape_char == '')
587 {
588 return $item;
589 }
590
591 foreach ($this->_reserved_identifiers as $id)
592 {
593 if (strpos($item, '.'.$id) !== FALSE)
594 {
Barry Mienydd671972010-10-04 16:33:58 +0200595 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // remove duplicates if the user already included the escape
598 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200599 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 if (strpos($item, '.') !== FALSE)
603 {
Barry Mienydd671972010-10-04 16:33:58 +0200604 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 }
606 else
607 {
608 $str = $this->_escape_char.$item.$this->_escape_char;
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // remove duplicates if the user already included the escape
612 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 // --------------------------------------------------------------------
616
617 /**
618 * From Tables
619 *
620 * This function implicitly groups FROM tables so there is no confusion
621 * about operator precedence in harmony with SQL standards
622 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300623 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * @param type
625 * @return type
626 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300627 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
629 if ( ! is_array($tables))
630 {
631 $tables = array($tables);
632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 return implode(', ', $tables);
635 }
636
637 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 /**
640 * Insert statement
641 *
642 * Generates a platform-specific insert string from the supplied data
643 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500644 * @access public
645 * @param string the table name
646 * @param array the insert keys
647 * @param array the insert values
648 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300650 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300652 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
654
655 // --------------------------------------------------------------------
656
657 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300658 * Insert_batch statement
659 *
660 * Generates a platform-specific insert string from the supplied data
661 *
Greg Aker03abee32011-12-25 00:31:29 -0600662 * @param string the table name
663 * @param array the insert keys
664 * @param array the insert values
665 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300666 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300667 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300668 {
669 $keys = implode(', ', $keys);
670 $sql = "INSERT ALL\n";
671
672 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300673 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300674 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300675 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300676
677 $sql .= 'SELECT * FROM dual';
678
679 return $sql;
680 }
681
682 // --------------------------------------------------------------------
683
684 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 * Update statement
686 *
687 * Generates a platform-specific update string from the supplied data
688 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300689 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 * @param string the table name
691 * @param array the update data
692 * @param array the where clause
693 * @param array the orderby clause
694 * @param array the limit clause
695 * @return string
696 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300697 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500699 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
701 $valstr[] = $key." = ".$val;
702 }
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
709
710 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
711
712 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 return $sql;
715 }
716
717 // --------------------------------------------------------------------
718
719 /**
720 * Truncate statement
721 *
722 * Generates a platform-specific truncate string from the supplied data
723 * If the database does not support the truncate() command
724 * This function maps to "DELETE FROM table"
725 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300726 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 * @param string the table name
728 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200729 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300730 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
732 return "TRUNCATE TABLE ".$table;
733 }
Barry Mienydd671972010-10-04 16:33:58 +0200734
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 // --------------------------------------------------------------------
736
737 /**
738 * Delete statement
739 *
740 * Generates a platform-specific delete string from the supplied data
741 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300742 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 * @param string the table name
744 * @param array the where clause
745 * @param string the limit clause
746 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200747 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300748 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
750 $conditions = '';
751
752 if (count($where) > 0 OR count($like) > 0)
753 {
754 $conditions = "\nWHERE ";
755 $conditions .= implode("\n", $this->ar_where);
756
757 if (count($where) > 0 && count($like) > 0)
758 {
759 $conditions .= " AND ";
760 }
761 $conditions .= implode("\n", $like);
762 }
763
764 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 return "DELETE FROM ".$table.$conditions.$limit;
767 }
768
769 // --------------------------------------------------------------------
770
771 /**
772 * Limit string
773 *
774 * Generates a platform-specific LIMIT clause
775 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300776 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500777 * @param string the sql query string
778 * @param integer the number of rows to limit the query to
779 * @param integer the offset value
780 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300782 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
784 $limit = $offset + $limit;
785 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
786
787 if ($offset != 0)
788 {
789 $newsql .= " WHERE rnum >= $offset";
790 }
791
792 // remember that we used limits
793 $this->limit_used = TRUE;
794
795 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200796 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000797
798 // --------------------------------------------------------------------
799
800 /**
801 * Close DB Connection
802 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300803 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500804 * @param resource
805 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300807 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300809 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 }
811
812
813}
814
815
816
817/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300818/* Location: ./system/database/drivers/oci8/oci8_driver.php */