blob: cc9557e7d03c1c148bd6b4489ff66cb7a03a0ce9 [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
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
28// ------------------------------------------------------------------------
29
30/**
31 * oci8 Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
Barry Mienydd671972010-10-04 16:33:58 +020037 * @package CodeIgniter
Derek Jones4b9c6292011-07-01 17:40:48 -050038 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43
44/**
45 * oci8 Database Adapter Class
46 *
47 * This is a modification of the DB_driver class to
48 * permit access to oracle databases
49 *
Derek Jones4b9c6292011-07-01 17:40:48 -050050 * @author Kelly McArdle
Derek Allard2067d1a2008-11-13 22:59:24 +000051 *
52 */
53
54class CI_DB_oci8_driver extends CI_DB {
55
Andrey Andreev24abcb92012-01-05 20:40:15 +020056 public $dbdriver = 'oci8';
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 // The character used for excaping
Andrey Andreev24abcb92012-01-05 20:40:15 +020059 protected $_escape_char = '"';
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Jonese4ed5832009-02-20 21:44:59 +000061 // clause and character used for LIKE escape sequences
Andrey Andreev24abcb92012-01-05 20:40:15 +020062 protected $_like_escape_str = " escape '%s' ";
63 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 /**
66 * The syntax to count rows is slightly different across different
67 * database engines, so this string appears in each driver and is
68 * used for the count_all() and count_all_results() functions.
69 */
Andrey Andreev24abcb92012-01-05 20:40:15 +020070 protected $_count_string = 'SELECT COUNT(1) AS ';
71 protected $_random_keyword = ' ASC'; // not currently supported
Derek Allard2067d1a2008-11-13 22:59:24 +000072
73 // Set "auto commit" by default
Andrey Andreev24abcb92012-01-05 20:40:15 +020074 protected $_commit = OCI_COMMIT_ON_SUCCESS;
Derek Allard2067d1a2008-11-13 22:59:24 +000075
76 // need to track statement id and cursor id
Andrey Andreev24abcb92012-01-05 20:40:15 +020077 public $stmt_id;
78 public $curs_id;
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 // if we use a limit, we will add a field that will
81 // throw off num_fields later
Andrey Andreev24abcb92012-01-05 20:40:15 +020082 public $limit_used;
Derek Allard2067d1a2008-11-13 22:59:24 +000083
84 /**
85 * Non-persistent database connection
86 *
Derek Jones4b9c6292011-07-01 17:40:48 -050087 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +000088 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +030089 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +030091 return @oci_connect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +000092 }
93
94 // --------------------------------------------------------------------
95
96 /**
97 * Persistent database connection
98 *
Derek Jones4b9c6292011-07-01 17:40:48 -050099 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300101 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300103 return @oci_pconnect($this->username, $this->password, $this->hostname, $this->char_set);
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 }
105
106 // --------------------------------------------------------------------
107
108 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000109 * Reconnect
110 *
111 * Keep / reestablish the db connection if no queries have been
112 * sent for a length of time exceeding the server's idle timeout
113 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000114 * @return void
115 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300116 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 {
118 // not implemented in oracle
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300119 return;
Derek Jones87cbafc2009-02-27 16:29:59 +0000120 }
121
122 // --------------------------------------------------------------------
123
124 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * Select the database
126 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500127 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300129 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300131 // Not in Oracle - schemas are actually usernames
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 return TRUE;
133 }
134
135 // --------------------------------------------------------------------
136
137 /**
138 * Set client character set
139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @param string
141 * @param string
142 * @return resource
143 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300144 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
narfbg068e3de2011-09-17 21:38:46 +0300146 // this is done upon connect
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 return TRUE;
148 }
149
150 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 /**
153 * Version number query string
154 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500155 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300157 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300159 return oci_server_version($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Execute the query
166 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500167 * @param string an SQL query
168 * @return resource
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300170 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Andrey Andreev24abcb92012-01-05 20:40:15 +0200172 /* Oracle must parse the query before it is run. All of the actions with
173 * the query are based on the statement id returned by oci_parse().
174 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 $this->stmt_id = FALSE;
176 $this->_set_stmt_id($sql);
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300177 oci_set_prefetch($this->stmt_id, 1000);
178 return @oci_execute($this->stmt_id, $this->_commit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180
181 /**
182 * Generate a statement ID
183 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500184 * @param string an SQL query
185 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300187 private function _set_stmt_id($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
189 if ( ! is_resource($this->stmt_id))
190 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300191 $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Prep the query
199 *
200 * If needed, each database adapter can prep the query string
201 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500202 * @param string an SQL query
203 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300205 private function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
207 return $sql;
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500213 * getCursor. Returns a cursor from the datbase
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500215 * @return cursor id
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300217 public function get_cursor()
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300219 $this->curs_id = oci_new_cursor($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 return $this->curs_id;
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500226 * Stored Procedure. Executes a stored procedure
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500228 * @param package package stored procedure is in
229 * @param procedure stored procedure to execute
230 * @param params array of parameters
231 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 *
233 * params array keys
234 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500235 * KEY OPTIONAL NOTES
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * name no the name of the parameter should be in :<param_name> format
Derek Jones4b9c6292011-07-01 17:40:48 -0500237 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * this should be a reference to a variable
239 * type yes the type of the parameter
240 * length yes the max size of the parameter
241 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300242 public function stored_procedure($package, $procedure, $params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
244 if ($package == '' OR $procedure == '' OR ! is_array($params))
245 {
246 if ($this->db_debug)
247 {
248 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
Derek Allardfac8fbc2010-02-05 16:14:49 +0000249 return $this->display_error('db_invalid_query');
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251 return FALSE;
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // build the query string
255 $sql = "begin $package.$procedure(";
256
257 $have_cursor = FALSE;
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500258 foreach ($params as $param)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 $sql .= $param['name'] . ",";
Barry Mienydd671972010-10-04 16:33:58 +0200261
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300262 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR))
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 $have_cursor = TRUE;
265 }
266 }
267 $sql = trim($sql, ",") . "); end;";
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 $this->stmt_id = FALSE;
270 $this->_set_stmt_id($sql);
271 $this->_bind_params($params);
272 $this->query($sql, FALSE, $have_cursor);
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // --------------------------------------------------------------------
276
277 /**
278 * Bind parameters
279 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500280 * @return none
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300282 private function _bind_params($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
284 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
285 {
286 return;
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 foreach ($params as $param)
290 {
Barry Mienydd671972010-10-04 16:33:58 +0200291 foreach (array('name', 'value', 'type', 'length') as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
293 if ( ! isset($param[$val]))
294 {
295 $param[$val] = '';
296 }
297 }
298
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300299 oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Begin Transaction
307 *
Barry Mienydd671972010-10-04 16:33:58 +0200308 * @return bool
309 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300310 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
312 if ( ! $this->trans_enabled)
313 {
314 return TRUE;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // When transactions are nested we only begin/commit/rollback the outermost ones
318 if ($this->_trans_depth > 0)
319 {
320 return TRUE;
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Reset the transaction failure flag.
324 // If the $test_mode flag is set to TRUE transactions will be rolled back
325 // even if the queries produce a successful result.
326 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200327
Andrey Andreev24abcb92012-01-05 20:40:15 +0200328 $this->_commit = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT;
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 return TRUE;
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Commit Transaction
336 *
Barry Mienydd671972010-10-04 16:33:58 +0200337 * @return bool
338 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300339 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
341 if ( ! $this->trans_enabled)
342 {
343 return TRUE;
344 }
345
346 // When transactions are nested we only begin/commit/rollback the outermost ones
347 if ($this->_trans_depth > 0)
348 {
349 return TRUE;
350 }
351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 $this->_commit = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200353 return oci_commit($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Rollback Transaction
360 *
Barry Mienydd671972010-10-04 16:33:58 +0200361 * @return bool
362 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300363 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
365 if ( ! $this->trans_enabled)
366 {
367 return TRUE;
368 }
369
370 // When transactions are nested we only begin/commit/rollback the outermost ones
371 if ($this->_trans_depth > 0)
372 {
373 return TRUE;
374 }
375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 $this->_commit = OCI_COMMIT_ON_SUCCESS;
Andrey Andreev24abcb92012-01-05 20:40:15 +0200377 return oci_rollback($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Escape String
384 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500385 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000386 * @param bool whether or not the string will be used in a LIKE condition
Derek Jones4b9c6292011-07-01 17:40:48 -0500387 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300389 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000391 if (is_array($str))
392 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500393 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200394 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000395 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200396 }
397
398 return $str;
399 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000400
Greg Aker757dda62010-04-14 19:06:19 -0500401 $str = remove_invisible_characters($str);
Michiel Vugteveeneaa55412011-08-25 21:22:49 +0200402 $str = str_replace("'", "''", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Jonese4ed5832009-02-20 21:44:59 +0000404 // escape LIKE condition wildcards
405 if ($like === TRUE)
406 {
407 $str = str_replace( array('%', '_', $this->_like_escape_chr),
408 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
409 $str);
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Jonese4ed5832009-02-20 21:44:59 +0000412 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Affected Rows
419 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500420 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300422 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300424 return @oci_num_rows($this->stmt_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Insert ID
431 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500432 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300434 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 // not supported in oracle
Derek Allardfac8fbc2010-02-05 16:14:49 +0000437 return $this->display_error('db_unsupported_function');
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440 // --------------------------------------------------------------------
441
442 /**
443 * "Count All" query
444 *
445 * Generates a platform-specific query string that counts all records in
446 * the specified database
447 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500448 * @param string
449 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 */
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300451 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000454 {
455 return 0;
456 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000457
Derek Allarde37ab382009-02-03 16:13:57 +0000458 $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 +0000459
460 if ($query == FALSE)
Derek Allarde37ab382009-02-03 16:13:57 +0000461 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 return 0;
Derek Allarde37ab382009-02-03 16:13:57 +0000463 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
465 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500466 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000467 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469
470 // --------------------------------------------------------------------
471
472 /**
473 * Show table query
474 *
475 * Generates a platform-specific query string so that the table names can be fetched
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @param boolean
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300478 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300480 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
483
484 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
485 {
Greg Aker0d424892010-01-26 02:14:44 +0000486 $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 +0000487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 return $sql;
490 }
491
492 // --------------------------------------------------------------------
493
494 /**
495 * Show column query
496 *
497 * Generates a platform-specific query string so that the column names can be fetched
498 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500499 * @param string the table name
500 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300502 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
505 }
506
507 // --------------------------------------------------------------------
508
509 /**
510 * Field data query
511 *
512 * Generates a platform-specific query so that the column data can be retrieved
513 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500514 * @param string the table name
515 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300517 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 return "SELECT * FROM ".$table." where rownum = 1";
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
525 * The error message string
526 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500527 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300529 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300531 // If the error was during connection, no conn_id should be passed
532 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 return $error['message'];
534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * The error message number
540 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500541 * @return integer
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300543 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300545 // Same as _error_message()
546 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 return $error['code'];
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // --------------------------------------------------------------------
551
552 /**
553 * Escape the SQL Identifiers
554 *
555 * This function escapes column and table names
556 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @param string
558 * @return string
559 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300560 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 {
562 if ($this->_escape_char == '')
563 {
564 return $item;
565 }
566
567 foreach ($this->_reserved_identifiers as $id)
568 {
569 if (strpos($item, '.'.$id) !== FALSE)
570 {
Barry Mienydd671972010-10-04 16:33:58 +0200571 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 // remove duplicates if the user already included the escape
574 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200575 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 if (strpos($item, '.') !== FALSE)
579 {
Barry Mienydd671972010-10-04 16:33:58 +0200580 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
582 else
583 {
584 $str = $this->_escape_char.$item.$this->_escape_char;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // remove duplicates if the user already included the escape
588 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 // --------------------------------------------------------------------
592
593 /**
594 * From Tables
595 *
596 * This function implicitly groups FROM tables so there is no confusion
597 * about operator precedence in harmony with SQL standards
598 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 * @param type
600 * @return type
601 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300602 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 if ( ! is_array($tables))
605 {
606 $tables = array($tables);
607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 return implode(', ', $tables);
610 }
611
612 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 /**
615 * Insert statement
616 *
617 * Generates a platform-specific insert string from the supplied data
618 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500619 * @param string the table name
620 * @param array the insert keys
621 * @param array the insert values
622 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300624 protected function _insert($table, $keys, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300626 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 }
628
629 // --------------------------------------------------------------------
630
631 /**
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300632 * Insert_batch statement
633 *
634 * Generates a platform-specific insert string from the supplied data
635 *
Greg Aker03abee32011-12-25 00:31:29 -0600636 * @param string the table name
637 * @param array the insert keys
638 * @param array the insert values
639 * @return string
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300640 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300641 protected function _insert_batch($table, $keys, $values)
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300642 {
643 $keys = implode(', ', $keys);
644 $sql = "INSERT ALL\n";
645
646 for ($i = 0, $c = count($values); $i < $c; $i++)
Andrey Andreevb83c4082011-09-23 03:32:45 +0300647 {
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300648 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n";
Andrey Andreevb83c4082011-09-23 03:32:45 +0300649 }
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300650
651 $sql .= 'SELECT * FROM dual';
652
653 return $sql;
654 }
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 {
674 $valstr[] = $key." = ".$val;
675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200678
Andrey Andreev24abcb92012-01-05 20:40:15 +0200679 $orderby = (count($orderby) > 0) ? ' ORDER BY '.implode(', ', $orderby) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200680
Andrey Andreev24abcb92012-01-05 20:40:15 +0200681 $sql = 'UPDATE '.$table.' SET '.implode(', ', $valstr);
682 $sql .= ($where != '' AND count($where) > 0) ? ' WHERE '.implode(' ', $where) : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 return $sql;
686 }
687
688 // --------------------------------------------------------------------
689
690 /**
691 * Truncate statement
692 *
693 * Generates a platform-specific truncate string from the supplied data
694 * If the database does not support the truncate() command
695 * This function maps to "DELETE FROM table"
696 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 * @param string the table name
698 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200699 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300700 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
702 return "TRUNCATE TABLE ".$table;
703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 // --------------------------------------------------------------------
706
707 /**
708 * Delete statement
709 *
710 * Generates a platform-specific delete string from the supplied data
711 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string the table name
713 * @param array the where clause
714 * @param string the limit clause
715 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200716 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300717 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
719 $conditions = '';
720
721 if (count($where) > 0 OR count($like) > 0)
722 {
723 $conditions = "\nWHERE ";
724 $conditions .= implode("\n", $this->ar_where);
725
726 if (count($where) > 0 && count($like) > 0)
727 {
728 $conditions .= " AND ";
729 }
730 $conditions .= implode("\n", $like);
731 }
732
733 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200734
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 return "DELETE FROM ".$table.$conditions.$limit;
736 }
737
738 // --------------------------------------------------------------------
739
740 /**
741 * Limit string
742 *
743 * Generates a platform-specific LIMIT clause
744 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500745 * @param string the sql query string
746 * @param integer the number of rows to limit the query to
747 * @param integer the offset value
748 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300750 protected function _limit($sql, $limit, $offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 {
752 $limit = $offset + $limit;
753 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
754
755 if ($offset != 0)
756 {
757 $newsql .= " WHERE rnum >= $offset";
758 }
759
760 // remember that we used limits
761 $this->limit_used = TRUE;
762
763 return $newsql;
Barry Mienydd671972010-10-04 16:33:58 +0200764 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000765
766 // --------------------------------------------------------------------
767
768 /**
769 * Close DB Connection
770 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500771 * @param resource
772 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 */
Andrey Andreevbc95e472011-10-20 09:44:48 +0300774 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 {
Andrey Andreev5c3a2022011-10-07 21:04:58 +0300776 @oci_close($conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779}
780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781/* End of file oci8_driver.php */
Andrey Andreev99c6dd42011-09-23 03:07:01 +0300782/* Location: ./system/database/drivers/oci8/oci8_driver.php */