blob: 292ccd0fdade83fbf872b19f070bd1336eefae75 [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 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * Version number query string
143 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300144 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500145 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300147 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300149 return oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Execute the query
156 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300157 * @access protected called by the base class
Derek Jones4b9c6292011-07-01 17:40:48 -0500158 * @param string an SQL query
159 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300161 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 // oracle must parse the query before it is run. All of the actions with
164 // the query are based on the statement id returned by ociparse
165 $this->stmt_id = FALSE;
166 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300167 oci_set_prefetch($this->stmt_id, 1000);
168 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171 /**
172 * Generate a statement ID
173 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500174 * @access private
175 * @param string an SQL query
176 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300178 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 if ( ! is_resource($this->stmt_id))
181 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300182 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Prep the query
190 *
191 * If needed, each database adapter can prep the query string
192 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500193 * @access private called by execute()
194 * @param string an SQL query
195 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300197 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
199 return $sql;
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500205 * getCursor. Returns a cursor from the datbase
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500207 * @access public
208 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300210 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300212 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 return $this->curs_id;
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500219 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500221 * @access public
222 * @param package package stored procedure is in
223 * @param procedure stored procedure to execute
224 * @param params array of parameters
225 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 *
227 * params array keys
228 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500229 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500231 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * this should be a reference to a variable
233 * type yes the type of the parameter
234 * length yes the max size of the parameter
235 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300236 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
238 if ($package == '' OR $procedure == '' OR ! is_array($params))
239 {
240 if ($this->db_debug)
241 {
242 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000243 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
245 return FALSE;
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // build the query string
249 $sql = "begin $package.$procedure(";
250
251 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500252 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200255
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300256 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 $have_cursor = TRUE;
259 }
260 }
261 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 $this->stmt_id = FALSE;
264 $this->_set_stmt_id($sql);
265 $this->_bind_params($params);
266 $this->query($sql, FALSE, $have_cursor);
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
270
271 /**
272 * Bind parameters
273 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500274 * @access private
275 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300277 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
280 {
281 return;
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 foreach ($params as $param)
285 {
Barry Mienydd671972010-10-04 16:33:58 +0200286 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
288 if ( ! isset($param[$val]))
289 {
290 $param[$val] = '';
291 }
292 }
293
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300294 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Begin Transaction
302 *
303 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200304 * @return bool
305 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300306 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
308 if ( ! $this->trans_enabled)
309 {
310 return TRUE;
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // When transactions are nested we only begin/commit/rollback the outermost ones
314 if ($this->_trans_depth > 0)
315 {
316 return TRUE;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Reset the transaction failure flag.
320 // If the $test_mode flag is set to TRUE transactions will be rolled back
321 // even if the queries produce a successful result.
322 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 $this->_commit = OCI_DEFAULT;
325 return TRUE;
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Commit Transaction
332 *
333 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200334 * @return bool
335 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300336 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 if ( ! $this->trans_enabled)
339 {
340 return TRUE;
341 }
342
343 // When transactions are nested we only begin/commit/rollback the outermost ones
344 if ($this->_trans_depth > 0)
345 {
346 return TRUE;
347 }
348
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300349 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 $this->_commit = OCI_COMMIT_ON_SUCCESS;
351 return $ret;
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * Rollback Transaction
358 *
359 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200360 * @return bool
361 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300362 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 if ( ! $this->trans_enabled)
365 {
366 return TRUE;
367 }
368
369 // When transactions are nested we only begin/commit/rollback the outermost ones
370 if ($this->_trans_depth > 0)
371 {
372 return TRUE;
373 }
374
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300375 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 $this->_commit = OCI_COMMIT_ON_SUCCESS;
377 return $ret;
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Escape String
384 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500385 * @access public
386 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000387 * @param bool whether or not the string will be used in a LIKE condition
Derek Jones4b9c6292011-07-01 17:40:48 -0500388 * @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
Greg Aker757dda62010-04-14 19:06:19 -0500402 $str = remove_invisible_characters($str);
Michiel Vugteveeneaa55412011-08-25 21:22:49 +0200403 $str = str_replace("'", "''", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Jonese4ed5832009-02-20 21:44:59 +0000405 // escape LIKE condition wildcards
406 if ($like === TRUE)
407 {
408 $str = str_replace( array('%', '_', $this->_like_escape_chr),
409 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
410 $str);
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Jonese4ed5832009-02-20 21:44:59 +0000413 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Affected Rows
420 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500421 * @access public
422 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300424 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300426 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Insert ID
433 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500434 * @access public
435 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300437 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000440 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * "Count All" query
447 *
448 * Generates a platform-specific query string that counts all records in
449 * the specified database
450 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500451 * @access public
452 * @param string
453 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300455 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000458 {
459 return 0;
460 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000461
Derek Allarde37ab382009-02-03 16:13:57 +0000462 $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 +0000463
464 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000465 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000467 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000468
469 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500470 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000471 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Show table query
478 *
479 * Generates a platform-specific query string so that the table names can be fetched
480 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300481 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @param boolean
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300483 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300485 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
488
489 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
490 {
Greg Aker0d424892010-01-26 02:14:44 +0000491 $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 +0000492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 return $sql;
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Show column query
501 *
502 * Generates a platform-specific query string so that the column names can be fetched
503 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300504 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500505 * @param string the table name
506 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300508 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Field data query
517 *
518 * Generates a platform-specific query so that the column data can be retrieved
519 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500520 * @access public
521 * @param string the table name
522 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300524 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 return "SELECT * FROM ".$table." where rownum = 1";
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * The error message string
533 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300534 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500535 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300537 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300539 // If the error was during connection, no conn_id should be passed
540 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 return $error['message'];
542 }
543
544 // --------------------------------------------------------------------
545
546 /**
547 * The error message number
548 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300549 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500550 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300552 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300554 // Same as _error_message()
555 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 return $error['code'];
557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
560
561 /**
562 * Escape the SQL Identifiers
563 *
564 * This function escapes column and table names
565 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300566 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * @param string
568 * @return string
569 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300570 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
572 if ($this->_escape_char == '')
573 {
574 return $item;
575 }
576
577 foreach ($this->_reserved_identifiers as $id)
578 {
579 if (strpos($item, '.'.$id) !== FALSE)
580 {
Barry Mienydd671972010-10-04 16:33:58 +0200581 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 // remove duplicates if the user already included the escape
584 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200585 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 }
Barry Mienydd671972010-10-04 16:33:58 +0200587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 if (strpos($item, '.') !== FALSE)
589 {
Barry Mienydd671972010-10-04 16:33:58 +0200590 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
592 else
593 {
594 $str = $this->_escape_char.$item.$this->_escape_char;
595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
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);
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 // --------------------------------------------------------------------
602
603 /**
604 * From Tables
605 *
606 * This function implicitly groups FROM tables so there is no confusion
607 * about operator precedence in harmony with SQL standards
608 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300609 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 * @param type
611 * @return type
612 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300613 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 if ( ! is_array($tables))
616 {
617 $tables = array($tables);
618 }
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 return implode(', ', $tables);
621 }
622
623 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 /**
626 * Insert statement
627 *
628 * Generates a platform-specific insert string from the supplied data
629 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500630 * @access public
631 * @param string the table name
632 * @param array the insert keys
633 * @param array the insert values
634 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300636 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300638 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
640
641 // --------------------------------------------------------------------
642
643 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300644 * Insert_batch statement
645 *
646 * Generates a platform-specific insert string from the supplied data
647 *
Greg Aker03abee32011-12-25 00:31:29 -0600648 * @param string the table name
649 * @param array the insert keys
650 * @param array the insert values
651 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300652 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300653 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300654 {
655 $keys = implode(', ', $keys);
656 $sql = "INSERT ALL\n";
657
658 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300659 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300660 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300661 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300662
663 $sql .= 'SELECT * FROM dual';
664
665 return $sql;
666 }
667
668 // --------------------------------------------------------------------
669
670 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * Update statement
672 *
673 * Generates a platform-specific update string from the supplied data
674 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300675 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 * @param string the table name
677 * @param array the update data
678 * @param array the where clause
679 * @param array the orderby clause
680 * @param array the limit clause
681 * @return string
682 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300683 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500685 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
687 $valstr[] = $key." = ".$val;
688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
695
696 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
697
698 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 return $sql;
701 }
702
703 // --------------------------------------------------------------------
704
705 /**
706 * Truncate statement
707 *
708 * Generates a platform-specific truncate string from the supplied data
709 * If the database does not support the truncate() command
710 * This function maps to "DELETE FROM table"
711 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300712 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 * @param string the table name
714 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200715 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300716 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 {
718 return "TRUNCATE TABLE ".$table;
719 }
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 // --------------------------------------------------------------------
722
723 /**
724 * Delete statement
725 *
726 * Generates a platform-specific delete string from the supplied data
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 * @param array the where clause
731 * @param string the limit clause
732 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200733 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300734 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 {
736 $conditions = '';
737
738 if (count($where) > 0 OR count($like) > 0)
739 {
740 $conditions = "\nWHERE ";
741 $conditions .= implode("\n", $this->ar_where);
742
743 if (count($where) > 0 && count($like) > 0)
744 {
745 $conditions .= " AND ";
746 }
747 $conditions .= implode("\n", $like);
748 }
749
750 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200751
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 return "DELETE FROM ".$table.$conditions.$limit;
753 }
754
755 // --------------------------------------------------------------------
756
757 /**
758 * Limit string
759 *
760 * Generates a platform-specific LIMIT clause
761 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300762 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500763 * @param string the sql query string
764 * @param integer the number of rows to limit the query to
765 * @param integer the offset value
766 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300768 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 {
770 $limit = $offset + $limit;
771 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
772
773 if ($offset != 0)
774 {
775 $newsql .= " WHERE rnum >= $offset";
776 }
777
778 // remember that we used limits
779 $this->limit_used = TRUE;
780
781 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200782 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000783
784 // --------------------------------------------------------------------
785
786 /**
787 * Close DB Connection
788 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300789 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500790 * @param resource
791 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300793 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300795 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 }
797
798
799}
800
801
802
803/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300804/* Location: ./system/database/drivers/oci8/oci8_driver.php */