blob: cc7af95052df83824d3c231e8948e3e131d2e24f [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 *
Barry Mienydd671972010-10-04 16:33:58 +02007 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones4b9c6292011-07-01 17:40:48 -05009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Barry Mienydd671972010-10-04 16:33:58 +020010 * @license http://codeigniter.com/user_guide/license.html
Derek Allard2067d1a2008-11-13 22:59:24 +000011 * @link http://codeigniter.com
Barry Mienydd671972010-10-04 16:33:58 +020012 * @since Version 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000013 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * oci8 Database Adapter Class
20 *
21 * Note: _DB is an extender class that the app controller
22 * creates dynamically based on whether the active record
23 * class is being used or not.
24 *
Barry Mienydd671972010-10-04 16:33:58 +020025 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050026 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000027 * @category Database
Barry Mienydd671972010-10-04 16:33:58 +020028 * @author ExpressionEngine Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000029 * @link http://codeigniter.com/user_guide/database/
30 */
31
32/**
33 * oci8 Database Adapter Class
34 *
35 * This is a modification of the DB_driver class to
36 * permit access to oracle databases
37 *
Derek Jones4b9c6292011-07-01 17:40:48 -050038 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000039 *
40 */
41
42class CI_DB_oci8_driver extends CI_DB {
43
44 var $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 // The character used for excaping
47 var $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Jonese4ed5832009-02-20 21:44:59 +000049 // clause and character used for LIKE escape sequences
50 var $_like_escape_str = " escape '%s' ";
51 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 /**
54 * The syntax to count rows is slightly different across different
55 * database engines, so this string appears in each driver and is
56 * used for the count_all() and count_all_results() functions.
57 */
58 var $_count_string = "SELECT COUNT(1) AS ";
59 var $_random_keyword = ' ASC'; // not currently supported
60
61 // Set "auto commit" by default
62 var $_commit = OCI_COMMIT_ON_SUCCESS;
63
64 // need to track statement id and cursor id
65 var $stmt_id;
66 var $curs_id;
67
68 // if we use a limit, we will add a field that will
69 // throw off num_fields later
70 var $limit_used;
71
72 /**
73 * Non-persistent database connection
74 *
Derek Jones4b9c6292011-07-01 17:40:48 -050075 * @access private called by the base class
76 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000077 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030078 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030080 return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
82
83 // --------------------------------------------------------------------
84
85 /**
86 * Persistent database connection
87 *
Derek Jones4b9c6292011-07-01 17:40:48 -050088 * @access private called by the base class
89 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000090 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030091 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030093 return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
95
96 // --------------------------------------------------------------------
97
98 /**
Derek Jones87cbafc2009-02-27 16:29:59 +000099 * Reconnect
100 *
101 * Keep / reestablish the db connection if no queries have been
102 * sent for a length of time exceeding the server's idle timeout
103 *
104 * @access public
105 * @return void
106 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300107 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000108 {
109 // not implemented in oracle
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300110 return;
Derek Jones87cbafc2009-02-27 16:29:59 +0000111 }
112
113 // --------------------------------------------------------------------
114
115 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 * Select the database
117 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500118 * @access private called by the base class
119 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300121 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300123 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 return TRUE;
125 }
126
127 // --------------------------------------------------------------------
128
129 /**
130 * Set client character set
131 *
132 * @access public
133 * @param string
134 * @param string
135 * @return resource
136 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300137 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
narfbg068e3de2011-09-17 21:38:46 +0300139 // this is done upon connect
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 return TRUE;
141 }
142
143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 /**
146 * Version number query string
147 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300148 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500149 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300151 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300153 return oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Execute the query
160 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300161 * @access protected called by the base class
Derek Jones4b9c6292011-07-01 17:40:48 -0500162 * @param string an SQL query
163 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300165 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
167 // oracle must parse the query before it is run. All of the actions with
168 // the query are based on the statement id returned by ociparse
169 $this->stmt_id = FALSE;
170 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300171 oci_set_prefetch($this->stmt_id, 1000);
172 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174
175 /**
176 * Generate a statement ID
177 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500178 * @access private
179 * @param string an SQL query
180 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300182 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 if ( ! is_resource($this->stmt_id))
185 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300186 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Prep the query
194 *
195 * If needed, each database adapter can prep the query string
196 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500197 * @access private called by execute()
198 * @param string an SQL query
199 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300201 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 return $sql;
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500209 * getCursor. Returns a cursor from the datbase
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500211 * @access public
212 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300214 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300216 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 return $this->curs_id;
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500223 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500225 * @access public
226 * @param package package stored procedure is in
227 * @param procedure stored procedure to execute
228 * @param params array of parameters
229 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 *
231 * params array keys
232 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500233 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500235 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * this should be a reference to a variable
237 * type yes the type of the parameter
238 * length yes the max size of the parameter
239 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300240 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 if ($package == '' OR $procedure == '' OR ! is_array($params))
243 {
244 if ($this->db_debug)
245 {
246 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000247 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249 return FALSE;
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // build the query string
253 $sql = "begin $package.$procedure(";
254
255 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500256 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200259
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300260 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
262 $have_cursor = TRUE;
263 }
264 }
265 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $this->stmt_id = FALSE;
268 $this->_set_stmt_id($sql);
269 $this->_bind_params($params);
270 $this->query($sql, FALSE, $have_cursor);
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // --------------------------------------------------------------------
274
275 /**
276 * Bind parameters
277 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500278 * @access private
279 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300281 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
284 {
285 return;
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 foreach ($params as $param)
289 {
Barry Mienydd671972010-10-04 16:33:58 +0200290 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 if ( ! isset($param[$val]))
293 {
294 $param[$val] = '';
295 }
296 }
297
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300298 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Begin Transaction
306 *
307 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200308 * @return bool
309 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300310 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
312 if ( ! $this->trans_enabled)
313 {
314 return TRUE;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // When transactions are nested we only begin/commit/rollback the outermost ones
318 if ($this->_trans_depth > 0)
319 {
320 return TRUE;
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Reset the transaction failure flag.
324 // If the $test_mode flag is set to TRUE transactions will be rolled back
325 // even if the queries produce a successful result.
326 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 $this->_commit = OCI_DEFAULT;
329 return TRUE;
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Commit Transaction
336 *
337 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200338 * @return bool
339 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300340 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 if ( ! $this->trans_enabled)
343 {
344 return TRUE;
345 }
346
347 // When transactions are nested we only begin/commit/rollback the outermost ones
348 if ($this->_trans_depth > 0)
349 {
350 return TRUE;
351 }
352
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300353 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 $this->_commit = OCI_COMMIT_ON_SUCCESS;
355 return $ret;
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Rollback Transaction
362 *
363 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200364 * @return bool
365 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300366 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 if ( ! $this->trans_enabled)
369 {
370 return TRUE;
371 }
372
373 // When transactions are nested we only begin/commit/rollback the outermost ones
374 if ($this->_trans_depth > 0)
375 {
376 return TRUE;
377 }
378
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300379 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 $this->_commit = OCI_COMMIT_ON_SUCCESS;
381 return $ret;
382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Escape String
388 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500389 * @access public
390 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000391 * @param bool whether or not the string will be used in a LIKE condition
Derek Jones4b9c6292011-07-01 17:40:48 -0500392 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300394 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000396 if (is_array($str))
397 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500398 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200399 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000400 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200401 }
402
403 return $str;
404 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000405
Greg Aker757dda62010-04-14 19:06:19 -0500406 $str = remove_invisible_characters($str);
Michiel Vugteveeneaa55412011-08-25 21:22:49 +0200407 $str = str_replace("'", "''", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Jonese4ed5832009-02-20 21:44:59 +0000409 // escape LIKE condition wildcards
410 if ($like === TRUE)
411 {
412 $str = str_replace( array('%', '_', $this->_like_escape_chr),
413 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
414 $str);
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Jonese4ed5832009-02-20 21:44:59 +0000417 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
419
420 // --------------------------------------------------------------------
421
422 /**
423 * Affected Rows
424 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500425 * @access public
426 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300428 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300430 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Insert ID
437 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500438 * @access public
439 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300441 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000444 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
446
447 // --------------------------------------------------------------------
448
449 /**
450 * "Count All" query
451 *
452 * Generates a platform-specific query string that counts all records in
453 * the specified database
454 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500455 * @access public
456 * @param string
457 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300459 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
461 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000462 {
463 return 0;
464 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000465
Derek Allarde37ab382009-02-03 16:13:57 +0000466 $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 +0000467
468 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000469 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000471 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000472
473 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500474 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000475 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Show table query
482 *
483 * Generates a platform-specific query string so that the table names can be fetched
484 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300485 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @param boolean
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300487 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300489 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
492
493 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
494 {
Greg Aker0d424892010-01-26 02:14:44 +0000495 $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 +0000496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 return $sql;
499 }
500
501 // --------------------------------------------------------------------
502
503 /**
504 * Show column query
505 *
506 * Generates a platform-specific query string so that the column names can be fetched
507 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300508 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500509 * @param string the table name
510 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300512 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
520 * Field data query
521 *
522 * Generates a platform-specific query so that the column data can be retrieved
523 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500524 * @access public
525 * @param string the table name
526 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300528 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
530 return "SELECT * FROM ".$table." where rownum = 1";
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * The error message string
537 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300538 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500539 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300541 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300543 // If the error was during connection, no conn_id should be passed
544 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 return $error['message'];
546 }
547
548 // --------------------------------------------------------------------
549
550 /**
551 * The error message number
552 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300553 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500554 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300556 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300558 // Same as _error_message()
559 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 return $error['code'];
561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 // --------------------------------------------------------------------
564
565 /**
566 * Escape the SQL Identifiers
567 *
568 * This function escapes column and table names
569 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300570 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @param string
572 * @return string
573 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300574 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
576 if ($this->_escape_char == '')
577 {
578 return $item;
579 }
580
581 foreach ($this->_reserved_identifiers as $id)
582 {
583 if (strpos($item, '.'.$id) !== FALSE)
584 {
Barry Mienydd671972010-10-04 16:33:58 +0200585 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // remove duplicates if the user already included the escape
588 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200589 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 if (strpos($item, '.') !== FALSE)
593 {
Barry Mienydd671972010-10-04 16:33:58 +0200594 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 }
596 else
597 {
598 $str = $this->_escape_char.$item.$this->_escape_char;
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 // remove duplicates if the user already included the escape
602 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
603 }
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 // --------------------------------------------------------------------
606
607 /**
608 * From Tables
609 *
610 * This function implicitly groups FROM tables so there is no confusion
611 * about operator precedence in harmony with SQL standards
612 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300613 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @param type
615 * @return type
616 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300617 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
619 if ( ! is_array($tables))
620 {
621 $tables = array($tables);
622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 return implode(', ', $tables);
625 }
626
627 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 /**
630 * Insert statement
631 *
632 * Generates a platform-specific insert string from the supplied data
633 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500634 * @access public
635 * @param string the table name
636 * @param array the insert keys
637 * @param array the insert values
638 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300640 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300642 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 }
644
645 // --------------------------------------------------------------------
646
647 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300648 * Insert_batch statement
649 *
650 * Generates a platform-specific insert string from the supplied data
651 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300652 * @access protected
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300653 * @param string the table name
654 * @param array the insert keys
655 * @param array the insert values
656 * @return string
657 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300658 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300659 {
660 $keys = implode(', ', $keys);
661 $sql = "INSERT ALL\n";
662
663 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300664 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300665 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300666 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300667
668 $sql .= 'SELECT * FROM dual';
669
670 return $sql;
671 }
672
673 // --------------------------------------------------------------------
674
675 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 * Update statement
677 *
678 * Generates a platform-specific update string from the supplied data
679 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300680 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 * @param string the table name
682 * @param array the update data
683 * @param array the where clause
684 * @param array the orderby clause
685 * @param array the limit clause
686 * @return string
687 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300688 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500690 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 $valstr[] = $key." = ".$val;
693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
700
701 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
702
703 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 return $sql;
706 }
707
708 // --------------------------------------------------------------------
709
710 /**
711 * Truncate statement
712 *
713 * Generates a platform-specific truncate string from the supplied data
714 * If the database does not support the truncate() command
715 * This function maps to "DELETE FROM table"
716 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300717 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 * @param string the table name
719 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200720 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300721 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
723 return "TRUNCATE TABLE ".$table;
724 }
Barry Mienydd671972010-10-04 16:33:58 +0200725
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 // --------------------------------------------------------------------
727
728 /**
729 * Delete statement
730 *
731 * Generates a platform-specific delete string from the supplied data
732 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300733 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 * @param string the table name
735 * @param array the where clause
736 * @param string the limit clause
737 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200738 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300739 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
741 $conditions = '';
742
743 if (count($where) > 0 OR count($like) > 0)
744 {
745 $conditions = "\nWHERE ";
746 $conditions .= implode("\n", $this->ar_where);
747
748 if (count($where) > 0 && count($like) > 0)
749 {
750 $conditions .= " AND ";
751 }
752 $conditions .= implode("\n", $like);
753 }
754
755 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 return "DELETE FROM ".$table.$conditions.$limit;
758 }
759
760 // --------------------------------------------------------------------
761
762 /**
763 * Limit string
764 *
765 * Generates a platform-specific LIMIT clause
766 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300767 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500768 * @param string the sql query string
769 * @param integer the number of rows to limit the query to
770 * @param integer the offset value
771 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300773 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
775 $limit = $offset + $limit;
776 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
777
778 if ($offset != 0)
779 {
780 $newsql .= " WHERE rnum >= $offset";
781 }
782
783 // remember that we used limits
784 $this->limit_used = TRUE;
785
786 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200787 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000788
789 // --------------------------------------------------------------------
790
791 /**
792 * Close DB Connection
793 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300794 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500795 * @param resource
796 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300798 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300800 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 }
802
803
804}
805
806
807
808/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300809/* Location: ./system/database/drivers/oci8/oci8_driver.php */