blob: 2a5149f9e54a465cb0decd1b3e647913df3566a1 [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 Andreevaa786c92012-01-16 12:14:45 +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 */
51
52class CI_DB_oci8_driver extends CI_DB {
53
Andrey Andreev24abcb92012-01-05 20:40:15 +020054 public $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 // The character used for excaping
Andrey Andreev24abcb92012-01-05 20:40:15 +020057 protected $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Jonese4ed5832009-02-20 21:44:59 +000059 // clause and character used for LIKE escape sequences
Andrey Andreev24abcb92012-01-05 20:40:15 +020060 protected $_like_escape_str = " escape '%s' ";
61 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 /**
64 * The syntax to count rows is slightly different across different
65 * database engines, so this string appears in each driver and is
66 * used for the count_all() and count_all_results() functions.
67 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020068 protected $_count_string = 'SELECT COUNT(1) AS ';
69 protected $_random_keyword = ' ASC'; // not currently supported
Derek Allard2067d1a2008-11-13 22:59:24 +000070
71 // Set "auto commit" by default
Andrey Andreev24abcb92012-01-05 20:40:15 +020072 protected $_commit = OCI_COMMIT_ON_SUCCESS;
Derek Allard2067d1a2008-11-13 22:59:24 +000073
74 // need to track statement id and cursor id
Andrey Andreev24abcb92012-01-05 20:40:15 +020075 public $stmt_id;
76 public $curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +000077
78 // if we use a limit, we will add a field that will
79 // throw off num_fields later
Andrey Andreev24abcb92012-01-05 20:40:15 +020080 public $limit_used;
Derek Allard2067d1a2008-11-13 22:59:24 +000081
82 /**
83 * Non-persistent database connection
84 *
Andrey Andreevaa786c92012-01-16 12:14:45 +020085 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000086 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030087 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030089 return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
91
92 // --------------------------------------------------------------------
93
94 /**
95 * Persistent database connection
96 *
Andrey Andreevaa786c92012-01-16 12:14:45 +020097 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000098 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030099 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300101 return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
103
104 // --------------------------------------------------------------------
105
106 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000107 * Reconnect
108 *
109 * Keep / reestablish the db connection if no queries have been
110 * sent for a length of time exceeding the server's idle timeout
111 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000112 * @return void
113 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300114 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000115 {
116 // not implemented in oracle
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300117 return;
Derek Jones87cbafc2009-02-27 16:29:59 +0000118 }
119
120 // --------------------------------------------------------------------
121
122 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * Select the database
124 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200125 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300127 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300129 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 return TRUE;
131 }
132
133 // --------------------------------------------------------------------
134
135 /**
136 * Set client character set
137 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * @param string
139 * @param string
140 * @return resource
141 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300142 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
narfbg068e3de2011-09-17 21:38:46 +0300144 // this is done upon connect
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 return TRUE;
146 }
147
148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 /**
151 * Version number query string
152 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200153 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300155 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300157 return oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 }
159
160 // --------------------------------------------------------------------
161
162 /**
163 * Execute the query
164 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200165 * @param string an SQL query
166 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300168 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200170 /* Oracle must parse the query before it is run. All of the actions with
171 * the query are based on the statement id returned by oci_parse().
172 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $this->stmt_id = FALSE;
174 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300175 oci_set_prefetch($this->stmt_id, 1000);
176 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
178
179 /**
180 * Generate a statement ID
181 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200182 * @param string an SQL query
183 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300185 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
187 if ( ! is_resource($this->stmt_id))
188 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300189 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Prep the query
197 *
198 * If needed, each database adapter can prep the query string
199 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200200 * @param string an SQL query
201 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300203 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 return $sql;
206 }
207
208 // --------------------------------------------------------------------
209
210 /**
Andrey Andreevaa786c92012-01-16 12:14:45 +0200211 * getCursor. Returns a cursor from the database
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200213 * @return resource cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300215 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200217 return $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 // --------------------------------------------------------------------
221
222 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500223 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200225 * @param string package name in which the stored procedure is in
226 * @param string stored procedure name to execute
227 * @param array parameters
228 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 *
230 * params array keys
231 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500232 * KEY OPTIONAL NOTES
Andrey Andreevaa786c92012-01-16 12:14:45 +0200233 * name no the name of the parameter should be in :<param_name> format
234 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
235 * this should be a reference to a variable
236 * type yes the type of the parameter
237 * length yes the max size of the parameter
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300239 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 if ($package == '' OR $procedure == '' OR ! is_array($params))
242 {
243 if ($this->db_debug)
244 {
245 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000246 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
248 return FALSE;
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 // build the query string
Andrey Andreevaa786c92012-01-16 12:14:45 +0200252 $sql = "BEGIN {$package}.{$procedure}(";
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
254 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500255 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200257 $sql .= $param['name'].',';
Barry Mienydd671972010-10-04 16:33:58 +0200258
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300259 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $have_cursor = TRUE;
262 }
263 }
Andrey Andreevaa786c92012-01-16 12:14:45 +0200264 $sql = trim($sql, ',') . '); END;';
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 $this->stmt_id = FALSE;
267 $this->_set_stmt_id($sql);
268 $this->_bind_params($params);
Andrey Andreevaa786c92012-01-16 12:14:45 +0200269 return $this->query($sql, FALSE, $have_cursor);
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
273
274 /**
275 * Bind parameters
276 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200277 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300279 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
282 {
283 return;
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 foreach ($params as $param)
287 {
Barry Mienydd671972010-10-04 16:33:58 +0200288 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
290 if ( ! isset($param[$val]))
291 {
292 $param[$val] = '';
293 }
294 }
295
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300296 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Begin Transaction
304 *
Barry Mienydd671972010-10-04 16:33:58 +0200305 * @return bool
306 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300307 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 if ( ! $this->trans_enabled)
310 {
311 return TRUE;
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // When transactions are nested we only begin/commit/rollback the outermost ones
315 if ($this->_trans_depth > 0)
316 {
317 return TRUE;
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Reset the transaction failure flag.
321 // If the $test_mode flag is set to TRUE transactions will be rolled back
322 // even if the queries produce a successful result.
323 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200324
Andrey Andreev24abcb92012-01-05 20:40:15 +0200325 $this->_commit = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 return TRUE;
327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Commit Transaction
333 *
Barry Mienydd671972010-10-04 16:33:58 +0200334 * @return bool
335 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300336 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 if ( ! $this->trans_enabled)
339 {
340 return TRUE;
341 }
342
343 // When transactions are nested we only begin/commit/rollback the outermost ones
344 if ($this->_trans_depth > 0)
345 {
346 return TRUE;
347 }
348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 $this->_commit = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200350 return oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * Rollback Transaction
357 *
Barry Mienydd671972010-10-04 16:33:58 +0200358 * @return bool
359 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300360 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 if ( ! $this->trans_enabled)
363 {
364 return TRUE;
365 }
366
367 // When transactions are nested we only begin/commit/rollback the outermost ones
368 if ($this->_trans_depth > 0)
369 {
370 return TRUE;
371 }
372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 $this->_commit = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200374 return oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Escape String
381 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200382 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000383 * @param bool whether or not the string will be used in a LIKE condition
Andrey Andreevaa786c92012-01-16 12:14:45 +0200384 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300386 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000388 if (is_array($str))
389 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500390 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200391 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000392 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200393 }
394
395 return $str;
396 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000397
Andrey Andreevaa786c92012-01-16 12:14:45 +0200398 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Jonese4ed5832009-02-20 21:44:59 +0000400 // escape LIKE condition wildcards
401 if ($like === TRUE)
402 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200403 return str_replace(array('%', '_', $this->_like_escape_chr),
404 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
405 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Jonese4ed5832009-02-20 21:44:59 +0000408 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Affected Rows
415 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200416 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300418 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300420 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Insert ID
427 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200428 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300430 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000433 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
435
436 // --------------------------------------------------------------------
437
438 /**
439 * "Count All" query
440 *
441 * Generates a platform-specific query string that counts all records in
442 * the specified database
443 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200444 * @param string
445 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300447 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000450 {
451 return 0;
452 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000453
Andrey Andreevaa786c92012-01-16 12:14:45 +0200454 $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 +0000455
456 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000457 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000459 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000460
461 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500462 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000463 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Show table query
470 *
471 * Generates a platform-specific query string so that the table names can be fetched
472 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200473 * @param bool
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300474 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300476 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200478 $sql = 'SELECT TABLE_NAME FROM ALL_TABLES';
Derek Allard2067d1a2008-11-13 22:59:24 +0000479
480 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
481 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200482 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 +0000483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 return $sql;
486 }
487
488 // --------------------------------------------------------------------
489
490 /**
491 * Show column query
492 *
493 * Generates a platform-specific query string so that the column names can be fetched
494 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200495 * @param string the table name
496 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300498 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200500 return 'SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = \''.$table.'\'';
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Field data query
507 *
508 * Generates a platform-specific query so that the column data can be retrieved
509 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200510 * @param string the table name
511 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300513 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200515 return 'SELECT * FROM '.$table.' WHERE rownum = 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
517
518 // --------------------------------------------------------------------
519
520 /**
521 * The error message string
522 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200523 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300525 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200527 $error = self::_get_error_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 return $error['message'];
529 }
530
531 // --------------------------------------------------------------------
532
533 /**
534 * The error message number
535 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200536 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300538 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200540 $error = self::_get_error_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 return $error['code'];
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // --------------------------------------------------------------------
545
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200546 /* Get error data
547 *
548 * Used by _error_message() and _error_number()
549 *
550 * @return array
551 */
552 private function _get_error_data()
553 {
554 $res = NULL;
555 foreach (array('curs_id', 'stmt_id', 'conn_id') as $key)
556 {
557 if (is_resource($this->$key))
558 {
Andrey Andreev6684ce52012-01-05 22:05:14 +0200559 $res = $this->$key;
Andrey Andreev6ea08e62012-01-05 21:43:17 +0200560 break;
561 }
562 }
563
564 return ( ! is_null($res)) ? oci_error($res) : oci_error();
565 }
566
567 // --------------------------------------------------------------------
568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 /**
570 * Escape the SQL Identifiers
571 *
572 * This function escapes column and table names
573 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * @param string
575 * @return string
576 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300577 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 if ($this->_escape_char == '')
580 {
581 return $item;
582 }
583
584 foreach ($this->_reserved_identifiers as $id)
585 {
586 if (strpos($item, '.'.$id) !== FALSE)
587 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200588 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // remove duplicates if the user already included the escape
Andrey Andreevaa786c92012-01-16 12:14:45 +0200591 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200592 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 if (strpos($item, '.') !== FALSE)
596 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200597 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // remove duplicates if the user already included the escape
Andrey Andreevaa786c92012-01-16 12:14:45 +0200601 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // --------------------------------------------------------------------
605
606 /**
607 * From Tables
608 *
609 * This function implicitly groups FROM tables so there is no confusion
610 * about operator precedence in harmony with SQL standards
611 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200612 * @param array
613 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300615 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 if ( ! is_array($tables))
618 {
619 $tables = array($tables);
620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 return implode(', ', $tables);
623 }
624
625 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 /**
628 * Insert statement
629 *
630 * Generates a platform-specific insert string from the supplied data
631 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200632 * @param string the table name
633 * @param array the insert keys
634 * @param array the insert values
635 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300637 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200639 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 }
641
642 // --------------------------------------------------------------------
643
644 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300645 * Insert_batch statement
646 *
647 * Generates a platform-specific insert string from the supplied data
648 *
Greg Aker03abee32011-12-25 00:31:29 -0600649 * @param string the table name
650 * @param array the insert keys
651 * @param array the insert values
652 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300653 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300654 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300655 {
656 $keys = implode(', ', $keys);
657 $sql = "INSERT ALL\n";
658
659 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300660 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200661 $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i].'\n';
Andrey Andreevb83c4082011-09-23 03:32:45 +0300662 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300663
Andrey Andreevaa786c92012-01-16 12:14:45 +0200664 return $sql.'SELECT * FROM dual';
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300665 }
666
667 // --------------------------------------------------------------------
668
669 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * Update statement
671 *
672 * Generates a platform-specific update string from the supplied data
673 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 * @param string the table name
675 * @param array the update data
676 * @param array the where clause
677 * @param array the orderby clause
678 * @param array the limit clause
679 * @return string
680 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300681 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500683 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200685 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Andrey Andreevaa786c92012-01-16 12:14:45 +0200688 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
689 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
690 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
691 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 }
693
694 // --------------------------------------------------------------------
695
696 /**
697 * Truncate statement
698 *
699 * Generates a platform-specific truncate string from the supplied data
700 * If the database does not support the truncate() command
701 * This function maps to "DELETE FROM table"
702 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 * @param string the table name
704 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200705 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300706 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200708 return 'TRUNCATE TABLE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 }
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 // --------------------------------------------------------------------
712
713 /**
714 * Delete statement
715 *
716 * Generates a platform-specific delete string from the supplied data
717 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 * @param string the table name
719 * @param array the where clause
720 * @param string the limit clause
721 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200722 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300723 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
725 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 if (count($where) > 0 OR count($like) > 0)
727 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200728 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000729
730 if (count($where) > 0 && count($like) > 0)
731 {
Andrey Andreevaa786c92012-01-16 12:14:45 +0200732 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
734 $conditions .= implode("\n", $like);
735 }
736
Andrey Andreevaa786c92012-01-16 12:14:45 +0200737 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
739
740 // --------------------------------------------------------------------
741
742 /**
743 * Limit string
744 *
745 * Generates a platform-specific LIMIT clause
746 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200747 * @param string the sql query string
748 * @param int the number of rows to limit the query to
749 * @param int the offset value
750 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300752 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 $this->limit_used = TRUE;
Andrey Andreevaa786c92012-01-16 12:14:45 +0200755 return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($offset + $limit).')'
756 .($offset != 0 ? ' WHERE rnum >= '.$offset : '');
Barry Mienydd671972010-10-04 16:33:58 +0200757 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000758
759 // --------------------------------------------------------------------
760
761 /**
762 * Close DB Connection
763 *
Andrey Andreevaa786c92012-01-16 12:14:45 +0200764 * @param resource
765 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300767 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300769 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 }
771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772}
773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300775/* Location: ./system/database/drivers/oci8/oci8_driver.php */