blob: 3bc8c114ca63d534e7e328eec3e3b1c80bccf76c [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 /**
189 * Select the database
190 *
Andrey Andreevda123732012-03-20 22:27:40 +0200191 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300193 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300195 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 return TRUE;
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200202 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 *
Andrey Andreev08856b82012-03-03 03:19:28 +0200204 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200206 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200208 return isset($this->data_cache['version'])
209 ? $this->data_cache['version']
210 : $this->data_cache['version'] = oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Execute the query
217 *
Andrey Andreevda123732012-03-20 22:27:40 +0200218 * @param string an SQL query
219 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300221 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 // oracle must parse the query before it is run. All of the actions with
224 // the query are based on the statement id returned by ociparse
225 $this->stmt_id = FALSE;
226 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300227 oci_set_prefetch($this->stmt_id, 1000);
228 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230
231 /**
232 * Generate a statement ID
233 *
Andrey Andreevda123732012-03-20 22:27:40 +0200234 * @param string an SQL query
235 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 */
Andrey Andreevda123732012-03-20 22:27:40 +0200237 protected function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 if ( ! is_resource($this->stmt_id))
240 {
Timothy Warrend2ff0bc2012-03-19 16:52:10 -0400241 $this->stmt_id = oci_parse($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243 }
Andrey Andreevda123732012-03-20 22:27:40 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // --------------------------------------------------------------------
246
247 /**
Andrey Andreevda123732012-03-20 22:27:40 +0200248 * Get cursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 *
Andrey Andreevda123732012-03-20 22:27:40 +0200250 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300252 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300254 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 return $this->curs_id;
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500261 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 *
Andrey Andreevda123732012-03-20 22:27:40 +0200263 * @param string package stored procedure is in
264 * @param string stored procedure to execute
265 * @param array parameters
266 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 *
268 * params array keys
269 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500270 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500272 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Timothy Warren48e1d2e2012-03-19 19:07:07 -0400273 * this should be a reference to a variable
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * type yes the type of the parameter
275 * length yes the max size of the parameter
276 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300277 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 if ($package == '' OR $procedure == '' OR ! is_array($params))
280 {
281 if ($this->db_debug)
282 {
283 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000284 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286 return FALSE;
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // build the query string
290 $sql = "begin $package.$procedure(";
291
292 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500293 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200296
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300297 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 $have_cursor = TRUE;
300 }
301 }
302 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 $this->stmt_id = FALSE;
305 $this->_set_stmt_id($sql);
306 $this->_bind_params($params);
307 $this->query($sql, FALSE, $have_cursor);
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // --------------------------------------------------------------------
311
312 /**
313 * Bind parameters
314 *
Andrey Andreevda123732012-03-20 22:27:40 +0200315 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 */
Andrey Andreevda123732012-03-20 22:27:40 +0200317 protected function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
320 {
321 return;
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 foreach ($params as $param)
325 {
Barry Mienydd671972010-10-04 16:33:58 +0200326 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 if ( ! isset($param[$val]))
329 {
330 $param[$val] = '';
331 }
332 }
333
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300334 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Begin Transaction
342 *
Barry Mienydd671972010-10-04 16:33:58 +0200343 * @return bool
344 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300345 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 if ( ! $this->trans_enabled)
348 {
349 return TRUE;
350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // When transactions are nested we only begin/commit/rollback the outermost ones
353 if ($this->_trans_depth > 0)
354 {
355 return TRUE;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // Reset the transaction failure flag.
359 // If the $test_mode flag is set to TRUE transactions will be rolled back
360 // even if the queries produce a successful result.
361 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 $this->_commit = OCI_DEFAULT;
364 return TRUE;
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Commit Transaction
371 *
Barry Mienydd671972010-10-04 16:33:58 +0200372 * @return bool
373 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300374 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 if ( ! $this->trans_enabled)
377 {
378 return TRUE;
379 }
380
381 // When transactions are nested we only begin/commit/rollback the outermost ones
382 if ($this->_trans_depth > 0)
383 {
384 return TRUE;
385 }
386
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300387 $ret = oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $this->_commit = OCI_COMMIT_ON_SUCCESS;
389 return $ret;
390 }
391
392 // --------------------------------------------------------------------
393
394 /**
395 * Rollback Transaction
396 *
Barry Mienydd671972010-10-04 16:33:58 +0200397 * @return bool
398 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300399 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 if ( ! $this->trans_enabled)
402 {
403 return TRUE;
404 }
405
406 // When transactions are nested we only begin/commit/rollback the outermost ones
407 if ($this->_trans_depth > 0)
408 {
409 return TRUE;
410 }
411
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300412 $ret = oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 $this->_commit = OCI_COMMIT_ON_SUCCESS;
414 return $ret;
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Escape String
421 *
Andrey Andreevc2905f52012-03-01 14:39:26 +0200422 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000423 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevc2905f52012-03-01 14:39:26 +0200424 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300426 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000428 if (is_array($str))
429 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500430 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200431 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000432 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200433 }
434
435 return $str;
436 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000437
Andrey Andreevc2905f52012-03-01 14:39:26 +0200438 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Jonese4ed5832009-02-20 21:44:59 +0000440 // escape LIKE condition wildcards
441 if ($like === TRUE)
442 {
Andrey Andreevc2905f52012-03-01 14:39:26 +0200443 return str_replace(array($this->_like_escape_chr, '%', '_'),
444 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
445 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Jonese4ed5832009-02-20 21:44:59 +0000448 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Affected Rows
455 *
Andrey Andreevda123732012-03-20 22:27:40 +0200456 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300458 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300460 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Insert ID
467 *
Andrey Andreevda123732012-03-20 22:27:40 +0200468 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300470 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000473 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * "Count All" query
480 *
481 * Generates a platform-specific query string that counts all records in
482 * the specified database
483 *
Andrey Andreevda123732012-03-20 22:27:40 +0200484 * @param string
485 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300487 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000490 {
491 return 0;
492 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000493
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200494 $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 +0000495 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000496 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000498 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
500 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500501 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000502 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Show table query
509 *
510 * Generates a platform-specific query string so that the table names can be fetched
511 *
Andrey Andreevda123732012-03-20 22:27:40 +0200512 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300513 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300515 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
518
519 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
520 {
Greg Aker0d424892010-01-26 02:14:44 +0000521 $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 +0000522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 return $sql;
525 }
526
527 // --------------------------------------------------------------------
528
529 /**
530 * Show column query
531 *
532 * Generates a platform-specific query string so that the column names can be fetched
533 *
Andrey Andreevda123732012-03-20 22:27:40 +0200534 * @param string the table name
535 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300537 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
540 }
541
542 // --------------------------------------------------------------------
543
544 /**
545 * Field data query
546 *
547 * Generates a platform-specific query so that the column data can be retrieved
548 *
Andrey Andreevda123732012-03-20 22:27:40 +0200549 * @param string the table name
550 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300552 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
554 return "SELECT * FROM ".$table." where rownum = 1";
555 }
556
557 // --------------------------------------------------------------------
558
559 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200560 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200562 * Returns an array containing code and message of the last
563 * database error that has occured.
Andrey Andreev601f8b22012-03-01 20:11:15 +0200564 *
565 * @return array
566 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200567 public function error()
Andrey Andreev601f8b22012-03-01 20:11:15 +0200568 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200569 /* oci_error() returns an array that already contains the
570 * 'code' and 'message' keys, so we can just return it.
571 */
Andrey Andreev601f8b22012-03-01 20:11:15 +0200572 if (is_resource($this->curs_id))
573 {
574 return oci_error($this->curs_id);
575 }
576 elseif (is_resource($this->stmt_id))
577 {
578 return oci_error($this->stmt_id);
579 }
580 elseif (is_resource($this->conn_id))
581 {
582 return oci_error($this->conn_id);
583 }
584
585 return oci_error();
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 * Escape the SQL Identifiers
592 *
593 * This function escapes column and table names
594 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 * @param string
596 * @return string
597 */
Andrey Andreevda123732012-03-20 22:27:40 +0200598 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
600 if ($this->_escape_char == '')
601 {
602 return $item;
603 }
604
605 foreach ($this->_reserved_identifiers as $id)
606 {
607 if (strpos($item, '.'.$id) !== FALSE)
608 {
Barry Mienydd671972010-10-04 16:33:58 +0200609 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // remove duplicates if the user already included the escape
612 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200613 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 if (strpos($item, '.') !== FALSE)
617 {
Barry Mienydd671972010-10-04 16:33:58 +0200618 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 }
620 else
621 {
622 $str = $this->_escape_char.$item.$this->_escape_char;
623 }
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 // remove duplicates if the user already included the escape
626 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // --------------------------------------------------------------------
630
631 /**
632 * From Tables
633 *
634 * This function implicitly groups FROM tables so there is no confusion
635 * about operator precedence in harmony with SQL standards
636 *
Andrey Andreevda123732012-03-20 22:27:40 +0200637 * @param array
638 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300640 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 if ( ! is_array($tables))
643 {
644 $tables = array($tables);
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 return implode(', ', $tables);
648 }
649
650 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 /**
653 * Insert statement
654 *
655 * Generates a platform-specific insert string from the supplied data
656 *
Andrey Andreevda123732012-03-20 22:27:40 +0200657 * @param string the table name
658 * @param array the insert keys
659 * @param array the insert values
660 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300662 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300664 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
666
667 // --------------------------------------------------------------------
668
669 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300670 * Insert_batch statement
671 *
672 * Generates a platform-specific insert string from the supplied data
673 *
Andrey Andreevda123732012-03-20 22:27:40 +0200674 * @param string the table name
675 * @param array the insert keys
676 * @param array the insert values
677 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300678 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300679 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300680 {
681 $keys = implode(', ', $keys);
682 $sql = "INSERT ALL\n";
683
684 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300685 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300686 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300687 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300688
689 $sql .= 'SELECT * FROM dual';
690
691 return $sql;
692 }
693
694 // --------------------------------------------------------------------
695
696 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 * Update statement
698 *
699 * Generates a platform-specific update string from the supplied data
700 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 * @param string the table name
702 * @param array the update data
703 * @param array the where clause
704 * @param array the orderby clause
705 * @param array the limit clause
706 * @return string
707 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300708 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500710 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
712 $valstr[] = $key." = ".$val;
713 }
Barry Mienydd671972010-10-04 16:33:58 +0200714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200716
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
720
721 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
722
723 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 return $sql;
726 }
727
728 // --------------------------------------------------------------------
729
730 /**
731 * Truncate statement
732 *
733 * Generates a platform-specific truncate string from the supplied data
734 * If the database does not support the truncate() command
735 * This function maps to "DELETE FROM table"
736 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 * @param string the table name
738 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200739 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300740 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
742 return "TRUNCATE TABLE ".$table;
743 }
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 // --------------------------------------------------------------------
746
747 /**
748 * Delete statement
749 *
750 * Generates a platform-specific delete string from the supplied data
751 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 * @param string the table name
753 * @param array the where clause
754 * @param string the limit clause
755 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200756 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300757 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
759 $conditions = '';
760
761 if (count($where) > 0 OR count($like) > 0)
762 {
763 $conditions = "\nWHERE ";
764 $conditions .= implode("\n", $this->ar_where);
765
766 if (count($where) > 0 && count($like) > 0)
767 {
768 $conditions .= " AND ";
769 }
770 $conditions .= implode("\n", $like);
771 }
772
773 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 return "DELETE FROM ".$table.$conditions.$limit;
776 }
777
778 // --------------------------------------------------------------------
779
780 /**
781 * Limit string
782 *
783 * Generates a platform-specific LIMIT clause
784 *
Andrey Andreevda123732012-03-20 22:27:40 +0200785 * @param string the sql query string
786 * @param int the number of rows to limit the query to
787 * @param int the offset value
788 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300790 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
792 $limit = $offset + $limit;
793 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
794
795 if ($offset != 0)
796 {
797 $newsql .= " WHERE rnum >= $offset";
798 }
799
800 // remember that we used limits
801 $this->limit_used = TRUE;
802
803 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200804 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000805
806 // --------------------------------------------------------------------
807
808 /**
809 * Close DB Connection
810 *
Andrey Andreevda123732012-03-20 22:27:40 +0200811 * @param resource
812 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300814 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300816 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 }
818
Derek Allard2067d1a2008-11-13 22:59:24 +0000819}
820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821/* End of file oci8_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400822/* Location: ./system/database/drivers/oci8/oci8_driver.php */