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