blob: 5fbc0a0a031c4932a034d2d658843210e3665352 [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
Jamie Rumbelow7efad202012-02-19 12:37:00 +000034 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * 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 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500401 * @access public
402 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000403 * @param bool whether or not the string will be used in a LIKE condition
Derek Jones4b9c6292011-07-01 17:40:48 -0500404 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300406 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000408 if (is_array($str))
409 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500410 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200411 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000412 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200413 }
414
415 return $str;
416 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000417
Greg Aker757dda62010-04-14 19:06:19 -0500418 $str = remove_invisible_characters($str);
Michiel Vugteveeneaa55412011-08-25 21:22:49 +0200419 $str = str_replace("'", "''", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Jonese4ed5832009-02-20 21:44:59 +0000421 // escape LIKE condition wildcards
422 if ($like === TRUE)
423 {
424 $str = str_replace( array('%', '_', $this->_like_escape_chr),
425 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
426 $str);
427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Jonese4ed5832009-02-20 21:44:59 +0000429 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Affected Rows
436 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500437 * @access public
438 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300440 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300442 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444
445 // --------------------------------------------------------------------
446
447 /**
448 * Insert ID
449 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500450 * @access public
451 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300453 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
455 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000456 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
458
459 // --------------------------------------------------------------------
460
461 /**
462 * "Count All" query
463 *
464 * Generates a platform-specific query string that counts all records in
465 * the specified database
466 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500467 * @access public
468 * @param string
469 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300471 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
473 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000474 {
475 return 0;
476 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000477
Derek Allarde37ab382009-02-03 16:13:57 +0000478 $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 +0000479
480 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000481 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484
485 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500486 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000487 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489
490 // --------------------------------------------------------------------
491
492 /**
493 * Show table query
494 *
495 * Generates a platform-specific query string so that the table names can be fetched
496 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300497 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * @param boolean
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300499 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300501 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
503 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
504
505 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
506 {
Greg Aker0d424892010-01-26 02:14:44 +0000507 $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 +0000508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 return $sql;
511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Show column query
517 *
518 * Generates a platform-specific query string so that the column names can be fetched
519 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300520 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500521 * @param string the table name
522 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300524 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Field data query
533 *
534 * Generates a platform-specific query so that the column data can be retrieved
535 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500536 * @access public
537 * @param string the table name
538 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300540 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 {
542 return "SELECT * FROM ".$table." where rownum = 1";
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * The error message string
549 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300550 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500551 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300553 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300555 // If the error was during connection, no conn_id should be passed
556 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 return $error['message'];
558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * The error message number
564 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300565 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500566 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300568 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300570 // Same as _error_message()
571 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 return $error['code'];
573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // --------------------------------------------------------------------
576
577 /**
578 * Escape the SQL Identifiers
579 *
580 * This function escapes column and table names
581 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300582 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * @param string
584 * @return string
585 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300586 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 if ($this->_escape_char == '')
589 {
590 return $item;
591 }
592
593 foreach ($this->_reserved_identifiers as $id)
594 {
595 if (strpos($item, '.'.$id) !== FALSE)
596 {
Barry Mienydd671972010-10-04 16:33:58 +0200597 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // remove duplicates if the user already included the escape
600 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200601 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 if (strpos($item, '.') !== FALSE)
605 {
Barry Mienydd671972010-10-04 16:33:58 +0200606 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
608 else
609 {
610 $str = $this->_escape_char.$item.$this->_escape_char;
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // remove duplicates if the user already included the escape
614 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 // --------------------------------------------------------------------
618
619 /**
620 * From Tables
621 *
622 * This function implicitly groups FROM tables so there is no confusion
623 * about operator precedence in harmony with SQL standards
624 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300625 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * @param type
627 * @return type
628 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300629 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
631 if ( ! is_array($tables))
632 {
633 $tables = array($tables);
634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 return implode(', ', $tables);
637 }
638
639 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 /**
642 * Insert statement
643 *
644 * Generates a platform-specific insert string from the supplied data
645 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500646 * @access public
647 * @param string the table name
648 * @param array the insert keys
649 * @param array the insert values
650 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300652 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300654 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 }
656
657 // --------------------------------------------------------------------
658
659 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300660 * Insert_batch statement
661 *
662 * Generates a platform-specific insert string from the supplied data
663 *
Greg Aker03abee32011-12-25 00:31:29 -0600664 * @param string the table name
665 * @param array the insert keys
666 * @param array the insert values
667 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300668 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300669 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300670 {
671 $keys = implode(', ', $keys);
672 $sql = "INSERT ALL\n";
673
674 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300675 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300676 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300677 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300678
679 $sql .= 'SELECT * FROM dual';
680
681 return $sql;
682 }
683
684 // --------------------------------------------------------------------
685
686 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 * Update statement
688 *
689 * Generates a platform-specific update string from the supplied data
690 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300691 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 * @param string the table name
693 * @param array the update data
694 * @param array the where clause
695 * @param array the orderby clause
696 * @param array the limit clause
697 * @return string
698 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300699 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500701 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
703 $valstr[] = $key." = ".$val;
704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
711
712 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
713
714 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 return $sql;
717 }
718
719 // --------------------------------------------------------------------
720
721 /**
722 * Truncate statement
723 *
724 * Generates a platform-specific truncate string from the supplied data
725 * If the database does not support the truncate() command
726 * This function maps to "DELETE FROM table"
727 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300728 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 * @param string the table name
730 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200731 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300732 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 {
734 return "TRUNCATE TABLE ".$table;
735 }
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 // --------------------------------------------------------------------
738
739 /**
740 * Delete statement
741 *
742 * Generates a platform-specific delete string from the supplied data
743 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300744 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 * @param string the table name
746 * @param array the where clause
747 * @param string the limit clause
748 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200749 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300750 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 {
752 $conditions = '';
753
754 if (count($where) > 0 OR count($like) > 0)
755 {
756 $conditions = "\nWHERE ";
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000757 $conditions .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000758
759 if (count($where) > 0 && count($like) > 0)
760 {
761 $conditions .= " AND ";
762 }
763 $conditions .= implode("\n", $like);
764 }
765
766 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 return "DELETE FROM ".$table.$conditions.$limit;
769 }
770
771 // --------------------------------------------------------------------
772
773 /**
774 * Limit string
775 *
776 * Generates a platform-specific LIMIT clause
777 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300778 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500779 * @param string the sql query string
780 * @param integer the number of rows to limit the query to
781 * @param integer the offset value
782 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300784 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 {
786 $limit = $offset + $limit;
787 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
788
789 if ($offset != 0)
790 {
791 $newsql .= " WHERE rnum >= $offset";
792 }
793
794 // remember that we used limits
795 $this->limit_used = TRUE;
796
797 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200798 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000799
800 // --------------------------------------------------------------------
801
802 /**
803 * Close DB Connection
804 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300805 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500806 * @param resource
807 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300809 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300811 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
813
814
815}
816
817
818
819/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300820/* Location: ./system/database/drivers/oci8/oci8_driver.php */