blob: 700cde4b897e7f7c0f677a06c257887ec63b1210 [file] [log] [blame]
Andrey Andreev24abcb92012-01-05 20:40:15 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
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
Andrey Andreev342a4662012-01-24 15:24:00 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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
51class 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 Jonese4ed5832009-02-20 21:44:59 +000058 // clause and character used for LIKE escape sequences
Andrey Andreev24abcb92012-01-05 20:40:15 +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 Andreev24abcb92012-01-05 20:40:15 +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 Andreev24abcb92012-01-05 20:40:15 +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 Andreev24abcb92012-01-05 20:40:15 +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 Andreev24abcb92012-01-05 20:40:15 +020079 public $limit_used;
Derek Allard2067d1a2008-11-13 22:59:24 +000080
81 /**
82 * Non-persistent database connection
83 *
Andrey Andreevaa786c92012-01-16 12:14:45 +020084 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000085 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030086 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030088 return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Persistent database connection
95 *
Andrey Andreevaa786c92012-01-16 12:14:45 +020096 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000097 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030098 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300100 return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
102
103 // --------------------------------------------------------------------
104
105 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000106 * Reconnect
107 *
108 * Keep / reestablish the db connection if no queries have been
109 * sent for a length of time exceeding the server's idle timeout
110 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000111 * @return void
112 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300113 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000114 {
115 // not implemented in oracle
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300116 return;
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 }
118
119 // --------------------------------------------------------------------
120
121 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 * Select the database
123 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200124 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300126 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300128 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 return TRUE;
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Set client character set
136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @param string
138 * @param string
139 * @return resource
140 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300141 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
narfbg068e3de2011-09-17 21:38:46 +0300143 // this is done upon connect
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 return TRUE;
145 }
146
147 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 /**
150 * Version number query string
151 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200152 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300154 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300156 return oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Execute the query
163 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200164 * @param string an SQL query
165 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300167 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200169 /* Oracle must parse the query before it is run. All of the actions with
170 * the query are based on the statement id returned by oci_parse().
171 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 $this->stmt_id = FALSE;
173 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300174 oci_set_prefetch($this->stmt_id, 1000);
175 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177
178 /**
179 * Generate a statement ID
180 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200181 * @param string an SQL query
182 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300184 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 if ( ! is_resource($this->stmt_id))
187 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300188 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Prep the query
196 *
197 * If needed, each database adapter can prep the query string
198 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200199 * @param string an SQL query
200 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300202 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 return $sql;
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
Andrey Andreevaa786c92012-01-16 12:14:45 +0200210 * getCursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200212 * @return resource cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300214 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200216 return $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218
219 // --------------------------------------------------------------------
220
221 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500222 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200224 * @param string package name in which the stored procedure is in
225 * @param string stored procedure name to execute
226 * @param array parameters
227 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 *
229 * params array keys
230 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500231 * KEY OPTIONAL NOTES
Andrey Andreevaa786c92012-01-16 12:14:45 +0200232 * name no the name of the parameter should be in :<param_name> format
233 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
234 * this should be a reference to a variable
235 * type yes the type of the parameter
236 * length yes the max size of the parameter
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300238 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 if ($package == '' OR $procedure == '' OR ! is_array($params))
241 {
242 if ($this->db_debug)
243 {
244 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000245 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247 return FALSE;
248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // build the query string
Andrey Andreeve35d7782012-01-19 15:56:20 +0200251 $sql = 'BEGIN '.$package.'.'.$procedure.'(';
Derek Allard2067d1a2008-11-13 22:59:24 +0000252
253 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500254 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200256 $sql .= $param['name'].',';
Barry Mienydd671972010-10-04 16:33:58 +0200257
Andrey Andreev85facfa2012-01-26 14:12:14 +0200258 if (isset($param['type']) && $param['type'] === OCI_B_CURSOR)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 $have_cursor = TRUE;
261 }
262 }
Andrey Andreevaa786c92012-01-16 12:14:45 +0200263 $sql = trim($sql, ',') . '); END;';
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $this->stmt_id = FALSE;
266 $this->_set_stmt_id($sql);
267 $this->_bind_params($params);
Andrey Andreevaa786c92012-01-16 12:14:45 +0200268 return $this->query($sql, FALSE, $have_cursor);
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // --------------------------------------------------------------------
272
273 /**
274 * Bind parameters
275 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200276 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300278 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
281 {
282 return;
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 foreach ($params as $param)
286 {
Barry Mienydd671972010-10-04 16:33:58 +0200287 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 if ( ! isset($param[$val]))
290 {
291 $param[$val] = '';
292 }
293 }
294
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300295 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Begin Transaction
303 *
Barry Mienydd671972010-10-04 16:33:58 +0200304 * @return bool
305 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300306 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
308 if ( ! $this->trans_enabled)
309 {
310 return TRUE;
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // When transactions are nested we only begin/commit/rollback the outermost ones
314 if ($this->_trans_depth > 0)
315 {
316 return TRUE;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Reset the transaction failure flag.
320 // If the $test_mode flag is set to TRUE transactions will be rolled back
321 // even if the queries produce a successful result.
322 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200323
Andrey Andreev24abcb92012-01-05 20:40:15 +0200324 $this->_commit = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 return TRUE;
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Commit Transaction
332 *
Barry Mienydd671972010-10-04 16:33:58 +0200333 * @return bool
334 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300335 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 if ( ! $this->trans_enabled)
338 {
339 return TRUE;
340 }
341
342 // When transactions are nested we only begin/commit/rollback the outermost ones
343 if ($this->_trans_depth > 0)
344 {
345 return TRUE;
346 }
347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $this->_commit = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200349 return oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Rollback Transaction
356 *
Barry Mienydd671972010-10-04 16:33:58 +0200357 * @return bool
358 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300359 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeve35d7782012-01-19 15:56:20 +0200362 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 return TRUE;
365 }
366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 $this->_commit = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200368 return oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * Escape String
375 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200376 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000377 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevaa786c92012-01-16 12:14:45 +0200378 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300380 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000382 if (is_array($str))
383 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500384 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200385 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000386 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200387 }
388
389 return $str;
390 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000391
Andrey Andreevaa786c92012-01-16 12:14:45 +0200392 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Jonese4ed5832009-02-20 21:44:59 +0000394 // escape LIKE condition wildcards
395 if ($like === TRUE)
396 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200397 return str_replace(array('%', '_', $this->_like_escape_chr),
398 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
399 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Jonese4ed5832009-02-20 21:44:59 +0000402 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Affected Rows
409 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200410 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300412 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300414 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Insert ID
421 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200422 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300424 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
426 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000427 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * "Count All" query
434 *
435 * Generates a platform-specific query string that counts all records in
436 * the specified database
437 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200438 * @param string
439 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300441 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000444 {
445 return 0;
446 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000447
Andrey Andreevaa786c92012-01-16 12:14:45 +0200448 $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 +0000449
450 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000451 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000453 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000454
455 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500456 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000457 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
459
460 // --------------------------------------------------------------------
461
462 /**
463 * Show table query
464 *
465 * Generates a platform-specific query string so that the table names can be fetched
466 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200467 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300468 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300470 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200472 $sql = 'SELECT TABLE_NAME FROM ALL_TABLES';
Derek Allard2067d1a2008-11-13 22:59:24 +0000473
Andrey Andreev1ab62ae2012-01-20 13:04:10 +0200474 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200476 return $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 +0000477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 return $sql;
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
485 * Show column query
486 *
487 * Generates a platform-specific query string so that the column names can be fetched
488 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200489 * @param string the table name
490 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300492 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200494 return 'SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = \''.$table.'\'';
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Field data query
501 *
502 * Generates a platform-specific query so that the column data can be retrieved
503 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200504 * @param string the table name
505 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300507 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200509 return 'SELECT * FROM '.$table.' WHERE rownum = 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512 // --------------------------------------------------------------------
513
514 /**
515 * The error message string
516 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200517 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300519 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200521 $error = self::_get_error_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 return $error['message'];
523 }
524
525 // --------------------------------------------------------------------
526
527 /**
528 * The error message number
529 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200530 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300532 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200534 $error = self::_get_error_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 return $error['code'];
536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 // --------------------------------------------------------------------
539
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200540 /* Get error data
541 *
542 * Used by _error_message() and _error_number()
543 *
544 * @return array
545 */
546 private function _get_error_data()
547 {
548 $res = NULL;
549 foreach (array('curs_id', 'stmt_id', 'conn_id') as $key)
550 {
551 if (is_resource($this->$key))
552 {
Andrey Andreev6684ce52012-01-05 22:05:14 +0200553 $res = $this->$key;
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200554 break;
555 }
556 }
557
558 return ( ! is_null($res)) ? oci_error($res) : oci_error();
559 }
560
561 // --------------------------------------------------------------------
562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 /**
564 * Escape the SQL Identifiers
565 *
566 * This function escapes column and table names
567 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * @param string
569 * @return string
570 */
Andrey Andreev4a315682012-01-26 02:03:10 +0200571 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 if ($this->_escape_char == '')
574 {
575 return $item;
576 }
577
578 foreach ($this->_reserved_identifiers as $id)
579 {
580 if (strpos($item, '.'.$id) !== FALSE)
581 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200582 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // remove duplicates if the user already included the escape
Andrey Andreevaa786c92012-01-16 12:14:45 +0200585 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200586 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 if (strpos($item, '.') !== FALSE)
590 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200591 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 }
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 // remove duplicates if the user already included the escape
Andrey Andreevaa786c92012-01-16 12:14:45 +0200595 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 // --------------------------------------------------------------------
599
600 /**
601 * From Tables
602 *
603 * This function implicitly groups FROM tables so there is no confusion
604 * about operator precedence in harmony with SQL standards
605 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200606 * @param array
607 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300609 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
Andrey Andreeve35d7782012-01-19 15:56:20 +0200611 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
613
614 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 /**
617 * Insert statement
618 *
619 * Generates a platform-specific insert string from the supplied data
620 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200621 * @param string the table name
622 * @param array the insert keys
623 * @param array the insert values
624 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300626 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200628 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
630
631 // --------------------------------------------------------------------
632
633 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300634 * Insert_batch statement
635 *
636 * Generates a platform-specific insert string from the supplied data
637 *
Greg Aker03abee32011-12-25 00:31:29 -0600638 * @param string the table name
639 * @param array the insert keys
640 * @param array the insert values
641 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300642 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300643 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300644 {
645 $keys = implode(', ', $keys);
646 $sql = "INSERT ALL\n";
647
648 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300649 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200650 $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i].'\n';
Andrey Andreevb83c4082011-09-23 03:32:45 +0300651 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300652
Andrey Andreevaa786c92012-01-16 12:14:45 +0200653 return $sql.'SELECT * FROM dual';
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300654 }
655
656 // --------------------------------------------------------------------
657
658 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 * Update statement
660 *
661 * Generates a platform-specific update string from the supplied data
662 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 * @param string the table name
664 * @param array the update data
665 * @param array the where clause
666 * @param array the orderby clause
667 * @param array the limit clause
668 * @return string
669 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300670 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500672 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200674 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Andrey Andreevaa786c92012-01-16 12:14:45 +0200677 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
678 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
679 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
680 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682
683 // --------------------------------------------------------------------
684
685 /**
686 * Truncate statement
687 *
688 * Generates a platform-specific truncate string from the supplied data
689 * If the database does not support the truncate() command
690 * This function maps to "DELETE FROM table"
691 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 * @param string the table name
693 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200694 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300695 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200697 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 }
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 // --------------------------------------------------------------------
701
702 /**
703 * Delete statement
704 *
705 * Generates a platform-specific delete string from the supplied data
706 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 * @param string the table name
708 * @param array the where clause
709 * @param string the limit clause
710 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200711 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300712 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
714 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 if (count($where) > 0 OR count($like) > 0)
716 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200717 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000718
719 if (count($where) > 0 && count($like) > 0)
720 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200721 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 }
723 $conditions .= implode("\n", $like);
724 }
725
Andrey Andreevaa786c92012-01-16 12:14:45 +0200726 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 }
728
729 // --------------------------------------------------------------------
730
731 /**
732 * Limit string
733 *
734 * Generates a platform-specific LIMIT clause
735 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200736 * @param string the sql query string
737 * @param int the number of rows to limit the query to
738 * @param int the offset value
739 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300741 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 $this->limit_used = TRUE;
Andrey Andreevaa786c92012-01-16 12:14:45 +0200744 return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($offset + $limit).')'
745 .($offset != 0 ? ' WHERE rnum >= '.$offset : '');
Barry Mienydd671972010-10-04 16:33:58 +0200746 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000747
748 // --------------------------------------------------------------------
749
750 /**
751 * Close DB Connection
752 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200753 * @param resource
754 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300756 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300758 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
760
Derek Allard2067d1a2008-11-13 22:59:24 +0000761}
762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300764/* Location: ./system/database/drivers/oci8/oci8_driver.php */