1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * oci8 Database Adapter Class
\r
21 * Note: _DB is an extender class that the app controller
\r
22 * creates dynamically based on whether the active record
\r
23 * class is being used or not.
\r
25 * @package CodeIgniter
\r
26 * @subpackage Drivers
\r
27 * @category Database
\r
28 * @author ExpressionEngine Dev Team
\r
29 * @link http://codeigniter.com/user_guide/database/
\r
33 * oci8 Database Adapter Class
\r
35 * This is a modification of the DB_driver class to
\r
36 * permit access to oracle databases
\r
38 * NOTE: this uses the PHP 4 oci methods
\r
40 * @author Kelly McArdle
\r
44 class CI_DB_oci8_driver extends CI_DB {
\r
46 var $dbdriver = 'oci8';
\r
48 // The character used for excaping
\r
49 var $_escape_char = '"';
\r
52 * The syntax to count rows is slightly different across different
\r
53 * database engines, so this string appears in each driver and is
\r
54 * used for the count_all() and count_all_results() functions.
\r
56 var $_count_string = "SELECT COUNT(1) AS ";
\r
57 var $_random_keyword = ' ASC'; // not currently supported
\r
59 // Set "auto commit" by default
\r
60 var $_commit = OCI_COMMIT_ON_SUCCESS;
\r
62 // need to track statement id and cursor id
\r
66 // if we use a limit, we will add a field that will
\r
67 // throw off num_fields later
\r
71 * Non-persistent database connection
\r
73 * @access private called by the base class
\r
76 function db_connect()
\r
78 return @ocilogon($this->username, $this->password, $this->hostname);
\r
81 // --------------------------------------------------------------------
\r
84 * Persistent database connection
\r
86 * @access private called by the base class
\r
89 function db_pconnect()
\r
91 return @ociplogon($this->username, $this->password, $this->hostname);
\r
94 // --------------------------------------------------------------------
\r
97 * Select the database
\r
99 * @access private called by the base class
\r
102 function db_select()
\r
107 // --------------------------------------------------------------------
\r
110 * Set client character set
\r
117 function db_set_charset($charset, $collation)
\r
119 // @todo - add support if needed
\r
123 // --------------------------------------------------------------------
\r
126 * Version number query string
\r
131 function _version()
\r
133 return ociserverversion($this->conn_id);
\r
136 // --------------------------------------------------------------------
\r
139 * Execute the query
\r
141 * @access private called by the base class
\r
142 * @param string an SQL query
\r
145 function _execute($sql)
\r
147 // oracle must parse the query before it is run. All of the actions with
\r
148 // the query are based on the statement id returned by ociparse
\r
149 $this->stmt_id = FALSE;
\r
150 $this->_set_stmt_id($sql);
\r
151 ocisetprefetch($this->stmt_id, 1000);
\r
152 return @ociexecute($this->stmt_id, $this->_commit);
\r
156 * Generate a statement ID
\r
159 * @param string an SQL query
\r
162 function _set_stmt_id($sql)
\r
164 if ( ! is_resource($this->stmt_id))
\r
166 $this->stmt_id = ociparse($this->conn_id, $this->_prep_query($sql));
\r
170 // --------------------------------------------------------------------
\r
175 * If needed, each database adapter can prep the query string
\r
177 * @access private called by execute()
\r
178 * @param string an SQL query
\r
181 function _prep_query($sql)
\r
186 // --------------------------------------------------------------------
\r
189 * getCursor. Returns a cursor from the datbase
\r
192 * @return cursor id
\r
194 function get_cursor()
\r
196 $this->curs_id = ocinewcursor($this->conn_id);
\r
197 return $this->curs_id;
\r
200 // --------------------------------------------------------------------
\r
203 * Stored Procedure. Executes a stored procedure
\r
206 * @param package package stored procedure is in
\r
207 * @param procedure stored procedure to execute
\r
208 * @param params array of parameters
\r
211 * params array keys
\r
213 * KEY OPTIONAL NOTES
\r
214 * name no the name of the parameter should be in :<param_name> format
\r
215 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
\r
216 * this should be a reference to a variable
\r
217 * type yes the type of the parameter
\r
218 * length yes the max size of the parameter
\r
220 function stored_procedure($package, $procedure, $params)
\r
222 if ($package == '' OR $procedure == '' OR ! is_array($params))
\r
224 if ($this->db_debug)
\r
226 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
\r
227 return $this->display_error('db_invalid_query');
\r
232 // build the query string
\r
233 $sql = "begin $package.$procedure(";
\r
235 $have_cursor = FALSE;
\r
236 foreach($params as $param)
\r
238 $sql .= $param['name'] . ",";
\r
240 if (array_key_exists('type', $param) && ($param['type'] == OCI_B_CURSOR))
\r
242 $have_cursor = TRUE;
\r
245 $sql = trim($sql, ",") . "); end;";
\r
247 $this->stmt_id = FALSE;
\r
248 $this->_set_stmt_id($sql);
\r
249 $this->_bind_params($params);
\r
250 $this->query($sql, FALSE, $have_cursor);
\r
253 // --------------------------------------------------------------------
\r
261 function _bind_params($params)
\r
263 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
\r
268 foreach ($params as $param)
\r
270 foreach (array('name', 'value', 'type', 'length') as $val)
\r
272 if ( ! isset($param[$val]))
\r
278 ocibindbyname($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
\r
282 // --------------------------------------------------------------------
\r
285 * Begin Transaction
\r
290 function trans_begin($test_mode = FALSE)
\r
292 if ( ! $this->trans_enabled)
\r
297 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
298 if ($this->_trans_depth > 0)
\r
303 // Reset the transaction failure flag.
\r
304 // If the $test_mode flag is set to TRUE transactions will be rolled back
\r
305 // even if the queries produce a successful result.
\r
306 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
\r
308 $this->_commit = OCI_DEFAULT;
\r
312 // --------------------------------------------------------------------
\r
315 * Commit Transaction
\r
320 function trans_commit()
\r
322 if ( ! $this->trans_enabled)
\r
327 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
328 if ($this->_trans_depth > 0)
\r
333 $ret = OCIcommit($this->conn_id);
\r
334 $this->_commit = OCI_COMMIT_ON_SUCCESS;
\r
338 // --------------------------------------------------------------------
\r
341 * Rollback Transaction
\r
346 function trans_rollback()
\r
348 if ( ! $this->trans_enabled)
\r
353 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
354 if ($this->_trans_depth > 0)
\r
359 $ret = OCIrollback($this->conn_id);
\r
360 $this->_commit = OCI_COMMIT_ON_SUCCESS;
\r
364 // --------------------------------------------------------------------
\r
373 function escape_str($str)
\r
375 // Access the CI object
\r
376 $CI =& get_instance();
\r
378 return $CI->_remove_invisible_characters($str);
\r
381 // --------------------------------------------------------------------
\r
389 function affected_rows()
\r
391 return @ocirowcount($this->stmt_id);
\r
394 // --------------------------------------------------------------------
\r
402 function insert_id()
\r
404 // not supported in oracle
\r
405 return $this->display_error('db_unsupported_function');
\r
408 // --------------------------------------------------------------------
\r
411 * "Count All" query
\r
413 * Generates a platform-specific query string that counts all records in
\r
414 * the specified database
\r
420 function count_all($table = '')
\r
425 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
\r
427 if ($query == FALSE)
\r
432 $row = $query->row();
\r
433 return $row->NUMROWS;
\r
436 // --------------------------------------------------------------------
\r
441 * Generates a platform-specific query string so that the table names can be fetched
\r
447 function _list_tables($prefix_limit = FALSE)
\r
449 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
\r
451 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
\r
453 $sql .= " WHERE TABLE_NAME LIKE '".$this->dbprefix."%'";
\r
459 // --------------------------------------------------------------------
\r
462 * Show column query
\r
464 * Generates a platform-specific query string so that the column names can be fetched
\r
467 * @param string the table name
\r
470 function _list_columns($table = '')
\r
472 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
\r
475 // --------------------------------------------------------------------
\r
480 * Generates a platform-specific query so that the column data can be retrieved
\r
483 * @param string the table name
\r
486 function _field_data($table)
\r
488 return "SELECT * FROM ".$table." where rownum = 1";
\r
491 // --------------------------------------------------------------------
\r
494 * The error message string
\r
499 function _error_message()
\r
501 $error = ocierror($this->conn_id);
\r
502 return $error['message'];
\r
505 // --------------------------------------------------------------------
\r
508 * The error message number
\r
513 function _error_number()
\r
515 $error = ocierror($this->conn_id);
\r
516 return $error['code'];
\r
519 // --------------------------------------------------------------------
\r
522 * Escape the SQL Identifiers
\r
524 * This function escapes column and table names
\r
530 function _escape_identifiers($item)
\r
532 if ($this->_escape_char == '')
\r
537 if (strpos($item, '.') !== FALSE)
\r
539 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
\r
543 $str = $this->_escape_char.$item.$this->_escape_char;
\r
546 // remove duplicates if the user already included the escape
\r
547 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
\r
550 // --------------------------------------------------------------------
\r
555 * This function implicitly groups FROM tables so there is no confusion
\r
556 * about operator precedence in harmony with SQL standards
\r
562 function _from_tables($tables)
\r
564 if ( ! is_array($tables))
\r
566 $tables = array($tables);
\r
569 return implode(', ', $tables);
\r
572 // --------------------------------------------------------------------
\r
577 * Generates a platform-specific insert string from the supplied data
\r
580 * @param string the table name
\r
581 * @param array the insert keys
\r
582 * @param array the insert values
\r
585 function _insert($table, $keys, $values)
\r
587 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
\r
590 // --------------------------------------------------------------------
\r
595 * Generates a platform-specific update string from the supplied data
\r
598 * @param string the table name
\r
599 * @param array the update data
\r
600 * @param array the where clause
\r
601 * @param array the orderby clause
\r
602 * @param array the limit clause
\r
605 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
\r
607 foreach($values as $key => $val)
\r
609 $valstr[] = $key." = ".$val;
\r
612 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
\r
614 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
\r
616 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
\r
618 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
\r
620 $sql .= $orderby.$limit;
\r
625 // --------------------------------------------------------------------
\r
628 * Truncate statement
\r
630 * Generates a platform-specific truncate string from the supplied data
\r
631 * If the database does not support the truncate() command
\r
632 * This function maps to "DELETE FROM table"
\r
635 * @param string the table name
\r
638 function _truncate($table)
\r
640 return "TRUNCATE TABLE ".$table;
\r
643 // --------------------------------------------------------------------
\r
648 * Generates a platform-specific delete string from the supplied data
\r
651 * @param string the table name
\r
652 * @param array the where clause
\r
653 * @param string the limit clause
\r
656 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
\r
660 if (count($where) > 0 OR count($like) > 0)
\r
662 $conditions = "\nWHERE ";
\r
663 $conditions .= implode("\n", $this->ar_where);
\r
665 if (count($where) > 0 && count($like) > 0)
\r
667 $conditions .= " AND ";
\r
669 $conditions .= implode("\n", $like);
\r
672 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
\r
674 return "DELETE FROM ".$table.$conditions.$limit;
\r
677 // --------------------------------------------------------------------
\r
682 * Generates a platform-specific LIMIT clause
\r
685 * @param string the sql query string
\r
686 * @param integer the number of rows to limit the query to
\r
687 * @param integer the offset value
\r
690 function _limit($sql, $limit, $offset)
\r
692 $limit = $offset + $limit;
\r
693 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
\r
697 $newsql .= " WHERE rnum >= $offset";
\r
700 // remember that we used limits
\r
701 $this->limit_used = TRUE;
\r
706 // --------------------------------------------------------------------
\r
709 * Close DB Connection
\r
715 function _close($conn_id)
\r
717 @ocilogoff($conn_id);
\r
725 /* End of file oci8_driver.php */
\r
726 /* Location: ./system/database/drivers/oci8/oci8_driver.php */