]>
jfr.im git - z_archive/KronOS.git/blob - system/database/drivers/oci8/oci8_driver.php
930177e62b851bf5660cc994ed86dad0ef5f82da
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * oci8 Database Adapter Class
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.
25 * @package CodeIgniter
28 * @author ExpressionEngine Dev Team
29 * @link http://codeigniter.com/user_guide/database/
33 * oci8 Database Adapter Class
35 * This is a modification of the DB_driver class to
36 * permit access to oracle databases
38 * @author Kelly McArdle
42 class CI_DB_oci8_driver
extends CI_DB
{
44 var $dbdriver = 'oci8';
46 // The character used for excaping
47 var $_escape_char = '"';
49 // clause and character used for LIKE escape sequences
50 var $_like_escape_str = " escape '%s' ";
51 var $_like_escape_chr = '!';
54 * The syntax to count rows is slightly different across different
55 * database engines, so this string appears in each driver and is
56 * used for the count_all() and count_all_results() functions.
58 var $_count_string = "SELECT COUNT(1) AS ";
59 var $_random_keyword = ' ASC'; // not currently supported
61 // Set "auto commit" by default
62 var $_commit = OCI_COMMIT_ON_SUCCESS
;
64 // need to track statement id and cursor id
68 // if we use a limit, we will add a field that will
69 // throw off num_fields later
73 * Non-persistent database connection
75 * @access private called by the base class
78 public function db_connect()
80 return @oci_connect($this->username
, $this->password
, $this->hostname
, $this->char_set
);
83 // --------------------------------------------------------------------
86 * Persistent database connection
88 * @access private called by the base class
91 public function db_pconnect()
93 return @oci_pconnect($this->username
, $this->password
, $this->hostname
, $this->char_set
);
96 // --------------------------------------------------------------------
101 * Keep / reestablish the db connection if no queries have been
102 * sent for a length of time exceeding the server's idle timeout
107 public function reconnect()
109 // not implemented in oracle
113 // --------------------------------------------------------------------
116 * Select the database
118 * @access private called by the base class
121 public function db_select()
123 // Not in Oracle - schemas are actually usernames
127 // --------------------------------------------------------------------
130 * Set client character set
137 public function db_set_charset($charset, $collation)
139 // @todo - add support if needed
143 // --------------------------------------------------------------------
146 * Version number query string
151 protected function _version()
153 return oci_server_version($this->conn_id
);
156 // --------------------------------------------------------------------
161 * @access protected called by the base class
162 * @param string an SQL query
165 protected function _execute($sql)
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 oci_set_prefetch($this->stmt_id
, 1000);
172 return @oci_execute($this->stmt_id
, $this->_commit
);
176 * Generate a statement ID
179 * @param string an SQL query
182 private function _set_stmt_id($sql)
184 if ( ! is_resource($this->stmt_id
))
186 $this->stmt_id
= oci_parse($this->conn_id
, $this->_prep_query($sql));
190 // --------------------------------------------------------------------
195 * If needed, each database adapter can prep the query string
197 * @access private called by execute()
198 * @param string an SQL query
201 private function _prep_query($sql)
206 // --------------------------------------------------------------------
209 * getCursor. Returns a cursor from the datbase
214 public function get_cursor()
216 $this->curs_id
= oci_new_cursor($this->conn_id
);
217 return $this->curs_id
;
220 // --------------------------------------------------------------------
223 * Stored Procedure. Executes a stored procedure
226 * @param package package stored procedure is in
227 * @param procedure stored procedure to execute
228 * @param params array of parameters
234 * name no the name of the parameter should be in :<param_name> format
235 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
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
240 public function stored_procedure($package, $procedure, $params)
242 if ($package == '' OR $procedure == '' OR ! is_array($params))
246 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
247 return $this->display_error('db_invalid_query');
252 // build the query string
253 $sql = "begin $package.$procedure(";
255 $have_cursor = FALSE;
256 foreach ($params as $param)
258 $sql .= $param['name'] . ",";
260 if (array_key_exists('type', $param) && ($param['type'] === OCI_B_CURSOR
))
265 $sql = trim($sql, ",") . "); end;";
267 $this->stmt_id
= FALSE;
268 $this->_set_stmt_id($sql);
269 $this->_bind_params($params);
270 $this->query($sql, FALSE, $have_cursor);
273 // --------------------------------------------------------------------
281 private function _bind_params($params)
283 if ( ! is_array($params) OR ! is_resource($this->stmt_id
))
288 foreach ($params as $param)
290 foreach (array('name', 'value', 'type', 'length') as $val)
292 if ( ! isset($param[$val]))
298 oci_bind_by_name($this->stmt_id
, $param['name'], $param['value'], $param['length'], $param['type']);
302 // --------------------------------------------------------------------
310 public function trans_begin($test_mode = FALSE)
312 if ( ! $this->trans_enabled
)
317 // When transactions are nested we only begin/commit/rollback the outermost ones
318 if ($this->_trans_depth
> 0)
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;
328 $this->_commit
= OCI_DEFAULT
;
332 // --------------------------------------------------------------------
340 public function trans_commit()
342 if ( ! $this->trans_enabled
)
347 // When transactions are nested we only begin/commit/rollback the outermost ones
348 if ($this->_trans_depth
> 0)
353 $ret = oci_commit($this->conn_id
);
354 $this->_commit
= OCI_COMMIT_ON_SUCCESS
;
358 // --------------------------------------------------------------------
361 * Rollback Transaction
366 public function trans_rollback()
368 if ( ! $this->trans_enabled
)
373 // When transactions are nested we only begin/commit/rollback the outermost ones
374 if ($this->_trans_depth
> 0)
379 $ret = oci_rollback($this->conn_id
);
380 $this->_commit
= OCI_COMMIT_ON_SUCCESS
;
384 // --------------------------------------------------------------------
391 * @param bool whether or not the string will be used in a LIKE condition
394 public function escape_str($str, $like = FALSE)
398 foreach ($str as $key => $val)
400 $str[$key] = $this->escape_str($val, $like);
406 $str = remove_invisible_characters($str);
408 // escape LIKE condition wildcards
411 $str = str_replace( array('%', '_', $this->_like_escape_chr
),
412 array($this->_like_escape_chr
.'%', $this->_like_escape_chr
.'_', $this->_like_escape_chr
.$this->_like_escape_chr
),
419 // --------------------------------------------------------------------
427 public function affected_rows()
429 return @oci_num_rows($this->stmt_id
);
432 // --------------------------------------------------------------------
440 public function insert_id()
442 // not supported in oracle
443 return $this->display_error('db_unsupported_function');
446 // --------------------------------------------------------------------
451 * Generates a platform-specific query string that counts all records in
452 * the specified database
458 public function count_all($table = '')
465 $query = $this->query($this->_count_string
. $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
472 $row = $query->row();
473 $this->_reset_select();
474 return (int) $row->numrows
;
477 // --------------------------------------------------------------------
482 * Generates a platform-specific query string so that the table names can be fetched
488 protected function _list_tables($prefix_limit = FALSE)
490 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";
492 if ($prefix_limit !== FALSE AND $this->dbprefix
!= '')
494 $sql .= " WHERE TABLE_NAME LIKE '".$this->escape_like_str($this->dbprefix
)."%' ".sprintf($this->_like_escape_str
, $this->_like_escape_chr
);
500 // --------------------------------------------------------------------
505 * Generates a platform-specific query string so that the column names can be fetched
508 * @param string the table name
511 protected function _list_columns($table = '')
513 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
516 // --------------------------------------------------------------------
521 * Generates a platform-specific query so that the column data can be retrieved
524 * @param string the table name
527 protected function _field_data($table)
529 return "SELECT
* FROM
".$table." where rownum
= 1";
532 // --------------------------------------------------------------------
535 * The error message string
540 protected function _error_message()
542 // If the error was during connection, no conn_id should be passed
543 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
544 return $error['message'];
547 // --------------------------------------------------------------------
550 * The error message number
555 protected function _error_number()
557 // Same as _error_message()
558 $error = is_resource($this->conn_id) ? oci_error($this->conn_id) : oci_error();
559 return $error['code'];
562 // --------------------------------------------------------------------
565 * Escape the SQL Identifiers
567 * This function escapes column and table names
573 protected function _escape_identifiers($item)
575 if ($this->_escape_char == '')
580 foreach ($this->_reserved_identifiers as $id)
582 if (strpos($item, '.'.$id) !== FALSE)
584 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
586 // remove duplicates if the user already included the escape
587 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
591 if (strpos($item, '.') !== FALSE)
593 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
597 $str = $this->_escape_char.$item.$this->_escape_char;
600 // remove duplicates if the user already included the escape
601 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
604 // --------------------------------------------------------------------
609 * This function implicitly groups FROM tables so there is no confusion
610 * about operator precedence in harmony with SQL standards
616 protected function _from_tables($tables)
618 if ( ! is_array($tables))
620 $tables = array($tables);
623 return implode(', ', $tables);
626 // --------------------------------------------------------------------
631 * Generates a platform-specific insert string from the supplied data
634 * @param string the table name
635 * @param array the insert keys
636 * @param array the insert values
639 protected function _insert($table, $keys, $values)
641 return "INSERT INTO
".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
644 // --------------------------------------------------------------------
647 * Insert_batch statement
649 * Generates a platform-specific insert string from the supplied data
652 * @param string the table name
653 * @param array the insert keys
654 * @param array the insert values
657 protected function _insert_batch($table, $keys, $values)
659 $keys = implode(', ', $keys);
660 $sql = "INSERT ALL\n
";
662 for ($i = 0, $c = count($values); $i < $c; $i++)
664 $sql .= ' INTO ' . $table . ' (' . $keys . ') VALUES ' . $values[$i] . "\n
";
667 $sql .= 'SELECT * FROM dual';
672 // --------------------------------------------------------------------
677 * Generates a platform-specific update string from the supplied data
680 * @param string the table name
681 * @param array the update data
682 * @param array the where clause
683 * @param array the orderby clause
684 * @param array the limit clause
687 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
689 foreach ($values as $key => $val)
691 $valstr[] = $key." = ".$val;
694 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
696 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
698 $sql = "UPDATE
".$table." SET
".implode(', ', $valstr);
700 $sql .= ($where != '' AND count($where) >=1) ? " WHERE
".implode(" ", $where) : '';
702 $sql .= $orderby.$limit;
707 // --------------------------------------------------------------------
712 * Generates a platform-specific truncate string from the supplied data
713 * If the database does not support the truncate() command
714 * This function maps to "DELETE FROM table
"
717 * @param string the table name
720 protected function _truncate($table)
722 return "TRUNCATE TABLE
".$table;
725 // --------------------------------------------------------------------
730 * Generates a platform-specific delete string from the supplied data
733 * @param string the table name
734 * @param array the where clause
735 * @param string the limit clause
738 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
742 if (count($where) > 0 OR count($like) > 0)
744 $conditions = "\nWHERE
";
745 $conditions .= implode("\n
", $this->ar_where);
747 if (count($where) > 0 && count($like) > 0)
749 $conditions .= " AND ";
751 $conditions .= implode("\n
", $like);
754 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
756 return "DELETE FROM
".$table.$conditions.$limit;
759 // --------------------------------------------------------------------
764 * Generates a platform-specific LIMIT clause
767 * @param string the sql query string
768 * @param integer the number of rows to limit the query to
769 * @param integer the offset value
772 protected function _limit($sql, $limit, $offset)
774 $limit = $offset + $limit;
775 $newsql = "SELECT
* FROM (select inner_query
.*, rownum rnum
FROM ($sql) inner_query WHERE rownum
< $limit)";
779 $newsql .= " WHERE rnum
>= $offset";
782 // remember that we used limits
783 $this->limit_used
= TRUE;
788 // --------------------------------------------------------------------
791 * Close DB Connection
797 protected function _close($conn_id)
799 @oci_close($conn_id);
807 /* End of file oci8_driver.php */
808 /* Location: ./system/database/drivers/oci8/oci8_driver.php */