blob: 45b0198eb7bc89d67240f68288cf29bb0cba26b0 [file] [log] [blame]
Andrey Andreevda123732012-03-20 22:27:40 +02001<?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
Andrey Andreevda123732012-03-20 22:27:40 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevda123732012-03-20 22:27:40 +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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * oci8 Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
32 * creates dynamically based on whether the active record
33 * class is being used or not.
34 *
Barry Mienydd671972010-10-04 16:33:58 +020035 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050036 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41
42/**
43 * oci8 Database Adapter Class
44 *
45 * This is a modification of the DB_driver class to
46 * permit access to oracle databases
47 *
Derek Jones4b9c6292011-07-01 17:40:48 -050048 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000049 *
50 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051class CI_DB_oci8_driver extends CI_DB {
52
Andrey Andreevda123732012-03-20 22:27:40 +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 Andreevda123732012-03-20 22:27:40 +020056 protected $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Jonese4ed5832009-02-20 21:44:59 +000058 // clause and character used for LIKE escape sequences
Andrey Andreevda123732012-03-20 22:27:40 +020059 protected $_like_escape_str = " ESCAPE '%s' ";
60 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
63 * The syntax to count rows is slightly different across different
64 * database engines, so this string appears in each driver and is
65 * used for the count_all() and count_all_results() functions.
66 */
Andrey Andreevda123732012-03-20 22:27:40 +020067 protected $_count_string = 'SELECT COUNT(1) AS ';
68 protected $_random_keyword = ' ASC'; // not currently supported
Derek Allard2067d1a2008-11-13 22:59:24 +000069
70 // Set "auto commit" by default
Andrey Andreevda123732012-03-20 22:27:40 +020071 protected $_commit = OCI_COMMIT_ON_SUCCESS;
Derek Allard2067d1a2008-11-13 22:59:24 +000072
73 // need to track statement id and cursor id
Andrey Andreevda123732012-03-20 22:27:40 +020074 public $stmt_id;
75 public $curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +000076
77 // if we use a limit, we will add a field that will
78 // throw off num_fields later
Andrey Andreevda123732012-03-20 22:27:40 +020079 public $limit_used;
Derek Allard2067d1a2008-11-13 22:59:24 +000080
Andrey Andreev59ad0af2012-03-26 12:47:45 +030081 public function __construct($params)
82 {
83 parent::__construct($params);
84
85 $valid_dsns = array(
86 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS
87 // Easy Connect string (Oracle 10g+)
88 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^\/])?(\/[a-z0-9$_]+)?$/i',
89 'in' => '/^[a-z0-9$_]+$/i' // Instance name (defined in tnsnames.ora)
90 );
91
92 /* Space characters don't have any effect when actually
93 * connecting, but can be a hassle while validating the DSN.
94 */
95 $this->dsn = str_replace(array("\n", "\r", "\t", ' '), '', $this->dsn);
96
97 if ($this->dsn !== '')
98 {
99 foreach ($valid_dsns as $regexp)
100 {
101 if (preg_match($regexp, $this->dsn))
102 {
103 return;
104 }
105 }
106 }
107
108 // Legacy support for TNS in the hostname configuration field
109 $this->hostname = str_replace(array("\n", "\r", "\t", ' '), '', $this->hostname);
110 if (preg_match($valid_dsns['tns'], $this->hostname))
111 {
112 $this->dsn = $this->hostname;
113 return;
114 }
115 elseif ($this->hostname !== '' && strpos($this->hostname, '/') === FALSE && strpos($this->hostname, ':') === FALSE
116 && (( ! empty($this->port) && ctype_digit($this->port)) OR $this->database !== ''))
117 {
118 /* If the hostname field isn't empty, doesn't contain
119 * ':' and/or '/' and if port and/or database aren't
120 * empty, then the hostname field is most likely indeed
121 * just a hostname. Therefore we'll try and build an
122 * Easy Connect string from these 3 settings, assuming
123 * that the database field is a service name.
124 */
125 $this->dsn = $this->hostname
126 .(( ! empty($this->port) && ctype_digit($this->port)) ? ':'.$this->port : '')
127 .($this->database !== '' ? '/'.ltrim($this->database, '/') : '');
128
129 if (preg_match($valid_dsns['ec'], $this->dsn))
130 {
131 return;
132 }
133 }
134
135 /* At this point, we can only try and validate the hostname and
136 * database fields separately as DSNs.
137 */
138 if (preg_match($valid_dsns['ec'], $this->hostname) OR preg_match($valid_dsns['in'], $this->hostname))
139 {
140 $this->dsn = $this->hostname;
141 return;
142 }
143
144 $this->database = str_replace(array("\n", "\r", "\t", ' '), '', $this->database);
145 foreach ($valid_dsns as $regexp)
146 {
147 if (preg_match($regexp, $this->database))
148 {
149 return;
150 }
151 }
152
153 /* Well - OK, an empty string should work as well.
154 * PHP will try to use environment variables to
155 * determine which Oracle instance to connect to.
156 */
157 $this->dsn = '';
158 }
159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 /**
161 * Non-persistent database connection
162 *
Andrey Andreevda123732012-03-20 22:27:40 +0200163 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300165 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Andrey Andreev59ad0af2012-03-26 12:47:45 +0300167 return ( ! empty($this->char_set))
168 ? @oci_connect($this->username, $this->password, $this->dsn, $this->char_set)
169 : @oci_connect($this->username, $this->password, $this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Persistent database connection
176 *
Andrey Andreevda123732012-03-20 22:27:40 +0200177 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300179 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev59ad0af2012-03-26 12:47:45 +0300181 return ( ! empty($this->char_set))
182 ? @oci_pconnect($this->username, $this->password, $this->dsn, $this->char_set)
183 : @oci_pconnect($this->username, $this->password, $this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
186 // --------------------------------------------------------------------
187
188 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200189 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200191 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200193 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200195 return isset($this->data_cache['version'])
196 ? $this->data_cache['version']
197 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * Execute the query
204 *
Andrey Andreevda123732012-03-20 22:27:40 +0200205 * @param string an SQL query
206 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300208 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 // oracle must parse the query before it is run. All of the actions with
211 // the query are based on the statement id returned by ociparse
212 $this->stmt_id = FALSE;
213 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300214 oci_set_prefetch($this->stmt_id, 1000);
215 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
217
218 /**
219 * Generate a statement ID
220 *
Andrey Andreevda123732012-03-20 22:27:40 +0200221 * @param string an SQL query
222 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 */
Andrey Andreevda123732012-03-20 22:27:40 +0200224 protected function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 if ( ! is_resource($this->stmt_id))
227 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400228 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230 }
Andrey Andreevda123732012-03-20 22:27:40 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 // --------------------------------------------------------------------
233
234 /**
Andrey Andreevda123732012-03-20 22:27:40 +0200235 * Get cursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 *
Andrey Andreevda123732012-03-20 22:27:40 +0200237 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300239 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300241 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 return $this->curs_id;
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500248 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 *
Andrey Andreevda123732012-03-20 22:27:40 +0200250 * @param string package stored procedure is in
251 * @param string stored procedure to execute
252 * @param array parameters
253 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 *
255 * params array keys
256 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500257 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500259 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Timothy Warren48e1d2e2012-03-19 19:07:07 -0400260 * this should be a reference to a variable
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 * type yes the type of the parameter
262 * length yes the max size of the parameter
263 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300264 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
266 if ($package == '' OR $procedure == '' OR ! is_array($params))
267 {
268 if ($this->db_debug)
269 {
270 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000271 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273 return FALSE;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // build the query string
277 $sql = "begin $package.$procedure(";
278
279 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500280 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200283
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300284 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 $have_cursor = TRUE;
287 }
288 }
289 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 $this->stmt_id = FALSE;
292 $this->_set_stmt_id($sql);
293 $this->_bind_params($params);
294 $this->query($sql, FALSE, $have_cursor);
295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // --------------------------------------------------------------------
298
299 /**
300 * Bind parameters
301 *
Andrey Andreevda123732012-03-20 22:27:40 +0200302 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 */
Andrey Andreevda123732012-03-20 22:27:40 +0200304 protected function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
306 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
307 {
308 return;
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 foreach ($params as $param)
312 {
Barry Mienydd671972010-10-04 16:33:58 +0200313 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
315 if ( ! isset($param[$val]))
316 {
317 $param[$val] = '';
318 }
319 }
320
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300321 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Begin Transaction
329 *
Barry Mienydd671972010-10-04 16:33:58 +0200330 * @return bool
331 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300332 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 if ( ! $this->trans_enabled)
335 {
336 return TRUE;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // When transactions are nested we only begin/commit/rollback the outermost ones
340 if ($this->_trans_depth > 0)
341 {
342 return TRUE;
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // Reset the transaction failure flag.
346 // If the $test_mode flag is set to TRUE transactions will be rolled back
347 // even if the queries produce a successful result.
348 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 $this->_commit = OCI_DEFAULT;
351 return TRUE;
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * Commit Transaction
358 *
Barry Mienydd671972010-10-04 16:33:58 +0200359 * @return bool
360 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300361 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
363 if ( ! $this->trans_enabled)
364 {
365 return TRUE;
366 }
367
368 // When transactions are nested we only begin/commit/rollback the outermost ones
369 if ($this->_trans_depth > 0)
370 {
371 return TRUE;
372 }
373
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300374 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 $this->_commit = OCI_COMMIT_ON_SUCCESS;
376 return $ret;
377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Rollback Transaction
383 *
Barry Mienydd671972010-10-04 16:33:58 +0200384 * @return bool
385 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300386 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 if ( ! $this->trans_enabled)
389 {
390 return TRUE;
391 }
392
393 // When transactions are nested we only begin/commit/rollback the outermost ones
394 if ($this->_trans_depth > 0)
395 {
396 return TRUE;
397 }
398
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300399 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 $this->_commit = OCI_COMMIT_ON_SUCCESS;
401 return $ret;
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * Escape String
408 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200409 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000410 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200411 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300413 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000415 if (is_array($str))
416 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500417 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200418 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000419 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200420 }
421
422 return $str;
423 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000424
Andrey Andreevc2905f52012-03-01 14:39:26 +0200425 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Jonese4ed5832009-02-20 21:44:59 +0000427 // escape LIKE condition wildcards
428 if ($like === TRUE)
429 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200430 return str_replace(array($this->_like_escape_chr, '%', '_'),
431 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
432 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Jonese4ed5832009-02-20 21:44:59 +0000435 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Affected Rows
442 *
Andrey Andreevda123732012-03-20 22:27:40 +0200443 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300445 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300447 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449
450 // --------------------------------------------------------------------
451
452 /**
453 * Insert ID
454 *
Andrey Andreevda123732012-03-20 22:27:40 +0200455 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300457 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
459 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000460 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * "Count All" query
467 *
468 * Generates a platform-specific query string that counts all records in
469 * the specified database
470 *
Andrey Andreevda123732012-03-20 22:27:40 +0200471 * @param string
472 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300474 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
476 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000477 {
478 return 0;
479 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000480
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200481 $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 +0000482 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000483 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000485 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000486
487 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500488 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000489 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491
492 // --------------------------------------------------------------------
493
494 /**
495 * Show table query
496 *
497 * Generates a platform-specific query string so that the table names can be fetched
498 *
Andrey Andreevda123732012-03-20 22:27:40 +0200499 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300500 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300502 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
505
506 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
507 {
Greg Aker0d424892010-01-26 02:14:44 +0000508 $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 +0000509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 return $sql;
512 }
513
514 // --------------------------------------------------------------------
515
516 /**
517 * Show column query
518 *
519 * Generates a platform-specific query string so that the column names can be fetched
520 *
Andrey Andreevda123732012-03-20 22:27:40 +0200521 * @param string the table name
522 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300524 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Field data query
533 *
534 * Generates a platform-specific query so that the column data can be retrieved
535 *
Andrey Andreevda123732012-03-20 22:27:40 +0200536 * @param string the table name
537 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300539 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
541 return "SELECT * FROM ".$table." where rownum = 1";
542 }
543
544 // --------------------------------------------------------------------
545
546 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200547 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200549 * Returns an array containing code and message of the last
550 * database error that has occured.
Andrey Andreev601f8b22012-03-01 20:11:15 +0200551 *
552 * @return array
553 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200554 public function error()
Andrey Andreev601f8b22012-03-01 20:11:15 +0200555 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200556 /* oci_error() returns an array that already contains the
557 * 'code' and 'message' keys, so we can just return it.
558 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200559 if (is_resource($this->curs_id))
560 {
561 return oci_error($this->curs_id);
562 }
563 elseif (is_resource($this->stmt_id))
564 {
565 return oci_error($this->stmt_id);
566 }
567 elseif (is_resource($this->conn_id))
568 {
569 return oci_error($this->conn_id);
570 }
571
572 return oci_error();
573 }
574
575 // --------------------------------------------------------------------
576
577 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 * Escape the SQL Identifiers
579 *
580 * This function escapes column and table names
581 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @param string
583 * @return string
584 */
Andrey Andreevda123732012-03-20 22:27:40 +0200585 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 if ($this->_escape_char == '')
588 {
589 return $item;
590 }
591
592 foreach ($this->_reserved_identifiers as $id)
593 {
594 if (strpos($item, '.'.$id) !== FALSE)
595 {
Barry Mienydd671972010-10-04 16:33:58 +0200596 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 // remove duplicates if the user already included the escape
599 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200600 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 if (strpos($item, '.') !== FALSE)
604 {
Barry Mienydd671972010-10-04 16:33:58 +0200605 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 }
607 else
608 {
609 $str = $this->_escape_char.$item.$this->_escape_char;
610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // remove duplicates if the user already included the escape
613 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // --------------------------------------------------------------------
617
618 /**
619 * From Tables
620 *
621 * This function implicitly groups FROM tables so there is no confusion
622 * about operator precedence in harmony with SQL standards
623 *
Andrey Andreevda123732012-03-20 22:27:40 +0200624 * @param array
625 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300627 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
629 if ( ! is_array($tables))
630 {
631 $tables = array($tables);
632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 return implode(', ', $tables);
635 }
636
637 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 /**
640 * Insert statement
641 *
642 * Generates a platform-specific insert string from the supplied data
643 *
Andrey Andreevda123732012-03-20 22:27:40 +0200644 * @param string the table name
645 * @param array the insert keys
646 * @param array the insert values
647 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300649 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300651 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 }
653
654 // --------------------------------------------------------------------
655
656 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300657 * Insert_batch statement
658 *
659 * Generates a platform-specific insert string from the supplied data
660 *
Andrey Andreevda123732012-03-20 22:27:40 +0200661 * @param string the table name
662 * @param array the insert keys
663 * @param array the insert values
664 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300665 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300666 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300667 {
668 $keys = implode(', ', $keys);
669 $sql = "INSERT ALL\n";
670
671 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300672 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300673 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300674 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300675
676 $sql .= 'SELECT * FROM dual';
677
678 return $sql;
679 }
680
681 // --------------------------------------------------------------------
682
683 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 * Update statement
685 *
686 * Generates a platform-specific update string from the supplied data
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param string the table name
689 * @param array the update data
690 * @param array the where clause
691 * @param array the orderby clause
692 * @param array the limit clause
693 * @return string
694 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300695 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500697 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
699 $valstr[] = $key." = ".$val;
700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
707
708 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
709
710 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 return $sql;
713 }
714
715 // --------------------------------------------------------------------
716
717 /**
718 * Truncate statement
719 *
720 * Generates a platform-specific truncate string from the supplied data
721 * If the database does not support the truncate() command
722 * This function maps to "DELETE FROM table"
723 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 * @param string the table name
725 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200726 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300727 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 {
729 return "TRUNCATE TABLE ".$table;
730 }
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 // --------------------------------------------------------------------
733
734 /**
735 * Delete statement
736 *
737 * Generates a platform-specific delete string from the supplied data
738 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 * @param string the table name
740 * @param array the where clause
741 * @param string the limit clause
742 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200743 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300744 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 {
746 $conditions = '';
747
748 if (count($where) > 0 OR count($like) > 0)
749 {
750 $conditions = "\nWHERE ";
751 $conditions .= implode("\n", $this->ar_where);
752
753 if (count($where) > 0 && count($like) > 0)
754 {
755 $conditions .= " AND ";
756 }
757 $conditions .= implode("\n", $like);
758 }
759
760 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 return "DELETE FROM ".$table.$conditions.$limit;
763 }
764
765 // --------------------------------------------------------------------
766
767 /**
768 * Limit string
769 *
770 * Generates a platform-specific LIMIT clause
771 *
Andrey Andreevda123732012-03-20 22:27:40 +0200772 * @param string the sql query string
773 * @param int the number of rows to limit the query to
774 * @param int the offset value
775 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300777 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
779 $limit = $offset + $limit;
780 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
781
782 if ($offset != 0)
783 {
784 $newsql .= " WHERE rnum >= $offset";
785 }
786
787 // remember that we used limits
788 $this->limit_used = TRUE;
789
790 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200791 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000792
793 // --------------------------------------------------------------------
794
795 /**
796 * Close DB Connection
797 *
Andrey Andreevda123732012-03-20 22:27:40 +0200798 * @param resource
799 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300801 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300803 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 }
805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806}
807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808/* End of file oci8_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400809/* Location: ./system/database/drivers/oci8/oci8_driver.php */