blob: 8d70406182b946d7f5d70252a66fa5738d58fb31 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Barry Mienydd671972010-10-04 16:33:58 +020019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
Barry Mienydd671972010-10-04 16:33:58 +020024 * @since Version 1.0
Derek Allard2067d1a2008-11-13 22:59:24 +000025 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * oci8 Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
Barry Mienydd671972010-10-04 16:33:58 +020037 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050038 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43
44/**
45 * oci8 Database Adapter Class
46 *
47 * This is a modification of the DB_driver class to
48 * permit access to oracle databases
49 *
Derek Jones4b9c6292011-07-01 17:40:48 -050050 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000051 *
52 */
53
54class CI_DB_oci8_driver extends CI_DB {
55
56 var $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 // The character used for excaping
59 var $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Jonese4ed5832009-02-20 21:44:59 +000061 // clause and character used for LIKE escape sequences
62 var $_like_escape_str = " escape '%s' ";
63 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 /**
66 * The syntax to count rows is slightly different across different
67 * database engines, so this string appears in each driver and is
68 * used for the count_all() and count_all_results() functions.
69 */
70 var $_count_string = "SELECT COUNT(1) AS ";
71 var $_random_keyword = ' ASC'; // not currently supported
72
73 // Set "auto commit" by default
74 var $_commit = OCI_COMMIT_ON_SUCCESS;
75
76 // need to track statement id and cursor id
77 var $stmt_id;
78 var $curs_id;
79
80 // if we use a limit, we will add a field that will
81 // throw off num_fields later
82 var $limit_used;
83
84 /**
85 * Non-persistent database connection
86 *
Derek Jones4b9c6292011-07-01 17:40:48 -050087 * @access private called by the base class
88 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000089 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030090 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030092 return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Persistent database connection
99 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500100 * @access private called by the base class
101 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300103 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300105 return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
107
108 // --------------------------------------------------------------------
109
110 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000111 * Reconnect
112 *
113 * Keep / reestablish the db connection if no queries have been
114 * sent for a length of time exceeding the server's idle timeout
115 *
116 * @access public
117 * @return void
118 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300119 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000120 {
121 // not implemented in oracle
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300122 return;
Derek Jones87cbafc2009-02-27 16:29:59 +0000123 }
124
125 // --------------------------------------------------------------------
126
127 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * Select the database
129 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500130 * @access private called by the base class
131 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300133 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300135 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 return TRUE;
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200142 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200144 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200146 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200148 return isset($this->data_cache['version'])
149 ? $this->data_cache['version']
150 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Execute the query
157 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300158 * @access protected called by the base class
Derek Jones4b9c6292011-07-01 17:40:48 -0500159 * @param string an SQL query
160 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300162 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 // oracle must parse the query before it is run. All of the actions with
165 // the query are based on the statement id returned by ociparse
166 $this->stmt_id = FALSE;
167 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300168 oci_set_prefetch($this->stmt_id, 1000);
169 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 /**
173 * Generate a statement ID
174 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500175 * @access private
176 * @param string an SQL query
177 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300179 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 if ( ! is_resource($this->stmt_id))
182 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400183 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185 }
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // --------------------------------------------------------------------
188
189 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500190 * getCursor. Returns a cursor from the datbase
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500192 * @access public
193 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300195 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300197 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 return $this->curs_id;
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500204 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500206 * @access public
207 * @param package package stored procedure is in
208 * @param procedure stored procedure to execute
209 * @param params array of parameters
210 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 *
212 * params array keys
213 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500214 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500216 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * this should be a reference to a variable
218 * type yes the type of the parameter
219 * length yes the max size of the parameter
220 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300221 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 if ($package == '' OR $procedure == '' OR ! is_array($params))
224 {
225 if ($this->db_debug)
226 {
227 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000228 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230 return FALSE;
231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // build the query string
234 $sql = "begin $package.$procedure(";
235
236 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500237 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200240
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300241 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 $have_cursor = TRUE;
244 }
245 }
246 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 $this->stmt_id = FALSE;
249 $this->_set_stmt_id($sql);
250 $this->_bind_params($params);
251 $this->query($sql, FALSE, $have_cursor);
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // --------------------------------------------------------------------
255
256 /**
257 * Bind parameters
258 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500259 * @access private
260 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300262 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
265 {
266 return;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 foreach ($params as $param)
270 {
Barry Mienydd671972010-10-04 16:33:58 +0200271 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 if ( ! isset($param[$val]))
274 {
275 $param[$val] = '';
276 }
277 }
278
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300279 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * Begin Transaction
287 *
288 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200289 * @return bool
290 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300291 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
293 if ( ! $this->trans_enabled)
294 {
295 return TRUE;
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // When transactions are nested we only begin/commit/rollback the outermost ones
299 if ($this->_trans_depth > 0)
300 {
301 return TRUE;
302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // Reset the transaction failure flag.
305 // If the $test_mode flag is set to TRUE transactions will be rolled back
306 // even if the queries produce a successful result.
307 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 $this->_commit = OCI_DEFAULT;
310 return TRUE;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Commit Transaction
317 *
318 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200319 * @return bool
320 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300321 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 if ( ! $this->trans_enabled)
324 {
325 return TRUE;
326 }
327
328 // When transactions are nested we only begin/commit/rollback the outermost ones
329 if ($this->_trans_depth > 0)
330 {
331 return TRUE;
332 }
333
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300334 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 $this->_commit = OCI_COMMIT_ON_SUCCESS;
336 return $ret;
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Rollback Transaction
343 *
344 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200345 * @return bool
346 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300347 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 if ( ! $this->trans_enabled)
350 {
351 return TRUE;
352 }
353
354 // When transactions are nested we only begin/commit/rollback the outermost ones
355 if ($this->_trans_depth > 0)
356 {
357 return TRUE;
358 }
359
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300360 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 $this->_commit = OCI_COMMIT_ON_SUCCESS;
362 return $ret;
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * Escape String
369 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200370 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000371 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200372 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300374 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000376 if (is_array($str))
377 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500378 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200379 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000380 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200381 }
382
383 return $str;
384 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000385
Andrey Andreevc2905f52012-03-01 14:39:26 +0200386 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Jonese4ed5832009-02-20 21:44:59 +0000388 // escape LIKE condition wildcards
389 if ($like === TRUE)
390 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200391 return str_replace(array($this->_like_escape_chr, '%', '_'),
392 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
393 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Jonese4ed5832009-02-20 21:44:59 +0000396 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Affected Rows
403 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500404 * @access public
405 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300407 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300409 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
411
412 // --------------------------------------------------------------------
413
414 /**
415 * Insert ID
416 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500417 * @access public
418 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300420 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000423 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
425
426 // --------------------------------------------------------------------
427
428 /**
429 * "Count All" query
430 *
431 * Generates a platform-specific query string that counts all records in
432 * the specified database
433 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500434 * @access public
435 * @param string
436 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300438 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
440 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000441 {
442 return 0;
443 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000444
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200445 $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 +0000446 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000447 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000449 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000450
451 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500452 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000453 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Show table query
460 *
461 * Generates a platform-specific query string so that the table names can be fetched
462 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300463 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @param boolean
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300465 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300467 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
470
471 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
472 {
Greg Aker0d424892010-01-26 02:14:44 +0000473 $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 +0000474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 return $sql;
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
482 * Show column query
483 *
484 * Generates a platform-specific query string so that the column names can be fetched
485 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300486 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500487 * @param string the table name
488 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300490 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Field data query
499 *
500 * Generates a platform-specific query so that the column data can be retrieved
501 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500502 * @access public
503 * @param string the table name
504 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300506 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 return "SELECT * FROM ".$table." where rownum = 1";
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200514 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200516 * Returns an array containing code and message of the last
517 * database error that has occured.
Andrey Andreev601f8b22012-03-01 20:11:15 +0200518 *
519 * @return array
520 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200521 public function error()
Andrey Andreev601f8b22012-03-01 20:11:15 +0200522 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200523 /* oci_error() returns an array that already contains the
524 * 'code' and 'message' keys, so we can just return it.
525 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200526 if (is_resource($this->curs_id))
527 {
528 return oci_error($this->curs_id);
529 }
530 elseif (is_resource($this->stmt_id))
531 {
532 return oci_error($this->stmt_id);
533 }
534 elseif (is_resource($this->conn_id))
535 {
536 return oci_error($this->conn_id);
537 }
538
539 return oci_error();
540 }
541
542 // --------------------------------------------------------------------
543
544 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * Escape the SQL Identifiers
546 *
547 * This function escapes column and table names
548 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300549 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @param string
551 * @return string
552 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300553 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
555 if ($this->_escape_char == '')
556 {
557 return $item;
558 }
559
560 foreach ($this->_reserved_identifiers as $id)
561 {
562 if (strpos($item, '.'.$id) !== FALSE)
563 {
Barry Mienydd671972010-10-04 16:33:58 +0200564 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 // remove duplicates if the user already included the escape
567 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200568 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 if (strpos($item, '.') !== FALSE)
572 {
Barry Mienydd671972010-10-04 16:33:58 +0200573 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
575 else
576 {
577 $str = $this->_escape_char.$item.$this->_escape_char;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // remove duplicates if the user already included the escape
581 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // --------------------------------------------------------------------
585
586 /**
587 * From Tables
588 *
589 * This function implicitly groups FROM tables so there is no confusion
590 * about operator precedence in harmony with SQL standards
591 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300592 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 * @param type
594 * @return type
595 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300596 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 if ( ! is_array($tables))
599 {
600 $tables = array($tables);
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 return implode(', ', $tables);
604 }
605
606 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200607
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 /**
609 * Insert statement
610 *
611 * Generates a platform-specific insert string from the supplied data
612 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500613 * @access public
614 * @param string the table name
615 * @param array the insert keys
616 * @param array the insert values
617 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300619 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300621 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 }
623
624 // --------------------------------------------------------------------
625
626 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300627 * Insert_batch statement
628 *
629 * Generates a platform-specific insert string from the supplied data
630 *
Greg Aker03abee32011-12-25 00:31:29 -0600631 * @param string the table name
632 * @param array the insert keys
633 * @param array the insert values
634 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300635 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300636 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300637 {
638 $keys = implode(', ', $keys);
639 $sql = "INSERT ALL\n";
640
641 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300642 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300643 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300644 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300645
646 $sql .= 'SELECT * FROM dual';
647
648 return $sql;
649 }
650
651 // --------------------------------------------------------------------
652
653 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * Update statement
655 *
656 * Generates a platform-specific update string from the supplied data
657 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300658 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 * @param string the table name
660 * @param array the update data
661 * @param array the where clause
662 * @param array the orderby clause
663 * @param array the limit clause
664 * @return string
665 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300666 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500668 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
670 $valstr[] = $key." = ".$val;
671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
678
679 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
680
681 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 return $sql;
684 }
685
686 // --------------------------------------------------------------------
687
688 /**
689 * Truncate statement
690 *
691 * Generates a platform-specific truncate string from the supplied data
692 * If the database does not support the truncate() command
693 * This function maps to "DELETE FROM table"
694 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300695 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 * @param string the table name
697 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200698 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300699 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
701 return "TRUNCATE TABLE ".$table;
702 }
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 // --------------------------------------------------------------------
705
706 /**
707 * Delete statement
708 *
709 * Generates a platform-specific delete string from the supplied data
710 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300711 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string the table name
713 * @param array the where clause
714 * @param string the limit clause
715 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200716 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300717 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
719 $conditions = '';
720
721 if (count($where) > 0 OR count($like) > 0)
722 {
723 $conditions = "\nWHERE ";
724 $conditions .= implode("\n", $this->ar_where);
725
726 if (count($where) > 0 && count($like) > 0)
727 {
728 $conditions .= " AND ";
729 }
730 $conditions .= implode("\n", $like);
731 }
732
733 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200734
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 return "DELETE FROM ".$table.$conditions.$limit;
736 }
737
738 // --------------------------------------------------------------------
739
740 /**
741 * Limit string
742 *
743 * Generates a platform-specific LIMIT clause
744 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300745 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500746 * @param string the sql query string
747 * @param integer the number of rows to limit the query to
748 * @param integer the offset value
749 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300751 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
753 $limit = $offset + $limit;
754 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
755
756 if ($offset != 0)
757 {
758 $newsql .= " WHERE rnum >= $offset";
759 }
760
761 // remember that we used limits
762 $this->limit_used = TRUE;
763
764 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200765 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000766
767 // --------------------------------------------------------------------
768
769 /**
770 * Close DB Connection
771 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300772 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500773 * @param resource
774 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300776 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300778 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 }
780
781
782}
783
784
785
786/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300787/* Location: ./system/database/drivers/oci8/oci8_driver.php */