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