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