blob: b1fd86a8e0c3c6ab06a08f9b44e4ca2f6086e4c9 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreev24abcb92012-01-05 20:40:15 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24abcb92012-01-05 20:40:15 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * oci8 Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
Barry Mienydd671972010-10-04 16:33:58 +020036 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050037 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42
43/**
44 * oci8 Database Adapter Class
45 *
46 * This is a modification of the DB_driver class to
47 * permit access to oracle databases
48 *
Derek Jones4b9c6292011-07-01 17:40:48 -050049 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051class CI_DB_oci8_driver extends CI_DB {
52
Andrey Andreev24abcb92012-01-05 20:40:15 +020053 public $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Allard2067d1a2008-11-13 22:59:24 +000055 // The character used for excaping
Andrey Andreev24abcb92012-01-05 20:40:15 +020056 protected $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 /**
59 * The syntax to count rows is slightly different across different
60 * database engines, so this string appears in each driver and is
61 * used for the count_all() and count_all_results() functions.
62 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020063 protected $_count_string = 'SELECT COUNT(1) AS ';
64 protected $_random_keyword = ' ASC'; // not currently supported
Derek Allard2067d1a2008-11-13 22:59:24 +000065
Andrey Andreevd3f13672012-06-24 22:13:21 +030066 protected $_reserved_identifiers = array('*', 'rownum');
67
Derek Allard2067d1a2008-11-13 22:59:24 +000068 // Set "auto commit" by default
Andrey Andreev99013ed2012-03-05 16:17:32 +020069 public $commit_mode = OCI_COMMIT_ON_SUCCESS;
Derek Allard2067d1a2008-11-13 22:59:24 +000070
71 // need to track statement id and cursor id
Andrey Andreev24abcb92012-01-05 20:40:15 +020072 public $stmt_id;
73 public $curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +000074
75 // if we use a limit, we will add a field that will
76 // throw off num_fields later
Andrey Andreev24abcb92012-01-05 20:40:15 +020077 public $limit_used;
Derek Allard2067d1a2008-11-13 22:59:24 +000078
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030079 /**
80 * Constructor
81 *
82 * @param array $params
83 * @return void
84 */
Andrey Andreevdad61c22012-02-13 01:08:06 +020085 public function __construct($params)
86 {
87 parent::__construct($params);
88
89 $valid_dsns = array(
Andrey Andreevdad61c22012-02-13 01:08:06 +020090 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS
Andrey Andreevfcd1f472012-02-13 09:30:16 +020091 // Easy Connect string (Oracle 10g+)
92 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i',
Andrey Andreevdad61c22012-02-13 01:08:06 +020093 'in' => '/^[a-z0-9$_]+$/i' // Instance name (defined in tnsnames.ora)
94 );
95
96 /* Space characters don't have any effect when actually
97 * connecting, but can be a hassle while validating the DSN.
98 */
99 $this->dsn = str_replace(array("\n", "\r", "\t", ' '), '', $this->dsn);
100
101 if ($this->dsn !== '')
102 {
103 foreach ($valid_dsns as $regexp)
104 {
105 if (preg_match($regexp, $this->dsn))
106 {
107 return;
108 }
109 }
110 }
111
112 // Legacy support for TNS in the hostname configuration field
113 $this->hostname = str_replace(array("\n", "\r", "\t", ' '), '', $this->hostname);
114 if (preg_match($valid_dsns['tns'], $this->hostname))
115 {
116 $this->dsn = $this->hostname;
117 return;
118 }
119 elseif ($this->hostname !== '' && strpos($this->hostname, '/') === FALSE && strpos($this->hostname, ':') === FALSE
120 && (( ! empty($this->port) && ctype_digit($this->port)) OR $this->database !== ''))
121 {
122 /* If the hostname field isn't empty, doesn't contain
123 * ':' and/or '/' and if port and/or database aren't
124 * empty, then the hostname field is most likely indeed
125 * just a hostname. Therefore we'll try and build an
126 * Easy Connect string from these 3 settings, assuming
127 * that the database field is a service name.
128 */
129 $this->dsn = $this->hostname
130 .(( ! empty($this->port) && ctype_digit($this->port)) ? ':'.$this->port : '')
131 .($this->database !== '' ? '/'.ltrim($this->database, '/') : '');
132
133 if (preg_match($valid_dsns['ec'], $this->dsn))
134 {
135 return;
136 }
137 }
138
139 /* At this point, we can only try and validate the hostname and
140 * database fields separately as DSNs.
141 */
142 if (preg_match($valid_dsns['ec'], $this->hostname) OR preg_match($valid_dsns['in'], $this->hostname))
143 {
144 $this->dsn = $this->hostname;
145 return;
146 }
147
148 $this->database = str_replace(array("\n", "\r", "\t", ' '), '', $this->database);
149 foreach ($valid_dsns as $regexp)
150 {
151 if (preg_match($regexp, $this->database))
152 {
153 return;
154 }
155 }
156
157 /* Well - OK, an empty string should work as well.
158 * PHP will try to use environment variables to
159 * determine which Oracle instance to connect to.
160 */
161 $this->dsn = '';
162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163
Andrey Andreevd5809992012-06-28 14:06:54 +0300164 // --------------------------------------------------------------------
165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 /**
167 * Non-persistent database connection
168 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200169 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300171 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Andrey Andreevdad61c22012-02-13 01:08:06 +0200173 return ( ! empty($this->char_set))
174 ? @oci_connect($this->username, $this->password, $this->dsn, $this->char_set)
175 : @oci_connect($this->username, $this->password, $this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Persistent database connection
182 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200183 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300185 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreevd5809992012-06-28 14:06:54 +0300187 return empty($this->char_set)
188 ? @oci_pconnect($this->username, $this->password, $this->dsn)
189 : @oci_pconnect($this->username, $this->password, $this->dsn, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191
192 // --------------------------------------------------------------------
193
194 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200195 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200197 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200199 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200201 return isset($this->data_cache['version'])
202 ? $this->data_cache['version']
203 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
205
206 // --------------------------------------------------------------------
207
208 /**
209 * Execute the query
210 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200211 * @param string an SQL query
212 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300214 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200216 /* Oracle must parse the query before it is run. All of the actions with
217 * the query are based on the statement id returned by oci_parse().
218 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 $this->stmt_id = FALSE;
220 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300221 oci_set_prefetch($this->stmt_id, 1000);
Andrey Andreev99013ed2012-03-05 16:17:32 +0200222 return @oci_execute($this->stmt_id, $this->commit_mode);
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224
Andrey Andreevd5809992012-06-28 14:06:54 +0300225 // --------------------------------------------------------------------
226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 /**
228 * Generate a statement ID
229 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200230 * @param string an SQL query
231 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 */
Andrey Andreevda123732012-03-20 22:27:40 +0200233 protected function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
235 if ( ! is_resource($this->stmt_id))
236 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400237 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239 }
240
241 // --------------------------------------------------------------------
242
243 /**
Andrey Andreevda123732012-03-20 22:27:40 +0200244 * Get cursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 *
Andrey Andreevd5809992012-06-28 14:06:54 +0300246 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300248 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200250 return $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 }
252
253 // --------------------------------------------------------------------
254
255 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500256 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200258 * @param string package name in which the stored procedure is in
259 * @param string stored procedure name to execute
260 * @param array parameters
261 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 *
263 * params array keys
264 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500265 * KEY OPTIONAL NOTES
Andrey Andreevaa786c92012-01-16 12:14:45 +0200266 * name no the name of the parameter should be in :<param_name> format
267 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
268 * this should be a reference to a variable
269 * type yes the type of the parameter
270 * length yes the max size of the parameter
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300272 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100274 if ($package === '' OR $procedure === '' OR ! is_array($params))
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 if ($this->db_debug)
277 {
278 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000279 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281 return FALSE;
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // build the query string
Andrey Andreeve35d7782012-01-19 15:56:20 +0200285 $sql = 'BEGIN '.$package.'.'.$procedure.'(';
Derek Allard2067d1a2008-11-13 22:59:24 +0000286
287 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500288 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200290 $sql .= $param['name'].',';
Barry Mienydd671972010-10-04 16:33:58 +0200291
Andrey Andreev85facfa2012-01-26 14:12:14 +0200292 if (isset($param['type']) && $param['type'] === OCI_B_CURSOR)
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 $have_cursor = TRUE;
295 }
296 }
Andrey Andreevaa786c92012-01-16 12:14:45 +0200297 $sql = trim($sql, ',') . '); END;';
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 $this->stmt_id = FALSE;
300 $this->_set_stmt_id($sql);
301 $this->_bind_params($params);
Andrey Andreevaa786c92012-01-16 12:14:45 +0200302 return $this->query($sql, FALSE, $have_cursor);
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // --------------------------------------------------------------------
306
307 /**
308 * Bind parameters
309 *
Andrey Andreevd5809992012-06-28 14:06:54 +0300310 * @param array
Andrey Andreevaa786c92012-01-16 12:14:45 +0200311 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 */
Andrey Andreev1b5a8562012-03-13 13:13:43 +0200313 protected function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
315 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
316 {
317 return;
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 foreach ($params as $param)
321 {
Barry Mienydd671972010-10-04 16:33:58 +0200322 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
324 if ( ! isset($param[$val]))
325 {
326 $param[$val] = '';
327 }
328 }
329
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300330 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Begin Transaction
338 *
Andrey Andreevd5809992012-06-28 14:06:54 +0300339 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200340 * @return bool
341 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300342 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
344 if ( ! $this->trans_enabled)
345 {
346 return TRUE;
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // When transactions are nested we only begin/commit/rollback the outermost ones
350 if ($this->_trans_depth > 0)
351 {
352 return TRUE;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Reset the transaction failure flag.
356 // If the $test_mode flag is set to TRUE transactions will be rolled back
357 // even if the queries produce a successful result.
358 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200359
Andrey Andreev99013ed2012-03-05 16:17:32 +0200360 $this->commit_mode = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 return TRUE;
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
367 * Commit Transaction
368 *
Barry Mienydd671972010-10-04 16:33:58 +0200369 * @return bool
370 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300371 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 if ( ! $this->trans_enabled)
374 {
375 return TRUE;
376 }
377
378 // When transactions are nested we only begin/commit/rollback the outermost ones
379 if ($this->_trans_depth > 0)
380 {
381 return TRUE;
382 }
383
Andrey Andreev99013ed2012-03-05 16:17:32 +0200384 $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200385 return oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Rollback Transaction
392 *
Barry Mienydd671972010-10-04 16:33:58 +0200393 * @return bool
394 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300395 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeve35d7782012-01-19 15:56:20 +0200398 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 return TRUE;
401 }
402
Andrey Andreev99013ed2012-03-05 16:17:32 +0200403 $this->commit_mode = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200404 return oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
408
409 /**
410 * Escape String
411 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200412 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000413 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200414 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300416 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000418 if (is_array($str))
419 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500420 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200421 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000422 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200423 }
424
425 return $str;
426 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000427
Andrey Andreevc2905f52012-03-01 14:39:26 +0200428 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Jonese4ed5832009-02-20 21:44:59 +0000430 // escape LIKE condition wildcards
431 if ($like === TRUE)
432 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200433 return str_replace(array($this->_like_escape_chr, '%', '_'),
434 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
435 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Jonese4ed5832009-02-20 21:44:59 +0000438 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Affected Rows
445 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200446 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300448 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300450 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452
453 // --------------------------------------------------------------------
454
455 /**
456 * Insert ID
457 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200458 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300460 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000463 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
465
466 // --------------------------------------------------------------------
467
468 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * Show table query
470 *
471 * Generates a platform-specific query string so that the table names can be fetched
472 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200473 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300474 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300476 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300478 $sql = 'SELECT "TABLE_NAME" FROM "ALL_TABLES"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000479
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100480 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300482 return $sql.' WHERE "TABLE_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
483 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 return $sql;
487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Show column query
493 *
494 * Generates a platform-specific query string so that the column names can be fetched
495 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200496 * @param string the table name
497 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300499 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300501 return 'SELECT "COLUMN_NAME" FROM "all_tab_columns" WHERE "TABLE_NAME" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Field data query
508 *
509 * Generates a platform-specific query so that the column data can be retrieved
510 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200511 * @param string the table name
512 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300514 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreevd3f13672012-06-24 22:13:21 +0300516 return 'SELECT * FROM '.$this->protect_identifiers($table).' WHERE rownum = 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
518
519 // --------------------------------------------------------------------
520
521 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200522 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200524 * Returns an array containing code and message of the last
525 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 *
Andrey Andreev601f8b22012-03-01 20:11:15 +0200527 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200529 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200531 /* oci_error() returns an array that already contains the
532 * 'code' and 'message' keys, so we can just return it.
533 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200534 if (is_resource($this->curs_id))
535 {
536 return oci_error($this->curs_id);
537 }
538 elseif (is_resource($this->stmt_id))
539 {
540 return oci_error($this->stmt_id);
541 }
542 elseif (is_resource($this->conn_id))
543 {
544 return oci_error($this->conn_id);
545 }
546
547 return oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // --------------------------------------------------------------------
551
552 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300553 * Insert_batch statement
554 *
555 * Generates a platform-specific insert string from the supplied data
556 *
Andrey Andreevda123732012-03-20 22:27:40 +0200557 * @param string the table name
558 * @param array the insert keys
559 * @param array the insert values
560 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300561 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300562 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300563 {
564 $keys = implode(', ', $keys);
565 $sql = "INSERT ALL\n";
566
567 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300568 {
Andrey Andreev97f36972012-04-05 12:44:36 +0300569 $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i]."\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300570 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300571
Andrey Andreevaa786c92012-01-16 12:14:45 +0200572 return $sql.'SELECT * FROM dual';
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Truncate statement
579 *
580 * Generates a platform-specific truncate string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 *
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300582 * If the database does not support the truncate() command,
583 * then this method maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * @param string the table name
586 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200587 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300588 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200590 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 // --------------------------------------------------------------------
594
595 /**
596 * Delete statement
597 *
598 * Generates a platform-specific delete string from the supplied data
599 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @param string the table name
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200602 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300603 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300605 if ($this->qb_limit)
606 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300607 $this->where('rownum <= ',$this->qb_limit, FALSE);
Andrey Andreevb0478652012-07-18 15:34:46 +0300608 $this->qb_limit = FALSE;
609 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000610
Andrey Andreevb0478652012-07-18 15:34:46 +0300611 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
613
614 // --------------------------------------------------------------------
615
616 /**
617 * Limit string
618 *
619 * Generates a platform-specific LIMIT clause
620 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200621 * @param string the sql query string
Andrey Andreevaa786c92012-01-16 12:14:45 +0200622 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300624 protected function _limit($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 $this->limit_used = TRUE;
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300627 return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($this->qb_offset + $this->qb_limit + 1).')'
628 .($this->qb_offset ? ' WHERE rnum >= '.($this->qb_offset + 1): '');
Barry Mienydd671972010-10-04 16:33:58 +0200629 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000630
631 // --------------------------------------------------------------------
632
633 /**
634 * Close DB Connection
635 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200636 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300638 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300640 @oci_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 }
642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643}
644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645/* End of file oci8_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300646/* Location: ./system/database/drivers/oci8/oci8_driver.php */