blob: 97efb4647339b688bead50f2d6d9c37d19f57a34 [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 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200385 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000386 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200387 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300389 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000391 if (is_array($str))
392 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500393 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200394 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000395 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200396 }
397
398 return $str;
399 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000400
Andrey Andreevc2905f52012-03-01 14:39:26 +0200401 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Jonese4ed5832009-02-20 21:44:59 +0000403 // escape LIKE condition wildcards
404 if ($like === TRUE)
405 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200406 return str_replace(array($this->_like_escape_chr, '%', '_'),
407 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
408 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Jonese4ed5832009-02-20 21:44:59 +0000411 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
413
414 // --------------------------------------------------------------------
415
416 /**
417 * Affected Rows
418 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500419 * @access public
420 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300422 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300424 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Insert ID
431 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500432 * @access public
433 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300435 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
437 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000438 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * "Count All" query
445 *
446 * Generates a platform-specific query string that counts all records in
447 * the specified database
448 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500449 * @access public
450 * @param string
451 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300453 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
455 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000456 {
457 return 0;
458 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000459
Derek Allarde37ab382009-02-03 16:13:57 +0000460 $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 +0000461
462 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000463 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000465 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000466
467 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500468 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000469 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Show table query
476 *
477 * Generates a platform-specific query string so that the table names can be fetched
478 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300479 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @param boolean
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300481 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300483 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
485 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
486
487 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
488 {
Greg Aker0d424892010-01-26 02:14:44 +0000489 $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 +0000490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 return $sql;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Show column query
499 *
500 * Generates a platform-specific query string so that the column names can be fetched
501 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300502 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500503 * @param string the table name
504 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300506 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Field data query
515 *
516 * Generates a platform-specific query so that the column data can be retrieved
517 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500518 * @access public
519 * @param string the table name
520 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300522 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 return "SELECT * FROM ".$table." where rownum = 1";
525 }
526
527 // --------------------------------------------------------------------
528
529 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200530 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200532 * Returns an array containing code and message of the last
533 * database error that has occured.
Andrey Andreev601f8b22012-03-01 20:11:15 +0200534 *
535 * @return array
536 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200537 public function error()
Andrey Andreev601f8b22012-03-01 20:11:15 +0200538 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200539 /* oci_error() returns an array that already contains the
540 * 'code' and 'message' keys, so we can just return it.
541 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200542 if (is_resource($this->curs_id))
543 {
544 return oci_error($this->curs_id);
545 }
546 elseif (is_resource($this->stmt_id))
547 {
548 return oci_error($this->stmt_id);
549 }
550 elseif (is_resource($this->conn_id))
551 {
552 return oci_error($this->conn_id);
553 }
554
555 return oci_error();
556 }
557
558 // --------------------------------------------------------------------
559
560 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * Escape the SQL Identifiers
562 *
563 * This function escapes column and table names
564 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300565 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * @param string
567 * @return string
568 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300569 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
571 if ($this->_escape_char == '')
572 {
573 return $item;
574 }
575
576 foreach ($this->_reserved_identifiers as $id)
577 {
578 if (strpos($item, '.'.$id) !== FALSE)
579 {
Barry Mienydd671972010-10-04 16:33:58 +0200580 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // remove duplicates if the user already included the escape
583 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200584 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 if (strpos($item, '.') !== FALSE)
588 {
Barry Mienydd671972010-10-04 16:33:58 +0200589 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591 else
592 {
593 $str = $this->_escape_char.$item.$this->_escape_char;
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // remove duplicates if the user already included the escape
597 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // --------------------------------------------------------------------
601
602 /**
603 * From Tables
604 *
605 * This function implicitly groups FROM tables so there is no confusion
606 * about operator precedence in harmony with SQL standards
607 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300608 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @param type
610 * @return type
611 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300612 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
614 if ( ! is_array($tables))
615 {
616 $tables = array($tables);
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 return implode(', ', $tables);
620 }
621
622 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 /**
625 * Insert statement
626 *
627 * Generates a platform-specific insert string from the supplied data
628 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500629 * @access public
630 * @param string the table name
631 * @param array the insert keys
632 * @param array the insert values
633 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300635 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300637 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
639
640 // --------------------------------------------------------------------
641
642 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300643 * Insert_batch statement
644 *
645 * Generates a platform-specific insert string from the supplied data
646 *
Greg Aker03abee32011-12-25 00:31:29 -0600647 * @param string the table name
648 * @param array the insert keys
649 * @param array the insert values
650 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300651 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300652 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300653 {
654 $keys = implode(', ', $keys);
655 $sql = "INSERT ALL\n";
656
657 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300658 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300659 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300660 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300661
662 $sql .= 'SELECT * FROM dual';
663
664 return $sql;
665 }
666
667 // --------------------------------------------------------------------
668
669 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * Update statement
671 *
672 * Generates a platform-specific update string from the supplied data
673 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300674 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 * @param string the table name
676 * @param array the update data
677 * @param array the where clause
678 * @param array the orderby clause
679 * @param array the limit clause
680 * @return string
681 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300682 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500684 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
686 $valstr[] = $key." = ".$val;
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
694
695 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
696
697 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 return $sql;
700 }
701
702 // --------------------------------------------------------------------
703
704 /**
705 * Truncate statement
706 *
707 * Generates a platform-specific truncate string from the supplied data
708 * If the database does not support the truncate() command
709 * This function maps to "DELETE FROM table"
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 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200714 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300715 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
717 return "TRUNCATE TABLE ".$table;
718 }
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 // --------------------------------------------------------------------
721
722 /**
723 * Delete statement
724 *
725 * Generates a platform-specific delete string from the supplied data
726 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300727 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 * @param string the table name
729 * @param array the where clause
730 * @param string the limit clause
731 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200732 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300733 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
735 $conditions = '';
736
737 if (count($where) > 0 OR count($like) > 0)
738 {
739 $conditions = "\nWHERE ";
740 $conditions .= implode("\n", $this->ar_where);
741
742 if (count($where) > 0 && count($like) > 0)
743 {
744 $conditions .= " AND ";
745 }
746 $conditions .= implode("\n", $like);
747 }
748
749 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 return "DELETE FROM ".$table.$conditions.$limit;
752 }
753
754 // --------------------------------------------------------------------
755
756 /**
757 * Limit string
758 *
759 * Generates a platform-specific LIMIT clause
760 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300761 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500762 * @param string the sql query string
763 * @param integer the number of rows to limit the query to
764 * @param integer the offset value
765 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300767 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
769 $limit = $offset + $limit;
770 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
771
772 if ($offset != 0)
773 {
774 $newsql .= " WHERE rnum >= $offset";
775 }
776
777 // remember that we used limits
778 $this->limit_used = TRUE;
779
780 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200781 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000782
783 // --------------------------------------------------------------------
784
785 /**
786 * Close DB Connection
787 *
Andrey Andreevbc95e472011-10-20 09:44:48 +0300788 * @access protected
Derek Jones4b9c6292011-07-01 17:40:48 -0500789 * @param resource
790 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300792 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300794 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
796
797
798}
799
800
801
802/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300803/* Location: ./system/database/drivers/oci8/oci8_driver.php */