Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | 342a466 | 2012-01-24 15:24:00 +0200 | [diff] [blame] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 24 | * @since Version 1.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 35 | * @package CodeIgniter |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 36 | * @subpackage Drivers |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 48 | * @author Kelly McArdle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | class CI_DB_oci8_driver extends CI_DB { |
| 52 | |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 53 | public $dbdriver = 'oci8'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | // The character used for excaping |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 56 | protected $_escape_char = '"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 57 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 58 | // clause and character used for LIKE escape sequences |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 59 | protected $_like_escape_str = " escape '%s' "; |
| 60 | protected $_like_escape_chr = '!'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | /** |
| 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 Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 67 | protected $_count_string = 'SELECT COUNT(1) AS '; |
| 68 | protected $_random_keyword = ' ASC'; // not currently supported |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | |
| 70 | // Set "auto commit" by default |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 71 | protected $_commit = OCI_COMMIT_ON_SUCCESS; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | |
| 73 | // need to track statement id and cursor id |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 74 | public $stmt_id; |
| 75 | public $curs_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | |
| 77 | // if we use a limit, we will add a field that will |
| 78 | // throw off num_fields later |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 79 | public $limit_used; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | |
Andrey Andreev | dad61c2 | 2012-02-13 01:08:06 +0200 | [diff] [blame^] | 81 | public function __construct($params) |
| 82 | { |
| 83 | parent::__construct($params); |
| 84 | |
| 85 | $valid_dsns = array( |
| 86 | // Easy Connect string - Oracle 10g+ |
| 87 | 'ec' => '/^(\/\/)?[a-z0-9.:_-]+(:[1-9][0-9]{0,4})?(\/[a-z0-9$_]+)?(:[^/])?(\/[a-z0-9$_]+)?$/i', |
| 88 | 'tns' => '/^\(DESCRIPTION=(\(.+\)){2,}\)$/', // TNS |
| 89 | 'in' => '/^[a-z0-9$_]+$/i' // Instance name (defined in tnsnames.ora) |
| 90 | ); |
| 91 | |
| 92 | /* Space characters don't have any effect when actually |
| 93 | * connecting, but can be a hassle while validating the DSN. |
| 94 | */ |
| 95 | $this->dsn = str_replace(array("\n", "\r", "\t", ' '), '', $this->dsn); |
| 96 | |
| 97 | if ($this->dsn !== '') |
| 98 | { |
| 99 | foreach ($valid_dsns as $regexp) |
| 100 | { |
| 101 | if (preg_match($regexp, $this->dsn)) |
| 102 | { |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Legacy support for TNS in the hostname configuration field |
| 109 | $this->hostname = str_replace(array("\n", "\r", "\t", ' '), '', $this->hostname); |
| 110 | if (preg_match($valid_dsns['tns'], $this->hostname)) |
| 111 | { |
| 112 | $this->dsn = $this->hostname; |
| 113 | return; |
| 114 | } |
| 115 | elseif ($this->hostname !== '' && strpos($this->hostname, '/') === FALSE && strpos($this->hostname, ':') === FALSE |
| 116 | && (( ! empty($this->port) && ctype_digit($this->port)) OR $this->database !== '')) |
| 117 | { |
| 118 | /* If the hostname field isn't empty, doesn't contain |
| 119 | * ':' and/or '/' and if port and/or database aren't |
| 120 | * empty, then the hostname field is most likely indeed |
| 121 | * just a hostname. Therefore we'll try and build an |
| 122 | * Easy Connect string from these 3 settings, assuming |
| 123 | * that the database field is a service name. |
| 124 | */ |
| 125 | $this->dsn = $this->hostname |
| 126 | .(( ! empty($this->port) && ctype_digit($this->port)) ? ':'.$this->port : '') |
| 127 | .($this->database !== '' ? '/'.ltrim($this->database, '/') : ''); |
| 128 | |
| 129 | if (preg_match($valid_dsns['ec'], $this->dsn)) |
| 130 | { |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* At this point, we can only try and validate the hostname and |
| 136 | * database fields separately as DSNs. |
| 137 | */ |
| 138 | if (preg_match($valid_dsns['ec'], $this->hostname) OR preg_match($valid_dsns['in'], $this->hostname)) |
| 139 | { |
| 140 | $this->dsn = $this->hostname; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $this->database = str_replace(array("\n", "\r", "\t", ' '), '', $this->database); |
| 145 | foreach ($valid_dsns as $regexp) |
| 146 | { |
| 147 | if (preg_match($regexp, $this->database)) |
| 148 | { |
| 149 | return; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* Well - OK, an empty string should work as well. |
| 154 | * PHP will try to use environment variables to |
| 155 | * determine which Oracle instance to connect to. |
| 156 | */ |
| 157 | $this->dsn = ''; |
| 158 | } |
| 159 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Non-persistent database connection |
| 162 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 163 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 165 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | { |
Andrey Andreev | dad61c2 | 2012-02-13 01:08:06 +0200 | [diff] [blame^] | 167 | return ( ! empty($this->char_set)) |
| 168 | ? @oci_connect($this->username, $this->password, $this->dsn, $this->char_set) |
| 169 | : @oci_connect($this->username, $this->password, $this->dsn); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
| 175 | * Persistent database connection |
| 176 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 177 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 179 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | { |
Andrey Andreev | dad61c2 | 2012-02-13 01:08:06 +0200 | [diff] [blame^] | 181 | return ( ! empty($this->char_set)) |
| 182 | ? @oci_pconnect($this->username, $this->password, $this->dsn, $this->char_set) |
| 183 | : @oci_pconnect($this->username, $this->password, $this->dsn); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // -------------------------------------------------------------------- |
| 187 | |
| 188 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 189 | * Reconnect |
| 190 | * |
| 191 | * Keep / reestablish the db connection if no queries have been |
| 192 | * sent for a length of time exceeding the server's idle timeout |
| 193 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 194 | * @return void |
| 195 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 196 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 197 | { |
| 198 | // not implemented in oracle |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 199 | return; |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | * Select the database |
| 206 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 207 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 209 | public function db_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 211 | // Not in Oracle - schemas are actually usernames |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | return TRUE; |
| 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Set client character set |
| 219 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | * @param string |
| 221 | * @param string |
| 222 | * @return resource |
| 223 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 224 | public function db_set_charset($charset, $collation) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
narfbg | 068e3de | 2011-09-17 21:38:46 +0300 | [diff] [blame] | 226 | // this is done upon connect |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | return TRUE; |
| 228 | } |
| 229 | |
| 230 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | /** |
| 233 | * Version number query string |
| 234 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 235 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 237 | protected function _version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 239 | return oci_server_version($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // -------------------------------------------------------------------- |
| 243 | |
| 244 | /** |
| 245 | * Execute the query |
| 246 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 247 | * @param string an SQL query |
| 248 | * @return resource |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 250 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | { |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 252 | /* Oracle must parse the query before it is run. All of the actions with |
| 253 | * the query are based on the statement id returned by oci_parse(). |
| 254 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | $this->stmt_id = FALSE; |
| 256 | $this->_set_stmt_id($sql); |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 257 | oci_set_prefetch($this->stmt_id, 1000); |
| 258 | return @oci_execute($this->stmt_id, $this->_commit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Generate a statement ID |
| 263 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 264 | * @param string an SQL query |
| 265 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 267 | private function _set_stmt_id($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | { |
| 269 | if ( ! is_resource($this->stmt_id)) |
| 270 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 271 | $this->stmt_id = oci_parse($this->conn_id, $this->_prep_query($sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | // -------------------------------------------------------------------- |
| 276 | |
| 277 | /** |
| 278 | * Prep the query |
| 279 | * |
| 280 | * If needed, each database adapter can prep the query string |
| 281 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 282 | * @param string an SQL query |
| 283 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 285 | private function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | { |
| 287 | return $sql; |
| 288 | } |
| 289 | |
| 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 293 | * getCursor. Returns a cursor from the database |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 295 | * @return resource cursor id |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 297 | public function get_cursor() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 299 | return $this->curs_id = oci_new_cursor($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // -------------------------------------------------------------------- |
| 303 | |
| 304 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 305 | * Stored Procedure. Executes a stored procedure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 307 | * @param string package name in which the stored procedure is in |
| 308 | * @param string stored procedure name to execute |
| 309 | * @param array parameters |
| 310 | * @return mixed |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | * |
| 312 | * params array keys |
| 313 | * |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 314 | * KEY OPTIONAL NOTES |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 315 | * name no the name of the parameter should be in :<param_name> format |
| 316 | * value no the value of the parameter. If this is an OUT or IN OUT parameter, |
| 317 | * this should be a reference to a variable |
| 318 | * type yes the type of the parameter |
| 319 | * length yes the max size of the parameter |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 321 | public function stored_procedure($package, $procedure, $params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | { |
| 323 | if ($package == '' OR $procedure == '' OR ! is_array($params)) |
| 324 | { |
| 325 | if ($this->db_debug) |
| 326 | { |
| 327 | log_message('error', 'Invalid query: '.$package.'.'.$procedure); |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 328 | return $this->display_error('db_invalid_query'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | } |
| 330 | return FALSE; |
| 331 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 332 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | // build the query string |
Andrey Andreev | e35d778 | 2012-01-19 15:56:20 +0200 | [diff] [blame] | 334 | $sql = 'BEGIN '.$package.'.'.$procedure.'('; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | |
| 336 | $have_cursor = FALSE; |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 337 | foreach ($params as $param) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 339 | $sql .= $param['name'].','; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 340 | |
Andrey Andreev | 85facfa | 2012-01-26 14:12:14 +0200 | [diff] [blame] | 341 | if (isset($param['type']) && $param['type'] === OCI_B_CURSOR) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | { |
| 343 | $have_cursor = TRUE; |
| 344 | } |
| 345 | } |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 346 | $sql = trim($sql, ',') . '); END;'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 347 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | $this->stmt_id = FALSE; |
| 349 | $this->_set_stmt_id($sql); |
| 350 | $this->_bind_params($params); |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 351 | return $this->query($sql, FALSE, $have_cursor); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | // -------------------------------------------------------------------- |
| 355 | |
| 356 | /** |
| 357 | * Bind parameters |
| 358 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 359 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 361 | private function _bind_params($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | { |
| 363 | if ( ! is_array($params) OR ! is_resource($this->stmt_id)) |
| 364 | { |
| 365 | return; |
| 366 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 367 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | foreach ($params as $param) |
| 369 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 370 | foreach (array('name', 'value', 'type', 'length') as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | { |
| 372 | if ( ! isset($param[$val])) |
| 373 | { |
| 374 | $param[$val] = ''; |
| 375 | } |
| 376 | } |
| 377 | |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 378 | oci_bind_by_name($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | // -------------------------------------------------------------------- |
| 383 | |
| 384 | /** |
| 385 | * Begin Transaction |
| 386 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 387 | * @return bool |
| 388 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 389 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | { |
| 391 | if ( ! $this->trans_enabled) |
| 392 | { |
| 393 | return TRUE; |
| 394 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 395 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 397 | if ($this->_trans_depth > 0) |
| 398 | { |
| 399 | return TRUE; |
| 400 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 401 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | // Reset the transaction failure flag. |
| 403 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 404 | // even if the queries produce a successful result. |
| 405 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 407 | $this->_commit = (is_php('5.3.2')) ? OCI_NO_AUTO_COMMIT : OCI_DEFAULT; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | return TRUE; |
| 409 | } |
| 410 | |
| 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | /** |
| 414 | * Commit Transaction |
| 415 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | * @return bool |
| 417 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 418 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | { |
| 420 | if ( ! $this->trans_enabled) |
| 421 | { |
| 422 | return TRUE; |
| 423 | } |
| 424 | |
| 425 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 426 | if ($this->_trans_depth > 0) |
| 427 | { |
| 428 | return TRUE; |
| 429 | } |
| 430 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | $this->_commit = OCI_COMMIT_ON_SUCCESS; |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 432 | return oci_commit($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // -------------------------------------------------------------------- |
| 436 | |
| 437 | /** |
| 438 | * Rollback Transaction |
| 439 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 440 | * @return bool |
| 441 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 442 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | e35d778 | 2012-01-19 15:56:20 +0200 | [diff] [blame] | 445 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | { |
| 447 | return TRUE; |
| 448 | } |
| 449 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | $this->_commit = OCI_COMMIT_ON_SUCCESS; |
Andrey Andreev | 24abcb9 | 2012-01-05 20:40:15 +0200 | [diff] [blame] | 451 | return oci_rollback($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // -------------------------------------------------------------------- |
| 455 | |
| 456 | /** |
| 457 | * Escape String |
| 458 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 459 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 460 | * @param bool whether or not the string will be used in a LIKE condition |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 461 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 463 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 465 | if (is_array($str)) |
| 466 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 467 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 468 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 469 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return $str; |
| 473 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 474 | |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 475 | $str = str_replace("'", "''", remove_invisible_characters($str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 476 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 477 | // escape LIKE condition wildcards |
| 478 | if ($like === TRUE) |
| 479 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 480 | return str_replace(array('%', '_', $this->_like_escape_chr), |
| 481 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 482 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 483 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 484 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 485 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // -------------------------------------------------------------------- |
| 489 | |
| 490 | /** |
| 491 | * Affected Rows |
| 492 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 493 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 495 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 497 | return @oci_num_rows($this->stmt_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // -------------------------------------------------------------------- |
| 501 | |
| 502 | /** |
| 503 | * Insert ID |
| 504 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 505 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 507 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | { |
| 509 | // not supported in oracle |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 510 | return $this->display_error('db_unsupported_function'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // -------------------------------------------------------------------- |
| 514 | |
| 515 | /** |
| 516 | * "Count All" query |
| 517 | * |
| 518 | * Generates a platform-specific query string that counts all records in |
| 519 | * the specified database |
| 520 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 521 | * @param string |
| 522 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | */ |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 524 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 525 | { |
| 526 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 527 | { |
| 528 | return 0; |
| 529 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 530 | |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 531 | $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 532 | |
| 533 | if ($query == FALSE) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 534 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | return 0; |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 536 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | |
| 538 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 539 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 540 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | // -------------------------------------------------------------------- |
| 544 | |
| 545 | /** |
| 546 | * Show table query |
| 547 | * |
| 548 | * Generates a platform-specific query string so that the table names can be fetched |
| 549 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 550 | * @param bool |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 551 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 553 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 555 | $sql = 'SELECT TABLE_NAME FROM ALL_TABLES'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | |
Andrey Andreev | 1ab62ae | 2012-01-20 13:04:10 +0200 | [diff] [blame] | 557 | if ($prefix_limit !== FALSE && $this->dbprefix != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 559 | return $sql." WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 561 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | return $sql; |
| 563 | } |
| 564 | |
| 565 | // -------------------------------------------------------------------- |
| 566 | |
| 567 | /** |
| 568 | * Show column query |
| 569 | * |
| 570 | * Generates a platform-specific query string so that the column names can be fetched |
| 571 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 572 | * @param string the table name |
| 573 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 575 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 577 | return 'SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = \''.$table.'\''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // -------------------------------------------------------------------- |
| 581 | |
| 582 | /** |
| 583 | * Field data query |
| 584 | * |
| 585 | * Generates a platform-specific query so that the column data can be retrieved |
| 586 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 587 | * @param string the table name |
| 588 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 590 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 592 | return 'SELECT * FROM '.$table.' WHERE rownum = 1'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // -------------------------------------------------------------------- |
| 596 | |
| 597 | /** |
| 598 | * The error message string |
| 599 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 600 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 602 | protected function _error_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | { |
Andrey Andreev | 6ea08e6 | 2012-01-05 21:43:17 +0200 | [diff] [blame] | 604 | $error = self::_get_error_data(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 605 | return $error['message']; |
| 606 | } |
| 607 | |
| 608 | // -------------------------------------------------------------------- |
| 609 | |
| 610 | /** |
| 611 | * The error message number |
| 612 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 613 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 614 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 615 | protected function _error_number() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | { |
Andrey Andreev | 6ea08e6 | 2012-01-05 21:43:17 +0200 | [diff] [blame] | 617 | $error = self::_get_error_data(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 618 | return $error['code']; |
| 619 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 620 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 621 | // -------------------------------------------------------------------- |
| 622 | |
Andrey Andreev | 6ea08e6 | 2012-01-05 21:43:17 +0200 | [diff] [blame] | 623 | /* Get error data |
| 624 | * |
| 625 | * Used by _error_message() and _error_number() |
| 626 | * |
| 627 | * @return array |
| 628 | */ |
| 629 | private function _get_error_data() |
| 630 | { |
| 631 | $res = NULL; |
| 632 | foreach (array('curs_id', 'stmt_id', 'conn_id') as $key) |
| 633 | { |
| 634 | if (is_resource($this->$key)) |
| 635 | { |
Andrey Andreev | 6684ce5 | 2012-01-05 22:05:14 +0200 | [diff] [blame] | 636 | $res = $this->$key; |
Andrey Andreev | 6ea08e6 | 2012-01-05 21:43:17 +0200 | [diff] [blame] | 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return ( ! is_null($res)) ? oci_error($res) : oci_error(); |
| 642 | } |
| 643 | |
| 644 | // -------------------------------------------------------------------- |
| 645 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 646 | /** |
| 647 | * Escape the SQL Identifiers |
| 648 | * |
| 649 | * This function escapes column and table names |
| 650 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | * @param string |
| 652 | * @return string |
| 653 | */ |
Andrey Andreev | 4a31568 | 2012-01-26 02:03:10 +0200 | [diff] [blame] | 654 | public function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | { |
| 656 | if ($this->_escape_char == '') |
| 657 | { |
| 658 | return $item; |
| 659 | } |
| 660 | |
| 661 | foreach ($this->_reserved_identifiers as $id) |
| 662 | { |
| 663 | if (strpos($item, '.'.$id) !== FALSE) |
| 664 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 665 | $item = str_replace('.', $this->_escape_char.'.', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 666 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 667 | // remove duplicates if the user already included the escape |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 668 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 669 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 670 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 671 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 672 | if (strpos($item, '.') !== FALSE) |
| 673 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 674 | $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 675 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 676 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 677 | // remove duplicates if the user already included the escape |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 678 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 679 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 680 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 681 | // -------------------------------------------------------------------- |
| 682 | |
| 683 | /** |
| 684 | * From Tables |
| 685 | * |
| 686 | * This function implicitly groups FROM tables so there is no confusion |
| 687 | * about operator precedence in harmony with SQL standards |
| 688 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 689 | * @param array |
| 690 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 691 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 692 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 693 | { |
Andrey Andreev | e35d778 | 2012-01-19 15:56:20 +0200 | [diff] [blame] | 694 | return is_array($tables) ? implode(', ', $tables) : $tables; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 698 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 699 | /** |
| 700 | * Insert statement |
| 701 | * |
| 702 | * Generates a platform-specific insert string from the supplied data |
| 703 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 704 | * @param string the table name |
| 705 | * @param array the insert keys |
| 706 | * @param array the insert values |
| 707 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 708 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 709 | protected function _insert($table, $keys, $values) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 710 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 711 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | // -------------------------------------------------------------------- |
| 715 | |
| 716 | /** |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 717 | * Insert_batch statement |
| 718 | * |
| 719 | * Generates a platform-specific insert string from the supplied data |
| 720 | * |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 721 | * @param string the table name |
| 722 | * @param array the insert keys |
| 723 | * @param array the insert values |
| 724 | * @return string |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 725 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 726 | protected function _insert_batch($table, $keys, $values) |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 727 | { |
| 728 | $keys = implode(', ', $keys); |
| 729 | $sql = "INSERT ALL\n"; |
| 730 | |
| 731 | for ($i = 0, $c = count($values); $i < $c; $i++) |
Andrey Andreev | b83c408 | 2011-09-23 03:32:45 +0300 | [diff] [blame] | 732 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 733 | $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i].'\n'; |
Andrey Andreev | b83c408 | 2011-09-23 03:32:45 +0300 | [diff] [blame] | 734 | } |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 735 | |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 736 | return $sql.'SELECT * FROM dual'; |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // -------------------------------------------------------------------- |
| 740 | |
| 741 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 742 | * Update statement |
| 743 | * |
| 744 | * Generates a platform-specific update string from the supplied data |
| 745 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | * @param string the table name |
| 747 | * @param array the update data |
| 748 | * @param array the where clause |
| 749 | * @param array the orderby clause |
| 750 | * @param array the limit clause |
| 751 | * @return string |
| 752 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 753 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 754 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 755 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 756 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 757 | $valstr[] = $key.' = '.$val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 758 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 759 | |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 760 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr) |
| 761 | .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') |
| 762 | .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') |
| 763 | .( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // -------------------------------------------------------------------- |
| 767 | |
| 768 | /** |
| 769 | * Truncate statement |
| 770 | * |
| 771 | * Generates a platform-specific truncate string from the supplied data |
| 772 | * If the database does not support the truncate() command |
| 773 | * This function maps to "DELETE FROM table" |
| 774 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 775 | * @param string the table name |
| 776 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 777 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 778 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 779 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 780 | return 'TRUNCATE TABLE '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 781 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 782 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 783 | // -------------------------------------------------------------------- |
| 784 | |
| 785 | /** |
| 786 | * Delete statement |
| 787 | * |
| 788 | * Generates a platform-specific delete string from the supplied data |
| 789 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 790 | * @param string the table name |
| 791 | * @param array the where clause |
| 792 | * @param string the limit clause |
| 793 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 794 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 795 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 796 | { |
| 797 | $conditions = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 798 | if (count($where) > 0 OR count($like) > 0) |
| 799 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 800 | $conditions = "\nWHERE ".implode("\n", $this->ar_where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 801 | |
| 802 | if (count($where) > 0 && count($like) > 0) |
| 803 | { |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 804 | $conditions .= ' AND '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 805 | } |
| 806 | $conditions .= implode("\n", $like); |
| 807 | } |
| 808 | |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 809 | return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // -------------------------------------------------------------------- |
| 813 | |
| 814 | /** |
| 815 | * Limit string |
| 816 | * |
| 817 | * Generates a platform-specific LIMIT clause |
| 818 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 819 | * @param string the sql query string |
| 820 | * @param int the number of rows to limit the query to |
| 821 | * @param int the offset value |
| 822 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 823 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 824 | protected function _limit($sql, $limit, $offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 825 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 826 | $this->limit_used = TRUE; |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 827 | return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($offset + $limit).')' |
| 828 | .($offset != 0 ? ' WHERE rnum >= '.$offset : ''); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 829 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 830 | |
| 831 | // -------------------------------------------------------------------- |
| 832 | |
| 833 | /** |
| 834 | * Close DB Connection |
| 835 | * |
Andrey Andreev | aa786c9 | 2012-01-16 12:14:45 +0200 | [diff] [blame] | 836 | * @param resource |
| 837 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 838 | */ |
Andrey Andreev | bc95e47 | 2011-10-20 09:44:48 +0300 | [diff] [blame] | 839 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 840 | { |
Andrey Andreev | 5c3a202 | 2011-10-07 21:04:58 +0300 | [diff] [blame] | 841 | @oci_close($conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 846 | /* End of file oci8_driver.php */ |
Andrey Andreev | 99c6dd4 | 2011-09-23 03:07:01 +0300 | [diff] [blame] | 847 | /* Location: ./system/database/drivers/oci8/oci8_driver.php */ |