From 7b613c7000a496fe0e4f9152ee8937d06eba2301 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 24 Sep 2006 18:05:17 +0000 Subject: Adding database folder --- system/database/DB_driver.php | 578 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 578 insertions(+) create mode 100644 system/database/DB_driver.php (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php new file mode 100644 index 000000000..ef37967dc --- /dev/null +++ b/system/database/DB_driver.php @@ -0,0 +1,578 @@ +initialize($params); + log_message('debug', 'Database Driver Class Initialized'); + } + + // -------------------------------------------------------------------- + + /** + * Initialize Database Settings + * + * @access private Called by the constructor + * @param mixed + * @return void + */ + function initialize($params = '') + { + if (is_array($params)) + { + foreach (array('hostname' => '', 'username' => '', 'password' => '', 'database' => '', 'dbdriver' => 'mysql', 'dbprefix' => '', 'port' => '', 'pconnect' => FALSE, 'db_debug' => FALSE) as $key => $val) + { + $this->$key = ( ! isset($params[$key])) ? $val : $params[$key]; + } + } + elseif (strpos($params, '://')) + { + if (FALSE === ($dsn = @parse_url($params))) + { + log_message('error', 'Invalid DB Connection String'); + + if ($this->db_debug) + { + return $this->display_error('db_invalid_connection_str'); + } + return FALSE; + } + + $this->hostname = ( ! isset($dsn['host'])) ? '' : rawurldecode($dsn['host']); + $this->username = ( ! isset($dsn['user'])) ? '' : rawurldecode($dsn['user']); + $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']); + $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); + } + + if ($this->pconnect == FALSE) + { + $this->conn_id = $this->db_connect(); + } + else + { + $this->conn_id = $this->db_pconnect(); + } + + if ( ! $this->conn_id) + { + log_message('error', 'Unable to connect to the database'); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_connect'); + } + } + else + { + if ( ! $this->db_select()) + { + log_message('error', 'Unable to select database: '.$this->database); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_select', $this->database); + } + } + } + } + + + // -------------------------------------------------------------------- + + /** + * Execute the query + * + * Accepts an SQL string as input and returns a result object upon + * successful execution of a "read" type query. Returns boolean TRUE + * upon successful execution of a "write" type query. Returns boolean + * FALSE upon failure, and if the $db_debug variable is set to TRUE + * will raise an error. + * + * @access public + * @param string An SQL query string + * @param array An array of binding data + * @return mixed + */ + function query($sql, $binds = FALSE, $return_object = TRUE) + { + if ($sql == '') + { + if ($this->db_debug) + { + log_message('error', 'Invalid query: '.$sql); + return $this->display_error('db_invalid_query'); + } + return FALSE; + } + + // Compile binds if needed + if ($binds !== FALSE) + { + $sql = $this->compile_binds($sql, $binds); + } + + // Save the query for debugging + $this->queries[] = $sql; + + // Start the Query Timer + $time_start = list($sm, $ss) = explode(' ', microtime()); + + // Run the Query + if (FALSE === ($this->result_id = $this->simple_query($sql))) + { + // This will trigger a rollback if transactions are being used + $this->_trans_failure = TRUE; + + if ($this->db_debug) + { + log_message('error', 'Query error: '.$this->error_message()); + return $this->display_error( + array( + 'Error Number: '.$this->error_number(), + $this->error_message(), + $sql + ) + ); + } + + return FALSE; + } + + // Stop and aggregate the query time results + $time_end = list($em, $es) = explode(' ', microtime()); + $this->benchmark += ($em + $es) - ($sm + $ss); + + // Increment the query counter + $this->query_count++; + + // Was the query a "write" type? + // If so we'll simply return true + if ($this->is_write_type($sql) === TRUE) + { + return TRUE; + } + + // Return TRUE if we don't need to create a result object + // Currently only the Oracle driver uses this when stored + // procedures are used + if ($return_object !== TRUE) + { + return TRUE; + } + + // Define the result driver name + $result = 'CI_DB_'.$this->dbdriver.'_result'; + + // Load the result classes + if ( ! class_exists($result)) + { + include_once(BASEPATH.'database/DB_result'.EXT); + include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); + } + + // Instantiate the result object + $RES = new $result(); + $RES->conn_id = $this->conn_id; + $RES->db_debug = $this->db_debug; + $RES->result_id = $this->result_id; + + if ($this->dbdriver == 'oci8') + { + $RES->stmt_id = $this->stmt_id; + $RES->curs_id = NULL; + $RES->limit_used = $this->limit_used; + } + + return $RES; + } + + // -------------------------------------------------------------------- + + /** + * Simple Query + * This is a simiplified version of the query() function. Internally + * we only use it when running transaction commands since they do + * not require all the features of the main query() function. + * + * @access public + * @param string the sql query + * @return mixed + */ + function simple_query($sql) + { + if ( ! $this->conn_id) + { + $this->initialize(); + } + + return $this->_execute($sql, $this->conn_id); + } + + // -------------------------------------------------------------------- + + /** + * Disable Transactions + * This permits transactions to be disabled at run-time. + * + * @access public + * @return void + */ + function trans_off() + { + $this->trans_enabled = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Start Transaction + * + * @access public + * @return void + */ + function trans_start($test_mode = FALSE) + { + if ( ! $this->trans_enabled) + { + return FALSE; + } + + // When transactions are nested we only begin/commit/rollback the outermost ones + if ($this->_trans_depth > 0) + { + $this->_trans_depth += 1; + return; + } + + $this->trans_begin($test_mode); + } + + // -------------------------------------------------------------------- + + /** + * Complete Transaction + * + * @access public + * @return bool + */ + function trans_complete() + { + if ( ! $this->trans_enabled) + { + return FALSE; + } + + // When transactions are nested we only begin/commit/rollback the outermost ones + if ($this->_trans_depth > 1) + { + $this->_trans_depth -= 1; + return TRUE; + } + + // The query() function will set this flag to TRUE in the event that a query failed + if ($this->_trans_failure === TRUE) + { + $this->trans_rollback(); + + if ($this->db_debug) + { + return $this->display_error('db_transaction_failure'); + } + return FALSE; + } + + $this->trans_commit(); + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Lets you retrieve the transaction flag to determine if it has failed + * + * @access public + * @return bool + */ + function trans_status() + { + return $this->_trans_failure; + } + + // -------------------------------------------------------------------- + + /** + * Compile Bindings + * + * @access public + * @param string the sql statement + * @param array an array of bind data + * @return string + */ + function compile_binds($sql, $binds) + { + if (FALSE === strpos($sql, $this->bind_marker)) + { + return $sql; + } + + if ( ! is_array($binds)) + { + $binds = array($binds); + } + + foreach ($binds as $val) + { + $val = $this->escape($val); + + // Just in case the replacement string contains the bind + // character we'll temporarily replace it with a marker + $val = str_replace($this->bind_marker, '{%bind_marker%}', $val); + $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1); + } + + return str_replace('{%bind_marker%}', $this->bind_marker, $sql); + } + + // -------------------------------------------------------------------- + + /** + * Determines if a query is a "write" type. + * + * @access public + * @param string An SQL query string + * @return boolean + */ + function is_write_type($sql) + { + if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) + { + return FALSE; + } + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Calculate the aggregate query elapsed time + * + * @access public + * @param intiger The number of decimal places + * @return integer + */ + function elapsed_time($decimals = 6) + { + return number_format($this->benchmark, $decimals); + } + + // -------------------------------------------------------------------- + + /** + * Returns the total number of queries + * + * @access public + * @return integer + */ + function total_queries() + { + return $this->query_count; + } + + // -------------------------------------------------------------------- + + /** + * Returns the last query that was executed + * + * @access public + * @return void + */ + function last_query() + { + return end($this->queries); + } + + // -------------------------------------------------------------------- + + /** + * "Smart" Escape String + * + * Escapes data based on type + * Sets boolean and null types + * + * @access public + * @param string + * @return integer + */ + function escape($str) + { + switch (gettype($str)) + { + case 'string' : $str = "'".$this->escape_str($str)."'"; + break; + case 'boolean' : $str = ($str === FALSE) ? 0 : 1; + break; + default : $str = ($str === NULL) ? 'NULL' : $str; + break; + } + + return $str; + } + + // -------------------------------------------------------------------- + + /** + * Enables a native PHP function to be run, using a platform agnostic wrapper. + * + * @access public + * @param string the function name + * @param mixed any parameters needed by the function + * @return mixed + */ + function call_function($function) + { + $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; + + if (FALSE === strpos($driver, $function)) + { + $function = $driver.$function; + } + + if ( ! function_exists($function)) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + else + { + $args = (func_num_args() > 1) ? array_shift(func_get_args()) : null; + + return call_user_func_array($function, $args); + } + } + + // -------------------------------------------------------------------- + + /** + * Close DB Connection + * + * @access public + * @return void + */ + function close() + { + if (is_resource($this->conn_id)) + { + $this->_close($this->conn_id); + } + $this->conn_id = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Display an error message + * + * @access public + * @param string the error message + * @param string any "swap" values + * @param boolean whether to localize the message + * @return string sends the application/errror_db.php template + */ + function display_error($error = '', $swap = '', $native = FALSE) + { + $LANG = new CI_Language(); + $LANG->load('db'); + + $heading = 'MySQL Error'; + + if ($native == TRUE) + { + $message = $error; + } + else + { + $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; + } + + if ( ! class_exists('CI_Exceptions')) + { + include_once(BASEPATH.'libraries/Exceptions'.EXT); + } + + $error = new CI_Exceptions(); + echo $error->show_error('An Error Was Encountered', $message, 'error_db'); + exit; + + } + +} + +?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From bb1d439a981023415569549d8b8c4987fa5b9b51 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 24 Sep 2006 20:14:38 +0000 Subject: --- system/database/DB_driver.php | 82 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 4 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index ef37967dc..3f7c82627 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -190,11 +190,11 @@ class CI_DB_driver { if ($this->db_debug) { - log_message('error', 'Query error: '.$this->error_message()); + log_message('error', 'Query error: '.$this->_error_message()); return $this->display_error( array( - 'Error Number: '.$this->error_number(), - $this->error_message(), + 'Error Number: '.$this->_error_number(), + $this->_error_message(), $sql ) ); @@ -481,7 +481,81 @@ class CI_DB_driver { } return $str; - } + } + + // -------------------------------------------------------------------- + + /** + * Generate an insert string + * + * @access public + * @param string the table upon which the query will be performed + * @param array an associative array data of key/values + * @return string + */ + function insert_string($table, $data) + { + $fields = array(); + $values = array(); + + foreach($data as $key => $val) + { + $fields[] = $key; + $values[] = $this->escape($val); + } + + return $this->_insert($this->dbprefix.$table, $fields, $values); + } + + // -------------------------------------------------------------------- + + /** + * Generate an update string + * + * @access public + * @param string the table upon which the query will be performed + * @param array an associative array data of key/values + * @param mixed the "where" statement + * @return string + */ + function update_string($table, $data, $where) + { + if ($where == '') + return false; + + $fields = array(); + foreach($data as $key => $val) + { + $fields[$key] = $this->escape($val); + } + + if ( ! is_array($where)) + { + $dest = array($where); + } + else + { + $dest = array(); + foreach ($where as $key => $val) + { + $prefix = (count($dest) == 0) ? '' : ' AND '; + + if ($val != '') + { + if ( ! $this->_has_operator($key)) + { + $key .= ' ='; + } + + $val = ' '.$this->escape($val); + } + + $dest[] = $prefix.$key.$val; + } + } + + return $this->_update($this->dbprefix.$table, $fields, $dest); + } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 9cd4e8e639a1a09fd6ca426f1af94586f30d4a80 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 25 Sep 2006 23:26:25 +0000 Subject: --- system/database/DB_driver.php | 140 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 3f7c82627..d25135ea6 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -142,6 +142,49 @@ class CI_DB_driver { } + // -------------------------------------------------------------------- + + /** + * The name of the platform in use (mysql, mssql, etc...) + * + * @access public + * @return string + */ + function platform() + { + return $this->dbdriver; + } + + // -------------------------------------------------------------------- + + /** + * Database Version Number. Returns a string containing the + * version of the database being used + * + * @access public + * @return string + */ + function version() + { + if (FALSE === ($sql = $this->_version())) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + if ($this->dbdriver == 'oci8') + { + return $sql; + } + + $query = $this->query($sql); + $row = $query->row(); + return $row->ver; + } + // -------------------------------------------------------------------- /** @@ -483,6 +526,103 @@ class CI_DB_driver { return $str; } + + + // -------------------------------------------------------------------- + + /** + * Primary + * + * Retrieves the primary key. It assumes that the row in the first + * position is the primary key + * + * @access public + * @param string the table name + * @return string + */ + function primary($table = '') + { + $fields = $this->field_names($table); + + if ( ! is_array($fields)) + { + return FALSE; + } + + return current($fields); + } + + // -------------------------------------------------------------------- + + /** + * Fetch MySQL Field Names + * + * @access public + * @param string the table name + * @return array + */ + function field_names($table = '') + { + if ($table == '') + { + if ($this->db_debug) + { + return $this->display_error('db_field_param_missing'); + } + return FALSE; + } + + if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table))) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + $query = $this->query($sql); + + $retval = array(); + foreach($query->result_array() as $row) + { + if (isset($row['COLUMN_NAME'])) + { + $retval[] = $row['COLUMN_NAME']; + } + else + { + $retval[] = current($row); + } + } + + return $retval; + } + + // -------------------------------------------------------------------- + + /** + * Returns an object with field data + * + * @access public + * @param string the table name + * @return object + */ + function field_data($table = '') + { + if ($table == '') + { + if ($this->db_debug) + { + return $this->display_error('db_field_param_missing'); + } + return FALSE; + } + + $query = $this->query($this->_field_data($this->dbprefix.$table)); + return $query->field_data(); + } + // -------------------------------------------------------------------- /** -- cgit v1.2.3-24-g4f1b From ee54c112bfb488f833fa032758b817e1bb2c1d7d Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 28 Sep 2006 17:13:38 +0000 Subject: --- system/database/DB_driver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index d25135ea6..94db84bbf 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -726,8 +726,8 @@ class CI_DB_driver { } else { - $args = (func_num_args() > 1) ? array_shift(func_get_args()) : null; - + $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; + return call_user_func_array($function, $args); } } -- cgit v1.2.3-24-g4f1b From 3ed8c51254a5b26d951fa675802fcf69adf9638e Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 29 Sep 2006 23:26:28 +0000 Subject: --- system/database/DB_driver.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 94db84bbf..275d51c53 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -45,6 +45,7 @@ class CI_DB_driver { var $query_count = 0; var $bind_marker = '?'; var $queries = array(); + var $cache = array(); var $trans_enabled = TRUE; var $_trans_depth = 0; var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur @@ -563,6 +564,12 @@ class CI_DB_driver { */ function field_names($table = '') { + // Is there a cached result? + if (isset($this->cache['field_names'][$table])) + { + return $this->cache['field_names'][$table]; + } + if ($table == '') { if ($this->db_debug) @@ -596,7 +603,7 @@ class CI_DB_driver { } } - return $retval; + return $this->cache['field_names'][$table] =& $retval; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 3dd978f680076be842bfcb5c9e2cbf35b926373b Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 30 Sep 2006 19:24:45 +0000 Subject: --- system/database/DB_driver.php | 101 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 275d51c53..5102cc74c 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -555,6 +555,95 @@ class CI_DB_driver { // -------------------------------------------------------------------- + // -------------------------------------------------------------------- + + /** + * List databases + * + * @access public + * @return bool + */ + function list_databases() + { + // Is there a cached result? + if (isset($this->cache['db_names'])) + { + return $this->cache['db_names']; + } + + $query = $this->query($this->_list_database()); + $dbs = array(); + if ($query->num_rows() > 0) + { + foreach ($query->result_array() as $row) + { + $dbs[] = current($row); + } + } + + return $this->cache['db_names'] =& $dbs; + } + + // -------------------------------------------------------------------- + + /** + * Returns an array of table names + * + * @access public + * @return array + */ + function list_tables() + { + // Is there a cached result? + if (isset($this->cache['table_names'])) + { + return $this->cache['table_names']; + } + + if (FALSE === ($sql = $this->_list_tables())) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + $retval = array(); + $query = $this->query($sql); + + if ($query->num_rows() > 0) + { + foreach($query->result_array() as $row) + { + if (isset($row['TABLE_NAME'])) + { + $retval[] = $row['TABLE_NAME']; + } + else + { + $retval[] = array_shift($row); + } + } + } + + return $this->cache['table_names'] =& $retval; + } + + // -------------------------------------------------------------------- + + /** + * Determine if a particular table exists + * @access public + * @return boolean + */ + function table_exists($table_name) + { + return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; + } + + // -------------------------------------------------------------------- + /** * Fetch MySQL Field Names * @@ -562,7 +651,7 @@ class CI_DB_driver { * @param string the table name * @return array */ - function field_names($table = '') + function list_fields($table = '') { // Is there a cached result? if (isset($this->cache['field_names'][$table])) @@ -605,6 +694,16 @@ class CI_DB_driver { return $this->cache['field_names'][$table] =& $retval; } + + // -------------------------------------------------------------------- + + /** + * DEPRECATED - use list_fields() + */ + function field_names($table = '') + { + return $this->list_fields($table); + } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From b2a9ceccdb85050cb494e6d0a98b0a49495d29bb Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 1 Oct 2006 03:38:04 +0000 Subject: --- system/database/DB_driver.php | 45 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 5102cc74c..81af466ef 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -555,37 +555,6 @@ class CI_DB_driver { // -------------------------------------------------------------------- - // -------------------------------------------------------------------- - - /** - * List databases - * - * @access public - * @return bool - */ - function list_databases() - { - // Is there a cached result? - if (isset($this->cache['db_names'])) - { - return $this->cache['db_names']; - } - - $query = $this->query($this->_list_database()); - $dbs = array(); - if ($query->num_rows() > 0) - { - foreach ($query->result_array() as $row) - { - $dbs[] = current($row); - } - } - - return $this->cache['db_names'] =& $dbs; - } - - // -------------------------------------------------------------------- - /** * Returns an array of table names * @@ -694,6 +663,20 @@ class CI_DB_driver { return $this->cache['field_names'][$table] =& $retval; } + + // -------------------------------------------------------------------- + + /** + * Determine if a particular field exists + * @access public + * @param string + * @param string + * @return boolean + */ + function field_exists($field_name, $table_name) + { + return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; + } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From e8f6eb62a0600daf5b192746e1eed3e81516bc92 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 2 Oct 2006 06:46:16 +0000 Subject: --- system/database/DB_driver.php | 107 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 8 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 81af466ef..73c723d96 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -41,11 +41,13 @@ class CI_DB_driver { var $conn_id = FALSE; var $result_id = FALSE; var $db_debug = FALSE; + var $query_caching = FALSE; + var $cache_dir = ''; var $benchmark = 0; var $query_count = 0; var $bind_marker = '?'; var $queries = array(); - var $cache = array(); + var $data_cache = array(); var $trans_enabled = TRUE; var $_trans_depth = 0; var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur @@ -55,6 +57,7 @@ class CI_DB_driver { var $curs_id; var $limit_used; + /** * Constructor. Accepts one parameter containing the database @@ -214,6 +217,21 @@ class CI_DB_driver { return FALSE; } + // Is query caching enabled? If the query is a "read type" we'll + // grab the previously cached query if it exists and return it. + if ($this->query_caching == TRUE) + { + if (stristr($sql, 'SELECT')) + { + $CACHE =& _load_cache_class(); + + if (FALSE !== ($CACHE->cache_exists($sql))) + { + return $CACHE->get_cache($sql); + } + } + } + // Compile binds if needed if ($binds !== FALSE) { @@ -257,7 +275,7 @@ class CI_DB_driver { // Was the query a "write" type? // If so we'll simply return true if ($this->is_write_type($sql) === TRUE) - { + { return TRUE; } @@ -564,9 +582,9 @@ class CI_DB_driver { function list_tables() { // Is there a cached result? - if (isset($this->cache['table_names'])) + if (isset($this->data_cache['table_names'])) { - return $this->cache['table_names']; + return $this->data_cache['table_names']; } if (FALSE === ($sql = $this->_list_tables())) @@ -596,7 +614,7 @@ class CI_DB_driver { } } - return $this->cache['table_names'] =& $retval; + return $this->data_cache['table_names'] =& $retval; } // -------------------------------------------------------------------- @@ -623,9 +641,9 @@ class CI_DB_driver { function list_fields($table = '') { // Is there a cached result? - if (isset($this->cache['field_names'][$table])) + if (isset($this->data_cache['field_names'][$table])) { - return $this->cache['field_names'][$table]; + return $this->data_cache['field_names'][$table]; } if ($table == '') @@ -661,7 +679,7 @@ class CI_DB_driver { } } - return $this->cache['field_names'][$table] =& $retval; + return $this->data_cache['field_names'][$table] =& $retval; } // -------------------------------------------------------------------- @@ -820,6 +838,79 @@ class CI_DB_driver { return call_user_func_array($function, $args); } } + + // -------------------------------------------------------------------- + + /** + * Set Cache Path + * + * @access public + * @param string the path to the cache directory + * @return void + */ + function set_cache_path($path = '') + { + if ( ! is_dir($path) OR ! is_writable($path)) + { + if ($this->db_debug) + { + return $this->display_error('db_invalid_cache_path'); + } + + $this->enable_caching(FALSE); + return FALSE; + } + + $this->cache_dir = $path; + } + + // -------------------------------------------------------------------- + + /** + * Enable Query Caching + * + * @access public + * @return void + */ + function cache_on() + { + $this->query_caching = TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Disable Query Caching + * + * @access public + * @return void + */ + function cache_off() + { + $this->query_caching = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Load Caching Class + * + * @access private + * @return object + */ + function _load_cache_class() + { + static $CACHE = NULL; + + if (is_object($CACHE)) + { + return $CACHE; + } + + require_once BASEPATH.'database/DB_cache'.EXT; + $CACHE = new DB_cache(); + return $CACHE; + } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 6871d95930a148722f2b99fa80ef6dd44f86f078 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 4 Oct 2006 00:36:18 +0000 Subject: --- system/database/DB_driver.php | 390 ++++++++++++++++++++++++++---------------- 1 file changed, 246 insertions(+), 144 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 73c723d96..811f09093 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -41,8 +41,6 @@ class CI_DB_driver { var $conn_id = FALSE; var $result_id = FALSE; var $db_debug = FALSE; - var $query_caching = FALSE; - var $cache_dir = ''; var $benchmark = 0; var $query_count = 0; var $bind_marker = '?'; @@ -51,11 +49,14 @@ class CI_DB_driver { var $trans_enabled = TRUE; var $_trans_depth = 0; var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur + var $cache_on = FALSE; + var $cachedir = ''; - // These are use with Oracle - var $stmt_id; - var $curs_id; - var $limit_used; + + // These are use with Oracle + var $stmt_id; + var $curs_id; + var $limit_used; @@ -86,10 +87,24 @@ class CI_DB_driver { * @return void */ function initialize($params = '') - { + { if (is_array($params)) { - foreach (array('hostname' => '', 'username' => '', 'password' => '', 'database' => '', 'dbdriver' => 'mysql', 'dbprefix' => '', 'port' => '', 'pconnect' => FALSE, 'db_debug' => FALSE) as $key => $val) + $defaults = array( + 'hostname' => '', + 'username' => '', + 'password' => '', + 'database' => '', + 'dbdriver' => 'mysql', + 'dbprefix' => '', + 'port' => '', + 'pconnect' => FALSE, + 'db_debug' => FALSE, + 'cachedir' => '', + 'cache_on' => FALSE + ); + + foreach ($defaults as $key => $val) { $this->$key = ( ! isset($params[$key])) ? $val : $params[$key]; } @@ -112,25 +127,19 @@ class CI_DB_driver { $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']); $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); } - - if ($this->pconnect == FALSE) - { - $this->conn_id = $this->db_connect(); - } - else - { - $this->conn_id = $this->db_pconnect(); - } - - if ( ! $this->conn_id) - { + + // Connect to the database + $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); + + if ( ! $this->conn_id) + { log_message('error', 'Unable to connect to the database'); - if ($this->db_debug) - { + if ($this->db_debug) + { $this->display_error('db_unable_to_connect'); - } - } + } + } else { if ( ! $this->db_select()) @@ -142,7 +151,13 @@ class CI_DB_driver { $this->display_error('db_unable_to_select', $this->database); } } - } + } + + // Is there a cache direcotry specified in the config file? + if ($this->cachedir != '') + { + $this->cache_set_dir($this->cachedir); + } } @@ -172,15 +187,15 @@ class CI_DB_driver { { if (FALSE === ($sql = $this->_version())) { - if ($this->db_debug) - { + if ($this->db_debug) + { return $this->display_error('db_unsupported_function'); - } - return FALSE; + } + return FALSE; } - if ($this->dbdriver == 'oci8') - { + if ($this->dbdriver == 'oci8') + { return $sql; } @@ -205,30 +220,25 @@ class CI_DB_driver { * @param array An array of binding data * @return mixed */ - function query($sql, $binds = FALSE, $return_object = TRUE) - { + function query($sql, $binds = FALSE, $return_object = TRUE) + { if ($sql == '') { - if ($this->db_debug) - { + if ($this->db_debug) + { log_message('error', 'Invalid query: '.$sql); return $this->display_error('db_invalid_query'); - } - return FALSE; + } + return FALSE; } - // Is query caching enabled? If the query is a "read type" we'll + // Is query caching enabled? If the query is a "read type" we will // grab the previously cached query if it exists and return it. - if ($this->query_caching == TRUE) - { - if (stristr($sql, 'SELECT')) + if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) + { + if (FALSE !== ($cache = $this->cache_read($sql))) { - $CACHE =& _load_cache_class(); - - if (FALSE !== ($CACHE->cache_exists($sql))) - { - return $CACHE->get_cache($sql); - } + return $cache; } } @@ -238,20 +248,20 @@ class CI_DB_driver { $sql = $this->compile_binds($sql, $binds); } - // Save the query for debugging - $this->queries[] = $sql; + // Save the query for debugging + $this->queries[] = $sql; // Start the Query Timer - $time_start = list($sm, $ss) = explode(' ', microtime()); - + $time_start = list($sm, $ss) = explode(' ', microtime()); + // Run the Query - if (FALSE === ($this->result_id = $this->simple_query($sql))) - { - // This will trigger a rollback if transactions are being used - $this->_trans_failure = TRUE; - - if ($this->db_debug) - { + if (FALSE === ($this->result_id = $this->simple_query($sql))) + { + // This will trigger a rollback if transactions are being used + $this->_trans_failure = TRUE; + + if ($this->db_debug) + { log_message('error', 'Query error: '.$this->_error_message()); return $this->display_error( array( @@ -260,18 +270,18 @@ class CI_DB_driver { $sql ) ); - } - - return FALSE; - } - + } + + return FALSE; + } + // Stop and aggregate the query time results $time_end = list($em, $es) = explode(' ', microtime()); $this->benchmark += ($em + $es) - ($sm + $ss); // Increment the query counter - $this->query_count++; - + $this->query_count++; + // Was the query a "write" type? // If so we'll simply return true if ($this->is_write_type($sql) === TRUE) @@ -298,17 +308,17 @@ class CI_DB_driver { } // Instantiate the result object - $RES = new $result(); - $RES->conn_id = $this->conn_id; - $RES->db_debug = $this->db_debug; - $RES->result_id = $this->result_id; - - if ($this->dbdriver == 'oci8') - { + $RES = new $result(); + $RES->conn_id = $this->conn_id; + $RES->db_debug = $this->db_debug; + $RES->result_id = $this->result_id; + + if ($this->dbdriver == 'oci8') + { $RES->stmt_id = $this->stmt_id; $RES->curs_id = NULL; $RES->limit_used = $this->limit_used; - } + } return $RES; } @@ -589,11 +599,11 @@ class CI_DB_driver { if (FALSE === ($sql = $this->_list_tables())) { - if ($this->db_debug) - { + if ($this->db_debug) + { return $this->display_error('db_unsupported_function'); - } - return FALSE; + } + return FALSE; } $retval = array(); @@ -638,35 +648,35 @@ class CI_DB_driver { * @param string the table name * @return array */ - function list_fields($table = '') - { + function list_fields($table = '') + { // Is there a cached result? if (isset($this->data_cache['field_names'][$table])) { return $this->data_cache['field_names'][$table]; } - - if ($table == '') - { + + if ($table == '') + { if ($this->db_debug) { return $this->display_error('db_field_param_missing'); } return FALSE; - } - + } + if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table))) { - if ($this->db_debug) - { + if ($this->db_debug) + { return $this->display_error('db_unsupported_function'); - } - return FALSE; + } + return FALSE; } - - $query = $this->query($sql); - - $retval = array(); + + $query = $this->query($sql); + + $retval = array(); foreach($query->result_array() as $row) { if (isset($row['COLUMN_NAME'])) @@ -676,11 +686,11 @@ class CI_DB_driver { else { $retval[] = current($row); - } + } } - + return $this->data_cache['field_names'][$table] =& $retval; - } + } // -------------------------------------------------------------------- @@ -695,16 +705,16 @@ class CI_DB_driver { { return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; } - + // -------------------------------------------------------------------- /** * DEPRECATED - use list_fields() */ - function field_names($table = '') - { - return $this->list_fields($table); - } + function field_names($table = '') + { + return $this->list_fields($table); + } // -------------------------------------------------------------------- @@ -717,15 +727,15 @@ class CI_DB_driver { */ function field_data($table = '') { - if ($table == '') - { + if ($table == '') + { if ($this->db_debug) { return $this->display_error('db_field_param_missing'); } return FALSE; - } - + } + $query = $this->query($this->_field_data($this->dbprefix.$table)); return $query->field_data(); } @@ -742,7 +752,7 @@ class CI_DB_driver { */ function insert_string($table, $data) { - $fields = array(); + $fields = array(); $values = array(); foreach($data as $key => $val) @@ -802,7 +812,7 @@ class CI_DB_driver { } return $this->_update($this->dbprefix.$table, $fields, $dest); - } + } // -------------------------------------------------------------------- @@ -838,18 +848,47 @@ class CI_DB_driver { return call_user_func_array($function, $args); } } + + // -------------------------------------------------------------------- + + /** + * Enable Query Caching + * + * @access public + * @return void + */ + function cache_on() + { + $this->query_caching = TRUE; + } // -------------------------------------------------------------------- /** - * Set Cache Path + * Disable Query Caching + * + * @access public + * @return void + */ + function cache_off() + { + $this->query_caching = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Set Cache Directory Path * * @access public * @param string the path to the cache directory * @return void */ - function set_cache_path($path = '') + function cache_set_dir($path = '') { + // Add a trailing slash to the path if needed + $path = preg_replace("/(.+?)\/*$/", "\\1/", $path); + if ( ! is_dir($path) OR ! is_writable($path)) { if ($this->db_debug) @@ -860,58 +899,121 @@ class CI_DB_driver { $this->enable_caching(FALSE); return FALSE; } - + $this->cache_dir = $path; } // -------------------------------------------------------------------- /** - * Enable Query Caching + * Set Cache Path * * @access public + * @param string the path to the cache directory * @return void - */ - function cache_on() + */ + function cache_set_path($sql) { - $this->query_caching = TRUE; + $obj =& get_instance(); + + // The URI being requested will become the name of the cache sub-folder + + $uri = ($obj->uri->uri_string() == '') ? 'index' : $obj->uri->uri_string(); + + // Convert the SQL query into a hash. This will become the cache file name. + + return md5($uri).'/'.md5($sql); } - + // -------------------------------------------------------------------- /** - * Disable Query Caching + * Retreive a cached query * * @access public - * @return void - */ - function cache_off() - { - $this->query_caching = FALSE; - } + * @return string + */ + function cache_read($sql) + { + if ( ! @is_dir($this->cache_dir)) + { + return $this->cache_on = FALSE; + } + + $filepath = $this->cache_set_path($sql); + + if ( ! @file_exists($this->cache_dir.$filepath)) + { + return FALSE; + } + + if ( ! $fp = @fopen($this->cache_dir.$filepath, 'rb')) + { + return FALSE; + } + + $cachedata = file_get_contents($this->cache_dir.$filepath); + + if ( ! is_string($cachedata)) + { + return FALSE; + } + + return unserialize($cachedata); + } // -------------------------------------------------------------------- /** - * Load Caching Class - * - * @access private - * @return object - */ - function _load_cache_class() + * Write a query to a cache file + * + * @access public + * @return bool + */ + function cache_write($sql, $object) { - static $CACHE = NULL; - - if (is_object($CACHE)) + if ( ! @is_dir($this->cache_dir)) { - return $CACHE; + return $this->cache_on = FALSE; } - require_once BASEPATH.'database/DB_cache'.EXT; - $CACHE = new DB_cache(); - return $CACHE; - } + $filepath = $this->cache_set_path($sql); + + + + + + $dirs = array(PATH_CACHE.'db_cache', substr($this->cache_dir, 0, -1)); + + foreach ($dirs as $dir) + { + if ( ! @is_dir($dir)) + { + if ( ! @mkdir($dir, 0777)) + { + return; + } + + @chmod($dir, 0777); + } + } + + if ( ! $fp = @fopen($this->cache_dir.$this->cache_file, 'wb')) + { + return FALSE; + } + + flock($fp, LOCK_EX); + fwrite($fp, $object); + flock($fp, LOCK_UN); + fclose($fp); + + @chmod($this->cache_dir.$this->cache_file, 0777); + + return TRUE; + } + // -------------------------------------------------------------------- /** @@ -920,14 +1022,14 @@ class CI_DB_driver { * @access public * @return void */ - function close() - { - if (is_resource($this->conn_id)) - { - $this->_close($this->conn_id); + function close() + { + if (is_resource($this->conn_id)) + { + $this->_close($this->conn_id); } $this->conn_id = FALSE; - } + } // -------------------------------------------------------------------- @@ -940,8 +1042,8 @@ class CI_DB_driver { * @param boolean whether to localize the message * @return string sends the application/errror_db.php template */ - function display_error($error = '', $swap = '', $native = FALSE) - { + function display_error($error = '', $swap = '', $native = FALSE) + { $LANG = new CI_Language(); $LANG->load('db'); @@ -965,7 +1067,7 @@ class CI_DB_driver { echo $error->show_error('An Error Was Encountered', $message, 'error_db'); exit; - } + } } -- cgit v1.2.3-24-g4f1b From db8a3744e2c40b57aa8d128e0293d098021c69b1 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 4 Oct 2006 07:43:35 +0000 Subject: --- system/database/DB_driver.php | 213 +++++++++++++++++++++++++----------------- 1 file changed, 125 insertions(+), 88 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 811f09093..a7a89a953 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -156,7 +156,7 @@ class CI_DB_driver { // Is there a cache direcotry specified in the config file? if ($this->cachedir != '') { - $this->cache_set_dir($this->cachedir); + $this->cache_set_path($this->cachedir); } } @@ -221,7 +221,7 @@ class CI_DB_driver { * @return mixed */ function query($sql, $binds = FALSE, $return_object = TRUE) - { + { if ($sql == '') { if ($this->db_debug) @@ -235,7 +235,7 @@ class CI_DB_driver { // Is query caching enabled? If the query is a "read type" we will // grab the previously cached query if it exists and return it. if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) - { + { if (FALSE !== ($cache = $this->cache_read($sql))) { return $cache; @@ -285,7 +285,15 @@ class CI_DB_driver { // Was the query a "write" type? // If so we'll simply return true if ($this->is_write_type($sql) === TRUE) - { + { + // If caching is enabled we'll auto-cleanup any + // existing files related to this particular URI + + if ($this->cache_on == TRUE) + { + $this->cache_delete(); + } + return TRUE; } @@ -296,19 +304,12 @@ class CI_DB_driver { { return TRUE; } - - // Define the result driver name - $result = 'CI_DB_'.$this->dbdriver.'_result'; - - // Load the result classes - if ( ! class_exists($result)) - { - include_once(BASEPATH.'database/DB_result'.EXT); - include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); - } + + // Load and instantiate the result driver - // Instantiate the result object - $RES = new $result(); + $driver = $this->load_rdriver(); + + $RES = new $driver(); $RES->conn_id = $this->conn_id; $RES->db_debug = $this->db_debug; $RES->result_id = $this->result_id; @@ -319,9 +320,37 @@ class CI_DB_driver { $RES->curs_id = NULL; $RES->limit_used = $this->limit_used; } - + + // Is query caching enabled? If so, we'll serialize the + // result object and save it to a cache file + if ($this->cache_on == TRUE) + { + $this->cache_write($sql, $RES); + } + return $RES; } + + // -------------------------------------------------------------------- + + /** + * Load the result drivers + * + * @access public + * @return string the name of the result class + */ + function load_rdriver() + { + $driver = 'CI_DB_'.$this->dbdriver.'_result'; + + if ( ! class_exists($driver)) + { + include_once(BASEPATH.'database/DB_result'.EXT); + include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); + } + + return $driver; + } // -------------------------------------------------------------------- @@ -859,7 +888,7 @@ class CI_DB_driver { */ function cache_on() { - $this->query_caching = TRUE; + return $this->query_caching = TRUE; } // -------------------------------------------------------------------- @@ -872,7 +901,7 @@ class CI_DB_driver { */ function cache_off() { - $this->query_caching = FALSE; + return $this->query_caching = FALSE; } // -------------------------------------------------------------------- @@ -882,10 +911,15 @@ class CI_DB_driver { * * @access public * @param string the path to the cache directory - * @return void + * @return bool */ - function cache_set_dir($path = '') + function cache_set_path($path = '') { + if ($path == '') + { + return $this->cache_off(); + } + // Add a trailing slash to the path if needed $path = preg_replace("/(.+?)\/*$/", "\\1/", $path); @@ -896,33 +930,12 @@ class CI_DB_driver { return $this->display_error('db_invalid_cache_path'); } - $this->enable_caching(FALSE); - return FALSE; + // If the path is wrong we'll turn off caching + return $this->cache_off(); } $this->cache_dir = $path; - } - - // -------------------------------------------------------------------- - - /** - * Set Cache Path - * - * @access public - * @param string the path to the cache directory - * @return void - */ - function cache_set_path($sql) - { - $obj =& get_instance(); - - // The URI being requested will become the name of the cache sub-folder - - $uri = ($obj->uri->uri_string() == '') ? 'index' : $obj->uri->uri_string(); - - // Convert the SQL query into a hash. This will become the cache file name. - - return md5($uri).'/'.md5($sql); + return TRUE; } // -------------------------------------------------------------------- @@ -930,31 +943,27 @@ class CI_DB_driver { /** * Retreive a cached query * + * The URI being requested will become the name of the cache sub-folder. + * An MD5 hash of the SQL statement will become the cache file name + * * @access public * @return string */ function cache_read($sql) - { - if ( ! @is_dir($this->cache_dir)) + { + if ( ! $this->cache_set_path($this->cache_dir)) { - return $this->cache_on = FALSE; + return $this->cache_off(); } - $filepath = $this->cache_set_path($sql); - - if ( ! @file_exists($this->cache_dir.$filepath)) - { - return FALSE; - } + $obj =& get_instance(); + $uri = ($obj->uri->segment(1) == FALSE) ? 'base' : $obj->uri->segment(2); + $uri .= ($obj->uri->segment(2) == FALSE) ? 'index' : $obj->uri->segment(2); - if ( ! $fp = @fopen($this->cache_dir.$filepath, 'rb')) - { - return FALSE; - } - - $cachedata = file_get_contents($this->cache_dir.$filepath); + $filepath = md5($uri).'/'.md5($sql); - if ( ! is_string($cachedata)) + $obj->load->helper('file'); + if (FALSE === ($cachedata = read_file($this->cache_dir.$filepath))) { return FALSE; } @@ -972,46 +981,74 @@ class CI_DB_driver { */ function cache_write($sql, $object) { - if ( ! @is_dir($this->cache_dir)) + if ( ! $this->cache_set_path($this->cache_dir)) { - return $this->cache_on = FALSE; + return $this->cache_off(); } - - $filepath = $this->cache_set_path($sql); - - - + + $obj =& get_instance(); + $uri = ($obj->uri->segment(1) == FALSE) ? 'base' : $obj->uri->segment(2); + $uri .= ($obj->uri->segment(2) == FALSE) ? 'index' : $obj->uri->segment(2); - - - $dirs = array(PATH_CACHE.'db_cache', substr($this->cache_dir, 0, -1)); + $dir_path = $this->cache_dir.md5($uri).'/'; - foreach ($dirs as $dir) - { - if ( ! @is_dir($dir)) + $filename = md5($sql); + + if ( ! @is_dir($dir_path)) + { + if ( ! @mkdir($dir_path, 0777)) { - if ( ! @mkdir($dir, 0777)) - { - return; - } - - @chmod($dir, 0777); + return FALSE; } + + @chmod($dir_path, 0777); } - - if ( ! $fp = @fopen($this->cache_dir.$this->cache_file, 'wb')) + + $obj->load->helper('file'); + if (write_file($dir_path.$filename, serialize($object)) === FALSE) { return FALSE; } - flock($fp, LOCK_EX); - fwrite($fp, $object); - flock($fp, LOCK_UN); - fclose($fp); + @chmod($dir_path.$filename, 0777); + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Delete cache files within a particular directory + * + * @access public + * @return bool + */ + function cache_delete() + { + $obj =& get_instance(); + $uri = ($obj->uri->segment(1) == FALSE) ? 'base' : $obj->uri->segment(2); + $uri .= ($obj->uri->segment(2) == FALSE) ? 'index' : $obj->uri->segment(2); + + $dir_path = $this->cache_dir.md5($uri).'/'; - @chmod($this->cache_dir.$this->cache_file, 0777); + $obj->load->helper('file'); + delete_files($dir_path, TRUE); + } - return TRUE; + // -------------------------------------------------------------------- + + /** + * Delete all existing cache files + * + * @access public + * @return bool + */ + // -------------------------------------------------------------------- + + function cache_delete_all() + { + $obj =& get_instance(); + $obj->load->helper('file'); + delete_files($this->cache_dir, TRUE); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 0c40565cbbf0218ad6927cbcbc80daf6aefc4377 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 4 Oct 2006 18:37:57 +0000 Subject: --- system/database/DB_driver.php | 170 +++++++----------------------------------- 1 file changed, 25 insertions(+), 145 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a7a89a953..24707aaca 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -51,6 +51,7 @@ class CI_DB_driver { var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur var $cache_on = FALSE; var $cachedir = ''; + var $cache; // The cache class object // These are use with Oracle @@ -152,12 +153,6 @@ class CI_DB_driver { } } } - - // Is there a cache direcotry specified in the config file? - if ($this->cachedir != '') - { - $this->cache_set_path($this->cachedir); - } } @@ -232,13 +227,17 @@ class CI_DB_driver { return FALSE; } - // Is query caching enabled? If the query is a "read type" we will - // grab the previously cached query if it exists and return it. + // Is query caching enabled? If the query is a "read type" + // we will load the caching class and return the previously + // cached query if it exists if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) - { - if (FALSE !== ($cache = $this->cache_read($sql))) + { + if ($this->_cache_init()) { - return $cache; + if (FALSE !== ($cache = $this->cache->read($sql))) + { + return $cache; + } } } @@ -289,9 +288,9 @@ class CI_DB_driver { // If caching is enabled we'll auto-cleanup any // existing files related to this particular URI - if ($this->cache_on == TRUE) + if ($this->cache_on == TRUE AND $this->_cache_init()) { - $this->cache_delete(); + $this->cache->delete(); } return TRUE; @@ -323,9 +322,9 @@ class CI_DB_driver { // Is query caching enabled? If so, we'll serialize the // result object and save it to a cache file - if ($this->cache_on == TRUE) + if ($this->cache_on == TRUE AND $this->_cache_init()) { - $this->cache_write($sql, $RES); + $this->cache->write($sql, $RES); } return $RES; @@ -903,153 +902,34 @@ class CI_DB_driver { { return $this->query_caching = FALSE; } - - // -------------------------------------------------------------------- - - /** - * Set Cache Directory Path - * - * @access public - * @param string the path to the cache directory - * @return bool - */ - function cache_set_path($path = '') - { - if ($path == '') - { - return $this->cache_off(); - } - - // Add a trailing slash to the path if needed - $path = preg_replace("/(.+?)\/*$/", "\\1/", $path); - - if ( ! is_dir($path) OR ! is_writable($path)) - { - if ($this->db_debug) - { - return $this->display_error('db_invalid_cache_path'); - } - - // If the path is wrong we'll turn off caching - return $this->cache_off(); - } - - $this->cache_dir = $path; - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Retreive a cached query - * - * The URI being requested will become the name of the cache sub-folder. - * An MD5 hash of the SQL statement will become the cache file name - * - * @access public - * @return string - */ - function cache_read($sql) - { - if ( ! $this->cache_set_path($this->cache_dir)) - { - return $this->cache_off(); - } - - $obj =& get_instance(); - $uri = ($obj->uri->segment(1) == FALSE) ? 'base' : $obj->uri->segment(2); - $uri .= ($obj->uri->segment(2) == FALSE) ? 'index' : $obj->uri->segment(2); - - $filepath = md5($uri).'/'.md5($sql); - - $obj->load->helper('file'); - if (FALSE === ($cachedata = read_file($this->cache_dir.$filepath))) - { - return FALSE; - } - - return unserialize($cachedata); - } // -------------------------------------------------------------------- /** - * Write a query to a cache file + * Initialize the Cache Class * - * @access public - * @return bool - */ - function cache_write($sql, $object) + * @access private + * @return void + */ + function _cache_init() { - if ( ! $this->cache_set_path($this->cache_dir)) + if (is_object($this->cache)) { - return $this->cache_off(); + return TRUE; } - - $obj =& get_instance(); - $uri = ($obj->uri->segment(1) == FALSE) ? 'base' : $obj->uri->segment(2); - $uri .= ($obj->uri->segment(2) == FALSE) ? 'index' : $obj->uri->segment(2); - - $dir_path = $this->cache_dir.md5($uri).'/'; - - $filename = md5($sql); - if ( ! @is_dir($dir_path)) + if ( ! class_exists('CI_DB_Cache')) { - if ( ! @mkdir($dir_path, 0777)) + if ( ! @include_once(BASEPATH.'database/DB_cache'.EXT)) { - return FALSE; + return $this->cache_off(); } - - @chmod($dir_path, 0777); } - $obj->load->helper('file'); - if (write_file($dir_path.$filename, serialize($object)) === FALSE) - { - return FALSE; - } - - @chmod($dir_path.$filename, 0777); + $this->cache = new CI_DB_Cache; return TRUE; } - // -------------------------------------------------------------------- - - /** - * Delete cache files within a particular directory - * - * @access public - * @return bool - */ - function cache_delete() - { - $obj =& get_instance(); - $uri = ($obj->uri->segment(1) == FALSE) ? 'base' : $obj->uri->segment(2); - $uri .= ($obj->uri->segment(2) == FALSE) ? 'index' : $obj->uri->segment(2); - - $dir_path = $this->cache_dir.md5($uri).'/'; - - $obj->load->helper('file'); - delete_files($dir_path, TRUE); - } - - // -------------------------------------------------------------------- - - /** - * Delete all existing cache files - * - * @access public - * @return bool - */ - // -------------------------------------------------------------------- - - function cache_delete_all() - { - $obj =& get_instance(); - $obj->load->helper('file'); - delete_files($this->cache_dir, TRUE); - } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From fa708175d3b3932e67af04858b841eb881c9579b Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 4 Oct 2006 18:42:42 +0000 Subject: --- system/database/DB_driver.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 24707aaca..39577ca83 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -902,6 +902,20 @@ class CI_DB_driver { { return $this->query_caching = FALSE; } + + // -------------------------------------------------------------------- + + /** + * Set Cache Directory Path + * + * @access public + * @param string the path to the cache directory + * @return void + */ + function cache_set_path($path = '') + { + $this->cachedir = $path; + } // -------------------------------------------------------------------- @@ -913,17 +927,14 @@ class CI_DB_driver { */ function _cache_init() { - if (is_object($this->cache)) + if (is_object($this->cache) AND class_exists('CI_DB_Cache')) { return TRUE; } - if ( ! class_exists('CI_DB_Cache')) + if ( ! @include_once(BASEPATH.'database/DB_cache'.EXT)) { - if ( ! @include_once(BASEPATH.'database/DB_cache'.EXT)) - { - return $this->cache_off(); - } + return $this->cache_off(); } $this->cache = new CI_DB_Cache; -- cgit v1.2.3-24-g4f1b From c2e3cd76579dd32b434a75287611985acea770bc Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 5 Oct 2006 04:22:30 +0000 Subject: --- system/database/DB_driver.php | 44 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 39577ca83..403255799 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -51,7 +51,7 @@ class CI_DB_driver { var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur var $cache_on = FALSE; var $cachedir = ''; - var $cache; // The cache class object + var $CACHE; // The cache class object // These are use with Oracle @@ -234,7 +234,7 @@ class CI_DB_driver { { if ($this->_cache_init()) { - if (FALSE !== ($cache = $this->cache->read($sql))) + if (FALSE !== ($cache = $this->CACHE->read($sql))) { return $cache; } @@ -287,10 +287,9 @@ class CI_DB_driver { { // If caching is enabled we'll auto-cleanup any // existing files related to this particular URI - if ($this->cache_on == TRUE AND $this->_cache_init()) { - $this->cache->delete(); + $this->CACHE->delete(); } return TRUE; @@ -305,26 +304,35 @@ class CI_DB_driver { } // Load and instantiate the result driver - - $driver = $this->load_rdriver(); - $RES = new $driver(); + $driver = $this->load_rdriver(); + $RES = new $driver(); $RES->conn_id = $this->conn_id; - $RES->db_debug = $this->db_debug; $RES->result_id = $this->result_id; if ($this->dbdriver == 'oci8') { - $RES->stmt_id = $this->stmt_id; - $RES->curs_id = NULL; - $RES->limit_used = $this->limit_used; + $RES->stmt_id = $this->stmt_id; + $RES->curs_id = NULL; + $RES->limit_used = $this->limit_used; } // Is query caching enabled? If so, we'll serialize the - // result object and save it to a cache file + // result object and save it to a cache file. if ($this->cache_on == TRUE AND $this->_cache_init()) - { - $this->cache->write($sql, $RES); + { + // We'll create a new instance of the result object + // only without the platform specific driver since + // we can't use it with cached data (the query result + // resource ID won't be any good once we've cached the + // result object, so we'll have to compile the data + // and save it) + $CR = new CI_DB_result(); + $CR->num_rows = $RES->num_rows(); + $CR->result_object = $RES->result_object(); + $CR->result_array = $RES->result_array(); + + $this->CACHE->write($sql, $CR); } return $RES; @@ -887,7 +895,7 @@ class CI_DB_driver { */ function cache_on() { - return $this->query_caching = TRUE; + return $this->cache_on = TRUE; } // -------------------------------------------------------------------- @@ -900,7 +908,7 @@ class CI_DB_driver { */ function cache_off() { - return $this->query_caching = FALSE; + return $this->cache_on = FALSE; } // -------------------------------------------------------------------- @@ -927,7 +935,7 @@ class CI_DB_driver { */ function _cache_init() { - if (is_object($this->cache) AND class_exists('CI_DB_Cache')) + if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) { return TRUE; } @@ -937,7 +945,7 @@ class CI_DB_driver { return $this->cache_off(); } - $this->cache = new CI_DB_Cache; + $this->CACHE = new CI_DB_Cache; return TRUE; } -- cgit v1.2.3-24-g4f1b From a658e314697e0b9e529a73df25edf4fffaf2b519 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 6 Oct 2006 01:29:36 +0000 Subject: --- system/database/DB_driver.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 403255799..f77b46609 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -51,6 +51,7 @@ class CI_DB_driver { var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur var $cache_on = FALSE; var $cachedir = ''; + var $cache_autodel = TRUE; var $CACHE; // The cache class object @@ -287,7 +288,7 @@ class CI_DB_driver { { // If caching is enabled we'll auto-cleanup any // existing files related to this particular URI - if ($this->cache_on == TRUE AND $this->_cache_init()) + if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) { $this->CACHE->delete(); } @@ -331,6 +332,10 @@ class CI_DB_driver { $CR->num_rows = $RES->num_rows(); $CR->result_object = $RES->result_object(); $CR->result_array = $RES->result_array(); + + // Reset these since cached objects can not utilize resource IDs. + $CR->conn_id = NULL; + $CR->result_id = NULL; $this->CACHE->write($sql, $CR); } @@ -910,6 +915,19 @@ class CI_DB_driver { { return $this->cache_on = FALSE; } + + // -------------------------------------------------------------------- + + /** + * Set the cache "auto-delete" value + * + * @access public + * @return void + */ + function cache_autodelete($val = TRUE) + { + $this->cache_autodel = ( ! is_bool($val)) ? TRUE : $val; + } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From c5f7fa3f8fea283b51ee6cd80b36b2112b2e81db Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 6 Oct 2006 02:10:23 +0000 Subject: --- system/database/DB_driver.php | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index f77b46609..e8c4a8236 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -51,7 +51,7 @@ class CI_DB_driver { var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur var $cache_on = FALSE; var $cachedir = ''; - var $cache_autodel = TRUE; + var $cache_autodel = FALSE; var $CACHE; // The cache class object @@ -889,7 +889,21 @@ class CI_DB_driver { return call_user_func_array($function, $args); } } - + + // -------------------------------------------------------------------- + + /** + * Set Cache Directory Path + * + * @access public + * @param string the path to the cache directory + * @return void + */ + function cache_set_path($path = '') + { + $this->cachedir = $path; + } + // -------------------------------------------------------------------- /** @@ -915,32 +929,41 @@ class CI_DB_driver { { return $this->cache_on = FALSE; } + // -------------------------------------------------------------------- /** - * Set the cache "auto-delete" value + * Delete the cache files associated with a particular URI * * @access public * @return void */ - function cache_autodelete($val = TRUE) + function cache_delete() { - $this->cache_autodel = ( ! is_bool($val)) ? TRUE : $val; + if ( ! $this->_cache_init()) + { + return FALSE; + } + return $this->CACHE->delete(); } - + // -------------------------------------------------------------------- /** - * Set Cache Directory Path + * Delete All cache files * * @access public - * @param string the path to the cache directory * @return void */ - function cache_set_path($path = '') + function cache_delete_all() { - $this->cachedir = $path; + if ( ! $this->_cache_init()) + { + return FALSE; + } + + return $this->CACHE->delete_all(); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From fd2750b8f85b4f204e536d255742e18018c3f1f2 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 6 Oct 2006 17:29:12 +0000 Subject: --- system/database/DB_driver.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index e8c4a8236..b89ebbf8d 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -156,6 +156,23 @@ class CI_DB_driver { } } + // -------------------------------------------------------------------- + + /** + * Load the Utilities Class + * + * @access public + * @return string + */ + function load_utilities() + { + $obj =& get_instance(); + + require_once(BASEPATH.'database/DB_utility'.EXT); + require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_utility'.EXT); + $class = 'CI_DB_'.$this->dbdriver.'_utility'; + $obj->dbutil = new $class(); + } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 88a8ad187b13b36d6120eae2344fe16c3a76219d Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 7 Oct 2006 03:16:32 +0000 Subject: --- system/database/DB_driver.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index b89ebbf8d..c8a00b7b1 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -165,13 +165,13 @@ class CI_DB_driver { * @return string */ function load_utilities() - { - $obj =& get_instance(); - + { require_once(BASEPATH.'database/DB_utility'.EXT); require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_utility'.EXT); $class = 'CI_DB_'.$this->dbdriver.'_utility'; - $obj->dbutil = new $class(); + + $CI =& get_instance(); + $CI->dbutil = new $class(); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 7099a589d1719311427d7552523ec962ebc3b650 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 10 Oct 2006 17:47:59 +0000 Subject: --- system/database/DB_driver.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index c8a00b7b1..99c95a6f2 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -682,7 +682,8 @@ class CI_DB_driver { } } - return $this->data_cache['table_names'] =& $retval; + $this->data_cache['table_names'] = $retval; + return $this->data_cache['table_names']; } // -------------------------------------------------------------------- @@ -747,7 +748,8 @@ class CI_DB_driver { } } - return $this->data_cache['field_names'][$table] =& $retval; + $this->data_cache['field_names'][$table] = $retval; + return $this->data_cache['field_names'][$table]; } // -------------------------------------------------------------------- @@ -931,7 +933,8 @@ class CI_DB_driver { */ function cache_on() { - return $this->cache_on = TRUE; + $this->cache_on = TRUE; + return TRUE; } // -------------------------------------------------------------------- @@ -944,7 +947,8 @@ class CI_DB_driver { */ function cache_off() { - return $this->cache_on = FALSE; + $this->cache_on = FALSE; + return FALSE; } -- cgit v1.2.3-24-g4f1b From 1b0ab4665720446eee6b03f864bb5576e6065a4a Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 10 Oct 2006 23:30:21 +0000 Subject: --- system/database/DB_driver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 99c95a6f2..6da645a38 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -960,13 +960,13 @@ class CI_DB_driver { * @access public * @return void */ - function cache_delete() + function cache_delete($segment_one = '', $segment_two = '') { if ( ! $this->_cache_init()) { return FALSE; } - return $this->CACHE->delete(); + return $this->CACHE->delete($segment_one, $segment_two); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 4003718f35247ef9b4a8d678389bda639677bac7 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 11 Oct 2006 19:16:58 +0000 Subject: --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 6da645a38..22f91ed4c 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -400,7 +400,7 @@ class CI_DB_driver { $this->initialize(); } - return $this->_execute($sql, $this->conn_id); + return $this->_execute($sql); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 606f99c043272f96f21911d89c21cd36c2ef59e4 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 11 Oct 2006 23:48:41 +0000 Subject: --- system/database/DB_driver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 22f91ed4c..a4131fd73 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -172,6 +172,7 @@ class CI_DB_driver { $CI =& get_instance(); $CI->dbutil = new $class(); + $CI->_ci_assign_to_models(); } // -------------------------------------------------------------------- @@ -629,7 +630,7 @@ class CI_DB_driver { */ function primary($table = '') { - $fields = $this->field_names($table); + $fields = $this->list_fields($table); if ( ! is_array($fields)) { -- cgit v1.2.3-24-g4f1b From 0aef222d246da84c90e9a89f31f6677cbe6b4ddc Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 12 Oct 2006 18:00:22 +0000 Subject: --- system/database/DB_driver.php | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a4131fd73..848d4f1c6 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -155,26 +155,7 @@ class CI_DB_driver { } } } - - // -------------------------------------------------------------------- - - /** - * Load the Utilities Class - * - * @access public - * @return string - */ - function load_utilities() - { - require_once(BASEPATH.'database/DB_utility'.EXT); - require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_utility'.EXT); - $class = 'CI_DB_'.$this->dbdriver.'_utility'; - $CI =& get_instance(); - $CI->dbutil = new $class(); - $CI->_ci_assign_to_models(); - } - // -------------------------------------------------------------------- /** -- cgit v1.2.3-24-g4f1b From 9fcc28a29299fbbc242f87bf1b1e61fda6543886 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 21 Oct 2006 17:49:47 +0000 Subject: --- system/database/DB_driver.php | 1 + 1 file changed, 1 insertion(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 848d4f1c6..a24bd20d8 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -236,6 +236,7 @@ class CI_DB_driver { { if (FALSE !== ($cache = $this->CACHE->read($sql))) { + $this->load_rdriver(); return $cache; } } -- cgit v1.2.3-24-g4f1b From fafe28bec4f414e48f63e01ed9105ae5c2c99802 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 21 Oct 2006 19:08:17 +0000 Subject: --- system/database/DB_driver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a24bd20d8..8a7b88f18 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -368,7 +368,7 @@ class CI_DB_driver { /** * Simple Query - * This is a simiplified version of the query() function. Internally + * This is a simplified version of the query() function. Internally * we only use it when running transaction commands since they do * not require all the features of the main query() function. * @@ -535,7 +535,7 @@ class CI_DB_driver { * Calculate the aggregate query elapsed time * * @access public - * @param intiger The number of decimal places + * @param integer The number of decimal places * @return integer */ function elapsed_time($decimals = 6) @@ -1021,7 +1021,7 @@ class CI_DB_driver { * @param string the error message * @param string any "swap" values * @param boolean whether to localize the message - * @return string sends the application/errror_db.php template + * @return string sends the application/error_db.php template */ function display_error($error = '', $swap = '', $native = FALSE) { -- cgit v1.2.3-24-g4f1b From e334c472fb4be44feec3a73402fc4a2b062cbfc0 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 21 Oct 2006 19:44:22 +0000 Subject: --- system/database/DB_driver.php | 176 +++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 88 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 8a7b88f18..ab8a6a8e2 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -7,17 +7,17 @@ * @package CodeIgniter * @author Rick Ellis * @copyright Copyright (c) 2006, pMachine, Inc. - * @license http://www.codeignitor.com/user_guide/license.html + * @license http://www.codeignitor.com/user_guide/license.html * @link http://www.codeigniter.com * @since Version 1.0 * @filesource */ - + // ------------------------------------------------------------------------ /** * Database Driver Class - * + * * This is the platform-independent base DB implementation class. * This class will not be called directly. Rather, the adapter * class for the specific database will extend and instantiate it. @@ -64,10 +64,10 @@ class CI_DB_driver { /** * Constructor. Accepts one parameter containing the database - * connection settings. + * connection settings. * - * Database settings can be passed as discreet - * parameters or as a data source name in the first + * Database settings can be passed as discreet + * parameters or as a data source name in the first * parameter. DSNs must have this prototype: * $dsn = 'driver://username:password@hostname/database'; * @@ -93,16 +93,16 @@ class CI_DB_driver { if (is_array($params)) { $defaults = array( - 'hostname' => '', - 'username' => '', - 'password' => '', - 'database' => '', - 'dbdriver' => 'mysql', - 'dbprefix' => '', - 'port' => '', - 'pconnect' => FALSE, - 'db_debug' => FALSE, - 'cachedir' => '', + 'hostname' => '', + 'username' => '', + 'password' => '', + 'database' => '', + 'dbdriver' => 'mysql', + 'dbprefix' => '', + 'port' => '', + 'pconnect' => FALSE, + 'db_debug' => FALSE, + 'cachedir' => '', 'cache_on' => FALSE ); @@ -132,9 +132,9 @@ class CI_DB_driver { // Connect to the database $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - + if ( ! $this->conn_id) - { + { log_message('error', 'Unable to connect to the database'); if ($this->db_debug) @@ -162,7 +162,7 @@ class CI_DB_driver { * The name of the platform in use (mysql, mssql, etc...) * * @access public - * @return string + * @return string */ function platform() { @@ -172,7 +172,7 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Database Version Number. Returns a string containing the + * Database Version Number. Returns a string containing the * version of the database being used * * @access public @@ -204,16 +204,16 @@ class CI_DB_driver { /** * Execute the query * - * Accepts an SQL string as input and returns a result object upon - * successful execution of a "read" type query. Returns boolean TRUE - * upon successful execution of a "write" type query. Returns boolean - * FALSE upon failure, and if the $db_debug variable is set to TRUE + * Accepts an SQL string as input and returns a result object upon + * successful execution of a "read" type query. Returns boolean TRUE + * upon successful execution of a "write" type query. Returns boolean + * FALSE upon failure, and if the $db_debug variable is set to TRUE * will raise an error. - * + * * @access public * @param string An SQL query string * @param array An array of binding data - * @return mixed + * @return mixed */ function query($sql, $binds = FALSE, $return_object = TRUE) { @@ -227,8 +227,8 @@ class CI_DB_driver { return FALSE; } - // Is query caching enabled? If the query is a "read type" - // we will load the caching class and return the previously + // Is query caching enabled? If the query is a "read type" + // we will load the caching class and return the previously // cached query if it exists if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) { @@ -253,10 +253,10 @@ class CI_DB_driver { // Start the Query Timer $time_start = list($sm, $ss) = explode(' ', microtime()); - + // Run the Query if (FALSE === ($this->result_id = $this->simple_query($sql))) - { + { // This will trigger a rollback if transactions are being used $this->_trans_failure = TRUE; @@ -265,13 +265,13 @@ class CI_DB_driver { log_message('error', 'Query error: '.$this->_error_message()); return $this->display_error( array( - 'Error Number: '.$this->_error_number(), + 'Error Number: '.$this->_error_number(), $this->_error_message(), $sql ) ); } - + return FALSE; } @@ -296,7 +296,7 @@ class CI_DB_driver { return TRUE; } - // Return TRUE if we don't need to create a result object + // Return TRUE if we don't need to create a result object // Currently only the Oracle driver uses this when stored // procedures are used if ($return_object !== TRUE) @@ -318,7 +318,7 @@ class CI_DB_driver { $RES->limit_used = $this->limit_used; } - // Is query caching enabled? If so, we'll serialize the + // Is query caching enabled? If so, we'll serialize the // result object and save it to a cache file. if ($this->cache_on == TRUE AND $this->_cache_init()) { @@ -347,9 +347,9 @@ class CI_DB_driver { /** * Load the result drivers - * + * * @access public - * @return string the name of the result class + * @return string the name of the result class */ function load_rdriver() { @@ -367,14 +367,14 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Simple Query + * Simple Query * This is a simplified version of the query() function. Internally * we only use it when running transaction commands since they do * not require all the features of the main query() function. - * + * * @access public * @param string the sql query - * @return mixed + * @return mixed */ function simple_query($sql) { @@ -391,9 +391,9 @@ class CI_DB_driver { /** * Disable Transactions * This permits transactions to be disabled at run-time. - * + * * @access public - * @return void + * @return void */ function trans_off() { @@ -404,9 +404,9 @@ class CI_DB_driver { /** * Start Transaction - * + * * @access public - * @return void + * @return void */ function trans_start($test_mode = FALSE) { @@ -429,9 +429,9 @@ class CI_DB_driver { /** * Complete Transaction - * + * * @access public - * @return bool + * @return bool */ function trans_complete() { @@ -467,9 +467,9 @@ class CI_DB_driver { /** * Lets you retrieve the transaction flag to determine if it has failed - * + * * @access public - * @return bool + * @return bool */ function trans_status() { @@ -480,11 +480,11 @@ class CI_DB_driver { /** * Compile Bindings - * + * * @access public * @param string the sql statement * @param array an array of bind data - * @return string + * @return string */ function compile_binds($sql, $binds) { @@ -514,15 +514,15 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Determines if a query is a "write" type. - * + * Determines if a query is a "write" type. + * * @access public * @param string An SQL query string - * @return boolean + * @return boolean */ function is_write_type($sql) { - if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) + if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) { return FALSE; } @@ -532,11 +532,11 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Calculate the aggregate query elapsed time - * + * Calculate the aggregate query elapsed time + * * @access public * @param integer The number of decimal places - * @return integer + * @return integer */ function elapsed_time($decimals = 6) { @@ -547,9 +547,9 @@ class CI_DB_driver { /** * Returns the total number of queries - * + * * @access public - * @return integer + * @return integer */ function total_queries() { @@ -560,9 +560,9 @@ class CI_DB_driver { /** * Returns the last query that was executed - * + * * @access public - * @return void + * @return void */ function last_query() { @@ -576,10 +576,10 @@ class CI_DB_driver { * * Escapes data based on type * Sets boolean and null types - * + * * @access public * @param string - * @return integer + * @return integer */ function escape($str) { @@ -605,10 +605,10 @@ class CI_DB_driver { * * Retrieves the primary key. It assumes that the row in the first * position is the primary key - * + * * @access public * @param string the table name - * @return string + * @return string */ function primary($table = '') { @@ -626,9 +626,9 @@ class CI_DB_driver { /** * Returns an array of table names - * + * * @access public - * @return array + * @return array */ function list_tables() { @@ -688,7 +688,7 @@ class CI_DB_driver { * * @access public * @param string the table name - * @return array + * @return array */ function list_fields($table = '') { @@ -754,7 +754,7 @@ class CI_DB_driver { /** * DEPRECATED - use list_fields() */ - function field_names($table = '') + function field_names($table = '') { return $this->list_fields($table); } @@ -763,10 +763,10 @@ class CI_DB_driver { /** * Returns an object with field data - * + * * @access public * @param string the table name - * @return object + * @return object */ function field_data($table = '') { @@ -787,18 +787,18 @@ class CI_DB_driver { /** * Generate an insert string - * + * * @access public * @param string the table upon which the query will be performed * @param array an associative array data of key/values - * @return string + * @return string */ function insert_string($table, $data) { - $fields = array(); + $fields = array(); $values = array(); - foreach($data as $key => $val) + foreach($data as $key => $val) { $fields[] = $key; $values[] = $this->escape($val); @@ -811,12 +811,12 @@ class CI_DB_driver { /** * Generate an update string - * + * * @access public * @param string the table upon which the query will be performed * @param array an associative array data of key/values * @param mixed the "where" statement - * @return string + * @return string */ function update_string($table, $data, $where) { @@ -824,7 +824,7 @@ class CI_DB_driver { return false; $fields = array(); - foreach($data as $key => $val) + foreach($data as $key => $val) { $fields[$key] = $this->escape($val); } @@ -861,11 +861,11 @@ class CI_DB_driver { /** * Enables a native PHP function to be run, using a platform agnostic wrapper. - * + * * @access public * @param string the function name * @param mixed any parameters needed by the function - * @return mixed + * @return mixed */ function call_function($function) { @@ -877,7 +877,7 @@ class CI_DB_driver { } if ( ! function_exists($function)) - { + { if ($this->db_debug) { return $this->display_error('db_unsupported_function'); @@ -888,7 +888,7 @@ class CI_DB_driver { { $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; - return call_user_func_array($function, $args); + return call_user_func_array($function, $args); } } @@ -999,16 +999,16 @@ class CI_DB_driver { /** * Close DB Connection - * + * * @access public - * @return void + * @return void */ function close() { if (is_resource($this->conn_id)) { $this->_close($this->conn_id); - } + } $this->conn_id = FALSE; } @@ -1016,14 +1016,14 @@ class CI_DB_driver { /** * Display an error message - * + * * @access public * @param string the error message * @param string any "swap" values * @param boolean whether to localize the message - * @return string sends the application/error_db.php template + * @return string sends the application/error_db.php template */ - function display_error($error = '', $swap = '', $native = FALSE) + function display_error($error = '', $swap = '', $native = FALSE) { $LANG = new CI_Language(); $LANG->load('db'); @@ -1048,7 +1048,7 @@ class CI_DB_driver { echo $error->show_error('An Error Was Encountered', $message, 'error_db'); exit; - } + } } -- cgit v1.2.3-24-g4f1b From 36e644241623461d6ac9531bdc5a61ee40b28be4 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 22 Oct 2006 01:30:50 +0000 Subject: --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index ab8a6a8e2..61f0d307f 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -234,9 +234,9 @@ class CI_DB_driver { { if ($this->_cache_init()) { + $this->load_rdriver(); if (FALSE !== ($cache = $this->CACHE->read($sql))) { - $this->load_rdriver(); return $cache; } } -- cgit v1.2.3-24-g4f1b From c1e23ce329575d2777dc1c87b87c4b1171c4e18b Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 23 Oct 2006 20:49:27 +0000 Subject: --- system/database/DB_driver.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 61f0d307f..139dbf7db 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -596,8 +596,6 @@ class CI_DB_driver { return $str; } - - // -------------------------------------------------------------------- /** -- cgit v1.2.3-24-g4f1b From 25701d75807a3fc8707c8afdb5d43062acfcf88e Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 24 Oct 2006 22:34:33 +0000 Subject: --- system/database/DB_driver.php | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 139dbf7db..d08e47f26 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -97,6 +97,7 @@ class CI_DB_driver { 'username' => '', 'password' => '', 'database' => '', + 'conn_id' => FALSE, 'dbdriver' => 'mysql', 'dbprefix' => '', 'port' => '', @@ -130,9 +131,17 @@ class CI_DB_driver { $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); } + // If an existing DB connection resource is supplied + // there is no need to connect and select the database + if (is_resource($this->conn_id)) + { + return TRUE; + } + // Connect to the database $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - + + // No connection? Throw an error if ( ! $this->conn_id) { log_message('error', 'Unable to connect to the database'); @@ -141,19 +150,22 @@ class CI_DB_driver { { $this->display_error('db_unable_to_connect'); } + return FALSE; } - else + + // Select the database + if ( ! $this->db_select()) { - if ( ! $this->db_select()) + log_message('error', 'Unable to select database: '.$this->database); + + if ($this->db_debug) { - log_message('error', 'Unable to select database: '.$this->database); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_select', $this->database); - } - } - } + $this->display_error('db_unable_to_select', $this->database); + } + return FALSE; + } + + return TRUE; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 59612e91b178e729ffd63cbabb0446fd51038e50 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 28 Oct 2006 18:50:23 +0000 Subject: --- system/database/DB_driver.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index d08e47f26..49ac8ab24 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -154,16 +154,19 @@ class CI_DB_driver { } // Select the database - if ( ! $this->db_select()) + if ($this->database != '') { - log_message('error', 'Unable to select database: '.$this->database); - - if ($this->db_debug) + if ( ! $this->db_select()) { - $this->display_error('db_unable_to_select', $this->database); + log_message('error', 'Unable to select database: '.$this->database); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_select', $this->database); + } + return FALSE; } - return FALSE; - } + } return TRUE; } -- cgit v1.2.3-24-g4f1b From f3a6204bb0f4538d6a77d9c85c56545be73ecd64 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 29 Oct 2006 20:24:11 +0000 Subject: --- system/database/DB_driver.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 49ac8ab24..77069d1a8 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -372,8 +372,8 @@ class CI_DB_driver { if ( ! class_exists($driver)) { - include_once(BASEPATH.'database/DB_result'.EXT); - include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); + include(BASEPATH.'database/DB_result'.EXT); + include(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); } return $driver; @@ -998,7 +998,7 @@ class CI_DB_driver { return TRUE; } - if ( ! @include_once(BASEPATH.'database/DB_cache'.EXT)) + if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) { return $this->cache_off(); } @@ -1054,13 +1054,12 @@ class CI_DB_driver { if ( ! class_exists('CI_Exceptions')) { - include_once(BASEPATH.'libraries/Exceptions'.EXT); + include(BASEPATH.'libraries/Exceptions'.EXT); } $error = new CI_Exceptions(); echo $error->show_error('An Error Was Encountered', $message, 'error_db'); exit; - } } -- cgit v1.2.3-24-g4f1b From bf9ee3f82dda6c738b65f2b558579b6cab7f7774 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 31 Oct 2006 02:35:04 +0000 Subject: --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 77069d1a8..03631d69a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -137,7 +137,7 @@ class CI_DB_driver { { return TRUE; } - + // Connect to the database $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); -- cgit v1.2.3-24-g4f1b From d1c638f0e9d1d838d80ff3641f1dc889f488cbf6 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Thu, 23 Nov 2006 18:56:39 +0000 Subject: --- system/database/DB_driver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 03631d69a..2a8510e13 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -372,8 +372,8 @@ class CI_DB_driver { if ( ! class_exists($driver)) { - include(BASEPATH.'database/DB_result'.EXT); - include(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); + include_once(BASEPATH.'database/DB_result'.EXT); + include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); } return $driver; -- cgit v1.2.3-24-g4f1b From d2df9bc7cc9d4b3e53818470c5d0977c9a36677c Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Sun, 15 Apr 2007 17:41:17 +0000 Subject: update pMachine to EllisLab update copyright year update Code Igniter to CodeIgniter --- system/database/DB_driver.php | 2132 ++++++++++++++++++++--------------------- 1 file changed, 1066 insertions(+), 1066 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 2a8510e13..7ff33246a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1,1067 +1,1067 @@ -initialize($params); - log_message('debug', 'Database Driver Class Initialized'); - } - - // -------------------------------------------------------------------- - - /** - * Initialize Database Settings - * - * @access private Called by the constructor - * @param mixed - * @return void - */ - function initialize($params = '') - { - if (is_array($params)) - { - $defaults = array( - 'hostname' => '', - 'username' => '', - 'password' => '', - 'database' => '', - 'conn_id' => FALSE, - 'dbdriver' => 'mysql', - 'dbprefix' => '', - 'port' => '', - 'pconnect' => FALSE, - 'db_debug' => FALSE, - 'cachedir' => '', - 'cache_on' => FALSE - ); - - foreach ($defaults as $key => $val) - { - $this->$key = ( ! isset($params[$key])) ? $val : $params[$key]; - } - } - elseif (strpos($params, '://')) - { - if (FALSE === ($dsn = @parse_url($params))) - { - log_message('error', 'Invalid DB Connection String'); - - if ($this->db_debug) - { - return $this->display_error('db_invalid_connection_str'); - } - return FALSE; - } - - $this->hostname = ( ! isset($dsn['host'])) ? '' : rawurldecode($dsn['host']); - $this->username = ( ! isset($dsn['user'])) ? '' : rawurldecode($dsn['user']); - $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']); - $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); - } - - // If an existing DB connection resource is supplied - // there is no need to connect and select the database - if (is_resource($this->conn_id)) - { - return TRUE; - } - - // Connect to the database - $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - - // No connection? Throw an error - if ( ! $this->conn_id) - { - log_message('error', 'Unable to connect to the database'); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_connect'); - } - return FALSE; - } - - // Select the database - if ($this->database != '') - { - if ( ! $this->db_select()) - { - log_message('error', 'Unable to select database: '.$this->database); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_select', $this->database); - } - return FALSE; - } - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * The name of the platform in use (mysql, mssql, etc...) - * - * @access public - * @return string - */ - function platform() - { - return $this->dbdriver; - } - - // -------------------------------------------------------------------- - - /** - * Database Version Number. Returns a string containing the - * version of the database being used - * - * @access public - * @return string - */ - function version() - { - if (FALSE === ($sql = $this->_version())) - { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; - } - - if ($this->dbdriver == 'oci8') - { - return $sql; - } - - $query = $this->query($sql); - $row = $query->row(); - return $row->ver; - } - - // -------------------------------------------------------------------- - - /** - * Execute the query - * - * Accepts an SQL string as input and returns a result object upon - * successful execution of a "read" type query. Returns boolean TRUE - * upon successful execution of a "write" type query. Returns boolean - * FALSE upon failure, and if the $db_debug variable is set to TRUE - * will raise an error. - * - * @access public - * @param string An SQL query string - * @param array An array of binding data - * @return mixed - */ - function query($sql, $binds = FALSE, $return_object = TRUE) - { - if ($sql == '') - { - if ($this->db_debug) - { - log_message('error', 'Invalid query: '.$sql); - return $this->display_error('db_invalid_query'); - } - return FALSE; - } - - // Is query caching enabled? If the query is a "read type" - // we will load the caching class and return the previously - // cached query if it exists - if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) - { - if ($this->_cache_init()) - { - $this->load_rdriver(); - if (FALSE !== ($cache = $this->CACHE->read($sql))) - { - return $cache; - } - } - } - - // Compile binds if needed - if ($binds !== FALSE) - { - $sql = $this->compile_binds($sql, $binds); - } - - // Save the query for debugging - $this->queries[] = $sql; - - // Start the Query Timer - $time_start = list($sm, $ss) = explode(' ', microtime()); - - // Run the Query - if (FALSE === ($this->result_id = $this->simple_query($sql))) - { - // This will trigger a rollback if transactions are being used - $this->_trans_failure = TRUE; - - if ($this->db_debug) - { - log_message('error', 'Query error: '.$this->_error_message()); - return $this->display_error( - array( - 'Error Number: '.$this->_error_number(), - $this->_error_message(), - $sql - ) - ); - } - - return FALSE; - } - - // Stop and aggregate the query time results - $time_end = list($em, $es) = explode(' ', microtime()); - $this->benchmark += ($em + $es) - ($sm + $ss); - - // Increment the query counter - $this->query_count++; - - // Was the query a "write" type? - // If so we'll simply return true - if ($this->is_write_type($sql) === TRUE) - { - // If caching is enabled we'll auto-cleanup any - // existing files related to this particular URI - if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) - { - $this->CACHE->delete(); - } - - return TRUE; - } - - // Return TRUE if we don't need to create a result object - // Currently only the Oracle driver uses this when stored - // procedures are used - if ($return_object !== TRUE) - { - return TRUE; - } - - // Load and instantiate the result driver - - $driver = $this->load_rdriver(); - $RES = new $driver(); - $RES->conn_id = $this->conn_id; - $RES->result_id = $this->result_id; - - if ($this->dbdriver == 'oci8') - { - $RES->stmt_id = $this->stmt_id; - $RES->curs_id = NULL; - $RES->limit_used = $this->limit_used; - } - - // Is query caching enabled? If so, we'll serialize the - // result object and save it to a cache file. - if ($this->cache_on == TRUE AND $this->_cache_init()) - { - // We'll create a new instance of the result object - // only without the platform specific driver since - // we can't use it with cached data (the query result - // resource ID won't be any good once we've cached the - // result object, so we'll have to compile the data - // and save it) - $CR = new CI_DB_result(); - $CR->num_rows = $RES->num_rows(); - $CR->result_object = $RES->result_object(); - $CR->result_array = $RES->result_array(); - - // Reset these since cached objects can not utilize resource IDs. - $CR->conn_id = NULL; - $CR->result_id = NULL; - - $this->CACHE->write($sql, $CR); - } - - return $RES; - } - - // -------------------------------------------------------------------- - - /** - * Load the result drivers - * - * @access public - * @return string the name of the result class - */ - function load_rdriver() - { - $driver = 'CI_DB_'.$this->dbdriver.'_result'; - - if ( ! class_exists($driver)) - { - include_once(BASEPATH.'database/DB_result'.EXT); - include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); - } - - return $driver; - } - - // -------------------------------------------------------------------- - - /** - * Simple Query - * This is a simplified version of the query() function. Internally - * we only use it when running transaction commands since they do - * not require all the features of the main query() function. - * - * @access public - * @param string the sql query - * @return mixed - */ - function simple_query($sql) - { - if ( ! $this->conn_id) - { - $this->initialize(); - } - - return $this->_execute($sql); - } - - // -------------------------------------------------------------------- - - /** - * Disable Transactions - * This permits transactions to be disabled at run-time. - * - * @access public - * @return void - */ - function trans_off() - { - $this->trans_enabled = FALSE; - } - - // -------------------------------------------------------------------- - - /** - * Start Transaction - * - * @access public - * @return void - */ - function trans_start($test_mode = FALSE) - { - if ( ! $this->trans_enabled) - { - return FALSE; - } - - // When transactions are nested we only begin/commit/rollback the outermost ones - if ($this->_trans_depth > 0) - { - $this->_trans_depth += 1; - return; - } - - $this->trans_begin($test_mode); - } - - // -------------------------------------------------------------------- - - /** - * Complete Transaction - * - * @access public - * @return bool - */ - function trans_complete() - { - if ( ! $this->trans_enabled) - { - return FALSE; - } - - // When transactions are nested we only begin/commit/rollback the outermost ones - if ($this->_trans_depth > 1) - { - $this->_trans_depth -= 1; - return TRUE; - } - - // The query() function will set this flag to TRUE in the event that a query failed - if ($this->_trans_failure === TRUE) - { - $this->trans_rollback(); - - if ($this->db_debug) - { - return $this->display_error('db_transaction_failure'); - } - return FALSE; - } - - $this->trans_commit(); - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Lets you retrieve the transaction flag to determine if it has failed - * - * @access public - * @return bool - */ - function trans_status() - { - return $this->_trans_failure; - } - - // -------------------------------------------------------------------- - - /** - * Compile Bindings - * - * @access public - * @param string the sql statement - * @param array an array of bind data - * @return string - */ - function compile_binds($sql, $binds) - { - if (FALSE === strpos($sql, $this->bind_marker)) - { - return $sql; - } - - if ( ! is_array($binds)) - { - $binds = array($binds); - } - - foreach ($binds as $val) - { - $val = $this->escape($val); - - // Just in case the replacement string contains the bind - // character we'll temporarily replace it with a marker - $val = str_replace($this->bind_marker, '{%bind_marker%}', $val); - $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1); - } - - return str_replace('{%bind_marker%}', $this->bind_marker, $sql); - } - - // -------------------------------------------------------------------- - - /** - * Determines if a query is a "write" type. - * - * @access public - * @param string An SQL query string - * @return boolean - */ - function is_write_type($sql) - { - if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) - { - return FALSE; - } - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Calculate the aggregate query elapsed time - * - * @access public - * @param integer The number of decimal places - * @return integer - */ - function elapsed_time($decimals = 6) - { - return number_format($this->benchmark, $decimals); - } - - // -------------------------------------------------------------------- - - /** - * Returns the total number of queries - * - * @access public - * @return integer - */ - function total_queries() - { - return $this->query_count; - } - - // -------------------------------------------------------------------- - - /** - * Returns the last query that was executed - * - * @access public - * @return void - */ - function last_query() - { - return end($this->queries); - } - - // -------------------------------------------------------------------- - - /** - * "Smart" Escape String - * - * Escapes data based on type - * Sets boolean and null types - * - * @access public - * @param string - * @return integer - */ - function escape($str) - { - switch (gettype($str)) - { - case 'string' : $str = "'".$this->escape_str($str)."'"; - break; - case 'boolean' : $str = ($str === FALSE) ? 0 : 1; - break; - default : $str = ($str === NULL) ? 'NULL' : $str; - break; - } - - return $str; - } - - // -------------------------------------------------------------------- - - /** - * Primary - * - * Retrieves the primary key. It assumes that the row in the first - * position is the primary key - * - * @access public - * @param string the table name - * @return string - */ - function primary($table = '') - { - $fields = $this->list_fields($table); - - if ( ! is_array($fields)) - { - return FALSE; - } - - return current($fields); - } - - // -------------------------------------------------------------------- - - /** - * Returns an array of table names - * - * @access public - * @return array - */ - function list_tables() - { - // Is there a cached result? - if (isset($this->data_cache['table_names'])) - { - return $this->data_cache['table_names']; - } - - if (FALSE === ($sql = $this->_list_tables())) - { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; - } - - $retval = array(); - $query = $this->query($sql); - - if ($query->num_rows() > 0) - { - foreach($query->result_array() as $row) - { - if (isset($row['TABLE_NAME'])) - { - $retval[] = $row['TABLE_NAME']; - } - else - { - $retval[] = array_shift($row); - } - } - } - - $this->data_cache['table_names'] = $retval; - return $this->data_cache['table_names']; - } - - // -------------------------------------------------------------------- - - /** - * Determine if a particular table exists - * @access public - * @return boolean - */ - function table_exists($table_name) - { - return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Fetch MySQL Field Names - * - * @access public - * @param string the table name - * @return array - */ - function list_fields($table = '') - { - // Is there a cached result? - if (isset($this->data_cache['field_names'][$table])) - { - return $this->data_cache['field_names'][$table]; - } - - if ($table == '') - { - if ($this->db_debug) - { - return $this->display_error('db_field_param_missing'); - } - return FALSE; - } - - if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table))) - { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; - } - - $query = $this->query($sql); - - $retval = array(); - foreach($query->result_array() as $row) - { - if (isset($row['COLUMN_NAME'])) - { - $retval[] = $row['COLUMN_NAME']; - } - else - { - $retval[] = current($row); - } - } - - $this->data_cache['field_names'][$table] = $retval; - return $this->data_cache['field_names'][$table]; - } - - // -------------------------------------------------------------------- - - /** - * Determine if a particular field exists - * @access public - * @param string - * @param string - * @return boolean - */ - function field_exists($field_name, $table_name) - { - return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; - } - - // -------------------------------------------------------------------- - - /** - * DEPRECATED - use list_fields() - */ - function field_names($table = '') - { - return $this->list_fields($table); - } - - // -------------------------------------------------------------------- - - /** - * Returns an object with field data - * - * @access public - * @param string the table name - * @return object - */ - function field_data($table = '') - { - if ($table == '') - { - if ($this->db_debug) - { - return $this->display_error('db_field_param_missing'); - } - return FALSE; - } - - $query = $this->query($this->_field_data($this->dbprefix.$table)); - return $query->field_data(); - } - - // -------------------------------------------------------------------- - - /** - * Generate an insert string - * - * @access public - * @param string the table upon which the query will be performed - * @param array an associative array data of key/values - * @return string - */ - function insert_string($table, $data) - { - $fields = array(); - $values = array(); - - foreach($data as $key => $val) - { - $fields[] = $key; - $values[] = $this->escape($val); - } - - return $this->_insert($this->dbprefix.$table, $fields, $values); - } - - // -------------------------------------------------------------------- - - /** - * Generate an update string - * - * @access public - * @param string the table upon which the query will be performed - * @param array an associative array data of key/values - * @param mixed the "where" statement - * @return string - */ - function update_string($table, $data, $where) - { - if ($where == '') - return false; - - $fields = array(); - foreach($data as $key => $val) - { - $fields[$key] = $this->escape($val); - } - - if ( ! is_array($where)) - { - $dest = array($where); - } - else - { - $dest = array(); - foreach ($where as $key => $val) - { - $prefix = (count($dest) == 0) ? '' : ' AND '; - - if ($val != '') - { - if ( ! $this->_has_operator($key)) - { - $key .= ' ='; - } - - $val = ' '.$this->escape($val); - } - - $dest[] = $prefix.$key.$val; - } - } - - return $this->_update($this->dbprefix.$table, $fields, $dest); - } - - // -------------------------------------------------------------------- - - /** - * Enables a native PHP function to be run, using a platform agnostic wrapper. - * - * @access public - * @param string the function name - * @param mixed any parameters needed by the function - * @return mixed - */ - function call_function($function) - { - $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; - - if (FALSE === strpos($driver, $function)) - { - $function = $driver.$function; - } - - if ( ! function_exists($function)) - { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; - } - else - { - $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; - - return call_user_func_array($function, $args); - } - } - - // -------------------------------------------------------------------- - - /** - * Set Cache Directory Path - * - * @access public - * @param string the path to the cache directory - * @return void - */ - function cache_set_path($path = '') - { - $this->cachedir = $path; - } - - // -------------------------------------------------------------------- - - /** - * Enable Query Caching - * - * @access public - * @return void - */ - function cache_on() - { - $this->cache_on = TRUE; - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Disable Query Caching - * - * @access public - * @return void - */ - function cache_off() - { - $this->cache_on = FALSE; - return FALSE; - } - - - // -------------------------------------------------------------------- - - /** - * Delete the cache files associated with a particular URI - * - * @access public - * @return void - */ - function cache_delete($segment_one = '', $segment_two = '') - { - if ( ! $this->_cache_init()) - { - return FALSE; - } - return $this->CACHE->delete($segment_one, $segment_two); - } - - // -------------------------------------------------------------------- - - /** - * Delete All cache files - * - * @access public - * @return void - */ - function cache_delete_all() - { - if ( ! $this->_cache_init()) - { - return FALSE; - } - - return $this->CACHE->delete_all(); - } - - // -------------------------------------------------------------------- - - /** - * Initialize the Cache Class - * - * @access private - * @return void - */ - function _cache_init() - { - if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) - { - return TRUE; - } - - if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) - { - return $this->cache_off(); - } - - $this->CACHE = new CI_DB_Cache; - return TRUE; - } - - - // -------------------------------------------------------------------- - - /** - * Close DB Connection - * - * @access public - * @return void - */ - function close() - { - if (is_resource($this->conn_id)) - { - $this->_close($this->conn_id); - } - $this->conn_id = FALSE; - } - - // -------------------------------------------------------------------- - - /** - * Display an error message - * - * @access public - * @param string the error message - * @param string any "swap" values - * @param boolean whether to localize the message - * @return string sends the application/error_db.php template - */ - function display_error($error = '', $swap = '', $native = FALSE) - { - $LANG = new CI_Language(); - $LANG->load('db'); - - $heading = 'MySQL Error'; - - if ($native == TRUE) - { - $message = $error; - } - else - { - $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; - } - - if ( ! class_exists('CI_Exceptions')) - { - include(BASEPATH.'libraries/Exceptions'.EXT); - } - - $error = new CI_Exceptions(); - echo $error->show_error('An Error Was Encountered', $message, 'error_db'); - exit; - } - -} - +initialize($params); + log_message('debug', 'Database Driver Class Initialized'); + } + + // -------------------------------------------------------------------- + + /** + * Initialize Database Settings + * + * @access private Called by the constructor + * @param mixed + * @return void + */ + function initialize($params = '') + { + if (is_array($params)) + { + $defaults = array( + 'hostname' => '', + 'username' => '', + 'password' => '', + 'database' => '', + 'conn_id' => FALSE, + 'dbdriver' => 'mysql', + 'dbprefix' => '', + 'port' => '', + 'pconnect' => FALSE, + 'db_debug' => FALSE, + 'cachedir' => '', + 'cache_on' => FALSE + ); + + foreach ($defaults as $key => $val) + { + $this->$key = ( ! isset($params[$key])) ? $val : $params[$key]; + } + } + elseif (strpos($params, '://')) + { + if (FALSE === ($dsn = @parse_url($params))) + { + log_message('error', 'Invalid DB Connection String'); + + if ($this->db_debug) + { + return $this->display_error('db_invalid_connection_str'); + } + return FALSE; + } + + $this->hostname = ( ! isset($dsn['host'])) ? '' : rawurldecode($dsn['host']); + $this->username = ( ! isset($dsn['user'])) ? '' : rawurldecode($dsn['user']); + $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']); + $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); + } + + // If an existing DB connection resource is supplied + // there is no need to connect and select the database + if (is_resource($this->conn_id)) + { + return TRUE; + } + + // Connect to the database + $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); + + // No connection? Throw an error + if ( ! $this->conn_id) + { + log_message('error', 'Unable to connect to the database'); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_connect'); + } + return FALSE; + } + + // Select the database + if ($this->database != '') + { + if ( ! $this->db_select()) + { + log_message('error', 'Unable to select database: '.$this->database); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_select', $this->database); + } + return FALSE; + } + } + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * The name of the platform in use (mysql, mssql, etc...) + * + * @access public + * @return string + */ + function platform() + { + return $this->dbdriver; + } + + // -------------------------------------------------------------------- + + /** + * Database Version Number. Returns a string containing the + * version of the database being used + * + * @access public + * @return string + */ + function version() + { + if (FALSE === ($sql = $this->_version())) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + if ($this->dbdriver == 'oci8') + { + return $sql; + } + + $query = $this->query($sql); + $row = $query->row(); + return $row->ver; + } + + // -------------------------------------------------------------------- + + /** + * Execute the query + * + * Accepts an SQL string as input and returns a result object upon + * successful execution of a "read" type query. Returns boolean TRUE + * upon successful execution of a "write" type query. Returns boolean + * FALSE upon failure, and if the $db_debug variable is set to TRUE + * will raise an error. + * + * @access public + * @param string An SQL query string + * @param array An array of binding data + * @return mixed + */ + function query($sql, $binds = FALSE, $return_object = TRUE) + { + if ($sql == '') + { + if ($this->db_debug) + { + log_message('error', 'Invalid query: '.$sql); + return $this->display_error('db_invalid_query'); + } + return FALSE; + } + + // Is query caching enabled? If the query is a "read type" + // we will load the caching class and return the previously + // cached query if it exists + if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) + { + if ($this->_cache_init()) + { + $this->load_rdriver(); + if (FALSE !== ($cache = $this->CACHE->read($sql))) + { + return $cache; + } + } + } + + // Compile binds if needed + if ($binds !== FALSE) + { + $sql = $this->compile_binds($sql, $binds); + } + + // Save the query for debugging + $this->queries[] = $sql; + + // Start the Query Timer + $time_start = list($sm, $ss) = explode(' ', microtime()); + + // Run the Query + if (FALSE === ($this->result_id = $this->simple_query($sql))) + { + // This will trigger a rollback if transactions are being used + $this->_trans_failure = TRUE; + + if ($this->db_debug) + { + log_message('error', 'Query error: '.$this->_error_message()); + return $this->display_error( + array( + 'Error Number: '.$this->_error_number(), + $this->_error_message(), + $sql + ) + ); + } + + return FALSE; + } + + // Stop and aggregate the query time results + $time_end = list($em, $es) = explode(' ', microtime()); + $this->benchmark += ($em + $es) - ($sm + $ss); + + // Increment the query counter + $this->query_count++; + + // Was the query a "write" type? + // If so we'll simply return true + if ($this->is_write_type($sql) === TRUE) + { + // If caching is enabled we'll auto-cleanup any + // existing files related to this particular URI + if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) + { + $this->CACHE->delete(); + } + + return TRUE; + } + + // Return TRUE if we don't need to create a result object + // Currently only the Oracle driver uses this when stored + // procedures are used + if ($return_object !== TRUE) + { + return TRUE; + } + + // Load and instantiate the result driver + + $driver = $this->load_rdriver(); + $RES = new $driver(); + $RES->conn_id = $this->conn_id; + $RES->result_id = $this->result_id; + + if ($this->dbdriver == 'oci8') + { + $RES->stmt_id = $this->stmt_id; + $RES->curs_id = NULL; + $RES->limit_used = $this->limit_used; + } + + // Is query caching enabled? If so, we'll serialize the + // result object and save it to a cache file. + if ($this->cache_on == TRUE AND $this->_cache_init()) + { + // We'll create a new instance of the result object + // only without the platform specific driver since + // we can't use it with cached data (the query result + // resource ID won't be any good once we've cached the + // result object, so we'll have to compile the data + // and save it) + $CR = new CI_DB_result(); + $CR->num_rows = $RES->num_rows(); + $CR->result_object = $RES->result_object(); + $CR->result_array = $RES->result_array(); + + // Reset these since cached objects can not utilize resource IDs. + $CR->conn_id = NULL; + $CR->result_id = NULL; + + $this->CACHE->write($sql, $CR); + } + + return $RES; + } + + // -------------------------------------------------------------------- + + /** + * Load the result drivers + * + * @access public + * @return string the name of the result class + */ + function load_rdriver() + { + $driver = 'CI_DB_'.$this->dbdriver.'_result'; + + if ( ! class_exists($driver)) + { + include_once(BASEPATH.'database/DB_result'.EXT); + include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); + } + + return $driver; + } + + // -------------------------------------------------------------------- + + /** + * Simple Query + * This is a simplified version of the query() function. Internally + * we only use it when running transaction commands since they do + * not require all the features of the main query() function. + * + * @access public + * @param string the sql query + * @return mixed + */ + function simple_query($sql) + { + if ( ! $this->conn_id) + { + $this->initialize(); + } + + return $this->_execute($sql); + } + + // -------------------------------------------------------------------- + + /** + * Disable Transactions + * This permits transactions to be disabled at run-time. + * + * @access public + * @return void + */ + function trans_off() + { + $this->trans_enabled = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Start Transaction + * + * @access public + * @return void + */ + function trans_start($test_mode = FALSE) + { + if ( ! $this->trans_enabled) + { + return FALSE; + } + + // When transactions are nested we only begin/commit/rollback the outermost ones + if ($this->_trans_depth > 0) + { + $this->_trans_depth += 1; + return; + } + + $this->trans_begin($test_mode); + } + + // -------------------------------------------------------------------- + + /** + * Complete Transaction + * + * @access public + * @return bool + */ + function trans_complete() + { + if ( ! $this->trans_enabled) + { + return FALSE; + } + + // When transactions are nested we only begin/commit/rollback the outermost ones + if ($this->_trans_depth > 1) + { + $this->_trans_depth -= 1; + return TRUE; + } + + // The query() function will set this flag to TRUE in the event that a query failed + if ($this->_trans_failure === TRUE) + { + $this->trans_rollback(); + + if ($this->db_debug) + { + return $this->display_error('db_transaction_failure'); + } + return FALSE; + } + + $this->trans_commit(); + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Lets you retrieve the transaction flag to determine if it has failed + * + * @access public + * @return bool + */ + function trans_status() + { + return $this->_trans_failure; + } + + // -------------------------------------------------------------------- + + /** + * Compile Bindings + * + * @access public + * @param string the sql statement + * @param array an array of bind data + * @return string + */ + function compile_binds($sql, $binds) + { + if (FALSE === strpos($sql, $this->bind_marker)) + { + return $sql; + } + + if ( ! is_array($binds)) + { + $binds = array($binds); + } + + foreach ($binds as $val) + { + $val = $this->escape($val); + + // Just in case the replacement string contains the bind + // character we'll temporarily replace it with a marker + $val = str_replace($this->bind_marker, '{%bind_marker%}', $val); + $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1); + } + + return str_replace('{%bind_marker%}', $this->bind_marker, $sql); + } + + // -------------------------------------------------------------------- + + /** + * Determines if a query is a "write" type. + * + * @access public + * @param string An SQL query string + * @return boolean + */ + function is_write_type($sql) + { + if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) + { + return FALSE; + } + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Calculate the aggregate query elapsed time + * + * @access public + * @param integer The number of decimal places + * @return integer + */ + function elapsed_time($decimals = 6) + { + return number_format($this->benchmark, $decimals); + } + + // -------------------------------------------------------------------- + + /** + * Returns the total number of queries + * + * @access public + * @return integer + */ + function total_queries() + { + return $this->query_count; + } + + // -------------------------------------------------------------------- + + /** + * Returns the last query that was executed + * + * @access public + * @return void + */ + function last_query() + { + return end($this->queries); + } + + // -------------------------------------------------------------------- + + /** + * "Smart" Escape String + * + * Escapes data based on type + * Sets boolean and null types + * + * @access public + * @param string + * @return integer + */ + function escape($str) + { + switch (gettype($str)) + { + case 'string' : $str = "'".$this->escape_str($str)."'"; + break; + case 'boolean' : $str = ($str === FALSE) ? 0 : 1; + break; + default : $str = ($str === NULL) ? 'NULL' : $str; + break; + } + + return $str; + } + + // -------------------------------------------------------------------- + + /** + * Primary + * + * Retrieves the primary key. It assumes that the row in the first + * position is the primary key + * + * @access public + * @param string the table name + * @return string + */ + function primary($table = '') + { + $fields = $this->list_fields($table); + + if ( ! is_array($fields)) + { + return FALSE; + } + + return current($fields); + } + + // -------------------------------------------------------------------- + + /** + * Returns an array of table names + * + * @access public + * @return array + */ + function list_tables() + { + // Is there a cached result? + if (isset($this->data_cache['table_names'])) + { + return $this->data_cache['table_names']; + } + + if (FALSE === ($sql = $this->_list_tables())) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + $retval = array(); + $query = $this->query($sql); + + if ($query->num_rows() > 0) + { + foreach($query->result_array() as $row) + { + if (isset($row['TABLE_NAME'])) + { + $retval[] = $row['TABLE_NAME']; + } + else + { + $retval[] = array_shift($row); + } + } + } + + $this->data_cache['table_names'] = $retval; + return $this->data_cache['table_names']; + } + + // -------------------------------------------------------------------- + + /** + * Determine if a particular table exists + * @access public + * @return boolean + */ + function table_exists($table_name) + { + return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Fetch MySQL Field Names + * + * @access public + * @param string the table name + * @return array + */ + function list_fields($table = '') + { + // Is there a cached result? + if (isset($this->data_cache['field_names'][$table])) + { + return $this->data_cache['field_names'][$table]; + } + + if ($table == '') + { + if ($this->db_debug) + { + return $this->display_error('db_field_param_missing'); + } + return FALSE; + } + + if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table))) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + $query = $this->query($sql); + + $retval = array(); + foreach($query->result_array() as $row) + { + if (isset($row['COLUMN_NAME'])) + { + $retval[] = $row['COLUMN_NAME']; + } + else + { + $retval[] = current($row); + } + } + + $this->data_cache['field_names'][$table] = $retval; + return $this->data_cache['field_names'][$table]; + } + + // -------------------------------------------------------------------- + + /** + * Determine if a particular field exists + * @access public + * @param string + * @param string + * @return boolean + */ + function field_exists($field_name, $table_name) + { + return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; + } + + // -------------------------------------------------------------------- + + /** + * DEPRECATED - use list_fields() + */ + function field_names($table = '') + { + return $this->list_fields($table); + } + + // -------------------------------------------------------------------- + + /** + * Returns an object with field data + * + * @access public + * @param string the table name + * @return object + */ + function field_data($table = '') + { + if ($table == '') + { + if ($this->db_debug) + { + return $this->display_error('db_field_param_missing'); + } + return FALSE; + } + + $query = $this->query($this->_field_data($this->dbprefix.$table)); + return $query->field_data(); + } + + // -------------------------------------------------------------------- + + /** + * Generate an insert string + * + * @access public + * @param string the table upon which the query will be performed + * @param array an associative array data of key/values + * @return string + */ + function insert_string($table, $data) + { + $fields = array(); + $values = array(); + + foreach($data as $key => $val) + { + $fields[] = $key; + $values[] = $this->escape($val); + } + + return $this->_insert($this->dbprefix.$table, $fields, $values); + } + + // -------------------------------------------------------------------- + + /** + * Generate an update string + * + * @access public + * @param string the table upon which the query will be performed + * @param array an associative array data of key/values + * @param mixed the "where" statement + * @return string + */ + function update_string($table, $data, $where) + { + if ($where == '') + return false; + + $fields = array(); + foreach($data as $key => $val) + { + $fields[$key] = $this->escape($val); + } + + if ( ! is_array($where)) + { + $dest = array($where); + } + else + { + $dest = array(); + foreach ($where as $key => $val) + { + $prefix = (count($dest) == 0) ? '' : ' AND '; + + if ($val != '') + { + if ( ! $this->_has_operator($key)) + { + $key .= ' ='; + } + + $val = ' '.$this->escape($val); + } + + $dest[] = $prefix.$key.$val; + } + } + + return $this->_update($this->dbprefix.$table, $fields, $dest); + } + + // -------------------------------------------------------------------- + + /** + * Enables a native PHP function to be run, using a platform agnostic wrapper. + * + * @access public + * @param string the function name + * @param mixed any parameters needed by the function + * @return mixed + */ + function call_function($function) + { + $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; + + if (FALSE === strpos($driver, $function)) + { + $function = $driver.$function; + } + + if ( ! function_exists($function)) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + else + { + $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; + + return call_user_func_array($function, $args); + } + } + + // -------------------------------------------------------------------- + + /** + * Set Cache Directory Path + * + * @access public + * @param string the path to the cache directory + * @return void + */ + function cache_set_path($path = '') + { + $this->cachedir = $path; + } + + // -------------------------------------------------------------------- + + /** + * Enable Query Caching + * + * @access public + * @return void + */ + function cache_on() + { + $this->cache_on = TRUE; + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Disable Query Caching + * + * @access public + * @return void + */ + function cache_off() + { + $this->cache_on = FALSE; + return FALSE; + } + + + // -------------------------------------------------------------------- + + /** + * Delete the cache files associated with a particular URI + * + * @access public + * @return void + */ + function cache_delete($segment_one = '', $segment_two = '') + { + if ( ! $this->_cache_init()) + { + return FALSE; + } + return $this->CACHE->delete($segment_one, $segment_two); + } + + // -------------------------------------------------------------------- + + /** + * Delete All cache files + * + * @access public + * @return void + */ + function cache_delete_all() + { + if ( ! $this->_cache_init()) + { + return FALSE; + } + + return $this->CACHE->delete_all(); + } + + // -------------------------------------------------------------------- + + /** + * Initialize the Cache Class + * + * @access private + * @return void + */ + function _cache_init() + { + if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) + { + return TRUE; + } + + if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) + { + return $this->cache_off(); + } + + $this->CACHE = new CI_DB_Cache; + return TRUE; + } + + + // -------------------------------------------------------------------- + + /** + * Close DB Connection + * + * @access public + * @return void + */ + function close() + { + if (is_resource($this->conn_id)) + { + $this->_close($this->conn_id); + } + $this->conn_id = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Display an error message + * + * @access public + * @param string the error message + * @param string any "swap" values + * @param boolean whether to localize the message + * @return string sends the application/error_db.php template + */ + function display_error($error = '', $swap = '', $native = FALSE) + { + $LANG = new CI_Language(); + $LANG->load('db'); + + $heading = 'MySQL Error'; + + if ($native == TRUE) + { + $message = $error; + } + else + { + $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; + } + + if ( ! class_exists('CI_Exceptions')) + { + include(BASEPATH.'libraries/Exceptions'.EXT); + } + + $error = new CI_Exceptions(); + echo $error->show_error('An Error Was Encountered', $message, 'error_db'); + exit; + } + +} + ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 28239ad0146eccecc9f2ca81764ca5605b3c50bf Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Mon, 11 Jun 2007 04:26:39 +0000 Subject: --- system/database/DB_driver.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 7ff33246a..8f238fc52 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -48,7 +48,7 @@ class CI_DB_driver { var $data_cache = array(); var $trans_enabled = TRUE; var $_trans_depth = 0; - var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur + var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur var $cache_on = FALSE; var $cachedir = ''; var $cache_autodel = FALSE; @@ -273,7 +273,7 @@ class CI_DB_driver { if (FALSE === ($this->result_id = $this->simple_query($sql))) { // This will trigger a rollback if transactions are being used - $this->_trans_failure = TRUE; + $this->_trans_status = FALSE; if ($this->db_debug) { @@ -463,7 +463,7 @@ class CI_DB_driver { } // The query() function will set this flag to TRUE in the event that a query failed - if ($this->_trans_failure === TRUE) + if ($this->_trans_status === FALSE) { $this->trans_rollback(); @@ -488,7 +488,7 @@ class CI_DB_driver { */ function trans_status() { - return $this->_trans_failure; + return $this->_trans_status; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 060052de1f91b9b1fc533d0957eb18dab9057bfe Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Sat, 14 Jul 2007 14:26:13 +0000 Subject: Fixed a bug in database driver where num_rows property wasn't getting updated --- system/database/DB_driver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 8f238fc52..381b70f42 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -325,7 +325,8 @@ class CI_DB_driver { $RES = new $driver(); $RES->conn_id = $this->conn_id; $RES->result_id = $this->result_id; - + $RES->num_rows = $RES->num_rows(); + if ($this->dbdriver == 'oci8') { $RES->stmt_id = $this->stmt_id; -- cgit v1.2.3-24-g4f1b From 409904637cd661be6ceccb59af1046468da09802 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Tue, 17 Jul 2007 21:40:44 +0000 Subject: --- system/database/DB_driver.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 381b70f42..ee965b74c 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -44,6 +44,7 @@ class CI_DB_driver { var $benchmark = 0; var $query_count = 0; var $bind_marker = '?'; + var $save_queries = TRUE; var $queries = array(); var $data_cache = array(); var $trans_enabled = TRUE; @@ -264,7 +265,10 @@ class CI_DB_driver { } // Save the query for debugging - $this->queries[] = $sql; + if ($this->save_queries == TRUE) + { + $this->queries[] = $sql; + } // Start the Query Timer $time_start = list($sm, $ss) = explode(' ', microtime()); -- cgit v1.2.3-24-g4f1b From 6838f00a708f53f834fb8a98af560177db1d1454 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Thu, 4 Oct 2007 19:29:59 +0000 Subject: Fixed a typo in the docblock comments that had CodeIgniter spelled CodeIgnitor. --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index ee965b74c..fc700ec32 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -7,7 +7,7 @@ * @package CodeIgniter * @author Rick Ellis * @copyright Copyright (c) 2006, EllisLab, Inc. - * @license http://www.codeignitor.com/user_guide/license.html + * @license http://www.codeigniter.com/user_guide/license.html * @link http://www.codeigniter.com * @since Version 1.0 * @filesource -- cgit v1.2.3-24-g4f1b From 6e848ed2afa39b5c99fda7bc061c864bd619269f Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Tue, 15 Jan 2008 14:26:23 +0000 Subject: Fixed a bug in the Oracle driver that prevented num_rows from working. --- system/database/DB_driver.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index fc700ec32..a7f03e3e5 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -329,7 +329,6 @@ class CI_DB_driver { $RES = new $driver(); $RES->conn_id = $this->conn_id; $RES->result_id = $this->result_id; - $RES->num_rows = $RES->num_rows(); if ($this->dbdriver == 'oci8') { @@ -337,7 +336,9 @@ class CI_DB_driver { $RES->curs_id = NULL; $RES->limit_used = $this->limit_used; } - + + $RES->num_rows = $RES->num_rows(); + // Is query caching enabled? If so, we'll serialize the // result object and save it to a cache file. if ($this->cache_on == TRUE AND $this->_cache_init()) -- cgit v1.2.3-24-g4f1b From 39b622db9bda38282a32bb45623da63efe685729 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Wed, 16 Jan 2008 21:10:09 +0000 Subject: Many new Active Record functions, and another whack of stuff --- system/database/DB_driver.php | 186 ++++++++++++++++++++++++++++++------------ 1 file changed, 135 insertions(+), 51 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a7f03e3e5..c2fa70a72 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -36,6 +36,8 @@ class CI_DB_driver { var $database; var $dbdriver = 'mysql'; var $dbprefix = ''; + var $autoinit = TRUE; // Whether to automatically initialize the DB + var $swap_pre = ''; var $port = ''; var $pconnect = FALSE; var $conn_id = FALSE; @@ -75,42 +77,12 @@ class CI_DB_driver { * @param mixed. Can be an array or a DSN string */ function CI_DB_driver($params) - { - $this->initialize($params); - log_message('debug', 'Database Driver Class Initialized'); - } - - // -------------------------------------------------------------------- - - /** - * Initialize Database Settings - * - * @access private Called by the constructor - * @param mixed - * @return void - */ - function initialize($params = '') { if (is_array($params)) { - $defaults = array( - 'hostname' => '', - 'username' => '', - 'password' => '', - 'database' => '', - 'conn_id' => FALSE, - 'dbdriver' => 'mysql', - 'dbprefix' => '', - 'port' => '', - 'pconnect' => FALSE, - 'db_debug' => FALSE, - 'cachedir' => '', - 'cache_on' => FALSE - ); - - foreach ($defaults as $key => $val) + foreach ($params as $key => $val) { - $this->$key = ( ! isset($params[$key])) ? $val : $params[$key]; + $this->$key = $val; } } elseif (strpos($params, '://')) @@ -131,7 +103,21 @@ class CI_DB_driver { $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']); $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); } - + + log_message('debug', 'Database Driver Class Initialized'); + } + + // -------------------------------------------------------------------- + + /** + * Initialize Database Settings + * + * @access private Called by the constructor + * @param mixed + * @return void + */ + function initialize($create_db = FALSE) + { // If an existing DB connection resource is supplied // there is no need to connect and select the database if (is_resource($this->conn_id)) @@ -159,6 +145,46 @@ class CI_DB_driver { { if ( ! $this->db_select()) { + // Should we attempt to create the database? + if ($create_db == TRUE) + { + // Load the DB utility class + $CI =& get_instance(); + $CI->load->dbutil(); + + // Create the DB + if ( ! $CI->dbutil->create_database($this->database)) + { + log_message('error', 'Unable to create database: '.$this->database); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_create', $this->database); + } + return FALSE; + } + else + { + // In the event the DB was created we need to select it + if ($this->db_select()) + { + if (! $this->db_set_charset($this->char_set, $this->dbcollat)) + { + log_message('error', 'Unable to set database connection charset: '.$this->char_set); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_set_charset', $this->char_set); + } + + return FALSE; + } + + return TRUE; + } + } + } + log_message('error', 'Unable to select database: '.$this->database); if ($this->db_debug) @@ -167,6 +193,18 @@ class CI_DB_driver { } return FALSE; } + + if (! $this->db_set_charset($this->char_set, $this->dbcollat)) + { + log_message('error', 'Unable to set database connection charset: '.$this->char_set); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_set_charset', $this->char_set); + } + + return FALSE; + } } return TRUE; @@ -211,8 +249,7 @@ class CI_DB_driver { } $query = $this->query($sql); - $row = $query->row(); - return $row->ver; + return $query->row('ver'); } // -------------------------------------------------------------------- @@ -242,6 +279,12 @@ class CI_DB_driver { } return FALSE; } + + // Verify table prefix and replace if necessary + if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) ) + { + $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); + } // Is query caching enabled? If the query is a "read type" // we will load the caching class and return the previously @@ -269,7 +312,7 @@ class CI_DB_driver { { $this->queries[] = $sql; } - + // Start the Query Timer $time_start = list($sm, $ss) = explode(' ', microtime()); @@ -291,7 +334,7 @@ class CI_DB_driver { ); } - return FALSE; + return FALSE; } // Stop and aggregate the query time results @@ -329,6 +372,7 @@ class CI_DB_driver { $RES = new $driver(); $RES->conn_id = $this->conn_id; $RES->result_id = $this->result_id; + $RES->num_rows = $RES->num_rows(); if ($this->dbdriver == 'oci8') { @@ -336,9 +380,7 @@ class CI_DB_driver { $RES->curs_id = NULL; $RES->limit_used = $this->limit_used; } - - $RES->num_rows = $RES->num_rows(); - + // Is query caching enabled? If so, we'll serialize the // result object and save it to a cache file. if ($this->cache_on == TRUE AND $this->_cache_init()) @@ -592,6 +634,23 @@ class CI_DB_driver { // -------------------------------------------------------------------- + /** + * Protect Identifiers + * + * This function adds backticks if appropriate based on db type + * + * @access private + * @param mixed the item to escape + * @param boolean only affect the first word + * @return mixed the item with backticks + */ + function protect_identifiers($item, $first_word_only = FALSE) + { + return $this->_protect_identifiers($item, $first_word_only = FALSE); + } + + // -------------------------------------------------------------------- + /** * "Smart" Escape String * @@ -649,7 +708,7 @@ class CI_DB_driver { * @access public * @return array */ - function list_tables() + function list_tables($constrain_by_prefix = FALSE) { // Is there a cached result? if (isset($this->data_cache['table_names'])) @@ -657,7 +716,7 @@ class CI_DB_driver { return $this->data_cache['table_names']; } - if (FALSE === ($sql = $this->_list_tables())) + if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) { if ($this->db_debug) { @@ -697,7 +756,7 @@ class CI_DB_driver { */ function table_exists($table_name) { - return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; + return ( ! in_array($this->prep_tablename($table_name), $this->list_tables())) ? FALSE : TRUE; } // -------------------------------------------------------------------- @@ -726,7 +785,7 @@ class CI_DB_driver { return FALSE; } - if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table))) + if (FALSE === ($sql = $this->_list_columns($this->prep_tablename($table)))) { if ($this->db_debug) { @@ -798,7 +857,7 @@ class CI_DB_driver { return FALSE; } - $query = $this->query($this->_field_data($this->dbprefix.$table)); + $query = $this->query($this->_field_data($this->prep_tablename($table))); return $query->field_data(); } @@ -822,9 +881,10 @@ class CI_DB_driver { $fields[] = $key; $values[] = $this->escape($val); } - - return $this->_insert($this->dbprefix.$table, $fields, $values); - } + + + return $this->_insert($this->prep_tablename($table), $fields, $values); + } // -------------------------------------------------------------------- @@ -859,7 +919,7 @@ class CI_DB_driver { { $prefix = (count($dest) == 0) ? '' : ' AND '; - if ($val != '') + if ($val !== '') { if ( ! $this->_has_operator($key)) { @@ -873,11 +933,34 @@ class CI_DB_driver { } } - return $this->_update($this->dbprefix.$table, $fields, $dest); + return $this->_update($this->prep_tablename($table), $fields, $dest); } // -------------------------------------------------------------------- + /** + * Prep the table name - simply adds the table prefix if needed + * + * @access public + * @param string the table name + * @return string + */ + function prep_tablename($table = '') + { + // Do we need to add the table prefix? + if ($this->dbprefix != '') + { + if (substr($table, 0, strlen($this->dbprefix)) != $this->dbprefix) + { + $table = $this->dbprefix.$table; + } + } + + return $table; + } + + // -------------------------------------------------------------------- + /** * Enables a native PHP function to be run, using a platform agnostic wrapper. * @@ -1013,7 +1096,6 @@ class CI_DB_driver { return TRUE; } - // -------------------------------------------------------------------- /** @@ -1044,6 +1126,7 @@ class CI_DB_driver { */ function display_error($error = '', $swap = '', $native = FALSE) { +// $LANG = new CI_Lang(); $LANG = new CI_Language(); $LANG->load('db'); @@ -1060,6 +1143,7 @@ class CI_DB_driver { if ( ! class_exists('CI_Exceptions')) { +// include(BASEPATH.'core/Exceptions'.EXT); include(BASEPATH.'libraries/Exceptions'.EXT); } -- cgit v1.2.3-24-g4f1b From 3d879d529107c0c9d3f1e6b894d9ed17b6e6c54f Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Fri, 18 Jan 2008 19:41:32 +0000 Subject: ExpressionEngine Dev Team in credit --- system/database/DB_driver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index c2fa70a72..bffdfc639 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -5,7 +5,7 @@ * An open source application development framework for PHP 4.3.2 or newer * * @package CodeIgniter - * @author Rick Ellis + * @author ExpressionEngine Dev Team * @copyright Copyright (c) 2006, EllisLab, Inc. * @license http://www.codeigniter.com/user_guide/license.html * @link http://www.codeigniter.com @@ -25,7 +25,7 @@ * @package CodeIgniter * @subpackage Drivers * @category Database - * @author Rick Ellis + * @author ExpressionEngine Dev Team * @link http://www.codeigniter.com/user_guide/database/ */ class CI_DB_driver { -- cgit v1.2.3-24-g4f1b From 7a9193afa6d890a91eb3528fa0e62df799b07ed6 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Mon, 21 Jan 2008 18:39:20 +0000 Subject: replaced www.codeigniter.com with codeigniter.com --- system/database/DB_driver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index bffdfc639..65be13dc9 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -7,8 +7,8 @@ * @package CodeIgniter * @author ExpressionEngine Dev Team * @copyright Copyright (c) 2006, EllisLab, Inc. - * @license http://www.codeigniter.com/user_guide/license.html - * @link http://www.codeigniter.com + * @license http://codeigniter.com/user_guide/license.html + * @link http://codeigniter.com * @since Version 1.0 * @filesource */ @@ -26,7 +26,7 @@ * @subpackage Drivers * @category Database * @author ExpressionEngine Dev Team - * @link http://www.codeigniter.com/user_guide/database/ + * @link http://codeigniter.com/user_guide/database/ */ class CI_DB_driver { -- cgit v1.2.3-24-g4f1b From 56e9fa581b9e7d590dfb56482b898339f29bd8f2 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 23 Jan 2008 17:26:37 +0000 Subject: added $query_times property to DB driver for profiling added individual query execution time to profiler output --- system/database/DB_driver.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 65be13dc9..dbd54bebe 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -48,6 +48,7 @@ class CI_DB_driver { var $bind_marker = '?'; var $save_queries = TRUE; var $queries = array(); + var $query_times = array(); var $data_cache = array(); var $trans_enabled = TRUE; var $_trans_depth = 0; @@ -341,6 +342,11 @@ class CI_DB_driver { $time_end = list($em, $es) = explode(' ', microtime()); $this->benchmark += ($em + $es) - ($sm + $ss); + if ($this->save_queries == TRUE) + { + $this->query_times[] = ($em + $es) - ($sm + $ss); + } + // Increment the query counter $this->query_count++; -- cgit v1.2.3-24-g4f1b From 1a704ceb6712b7f27402f4806f3894f48564e131 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Sun, 27 Jan 2008 15:03:06 +0000 Subject: fixed an overwritten parameter in protect_identifiers --- system/database/DB_driver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index dbd54bebe..966fd3ad5 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -652,7 +652,7 @@ class CI_DB_driver { */ function protect_identifiers($item, $first_word_only = FALSE) { - return $this->_protect_identifiers($item, $first_word_only = FALSE); + return $this->_protect_identifiers($item, $first_word_only); } // -------------------------------------------------------------------- @@ -1136,7 +1136,7 @@ class CI_DB_driver { $LANG = new CI_Language(); $LANG->load('db'); - $heading = 'MySQL Error'; + $heading = 'Database Error'; if ($native == TRUE) { -- cgit v1.2.3-24-g4f1b From c0743381b20910a3fc23b391e8b2009ac5771ae8 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Mon, 11 Feb 2008 05:54:44 +0000 Subject: database enhancements, compatibility additions and bugfixes --- system/database/DB_driver.php | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 966fd3ad5..6b3a74b94 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -556,8 +556,8 @@ class CI_DB_driver { * @return string */ function compile_binds($sql, $binds) - { - if (FALSE === strpos($sql, $this->bind_marker)) + { + if (strpos($sql, $this->bind_marker) === FALSE) { return $sql; } @@ -567,17 +567,25 @@ class CI_DB_driver { $binds = array($binds); } - foreach ($binds as $val) + // Get the sql segments around the bind markers + $segments = explode($this->bind_marker, $sql); + + // The count of bind should be 1 less then the count of segments + // If there are more bind arguments trim it down + if (count($binds) >= count($segments)) { + $binds = array_slice($binds, 0, count($segments)-1); + } + + // Construct the binded query + $result = $segments[0]; + $i = 0; + foreach ($binds as $bind) { - $val = $this->escape($val); - - // Just in case the replacement string contains the bind - // character we'll temporarily replace it with a marker - $val = str_replace($this->bind_marker, '{%bind_marker%}', $val); - $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1); + $result .= $this->escape($bind); + $result .= $segments[++$i]; } - return str_replace('{%bind_marker%}', $this->bind_marker, $sql); + return $result; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 7dd3838f188f35d63a30c30b435fbb7f2e2d3d7e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 13 Feb 2008 04:52:58 +0000 Subject: fixed bug #3419, moved DSN parsing to DB.php so the driver could properly be set to instantiate the correct db driver class. --- system/database/DB_driver.php | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 6b3a74b94..b1013178e 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -70,12 +70,7 @@ class CI_DB_driver { * Constructor. Accepts one parameter containing the database * connection settings. * - * Database settings can be passed as discreet - * parameters or as a data source name in the first - * parameter. DSNs must have this prototype: - * $dsn = 'driver://username:password@hostname/database'; - * - * @param mixed. Can be an array or a DSN string + * @param array */ function CI_DB_driver($params) { @@ -86,24 +81,6 @@ class CI_DB_driver { $this->$key = $val; } } - elseif (strpos($params, '://')) - { - if (FALSE === ($dsn = @parse_url($params))) - { - log_message('error', 'Invalid DB Connection String'); - - if ($this->db_debug) - { - return $this->display_error('db_invalid_connection_str'); - } - return FALSE; - } - - $this->hostname = ( ! isset($dsn['host'])) ? '' : rawurldecode($dsn['host']); - $this->username = ( ! isset($dsn['user'])) ? '' : rawurldecode($dsn['user']); - $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']); - $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1)); - } log_message('debug', 'Database Driver Class Initialized'); } -- cgit v1.2.3-24-g4f1b From 7327499064ae165468c7440f8571c3e570b58a0b Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Mon, 5 May 2008 16:39:18 +0000 Subject: Added get_dir_file_info(), get_file_info(), and get_mime_by_extension() to the File Helper. Changed ( ! condition) into (! condition) within the code --- system/database/DB_driver.php | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index b1013178e..0f3a1ea0a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -107,7 +107,7 @@ class CI_DB_driver { $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); // No connection? Throw an error - if ( ! $this->conn_id) + if (! $this->conn_id) { log_message('error', 'Unable to connect to the database'); @@ -121,7 +121,7 @@ class CI_DB_driver { // Select the database if ($this->database != '') { - if ( ! $this->db_select()) + if (! $this->db_select()) { // Should we attempt to create the database? if ($create_db == TRUE) @@ -131,7 +131,7 @@ class CI_DB_driver { $CI->load->dbutil(); // Create the DB - if ( ! $CI->dbutil->create_database($this->database)) + if (! $CI->dbutil->create_database($this->database)) { log_message('error', 'Unable to create database: '.$this->database); @@ -401,7 +401,7 @@ class CI_DB_driver { { $driver = 'CI_DB_'.$this->dbdriver.'_result'; - if ( ! class_exists($driver)) + if (! class_exists($driver)) { include_once(BASEPATH.'database/DB_result'.EXT); include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); @@ -424,7 +424,7 @@ class CI_DB_driver { */ function simple_query($sql) { - if ( ! $this->conn_id) + if (! $this->conn_id) { $this->initialize(); } @@ -456,7 +456,7 @@ class CI_DB_driver { */ function trans_start($test_mode = FALSE) { - if ( ! $this->trans_enabled) + if (! $this->trans_enabled) { return FALSE; } @@ -481,7 +481,7 @@ class CI_DB_driver { */ function trans_complete() { - if ( ! $this->trans_enabled) + if (! $this->trans_enabled) { return FALSE; } @@ -539,7 +539,7 @@ class CI_DB_driver { return $sql; } - if ( ! is_array($binds)) + if (! is_array($binds)) { $binds = array($binds); } @@ -576,7 +576,7 @@ class CI_DB_driver { */ function is_write_type($sql) { - if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) + if (! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) { return FALSE; } @@ -683,7 +683,7 @@ class CI_DB_driver { { $fields = $this->list_fields($table); - if ( ! is_array($fields)) + if (! is_array($fields)) { return FALSE; } @@ -747,7 +747,7 @@ class CI_DB_driver { */ function table_exists($table_name) { - return ( ! in_array($this->prep_tablename($table_name), $this->list_tables())) ? FALSE : TRUE; + return (! in_array($this->prep_tablename($table_name), $this->list_tables())) ? FALSE : TRUE; } // -------------------------------------------------------------------- @@ -815,7 +815,7 @@ class CI_DB_driver { */ function field_exists($field_name, $table_name) { - return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; + return (! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; } // -------------------------------------------------------------------- @@ -899,7 +899,7 @@ class CI_DB_driver { $fields[$key] = $this->escape($val); } - if ( ! is_array($where)) + if (! is_array($where)) { $dest = array($where); } @@ -912,7 +912,7 @@ class CI_DB_driver { if ($val !== '') { - if ( ! $this->_has_operator($key)) + if (! $this->_has_operator($key)) { $key .= ' ='; } @@ -969,7 +969,7 @@ class CI_DB_driver { $function = $driver.$function; } - if ( ! function_exists($function)) + if (! function_exists($function)) { if ($this->db_debug) { @@ -1038,7 +1038,7 @@ class CI_DB_driver { */ function cache_delete($segment_one = '', $segment_two = '') { - if ( ! $this->_cache_init()) + if (! $this->_cache_init()) { return FALSE; } @@ -1055,7 +1055,7 @@ class CI_DB_driver { */ function cache_delete_all() { - if ( ! $this->_cache_init()) + if (! $this->_cache_init()) { return FALSE; } @@ -1078,7 +1078,7 @@ class CI_DB_driver { return TRUE; } - if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) + if (! @include(BASEPATH.'database/DB_cache'.EXT)) { return $this->cache_off(); } @@ -1129,10 +1129,10 @@ class CI_DB_driver { } else { - $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; + $message = (! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; } - if ( ! class_exists('CI_Exceptions')) + if (! class_exists('CI_Exceptions')) { // include(BASEPATH.'core/Exceptions'.EXT); include(BASEPATH.'libraries/Exceptions'.EXT); -- cgit v1.2.3-24-g4f1b From 05097759476d3e18f2699503c1c47a30dba702af Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 7 May 2008 19:58:23 +0000 Subject: Added checks for objects in DB driver instead of just resources to accommodate MySQLi, and fixed check in mysqli_result.php checking for a resource. http://codeigniter.com/bug_tracker/bug/3461/ --- system/database/DB_driver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 0f3a1ea0a..5a55002f7 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -98,7 +98,7 @@ class CI_DB_driver { { // If an existing DB connection resource is supplied // there is no need to connect and select the database - if (is_resource($this->conn_id)) + if (is_resource($this->conn_id) OR is_object($this->conn_id)) { return TRUE; } @@ -1097,7 +1097,7 @@ class CI_DB_driver { */ function close() { - if (is_resource($this->conn_id)) + if (is_resource($this->conn_id) OR is_object($this->conn_id)) { $this->_close($this->conn_id); } -- cgit v1.2.3-24-g4f1b From 0f13a13bbf7bc54dc2ef4dda44bbe2f3890af8c6 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Thu, 8 May 2008 18:54:03 +0000 Subject: added SET to the list of write type queries --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 5a55002f7..a5ca75dd6 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -576,7 +576,7 @@ class CI_DB_driver { */ function is_write_type($sql) { - if (! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) + if (! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) { return FALSE; } -- cgit v1.2.3-24-g4f1b From 5583e1aae64ff7e902136c4ba610d438dc2015d4 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Sun, 11 May 2008 15:48:20 +0000 Subject: removed closing PHP tag from all framework files --- system/database/DB_driver.php | 1 - 1 file changed, 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a5ca75dd6..dd235103e 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1145,4 +1145,3 @@ class CI_DB_driver { } -?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From c7deac9f2f9e43cedb18202542e8a46061df046e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Sun, 11 May 2008 16:27:41 +0000 Subject: Undoing change committed in r1115 --- system/database/DB_driver.php | 1 + 1 file changed, 1 insertion(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index dd235103e..a5ca75dd6 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1145,3 +1145,4 @@ class CI_DB_driver { } +?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b From a3ffbbb75ab9403941e4f810703313432b3993cc Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Sun, 11 May 2008 18:18:29 +0000 Subject: Removed closing PHP tags, replaced with a comment block identifying the end of the file --- system/database/DB_driver.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a5ca75dd6..c296d4ca8 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1145,4 +1145,6 @@ class CI_DB_driver { } -?> \ No newline at end of file + +/* End of file DB_driver.php */ +/* Location: ./system/database/DB_driver.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 7f88aa51fb56cb6e0d50f55f07fd8638d7a625b2 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Mon, 12 May 2008 00:03:51 +0000 Subject: changed class instantiations to reference global $LANG and fetch existing Exceptions class, and added language variable for database error heading http://codeigniter.com/bug_tracker/bug/4421/ --- system/database/DB_driver.php | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index c296d4ca8..35bdc29b6 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1117,12 +1117,11 @@ class CI_DB_driver { */ function display_error($error = '', $swap = '', $native = FALSE) { -// $LANG = new CI_Lang(); - $LANG = new CI_Language(); + global $LANG; $LANG->load('db'); - $heading = 'Database Error'; - + $heading = $LANG->line('db_error_heading'); + if ($native == TRUE) { $message = $error; @@ -1131,20 +1130,14 @@ class CI_DB_driver { { $message = (! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; } - - if (! class_exists('CI_Exceptions')) - { -// include(BASEPATH.'core/Exceptions'.EXT); - include(BASEPATH.'libraries/Exceptions'.EXT); - } - $error = new CI_Exceptions(); - echo $error->show_error('An Error Was Encountered', $message, 'error_db'); + $error =& load_class('Exceptions'); + echo $error->show_error($heading, $message, 'error_db'); exit; } } - -/* End of file DB_driver.php */ + +/* End of file DB_driver.php */ /* Location: ./system/database/DB_driver.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From d36ade01b57ac39c328d9e0278b28b04fbef895e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Mon, 12 May 2008 15:17:41 +0000 Subject: passed db object by reference to DB Cache class, and changed the cache class to use that db object instead of $CI->db, to support returned db objects and multiple db connections http://codeigniter.com/bug_tracker/bug/4223/ --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 35bdc29b6..b055d6dc3 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1083,7 +1083,7 @@ class CI_DB_driver { return $this->cache_off(); } - $this->CACHE = new CI_DB_Cache; + $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects return TRUE; } -- cgit v1.2.3-24-g4f1b From 244b4c7bcf88b2ed10cb48ca961d615e5a5002ed Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Mon, 12 May 2008 18:21:33 +0000 Subject: --- system/database/DB_driver.php | 79 +++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 22 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index b055d6dc3..9ad235ded 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -51,6 +51,7 @@ class CI_DB_driver { var $query_times = array(); var $data_cache = array(); var $trans_enabled = TRUE; + var $trans_strict = TRUE; var $_trans_depth = 0; var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur var $cache_on = FALSE; @@ -107,7 +108,7 @@ class CI_DB_driver { $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); // No connection? Throw an error - if (! $this->conn_id) + if ( ! $this->conn_id) { log_message('error', 'Unable to connect to the database'); @@ -121,7 +122,7 @@ class CI_DB_driver { // Select the database if ($this->database != '') { - if (! $this->db_select()) + if ( ! $this->db_select()) { // Should we attempt to create the database? if ($create_db == TRUE) @@ -131,7 +132,7 @@ class CI_DB_driver { $CI->load->dbutil(); // Create the DB - if (! $CI->dbutil->create_database($this->database)) + if ( ! $CI->dbutil->create_database($this->database)) { log_message('error', 'Unable to create database: '.$this->database); @@ -297,11 +298,23 @@ class CI_DB_driver { // Run the Query if (FALSE === ($this->result_id = $this->simple_query($sql))) { + if ($this->save_queries == TRUE) + { + $this->query_times[] = 0; + } + // This will trigger a rollback if transactions are being used $this->_trans_status = FALSE; - + if ($this->db_debug) { + // We call this function in order to roll-back queries + // if transactions are enabled. If we don't call this here + // the error message will trigger an exit, causing the + // transactions to remain in limbo. + $this->trans_complete(); + + // Log and display errors log_message('error', 'Query error: '.$this->_error_message()); return $this->display_error( array( @@ -401,7 +414,7 @@ class CI_DB_driver { { $driver = 'CI_DB_'.$this->dbdriver.'_result'; - if (! class_exists($driver)) + if ( ! class_exists($driver)) { include_once(BASEPATH.'database/DB_result'.EXT); include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); @@ -424,7 +437,7 @@ class CI_DB_driver { */ function simple_query($sql) { - if (! $this->conn_id) + if ( ! $this->conn_id) { $this->initialize(); } @@ -448,6 +461,23 @@ class CI_DB_driver { // -------------------------------------------------------------------- + /** + * Enable/disable Transaction Strict Mode + * When strict mode is enabled, if you are running multiple groups of + * transactions, if one group fails all groups will be rolled back. + * If strict mode is disabled, each group is treated autonomously, meaning + * a failure of one group will not affect any others + * + * @access public + * @return void + */ + function trans_strict($mode = TRUE) + { + $this->trans_strict = is_bool($mode) ? $mode : TRUE; + } + + // -------------------------------------------------------------------- + /** * Start Transaction * @@ -456,7 +486,7 @@ class CI_DB_driver { */ function trans_start($test_mode = FALSE) { - if (! $this->trans_enabled) + if ( ! $this->trans_enabled) { return FALSE; } @@ -481,7 +511,7 @@ class CI_DB_driver { */ function trans_complete() { - if (! $this->trans_enabled) + if ( ! $this->trans_enabled) { return FALSE; } @@ -493,15 +523,20 @@ class CI_DB_driver { return TRUE; } - // The query() function will set this flag to TRUE in the event that a query failed + // The query() function will set this flag to FALSE in the event that a query failed if ($this->_trans_status === FALSE) { $this->trans_rollback(); - if ($this->db_debug) + // If we are NOT running in strict mode, we will reset + // the _trans_status flag so that subsequent groups of transactions + // will be permitted. + if ($this->trans_strict === FALSE) { - return $this->display_error('db_transaction_failure'); + $this->_trans_status = TRUE; } + + log_message('debug', 'DB Transaction Failure'); return FALSE; } @@ -539,7 +574,7 @@ class CI_DB_driver { return $sql; } - if (! is_array($binds)) + if ( ! is_array($binds)) { $binds = array($binds); } @@ -576,7 +611,7 @@ class CI_DB_driver { */ function is_write_type($sql) { - if (! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) + if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) { return FALSE; } @@ -683,7 +718,7 @@ class CI_DB_driver { { $fields = $this->list_fields($table); - if (! is_array($fields)) + if ( ! is_array($fields)) { return FALSE; } @@ -747,7 +782,7 @@ class CI_DB_driver { */ function table_exists($table_name) { - return (! in_array($this->prep_tablename($table_name), $this->list_tables())) ? FALSE : TRUE; + return ( ! in_array($this->prep_tablename($table_name), $this->list_tables())) ? FALSE : TRUE; } // -------------------------------------------------------------------- @@ -815,7 +850,7 @@ class CI_DB_driver { */ function field_exists($field_name, $table_name) { - return (! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; + return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; } // -------------------------------------------------------------------- @@ -899,7 +934,7 @@ class CI_DB_driver { $fields[$key] = $this->escape($val); } - if (! is_array($where)) + if ( ! is_array($where)) { $dest = array($where); } @@ -912,7 +947,7 @@ class CI_DB_driver { if ($val !== '') { - if (! $this->_has_operator($key)) + if ( ! $this->_has_operator($key)) { $key .= ' ='; } @@ -969,7 +1004,7 @@ class CI_DB_driver { $function = $driver.$function; } - if (! function_exists($function)) + if ( ! function_exists($function)) { if ($this->db_debug) { @@ -1038,7 +1073,7 @@ class CI_DB_driver { */ function cache_delete($segment_one = '', $segment_two = '') { - if (! $this->_cache_init()) + if ( ! $this->_cache_init()) { return FALSE; } @@ -1055,7 +1090,7 @@ class CI_DB_driver { */ function cache_delete_all() { - if (! $this->_cache_init()) + if ( ! $this->_cache_init()) { return FALSE; } @@ -1078,7 +1113,7 @@ class CI_DB_driver { return TRUE; } - if (! @include(BASEPATH.'database/DB_cache'.EXT)) + if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) { return $this->cache_off(); } -- cgit v1.2.3-24-g4f1b From 0b59f270a432f8c7b6128981f0a39b4a2e2fbd34 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 13 May 2008 04:22:33 +0000 Subject: Some sweeping syntax changes for consistency: (! foo) changed to ( ! foo) || changed to OR changed newline standardization code in various places from preg_replace to str_replace --- system/database/DB_driver.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 9ad235ded..306f09d7d 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1,4 +1,4 @@ -db_select()) { - if (! $this->db_set_charset($this->char_set, $this->dbcollat)) + if ( ! $this->db_set_charset($this->char_set, $this->dbcollat)) { log_message('error', 'Unable to set database connection charset: '.$this->char_set); @@ -173,7 +173,7 @@ class CI_DB_driver { return FALSE; } - if (! $this->db_set_charset($this->char_set, $this->dbcollat)) + if ( ! $this->db_set_charset($this->char_set, $this->dbcollat)) { log_message('error', 'Unable to set database connection charset: '.$this->char_set); @@ -1163,7 +1163,7 @@ class CI_DB_driver { } else { - $message = (! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; + $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; } $error =& load_class('Exceptions'); -- cgit v1.2.3-24-g4f1b From f38fe09dfecde176dc2803dd6a55177666f7d616 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 13 May 2008 20:28:11 +0000 Subject: hotfix for a bug in database error display introduced by 1.6.2 fix for bugs #4451, #4299, and #4339 --- system/database/DB_driver.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 306f09d7d..a0833d0ce 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -308,18 +308,23 @@ class CI_DB_driver { if ($this->db_debug) { + // grab the error number and message now, as we might run some + // additional queries before displaying the error + $error_no = $this->_error_number(); + $error_msg = $this->_error_message(); + // We call this function in order to roll-back queries // if transactions are enabled. If we don't call this here // the error message will trigger an exit, causing the // transactions to remain in limbo. $this->trans_complete(); - + // Log and display errors log_message('error', 'Query error: '.$this->_error_message()); return $this->display_error( array( - 'Error Number: '.$this->_error_number(), - $this->_error_message(), + 'Error Number: '.$error_no, + $error_msg, $sql ) ); -- cgit v1.2.3-24-g4f1b From 97bc010749830b183fffa7c5faf29744c095864e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 14 May 2008 15:01:50 +0000 Subject: fixed bug #3419 where the 'database' setting for DSN connections was using the host portion of the URL instead of the path. Added ability to set other db config values in DSN connections via query string --- system/database/DB_driver.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a0833d0ce..c9cece621 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -36,6 +36,8 @@ class CI_DB_driver { var $database; var $dbdriver = 'mysql'; var $dbprefix = ''; + var $char_set = ''; + var $dbcollat = ''; var $autoinit = TRUE; // Whether to automatically initialize the DB var $swap_pre = ''; var $port = ''; -- cgit v1.2.3-24-g4f1b From cafd63e83b20c40b1d9bd234dd2c20b2cdaab2c1 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 14 May 2008 15:08:17 +0000 Subject: set $DB->char_set and $DB->dbcollat defaults to utf8 and utf8_general_ci respectively --- system/database/DB_driver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index c9cece621..eeaf6ea78 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -36,8 +36,8 @@ class CI_DB_driver { var $database; var $dbdriver = 'mysql'; var $dbprefix = ''; - var $char_set = ''; - var $dbcollat = ''; + var $char_set = 'utf8'; + var $dbcollat = 'utf8_general_ci'; var $autoinit = TRUE; // Whether to automatically initialize the DB var $swap_pre = ''; var $port = ''; -- cgit v1.2.3-24-g4f1b From 513ce070bfcbb2b59d0aabd1732b7666b90b4d83 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Sun, 18 May 2008 12:23:11 +0000 Subject: Moved the _has_operators() function into DB_driver from DB_active_rec. --- system/database/DB_driver.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index eeaf6ea78..60f51d757 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -933,7 +933,9 @@ class CI_DB_driver { function update_string($table, $data, $where) { if ($where == '') + { return false; + } $fields = array(); foreach($data as $key => $val) @@ -971,6 +973,26 @@ class CI_DB_driver { // -------------------------------------------------------------------- + /** + * Tests whether the string has an SQL operator + * + * @access private + * @param string + * @return bool + */ + function _has_operator($str) + { + $str = trim($str); + if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) + { + return FALSE; + } + + return TRUE; + } + + // -------------------------------------------------------------------- + /** * Prep the table name - simply adds the table prefix if needed * -- cgit v1.2.3-24-g4f1b From 993925b47a0bfb08e79961c47bcc3d247a03a5dd Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Thu, 21 Aug 2008 12:43:31 +0000 Subject: whitespace fixes a minor re-ordering of the changelog --- system/database/DB_driver.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 60f51d757..1678cffd5 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -142,7 +142,7 @@ class CI_DB_driver { { $this->display_error('db_unable_to_create', $this->database); } - return FALSE; + return FALSE; } else { @@ -221,7 +221,7 @@ class CI_DB_driver { { return $this->display_error('db_unsupported_function'); } - return FALSE; + return FALSE; } if ($this->dbdriver == 'oci8') @@ -258,7 +258,7 @@ class CI_DB_driver { log_message('error', 'Invalid query: '.$sql); return $this->display_error('db_invalid_query'); } - return FALSE; + return FALSE; } // Verify table prefix and replace if necessary @@ -543,12 +543,12 @@ class CI_DB_driver { $this->_trans_status = TRUE; } - log_message('debug', 'DB Transaction Failure'); - return FALSE; + log_message('debug', 'DB Transaction Failure'); + return FALSE; } $this->trans_commit(); - return TRUE; + return TRUE; } // -------------------------------------------------------------------- @@ -755,7 +755,7 @@ class CI_DB_driver { { return $this->display_error('db_unsupported_function'); } - return FALSE; + return FALSE; } $retval = array(); @@ -815,7 +815,7 @@ class CI_DB_driver { { return $this->display_error('db_field_param_missing'); } - return FALSE; + return FALSE; } if (FALSE === ($sql = $this->_list_columns($this->prep_tablename($table)))) @@ -824,7 +824,7 @@ class CI_DB_driver { { return $this->display_error('db_unsupported_function'); } - return FALSE; + return FALSE; } $query = $this->query($sql); @@ -887,7 +887,7 @@ class CI_DB_driver { { return $this->display_error('db_field_param_missing'); } - return FALSE; + return FALSE; } $query = $this->query($this->_field_data($this->prep_tablename($table))); @@ -906,7 +906,7 @@ class CI_DB_driver { */ function insert_string($table, $data) { - $fields = array(); + $fields = array(); $values = array(); foreach($data as $key => $val) @@ -1039,7 +1039,7 @@ class CI_DB_driver { { return $this->display_error('db_unsupported_function'); } - return FALSE; + return FALSE; } else { -- cgit v1.2.3-24-g4f1b From 37b3ecff3043b3cf8359446052ec723e52944742 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Fri, 12 Sep 2008 23:34:18 +0000 Subject: updated copyright --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 1678cffd5..1450a0644 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -6,7 +6,7 @@ * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2006, EllisLab, Inc. + * @copyright Copyright (c) 2008, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 52dc8ca4372eb36e9186cef0e34bf0cafe5b1cd8 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Tue, 30 Sep 2008 19:53:52 +0000 Subject: Added backticks to column names when using insert_string and update_string. Relates to this bug report: http://codeigniter.com/bug_tracker/bug/4509/ --- system/database/DB_driver.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 1450a0644..b937ffd6a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -911,11 +911,10 @@ class CI_DB_driver { foreach($data as $key => $val) { - $fields[] = $key; + $fields[] = $this->_escape_column($key); $values[] = $this->escape($val); } - return $this->_insert($this->prep_tablename($table), $fields, $values); } @@ -940,7 +939,7 @@ class CI_DB_driver { $fields = array(); foreach($data as $key => $val) { - $fields[$key] = $this->escape($val); + $fields[$this->_escape_column($key)] = $this->escape($val); } if ( ! is_array($where)) -- cgit v1.2.3-24-g4f1b From a2a240a87baa0cf6a425837158880f5235e3bca6 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Tue, 7 Oct 2008 00:55:41 +0000 Subject: Fixed a bug (#2985) in which multiple queries using a single request were not being honored --- system/database/DB_driver.php | 1 + 1 file changed, 1 insertion(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index b937ffd6a..257f1ee9b 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -382,6 +382,7 @@ class CI_DB_driver { $RES->stmt_id = $this->stmt_id; $RES->curs_id = NULL; $RES->limit_used = $this->limit_used; + $this->stmt_id = FALSE; } // Is query caching enabled? If so, we'll serialize the -- cgit v1.2.3-24-g4f1b From a8664295b31968aa29556b93b13bb6a6080be19e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 8 Oct 2008 22:38:31 +0000 Subject: moved call to $RES->num_rows() further down in code so that oracle-specific class variables would be set properly as needed before executing --- system/database/DB_driver.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 257f1ee9b..572595f42 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -375,7 +375,6 @@ class CI_DB_driver { $RES = new $driver(); $RES->conn_id = $this->conn_id; $RES->result_id = $this->result_id; - $RES->num_rows = $RES->num_rows(); if ($this->dbdriver == 'oci8') { @@ -385,6 +384,9 @@ class CI_DB_driver { $this->stmt_id = FALSE; } + // oci8 vars must be set before calling this + $RES->num_rows = $RES->num_rows(); + // Is query caching enabled? If so, we'll serialize the // result object and save it to a cache file. if ($this->cache_on == TRUE AND $this->_cache_init()) -- cgit v1.2.3-24-g4f1b From 59523592c58abc303a6eae1904c80fa6a428c3d3 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Fri, 17 Oct 2008 04:07:40 +0000 Subject: Fixed a number of bug reports related to table/db names not being escaped or prefixed correctly. --- system/database/DB_driver.php | 334 ++++++++++++++++++++++++++++-------------- 1 file changed, 222 insertions(+), 112 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 572595f42..9508ded8c 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -61,6 +61,9 @@ class CI_DB_driver { var $cache_autodel = FALSE; var $CACHE; // The cache class object + // Private variables + var $_protect_identifiers = TRUE; + var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped // These are use with Oracle var $stmt_id; @@ -97,19 +100,21 @@ class CI_DB_driver { * @param mixed * @return void */ - function initialize($create_db = FALSE) + function initialize() { - // If an existing DB connection resource is supplied + // If an existing connection resource is available // there is no need to connect and select the database if (is_resource($this->conn_id) OR is_object($this->conn_id)) { return TRUE; } + + // ---------------------------------------------------------------- - // Connect to the database + // Connect to the database and set the connection ID $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - // No connection? Throw an error + // No connection resource? Throw an error if ( ! $this->conn_id) { log_message('error', 'Unable to connect to the database'); @@ -121,70 +126,30 @@ class CI_DB_driver { return FALSE; } - // Select the database + // ---------------------------------------------------------------- + + // Select the DB... assuming a database name is specified in the config file if ($this->database != '') { if ( ! $this->db_select()) { - // Should we attempt to create the database? - if ($create_db == TRUE) - { - // Load the DB utility class - $CI =& get_instance(); - $CI->load->dbutil(); - - // Create the DB - if ( ! $CI->dbutil->create_database($this->database)) - { - log_message('error', 'Unable to create database: '.$this->database); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_create', $this->database); - } - return FALSE; - } - else - { - // In the event the DB was created we need to select it - if ($this->db_select()) - { - if ( ! $this->db_set_charset($this->char_set, $this->dbcollat)) - { - log_message('error', 'Unable to set database connection charset: '.$this->char_set); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_set_charset', $this->char_set); - } - - return FALSE; - } - - return TRUE; - } - } - } - log_message('error', 'Unable to select database: '.$this->database); if ($this->db_debug) { $this->display_error('db_unable_to_select', $this->database); } - return FALSE; + return FALSE; } - - if ( ! $this->db_set_charset($this->char_set, $this->dbcollat)) + else { - log_message('error', 'Unable to set database connection charset: '.$this->char_set); - - if ($this->db_debug) + // We've selected the DB. Now we set the character set + if ( ! $this->db_set_charset($this->char_set, $this->dbcollat)) { - $this->display_error('db_unable_to_set_charset', $this->char_set); + return FALSE; } - - return FALSE; + + return TRUE; } } @@ -193,6 +158,33 @@ class CI_DB_driver { // -------------------------------------------------------------------- + /** + * Set client character set + * + * @access public + * @param string + * @param string + * @return resource + */ + function db_set_charset($charset, $collation) + { + if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat)) + { + log_message('error', 'Unable to set database connection charset: '.$this->char_set); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_set_charset', $this->char_set); + } + + return FALSE; + } + + return TRUE; + } + + // -------------------------------------------------------------------- + /** * The name of the platform in use (mysql, mssql, etc...) * @@ -667,23 +659,6 @@ class CI_DB_driver { { return end($this->queries); } - - // -------------------------------------------------------------------- - - /** - * Protect Identifiers - * - * This function adds backticks if appropriate based on db type - * - * @access private - * @param mixed the item to escape - * @param boolean only affect the first word - * @return mixed the item with backticks - */ - function protect_identifiers($item, $first_word_only = FALSE) - { - return $this->_protect_identifiers($item, $first_word_only); - } // -------------------------------------------------------------------- @@ -791,8 +766,8 @@ class CI_DB_driver { * @return boolean */ function table_exists($table_name) - { - return ( ! in_array($this->prep_tablename($table_name), $this->list_tables())) ? FALSE : TRUE; + { + return ( ! in_array($this->_protect_identifiers($table_name, TRUE, NULL, FALSE), $this->list_tables())) ? FALSE : TRUE; } // -------------------------------------------------------------------- @@ -821,7 +796,7 @@ class CI_DB_driver { return FALSE; } - if (FALSE === ($sql = $this->_list_columns($this->prep_tablename($table)))) + if (FALSE === ($sql = $this->_list_columns($this->_protect_identifiers($table, TRUE, NULL, FALSE)))) { if ($this->db_debug) { @@ -865,16 +840,6 @@ class CI_DB_driver { // -------------------------------------------------------------------- - /** - * DEPRECATED - use list_fields() - */ - function field_names($table = '') - { - return $this->list_fields($table); - } - - // -------------------------------------------------------------------- - /** * Returns an object with field data * @@ -893,7 +858,8 @@ class CI_DB_driver { return FALSE; } - $query = $this->query($this->_field_data($this->prep_tablename($table))); + $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); + return $query->field_data(); } @@ -914,11 +880,11 @@ class CI_DB_driver { foreach($data as $key => $val) { - $fields[] = $this->_escape_column($key); + $fields[] = $this->_escape_identifiers($key); $values[] = $this->escape($val); } - return $this->_insert($this->prep_tablename($table), $fields, $values); + return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); } // -------------------------------------------------------------------- @@ -942,7 +908,7 @@ class CI_DB_driver { $fields = array(); foreach($data as $key => $val) { - $fields[$this->_escape_column($key)] = $this->escape($val); + $fields[$this->_protect_identifiers($key)] = $this->escape($val); } if ( ! is_array($where)) @@ -970,7 +936,7 @@ class CI_DB_driver { } } - return $this->_update($this->prep_tablename($table), $fields, $dest); + return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest); } // -------------------------------------------------------------------- @@ -992,29 +958,6 @@ class CI_DB_driver { return TRUE; } - - // -------------------------------------------------------------------- - - /** - * Prep the table name - simply adds the table prefix if needed - * - * @access public - * @param string the table name - * @return string - */ - function prep_tablename($table = '') - { - // Do we need to add the table prefix? - if ($this->dbprefix != '') - { - if (substr($table, 0, strlen($this->dbprefix)) != $this->dbprefix) - { - $table = $this->dbprefix.$table; - } - } - - return $table; - } // -------------------------------------------------------------------- @@ -1201,7 +1144,174 @@ class CI_DB_driver { echo $error->show_error($heading, $message, 'error_db'); exit; } + + // -------------------------------------------------------------------- + + /** + * Protect Identifiers + * + * This function adds backticks if appropriate based on db type + * + * @access private + * @param mixed the item to escape + * @return mixed the item with backticks + */ + function protect_identifiers($item, $prefix_single = FALSE) + { + return $this->_protect_identifiers($item, $prefix_single); + } + + // -------------------------------------------------------------------- + + /** + * Protect Identifiers + * + * This function is used extensively by the Active Record class, and by + * a couple functions in this class. + * It takes a column or table name (optionally with an alias) and inserts + * the table prefix onto it. Some logic is necessary in order to deal with + * column names that include the path. Consider a query like this: + * + * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table + * + * Or a query with aliasing: + * + * SELECT m.member_id, m.member_name FROM members AS m + * + * Since the column name can include up to four segments (host, DB, table, column) + * or also have an alias prefix, we need to do a bit of work to figure this out and + * insert the table prefix (if it exists) in the proper position, and escape only + * the correct identifiers. + * + * @access private + * @param string + * @param bool + * @param mixed + * @param bool + * @return string + */ + function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) + { + if ( ! is_bool($protect_identifiers)) + { + $protect_identifiers = $this->_protect_identifiers; + } + + // Convert tabs or multiple spaces into single spaces + $item = preg_replace('/[\t| ]+/', ' ', $item); + // If the item has an alias declaration we remove it and set it aside. + // Basically we remove everything to the right of the first space + $alias = ''; + if (strpos($item, ' ') !== FALSE) + { + $alias = strstr($item, " "); + $item = substr($item, 0, - strlen($alias)); + } + + // Break the string apart if it contains periods, then insert the table prefix + // in the correct location, assuming the period doesn't indicate that we're dealing + // with an alias. While we're at it, we will escape the components + if (strpos($item, '.') !== FALSE) + { + $parts = explode('.', $item); + + // Does the first segment of the exploded item match + // one of the aliases previously identified? If so, + // we have nothing more to do other then escape the item + if (in_array($parts[0], $this->ar_aliased_tables)) + { + if ($protect_identifiers === TRUE) + { + foreach ($parts as $key => $val) + { + if ( ! in_array($val, $this->_reserved_identifiers)) + { + $parts[$key] = $this->_escape_identifiers($val); + } + } + + $item = implode('.', $parts); + } + return $item.$alias; + } + + // Is there a table prefix defined in the config file? If not, no need to do anything + if ($this->dbprefix != '') + { + // We now add the table prefix based on some logic. + // Do we have 4 segments (hostname.database.table.column)? + // If so, we add the table prefix to the column name in the 3rd segment. + if (isset($parts[3])) + { + $i = 2; + } + // Do we have 3 segments (database.table.column)? + // If so, we add the table prefix to the column name in 2nd position + elseif (isset($parts[2])) + { + $i = 1; + } + // Do we have 2 segments (table.column)? + // If so, we add the table prefix to the column name in 1st segment + else + { + $i = 0; + } + + // This flag is set when the supplied $item does not contain a field name. + // This can happen when this function is being called from a JOIN. + if ($field_exists == FALSE) + { + $i++; + } + + // We only add the table prefix if it does not already exist + if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) + { + $parts[$i] = $this->dbprefix.$parts[$i]; + } + + // Put the parts back together + $item = implode('.', $parts); + } + + if ($protect_identifiers === TRUE) + { + $item = $this->_escape_identifiers($item); + } + + return $item.$alias; + } + + // This is basically a bug fix for queries that use MAX, MIN, etc. + // If a parenthesis is found we know that we do not need to + // escape the data or add a prefix. There's probably a more graceful + // way to deal with this, but I'm not thinking of it -- Rick + if (strpos($item, '(') !== FALSE) + { + return $item.$alias; + } + + // Is there a table prefix? If not, no need to insert it + if ($this->dbprefix != '') + { + // Do we prefix an item with no segments? + if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) + { + $item = $this->dbprefix.$item; + } + } + + if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) + { + $item = $this->_escape_identifiers($item); + } + + return $item.$alias; + } + + } -- cgit v1.2.3-24-g4f1b From 62637e9383bc21a38a49e4ce236ee9baf9c20f65 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Fri, 17 Oct 2008 06:48:16 +0000 Subject: Fixed an error reporting bug: 4900 --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 9508ded8c..6b60d67dd 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -314,7 +314,7 @@ class CI_DB_driver { $this->trans_complete(); // Log and display errors - log_message('error', 'Query error: '.$this->_error_message()); + log_message('error', 'Query error: '.$error_msg); return $this->display_error( array( 'Error Number: '.$error_no, -- cgit v1.2.3-24-g4f1b From 7799f52d9ba6efc0c309b04d23178f5c84d12e94 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Fri, 17 Oct 2008 07:08:31 +0000 Subject: Fixed bug: 5043 --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 6b60d67dd..1682b5963 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1126,7 +1126,7 @@ class CI_DB_driver { */ function display_error($error = '', $swap = '', $native = FALSE) { - global $LANG; + $LANG =& load_class('Language'); $LANG->load('db'); $heading = $LANG->line('db_error_heading'); -- cgit v1.2.3-24-g4f1b From 7709aca4a8325da5906a7585a831456cbd8abf47 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Wed, 22 Oct 2008 21:07:22 +0000 Subject: Fixed a bug in the table_exists function --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 1682b5963..07c19c51b 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -767,7 +767,7 @@ class CI_DB_driver { */ function table_exists($table_name) { - return ( ! in_array($this->_protect_identifiers($table_name, TRUE, NULL, FALSE), $this->list_tables())) ? FALSE : TRUE; + return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From ac91bd701c11e8c9e5cb42b1120eb9f88f73456a Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Sun, 26 Oct 2008 21:52:41 +0000 Subject: Fixed some typos --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 07c19c51b..866b95612 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1218,7 +1218,7 @@ class CI_DB_driver { // Does the first segment of the exploded item match // one of the aliases previously identified? If so, - // we have nothing more to do other then escape the item + // we have nothing more to do other than escape the item if (in_array($parts[0], $this->ar_aliased_tables)) { if ($protect_identifiers === TRUE) -- cgit v1.2.3-24-g4f1b From a0e86293949ff7761cca573853e54146d76f9ba7 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Sun, 26 Oct 2008 22:46:55 +0000 Subject: Fixed a bug in which identifers were not being escaped properly when reserved characters were used --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 866b95612..dbd82dbc4 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1302,7 +1302,7 @@ class CI_DB_driver { $item = $this->dbprefix.$item; } } - + if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) { $item = $this->_escape_identifiers($item); -- cgit v1.2.3-24-g4f1b From 2067d1a727e7eb5e5ffb40e967f3d1fc4c8a41b2 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Thu, 13 Nov 2008 22:59:24 +0000 Subject: Changing EOL style to LF --- system/database/DB_driver.php | 2636 ++++++++++++++++++++--------------------- 1 file changed, 1318 insertions(+), 1318 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index dbd82dbc4..4293acc91 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1,1319 +1,1319 @@ - $val) - { - $this->$key = $val; - } - } - - log_message('debug', 'Database Driver Class Initialized'); - } - - // -------------------------------------------------------------------- - - /** - * Initialize Database Settings - * - * @access private Called by the constructor - * @param mixed - * @return void - */ - function initialize() - { - // If an existing connection resource is available - // there is no need to connect and select the database - if (is_resource($this->conn_id) OR is_object($this->conn_id)) - { - return TRUE; - } - - // ---------------------------------------------------------------- - - // Connect to the database and set the connection ID - $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - - // No connection resource? Throw an error - if ( ! $this->conn_id) - { - log_message('error', 'Unable to connect to the database'); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_connect'); - } - return FALSE; - } - - // ---------------------------------------------------------------- - - // Select the DB... assuming a database name is specified in the config file - if ($this->database != '') - { - if ( ! $this->db_select()) - { - log_message('error', 'Unable to select database: '.$this->database); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_select', $this->database); - } - return FALSE; - } - else - { - // We've selected the DB. Now we set the character set - if ( ! $this->db_set_charset($this->char_set, $this->dbcollat)) - { - return FALSE; - } - - return TRUE; - } - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Set client character set - * - * @access public - * @param string - * @param string - * @return resource - */ - function db_set_charset($charset, $collation) - { - if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat)) - { - log_message('error', 'Unable to set database connection charset: '.$this->char_set); - - if ($this->db_debug) - { - $this->display_error('db_unable_to_set_charset', $this->char_set); - } - - return FALSE; - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * The name of the platform in use (mysql, mssql, etc...) - * - * @access public - * @return string - */ - function platform() - { - return $this->dbdriver; - } - - // -------------------------------------------------------------------- - - /** - * Database Version Number. Returns a string containing the - * version of the database being used - * - * @access public - * @return string - */ - function version() - { - if (FALSE === ($sql = $this->_version())) - { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; - } - - if ($this->dbdriver == 'oci8') - { - return $sql; - } - - $query = $this->query($sql); - return $query->row('ver'); - } - - // -------------------------------------------------------------------- - - /** - * Execute the query - * - * Accepts an SQL string as input and returns a result object upon - * successful execution of a "read" type query. Returns boolean TRUE - * upon successful execution of a "write" type query. Returns boolean - * FALSE upon failure, and if the $db_debug variable is set to TRUE - * will raise an error. - * - * @access public - * @param string An SQL query string - * @param array An array of binding data - * @return mixed - */ - function query($sql, $binds = FALSE, $return_object = TRUE) - { - if ($sql == '') - { - if ($this->db_debug) - { - log_message('error', 'Invalid query: '.$sql); - return $this->display_error('db_invalid_query'); - } - return FALSE; - } - - // Verify table prefix and replace if necessary - if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) ) - { - $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); - } - - // Is query caching enabled? If the query is a "read type" - // we will load the caching class and return the previously - // cached query if it exists - if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) - { - if ($this->_cache_init()) - { - $this->load_rdriver(); - if (FALSE !== ($cache = $this->CACHE->read($sql))) - { - return $cache; - } - } - } - - // Compile binds if needed - if ($binds !== FALSE) - { - $sql = $this->compile_binds($sql, $binds); - } - - // Save the query for debugging - if ($this->save_queries == TRUE) - { - $this->queries[] = $sql; - } - - // Start the Query Timer - $time_start = list($sm, $ss) = explode(' ', microtime()); - - // Run the Query - if (FALSE === ($this->result_id = $this->simple_query($sql))) - { - if ($this->save_queries == TRUE) - { - $this->query_times[] = 0; - } - - // This will trigger a rollback if transactions are being used - $this->_trans_status = FALSE; - - if ($this->db_debug) - { - // grab the error number and message now, as we might run some - // additional queries before displaying the error - $error_no = $this->_error_number(); - $error_msg = $this->_error_message(); - - // We call this function in order to roll-back queries - // if transactions are enabled. If we don't call this here - // the error message will trigger an exit, causing the - // transactions to remain in limbo. - $this->trans_complete(); - - // Log and display errors - log_message('error', 'Query error: '.$error_msg); - return $this->display_error( - array( - 'Error Number: '.$error_no, - $error_msg, - $sql - ) - ); - } - - return FALSE; - } - - // Stop and aggregate the query time results - $time_end = list($em, $es) = explode(' ', microtime()); - $this->benchmark += ($em + $es) - ($sm + $ss); - - if ($this->save_queries == TRUE) - { - $this->query_times[] = ($em + $es) - ($sm + $ss); - } - - // Increment the query counter - $this->query_count++; - - // Was the query a "write" type? - // If so we'll simply return true - if ($this->is_write_type($sql) === TRUE) - { - // If caching is enabled we'll auto-cleanup any - // existing files related to this particular URI - if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) - { - $this->CACHE->delete(); - } - - return TRUE; - } - - // Return TRUE if we don't need to create a result object - // Currently only the Oracle driver uses this when stored - // procedures are used - if ($return_object !== TRUE) - { - return TRUE; - } - - // Load and instantiate the result driver - - $driver = $this->load_rdriver(); - $RES = new $driver(); - $RES->conn_id = $this->conn_id; - $RES->result_id = $this->result_id; - - if ($this->dbdriver == 'oci8') - { - $RES->stmt_id = $this->stmt_id; - $RES->curs_id = NULL; - $RES->limit_used = $this->limit_used; - $this->stmt_id = FALSE; - } - - // oci8 vars must be set before calling this - $RES->num_rows = $RES->num_rows(); - - // Is query caching enabled? If so, we'll serialize the - // result object and save it to a cache file. - if ($this->cache_on == TRUE AND $this->_cache_init()) - { - // We'll create a new instance of the result object - // only without the platform specific driver since - // we can't use it with cached data (the query result - // resource ID won't be any good once we've cached the - // result object, so we'll have to compile the data - // and save it) - $CR = new CI_DB_result(); - $CR->num_rows = $RES->num_rows(); - $CR->result_object = $RES->result_object(); - $CR->result_array = $RES->result_array(); - - // Reset these since cached objects can not utilize resource IDs. - $CR->conn_id = NULL; - $CR->result_id = NULL; - - $this->CACHE->write($sql, $CR); - } - - return $RES; - } - - // -------------------------------------------------------------------- - - /** - * Load the result drivers - * - * @access public - * @return string the name of the result class - */ - function load_rdriver() - { - $driver = 'CI_DB_'.$this->dbdriver.'_result'; - - if ( ! class_exists($driver)) - { - include_once(BASEPATH.'database/DB_result'.EXT); - include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); - } - - return $driver; - } - - // -------------------------------------------------------------------- - - /** - * Simple Query - * This is a simplified version of the query() function. Internally - * we only use it when running transaction commands since they do - * not require all the features of the main query() function. - * - * @access public - * @param string the sql query - * @return mixed - */ - function simple_query($sql) - { - if ( ! $this->conn_id) - { - $this->initialize(); - } - - return $this->_execute($sql); - } - - // -------------------------------------------------------------------- - - /** - * Disable Transactions - * This permits transactions to be disabled at run-time. - * - * @access public - * @return void - */ - function trans_off() - { - $this->trans_enabled = FALSE; - } - - // -------------------------------------------------------------------- - - /** - * Enable/disable Transaction Strict Mode - * When strict mode is enabled, if you are running multiple groups of - * transactions, if one group fails all groups will be rolled back. - * If strict mode is disabled, each group is treated autonomously, meaning - * a failure of one group will not affect any others - * - * @access public - * @return void - */ - function trans_strict($mode = TRUE) - { - $this->trans_strict = is_bool($mode) ? $mode : TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Start Transaction - * - * @access public - * @return void - */ - function trans_start($test_mode = FALSE) - { - if ( ! $this->trans_enabled) - { - return FALSE; - } - - // When transactions are nested we only begin/commit/rollback the outermost ones - if ($this->_trans_depth > 0) - { - $this->_trans_depth += 1; - return; - } - - $this->trans_begin($test_mode); - } - - // -------------------------------------------------------------------- - - /** - * Complete Transaction - * - * @access public - * @return bool - */ - function trans_complete() - { - if ( ! $this->trans_enabled) - { - return FALSE; - } - - // When transactions are nested we only begin/commit/rollback the outermost ones - if ($this->_trans_depth > 1) - { - $this->_trans_depth -= 1; - return TRUE; - } - - // The query() function will set this flag to FALSE in the event that a query failed - if ($this->_trans_status === FALSE) - { - $this->trans_rollback(); - - // If we are NOT running in strict mode, we will reset - // the _trans_status flag so that subsequent groups of transactions - // will be permitted. - if ($this->trans_strict === FALSE) - { - $this->_trans_status = TRUE; - } - - log_message('debug', 'DB Transaction Failure'); - return FALSE; - } - - $this->trans_commit(); - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Lets you retrieve the transaction flag to determine if it has failed - * - * @access public - * @return bool - */ - function trans_status() - { - return $this->_trans_status; - } - - // -------------------------------------------------------------------- - - /** - * Compile Bindings - * - * @access public - * @param string the sql statement - * @param array an array of bind data - * @return string - */ - function compile_binds($sql, $binds) - { - if (strpos($sql, $this->bind_marker) === FALSE) - { - return $sql; - } - - if ( ! is_array($binds)) - { - $binds = array($binds); - } - - // Get the sql segments around the bind markers - $segments = explode($this->bind_marker, $sql); - - // The count of bind should be 1 less then the count of segments - // If there are more bind arguments trim it down - if (count($binds) >= count($segments)) { - $binds = array_slice($binds, 0, count($segments)-1); - } - - // Construct the binded query - $result = $segments[0]; - $i = 0; - foreach ($binds as $bind) - { - $result .= $this->escape($bind); - $result .= $segments[++$i]; - } - - return $result; - } - - // -------------------------------------------------------------------- - - /** - * Determines if a query is a "write" type. - * - * @access public - * @param string An SQL query string - * @return boolean - */ - function is_write_type($sql) - { - if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) - { - return FALSE; - } - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Calculate the aggregate query elapsed time - * - * @access public - * @param integer The number of decimal places - * @return integer - */ - function elapsed_time($decimals = 6) - { - return number_format($this->benchmark, $decimals); - } - - // -------------------------------------------------------------------- - - /** - * Returns the total number of queries - * - * @access public - * @return integer - */ - function total_queries() - { - return $this->query_count; - } - - // -------------------------------------------------------------------- - - /** - * Returns the last query that was executed - * - * @access public - * @return void - */ - function last_query() - { - return end($this->queries); - } - - // -------------------------------------------------------------------- - - /** - * "Smart" Escape String - * - * Escapes data based on type - * Sets boolean and null types - * - * @access public - * @param string - * @return integer - */ - function escape($str) - { - switch (gettype($str)) - { - case 'string' : $str = "'".$this->escape_str($str)."'"; - break; - case 'boolean' : $str = ($str === FALSE) ? 0 : 1; - break; - default : $str = ($str === NULL) ? 'NULL' : $str; - break; - } - - return $str; - } - - // -------------------------------------------------------------------- - - /** - * Primary - * - * Retrieves the primary key. It assumes that the row in the first - * position is the primary key - * - * @access public - * @param string the table name - * @return string - */ - function primary($table = '') - { - $fields = $this->list_fields($table); - - if ( ! is_array($fields)) - { - return FALSE; - } - - return current($fields); - } - - // -------------------------------------------------------------------- - - /** - * Returns an array of table names - * - * @access public - * @return array - */ - function list_tables($constrain_by_prefix = FALSE) - { - // Is there a cached result? - if (isset($this->data_cache['table_names'])) - { - return $this->data_cache['table_names']; - } - - if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) - { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; - } - - $retval = array(); - $query = $this->query($sql); - - if ($query->num_rows() > 0) - { - foreach($query->result_array() as $row) - { - if (isset($row['TABLE_NAME'])) - { - $retval[] = $row['TABLE_NAME']; - } - else - { - $retval[] = array_shift($row); - } - } - } - - $this->data_cache['table_names'] = $retval; - return $this->data_cache['table_names']; - } - - // -------------------------------------------------------------------- - - /** - * Determine if a particular table exists - * @access public - * @return boolean - */ - function table_exists($table_name) - { - return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Fetch MySQL Field Names - * - * @access public - * @param string the table name - * @return array - */ - function list_fields($table = '') - { - // Is there a cached result? - if (isset($this->data_cache['field_names'][$table])) - { - return $this->data_cache['field_names'][$table]; - } - - if ($table == '') - { - if ($this->db_debug) - { - return $this->display_error('db_field_param_missing'); - } - return FALSE; - } - - if (FALSE === ($sql = $this->_list_columns($this->_protect_identifiers($table, TRUE, NULL, FALSE)))) - { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; - } - - $query = $this->query($sql); - - $retval = array(); - foreach($query->result_array() as $row) - { - if (isset($row['COLUMN_NAME'])) - { - $retval[] = $row['COLUMN_NAME']; - } - else - { - $retval[] = current($row); - } - } - - $this->data_cache['field_names'][$table] = $retval; - return $this->data_cache['field_names'][$table]; - } - - // -------------------------------------------------------------------- - - /** - * Determine if a particular field exists - * @access public - * @param string - * @param string - * @return boolean - */ - function field_exists($field_name, $table_name) - { - return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Returns an object with field data - * - * @access public - * @param string the table name - * @return object - */ - function field_data($table = '') - { - if ($table == '') - { - if ($this->db_debug) - { - return $this->display_error('db_field_param_missing'); - } - return FALSE; - } - - $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); - - return $query->field_data(); - } - - // -------------------------------------------------------------------- - - /** - * Generate an insert string - * - * @access public - * @param string the table upon which the query will be performed - * @param array an associative array data of key/values - * @return string - */ - function insert_string($table, $data) - { - $fields = array(); - $values = array(); - - foreach($data as $key => $val) - { - $fields[] = $this->_escape_identifiers($key); - $values[] = $this->escape($val); - } - - return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); - } - - // -------------------------------------------------------------------- - - /** - * Generate an update string - * - * @access public - * @param string the table upon which the query will be performed - * @param array an associative array data of key/values - * @param mixed the "where" statement - * @return string - */ - function update_string($table, $data, $where) - { - if ($where == '') - { - return false; - } - - $fields = array(); - foreach($data as $key => $val) - { - $fields[$this->_protect_identifiers($key)] = $this->escape($val); - } - - if ( ! is_array($where)) - { - $dest = array($where); - } - else - { - $dest = array(); - foreach ($where as $key => $val) - { - $prefix = (count($dest) == 0) ? '' : ' AND '; - - if ($val !== '') - { - if ( ! $this->_has_operator($key)) - { - $key .= ' ='; - } - - $val = ' '.$this->escape($val); - } - - $dest[] = $prefix.$key.$val; - } - } - - return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest); - } - - // -------------------------------------------------------------------- - - /** - * Tests whether the string has an SQL operator - * - * @access private - * @param string - * @return bool - */ - function _has_operator($str) - { - $str = trim($str); - if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) - { - return FALSE; - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Enables a native PHP function to be run, using a platform agnostic wrapper. - * - * @access public - * @param string the function name - * @param mixed any parameters needed by the function - * @return mixed - */ - function call_function($function) - { - $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; - - if (FALSE === strpos($driver, $function)) - { - $function = $driver.$function; - } - - if ( ! function_exists($function)) - { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; - } - else - { - $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; - - return call_user_func_array($function, $args); - } - } - - // -------------------------------------------------------------------- - - /** - * Set Cache Directory Path - * - * @access public - * @param string the path to the cache directory - * @return void - */ - function cache_set_path($path = '') - { - $this->cachedir = $path; - } - - // -------------------------------------------------------------------- - - /** - * Enable Query Caching - * - * @access public - * @return void - */ - function cache_on() - { - $this->cache_on = TRUE; - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Disable Query Caching - * - * @access public - * @return void - */ - function cache_off() - { - $this->cache_on = FALSE; - return FALSE; - } - - - // -------------------------------------------------------------------- - - /** - * Delete the cache files associated with a particular URI - * - * @access public - * @return void - */ - function cache_delete($segment_one = '', $segment_two = '') - { - if ( ! $this->_cache_init()) - { - return FALSE; - } - return $this->CACHE->delete($segment_one, $segment_two); - } - - // -------------------------------------------------------------------- - - /** - * Delete All cache files - * - * @access public - * @return void - */ - function cache_delete_all() - { - if ( ! $this->_cache_init()) - { - return FALSE; - } - - return $this->CACHE->delete_all(); - } - - // -------------------------------------------------------------------- - - /** - * Initialize the Cache Class - * - * @access private - * @return void - */ - function _cache_init() - { - if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) - { - return TRUE; - } - - if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) - { - return $this->cache_off(); - } - - $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Close DB Connection - * - * @access public - * @return void - */ - function close() - { - if (is_resource($this->conn_id) OR is_object($this->conn_id)) - { - $this->_close($this->conn_id); - } - $this->conn_id = FALSE; - } - - // -------------------------------------------------------------------- - - /** - * Display an error message - * - * @access public - * @param string the error message - * @param string any "swap" values - * @param boolean whether to localize the message - * @return string sends the application/error_db.php template - */ - function display_error($error = '', $swap = '', $native = FALSE) - { - $LANG =& load_class('Language'); - $LANG->load('db'); - - $heading = $LANG->line('db_error_heading'); - - if ($native == TRUE) - { - $message = $error; - } - else - { - $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; - } - - $error =& load_class('Exceptions'); - echo $error->show_error($heading, $message, 'error_db'); - exit; - } - - // -------------------------------------------------------------------- - - /** - * Protect Identifiers - * - * This function adds backticks if appropriate based on db type - * - * @access private - * @param mixed the item to escape - * @return mixed the item with backticks - */ - function protect_identifiers($item, $prefix_single = FALSE) - { - return $this->_protect_identifiers($item, $prefix_single); - } - - // -------------------------------------------------------------------- - - /** - * Protect Identifiers - * - * This function is used extensively by the Active Record class, and by - * a couple functions in this class. - * It takes a column or table name (optionally with an alias) and inserts - * the table prefix onto it. Some logic is necessary in order to deal with - * column names that include the path. Consider a query like this: - * - * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table - * - * Or a query with aliasing: - * - * SELECT m.member_id, m.member_name FROM members AS m - * - * Since the column name can include up to four segments (host, DB, table, column) - * or also have an alias prefix, we need to do a bit of work to figure this out and - * insert the table prefix (if it exists) in the proper position, and escape only - * the correct identifiers. - * - * @access private - * @param string - * @param bool - * @param mixed - * @param bool - * @return string - */ - function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) - { - if ( ! is_bool($protect_identifiers)) - { - $protect_identifiers = $this->_protect_identifiers; - } - - // Convert tabs or multiple spaces into single spaces - $item = preg_replace('/[\t| ]+/', ' ', $item); - - // If the item has an alias declaration we remove it and set it aside. - // Basically we remove everything to the right of the first space - $alias = ''; - if (strpos($item, ' ') !== FALSE) - { - $alias = strstr($item, " "); - $item = substr($item, 0, - strlen($alias)); - } - - // Break the string apart if it contains periods, then insert the table prefix - // in the correct location, assuming the period doesn't indicate that we're dealing - // with an alias. While we're at it, we will escape the components - if (strpos($item, '.') !== FALSE) - { - $parts = explode('.', $item); - - // Does the first segment of the exploded item match - // one of the aliases previously identified? If so, - // we have nothing more to do other than escape the item - if (in_array($parts[0], $this->ar_aliased_tables)) - { - if ($protect_identifiers === TRUE) - { - foreach ($parts as $key => $val) - { - if ( ! in_array($val, $this->_reserved_identifiers)) - { - $parts[$key] = $this->_escape_identifiers($val); - } - } - - $item = implode('.', $parts); - } - return $item.$alias; - } - - // Is there a table prefix defined in the config file? If not, no need to do anything - if ($this->dbprefix != '') - { - // We now add the table prefix based on some logic. - // Do we have 4 segments (hostname.database.table.column)? - // If so, we add the table prefix to the column name in the 3rd segment. - if (isset($parts[3])) - { - $i = 2; - } - // Do we have 3 segments (database.table.column)? - // If so, we add the table prefix to the column name in 2nd position - elseif (isset($parts[2])) - { - $i = 1; - } - // Do we have 2 segments (table.column)? - // If so, we add the table prefix to the column name in 1st segment - else - { - $i = 0; - } - - // This flag is set when the supplied $item does not contain a field name. - // This can happen when this function is being called from a JOIN. - if ($field_exists == FALSE) - { - $i++; - } - - // We only add the table prefix if it does not already exist - if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) - { - $parts[$i] = $this->dbprefix.$parts[$i]; - } - - // Put the parts back together - $item = implode('.', $parts); - } - - if ($protect_identifiers === TRUE) - { - $item = $this->_escape_identifiers($item); - } - - return $item.$alias; - } - - // This is basically a bug fix for queries that use MAX, MIN, etc. - // If a parenthesis is found we know that we do not need to - // escape the data or add a prefix. There's probably a more graceful - // way to deal with this, but I'm not thinking of it -- Rick - if (strpos($item, '(') !== FALSE) - { - return $item.$alias; - } - - // Is there a table prefix? If not, no need to insert it - if ($this->dbprefix != '') - { - // Do we prefix an item with no segments? - if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) - { - $item = $this->dbprefix.$item; - } - } - - if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) - { - $item = $this->_escape_identifiers($item); - } - - return $item.$alias; - } - - -} - - -/* End of file DB_driver.php */ + $val) + { + $this->$key = $val; + } + } + + log_message('debug', 'Database Driver Class Initialized'); + } + + // -------------------------------------------------------------------- + + /** + * Initialize Database Settings + * + * @access private Called by the constructor + * @param mixed + * @return void + */ + function initialize() + { + // If an existing connection resource is available + // there is no need to connect and select the database + if (is_resource($this->conn_id) OR is_object($this->conn_id)) + { + return TRUE; + } + + // ---------------------------------------------------------------- + + // Connect to the database and set the connection ID + $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); + + // No connection resource? Throw an error + if ( ! $this->conn_id) + { + log_message('error', 'Unable to connect to the database'); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_connect'); + } + return FALSE; + } + + // ---------------------------------------------------------------- + + // Select the DB... assuming a database name is specified in the config file + if ($this->database != '') + { + if ( ! $this->db_select()) + { + log_message('error', 'Unable to select database: '.$this->database); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_select', $this->database); + } + return FALSE; + } + else + { + // We've selected the DB. Now we set the character set + if ( ! $this->db_set_charset($this->char_set, $this->dbcollat)) + { + return FALSE; + } + + return TRUE; + } + } + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Set client character set + * + * @access public + * @param string + * @param string + * @return resource + */ + function db_set_charset($charset, $collation) + { + if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat)) + { + log_message('error', 'Unable to set database connection charset: '.$this->char_set); + + if ($this->db_debug) + { + $this->display_error('db_unable_to_set_charset', $this->char_set); + } + + return FALSE; + } + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * The name of the platform in use (mysql, mssql, etc...) + * + * @access public + * @return string + */ + function platform() + { + return $this->dbdriver; + } + + // -------------------------------------------------------------------- + + /** + * Database Version Number. Returns a string containing the + * version of the database being used + * + * @access public + * @return string + */ + function version() + { + if (FALSE === ($sql = $this->_version())) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + if ($this->dbdriver == 'oci8') + { + return $sql; + } + + $query = $this->query($sql); + return $query->row('ver'); + } + + // -------------------------------------------------------------------- + + /** + * Execute the query + * + * Accepts an SQL string as input and returns a result object upon + * successful execution of a "read" type query. Returns boolean TRUE + * upon successful execution of a "write" type query. Returns boolean + * FALSE upon failure, and if the $db_debug variable is set to TRUE + * will raise an error. + * + * @access public + * @param string An SQL query string + * @param array An array of binding data + * @return mixed + */ + function query($sql, $binds = FALSE, $return_object = TRUE) + { + if ($sql == '') + { + if ($this->db_debug) + { + log_message('error', 'Invalid query: '.$sql); + return $this->display_error('db_invalid_query'); + } + return FALSE; + } + + // Verify table prefix and replace if necessary + if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) ) + { + $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); + } + + // Is query caching enabled? If the query is a "read type" + // we will load the caching class and return the previously + // cached query if it exists + if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) + { + if ($this->_cache_init()) + { + $this->load_rdriver(); + if (FALSE !== ($cache = $this->CACHE->read($sql))) + { + return $cache; + } + } + } + + // Compile binds if needed + if ($binds !== FALSE) + { + $sql = $this->compile_binds($sql, $binds); + } + + // Save the query for debugging + if ($this->save_queries == TRUE) + { + $this->queries[] = $sql; + } + + // Start the Query Timer + $time_start = list($sm, $ss) = explode(' ', microtime()); + + // Run the Query + if (FALSE === ($this->result_id = $this->simple_query($sql))) + { + if ($this->save_queries == TRUE) + { + $this->query_times[] = 0; + } + + // This will trigger a rollback if transactions are being used + $this->_trans_status = FALSE; + + if ($this->db_debug) + { + // grab the error number and message now, as we might run some + // additional queries before displaying the error + $error_no = $this->_error_number(); + $error_msg = $this->_error_message(); + + // We call this function in order to roll-back queries + // if transactions are enabled. If we don't call this here + // the error message will trigger an exit, causing the + // transactions to remain in limbo. + $this->trans_complete(); + + // Log and display errors + log_message('error', 'Query error: '.$error_msg); + return $this->display_error( + array( + 'Error Number: '.$error_no, + $error_msg, + $sql + ) + ); + } + + return FALSE; + } + + // Stop and aggregate the query time results + $time_end = list($em, $es) = explode(' ', microtime()); + $this->benchmark += ($em + $es) - ($sm + $ss); + + if ($this->save_queries == TRUE) + { + $this->query_times[] = ($em + $es) - ($sm + $ss); + } + + // Increment the query counter + $this->query_count++; + + // Was the query a "write" type? + // If so we'll simply return true + if ($this->is_write_type($sql) === TRUE) + { + // If caching is enabled we'll auto-cleanup any + // existing files related to this particular URI + if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) + { + $this->CACHE->delete(); + } + + return TRUE; + } + + // Return TRUE if we don't need to create a result object + // Currently only the Oracle driver uses this when stored + // procedures are used + if ($return_object !== TRUE) + { + return TRUE; + } + + // Load and instantiate the result driver + + $driver = $this->load_rdriver(); + $RES = new $driver(); + $RES->conn_id = $this->conn_id; + $RES->result_id = $this->result_id; + + if ($this->dbdriver == 'oci8') + { + $RES->stmt_id = $this->stmt_id; + $RES->curs_id = NULL; + $RES->limit_used = $this->limit_used; + $this->stmt_id = FALSE; + } + + // oci8 vars must be set before calling this + $RES->num_rows = $RES->num_rows(); + + // Is query caching enabled? If so, we'll serialize the + // result object and save it to a cache file. + if ($this->cache_on == TRUE AND $this->_cache_init()) + { + // We'll create a new instance of the result object + // only without the platform specific driver since + // we can't use it with cached data (the query result + // resource ID won't be any good once we've cached the + // result object, so we'll have to compile the data + // and save it) + $CR = new CI_DB_result(); + $CR->num_rows = $RES->num_rows(); + $CR->result_object = $RES->result_object(); + $CR->result_array = $RES->result_array(); + + // Reset these since cached objects can not utilize resource IDs. + $CR->conn_id = NULL; + $CR->result_id = NULL; + + $this->CACHE->write($sql, $CR); + } + + return $RES; + } + + // -------------------------------------------------------------------- + + /** + * Load the result drivers + * + * @access public + * @return string the name of the result class + */ + function load_rdriver() + { + $driver = 'CI_DB_'.$this->dbdriver.'_result'; + + if ( ! class_exists($driver)) + { + include_once(BASEPATH.'database/DB_result'.EXT); + include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); + } + + return $driver; + } + + // -------------------------------------------------------------------- + + /** + * Simple Query + * This is a simplified version of the query() function. Internally + * we only use it when running transaction commands since they do + * not require all the features of the main query() function. + * + * @access public + * @param string the sql query + * @return mixed + */ + function simple_query($sql) + { + if ( ! $this->conn_id) + { + $this->initialize(); + } + + return $this->_execute($sql); + } + + // -------------------------------------------------------------------- + + /** + * Disable Transactions + * This permits transactions to be disabled at run-time. + * + * @access public + * @return void + */ + function trans_off() + { + $this->trans_enabled = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Enable/disable Transaction Strict Mode + * When strict mode is enabled, if you are running multiple groups of + * transactions, if one group fails all groups will be rolled back. + * If strict mode is disabled, each group is treated autonomously, meaning + * a failure of one group will not affect any others + * + * @access public + * @return void + */ + function trans_strict($mode = TRUE) + { + $this->trans_strict = is_bool($mode) ? $mode : TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Start Transaction + * + * @access public + * @return void + */ + function trans_start($test_mode = FALSE) + { + if ( ! $this->trans_enabled) + { + return FALSE; + } + + // When transactions are nested we only begin/commit/rollback the outermost ones + if ($this->_trans_depth > 0) + { + $this->_trans_depth += 1; + return; + } + + $this->trans_begin($test_mode); + } + + // -------------------------------------------------------------------- + + /** + * Complete Transaction + * + * @access public + * @return bool + */ + function trans_complete() + { + if ( ! $this->trans_enabled) + { + return FALSE; + } + + // When transactions are nested we only begin/commit/rollback the outermost ones + if ($this->_trans_depth > 1) + { + $this->_trans_depth -= 1; + return TRUE; + } + + // The query() function will set this flag to FALSE in the event that a query failed + if ($this->_trans_status === FALSE) + { + $this->trans_rollback(); + + // If we are NOT running in strict mode, we will reset + // the _trans_status flag so that subsequent groups of transactions + // will be permitted. + if ($this->trans_strict === FALSE) + { + $this->_trans_status = TRUE; + } + + log_message('debug', 'DB Transaction Failure'); + return FALSE; + } + + $this->trans_commit(); + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Lets you retrieve the transaction flag to determine if it has failed + * + * @access public + * @return bool + */ + function trans_status() + { + return $this->_trans_status; + } + + // -------------------------------------------------------------------- + + /** + * Compile Bindings + * + * @access public + * @param string the sql statement + * @param array an array of bind data + * @return string + */ + function compile_binds($sql, $binds) + { + if (strpos($sql, $this->bind_marker) === FALSE) + { + return $sql; + } + + if ( ! is_array($binds)) + { + $binds = array($binds); + } + + // Get the sql segments around the bind markers + $segments = explode($this->bind_marker, $sql); + + // The count of bind should be 1 less then the count of segments + // If there are more bind arguments trim it down + if (count($binds) >= count($segments)) { + $binds = array_slice($binds, 0, count($segments)-1); + } + + // Construct the binded query + $result = $segments[0]; + $i = 0; + foreach ($binds as $bind) + { + $result .= $this->escape($bind); + $result .= $segments[++$i]; + } + + return $result; + } + + // -------------------------------------------------------------------- + + /** + * Determines if a query is a "write" type. + * + * @access public + * @param string An SQL query string + * @return boolean + */ + function is_write_type($sql) + { + if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) + { + return FALSE; + } + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Calculate the aggregate query elapsed time + * + * @access public + * @param integer The number of decimal places + * @return integer + */ + function elapsed_time($decimals = 6) + { + return number_format($this->benchmark, $decimals); + } + + // -------------------------------------------------------------------- + + /** + * Returns the total number of queries + * + * @access public + * @return integer + */ + function total_queries() + { + return $this->query_count; + } + + // -------------------------------------------------------------------- + + /** + * Returns the last query that was executed + * + * @access public + * @return void + */ + function last_query() + { + return end($this->queries); + } + + // -------------------------------------------------------------------- + + /** + * "Smart" Escape String + * + * Escapes data based on type + * Sets boolean and null types + * + * @access public + * @param string + * @return integer + */ + function escape($str) + { + switch (gettype($str)) + { + case 'string' : $str = "'".$this->escape_str($str)."'"; + break; + case 'boolean' : $str = ($str === FALSE) ? 0 : 1; + break; + default : $str = ($str === NULL) ? 'NULL' : $str; + break; + } + + return $str; + } + + // -------------------------------------------------------------------- + + /** + * Primary + * + * Retrieves the primary key. It assumes that the row in the first + * position is the primary key + * + * @access public + * @param string the table name + * @return string + */ + function primary($table = '') + { + $fields = $this->list_fields($table); + + if ( ! is_array($fields)) + { + return FALSE; + } + + return current($fields); + } + + // -------------------------------------------------------------------- + + /** + * Returns an array of table names + * + * @access public + * @return array + */ + function list_tables($constrain_by_prefix = FALSE) + { + // Is there a cached result? + if (isset($this->data_cache['table_names'])) + { + return $this->data_cache['table_names']; + } + + if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + $retval = array(); + $query = $this->query($sql); + + if ($query->num_rows() > 0) + { + foreach($query->result_array() as $row) + { + if (isset($row['TABLE_NAME'])) + { + $retval[] = $row['TABLE_NAME']; + } + else + { + $retval[] = array_shift($row); + } + } + } + + $this->data_cache['table_names'] = $retval; + return $this->data_cache['table_names']; + } + + // -------------------------------------------------------------------- + + /** + * Determine if a particular table exists + * @access public + * @return boolean + */ + function table_exists($table_name) + { + return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Fetch MySQL Field Names + * + * @access public + * @param string the table name + * @return array + */ + function list_fields($table = '') + { + // Is there a cached result? + if (isset($this->data_cache['field_names'][$table])) + { + return $this->data_cache['field_names'][$table]; + } + + if ($table == '') + { + if ($this->db_debug) + { + return $this->display_error('db_field_param_missing'); + } + return FALSE; + } + + if (FALSE === ($sql = $this->_list_columns($this->_protect_identifiers($table, TRUE, NULL, FALSE)))) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + + $query = $this->query($sql); + + $retval = array(); + foreach($query->result_array() as $row) + { + if (isset($row['COLUMN_NAME'])) + { + $retval[] = $row['COLUMN_NAME']; + } + else + { + $retval[] = current($row); + } + } + + $this->data_cache['field_names'][$table] = $retval; + return $this->data_cache['field_names'][$table]; + } + + // -------------------------------------------------------------------- + + /** + * Determine if a particular field exists + * @access public + * @param string + * @param string + * @return boolean + */ + function field_exists($field_name, $table_name) + { + return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Returns an object with field data + * + * @access public + * @param string the table name + * @return object + */ + function field_data($table = '') + { + if ($table == '') + { + if ($this->db_debug) + { + return $this->display_error('db_field_param_missing'); + } + return FALSE; + } + + $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); + + return $query->field_data(); + } + + // -------------------------------------------------------------------- + + /** + * Generate an insert string + * + * @access public + * @param string the table upon which the query will be performed + * @param array an associative array data of key/values + * @return string + */ + function insert_string($table, $data) + { + $fields = array(); + $values = array(); + + foreach($data as $key => $val) + { + $fields[] = $this->_escape_identifiers($key); + $values[] = $this->escape($val); + } + + return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); + } + + // -------------------------------------------------------------------- + + /** + * Generate an update string + * + * @access public + * @param string the table upon which the query will be performed + * @param array an associative array data of key/values + * @param mixed the "where" statement + * @return string + */ + function update_string($table, $data, $where) + { + if ($where == '') + { + return false; + } + + $fields = array(); + foreach($data as $key => $val) + { + $fields[$this->_protect_identifiers($key)] = $this->escape($val); + } + + if ( ! is_array($where)) + { + $dest = array($where); + } + else + { + $dest = array(); + foreach ($where as $key => $val) + { + $prefix = (count($dest) == 0) ? '' : ' AND '; + + if ($val !== '') + { + if ( ! $this->_has_operator($key)) + { + $key .= ' ='; + } + + $val = ' '.$this->escape($val); + } + + $dest[] = $prefix.$key.$val; + } + } + + return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest); + } + + // -------------------------------------------------------------------- + + /** + * Tests whether the string has an SQL operator + * + * @access private + * @param string + * @return bool + */ + function _has_operator($str) + { + $str = trim($str); + if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) + { + return FALSE; + } + + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Enables a native PHP function to be run, using a platform agnostic wrapper. + * + * @access public + * @param string the function name + * @param mixed any parameters needed by the function + * @return mixed + */ + function call_function($function) + { + $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; + + if (FALSE === strpos($driver, $function)) + { + $function = $driver.$function; + } + + if ( ! function_exists($function)) + { + if ($this->db_debug) + { + return $this->display_error('db_unsupported_function'); + } + return FALSE; + } + else + { + $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; + + return call_user_func_array($function, $args); + } + } + + // -------------------------------------------------------------------- + + /** + * Set Cache Directory Path + * + * @access public + * @param string the path to the cache directory + * @return void + */ + function cache_set_path($path = '') + { + $this->cachedir = $path; + } + + // -------------------------------------------------------------------- + + /** + * Enable Query Caching + * + * @access public + * @return void + */ + function cache_on() + { + $this->cache_on = TRUE; + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Disable Query Caching + * + * @access public + * @return void + */ + function cache_off() + { + $this->cache_on = FALSE; + return FALSE; + } + + + // -------------------------------------------------------------------- + + /** + * Delete the cache files associated with a particular URI + * + * @access public + * @return void + */ + function cache_delete($segment_one = '', $segment_two = '') + { + if ( ! $this->_cache_init()) + { + return FALSE; + } + return $this->CACHE->delete($segment_one, $segment_two); + } + + // -------------------------------------------------------------------- + + /** + * Delete All cache files + * + * @access public + * @return void + */ + function cache_delete_all() + { + if ( ! $this->_cache_init()) + { + return FALSE; + } + + return $this->CACHE->delete_all(); + } + + // -------------------------------------------------------------------- + + /** + * Initialize the Cache Class + * + * @access private + * @return void + */ + function _cache_init() + { + if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) + { + return TRUE; + } + + if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) + { + return $this->cache_off(); + } + + $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Close DB Connection + * + * @access public + * @return void + */ + function close() + { + if (is_resource($this->conn_id) OR is_object($this->conn_id)) + { + $this->_close($this->conn_id); + } + $this->conn_id = FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Display an error message + * + * @access public + * @param string the error message + * @param string any "swap" values + * @param boolean whether to localize the message + * @return string sends the application/error_db.php template + */ + function display_error($error = '', $swap = '', $native = FALSE) + { + $LANG =& load_class('Language'); + $LANG->load('db'); + + $heading = $LANG->line('db_error_heading'); + + if ($native == TRUE) + { + $message = $error; + } + else + { + $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; + } + + $error =& load_class('Exceptions'); + echo $error->show_error($heading, $message, 'error_db'); + exit; + } + + // -------------------------------------------------------------------- + + /** + * Protect Identifiers + * + * This function adds backticks if appropriate based on db type + * + * @access private + * @param mixed the item to escape + * @return mixed the item with backticks + */ + function protect_identifiers($item, $prefix_single = FALSE) + { + return $this->_protect_identifiers($item, $prefix_single); + } + + // -------------------------------------------------------------------- + + /** + * Protect Identifiers + * + * This function is used extensively by the Active Record class, and by + * a couple functions in this class. + * It takes a column or table name (optionally with an alias) and inserts + * the table prefix onto it. Some logic is necessary in order to deal with + * column names that include the path. Consider a query like this: + * + * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table + * + * Or a query with aliasing: + * + * SELECT m.member_id, m.member_name FROM members AS m + * + * Since the column name can include up to four segments (host, DB, table, column) + * or also have an alias prefix, we need to do a bit of work to figure this out and + * insert the table prefix (if it exists) in the proper position, and escape only + * the correct identifiers. + * + * @access private + * @param string + * @param bool + * @param mixed + * @param bool + * @return string + */ + function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) + { + if ( ! is_bool($protect_identifiers)) + { + $protect_identifiers = $this->_protect_identifiers; + } + + // Convert tabs or multiple spaces into single spaces + $item = preg_replace('/[\t| ]+/', ' ', $item); + + // If the item has an alias declaration we remove it and set it aside. + // Basically we remove everything to the right of the first space + $alias = ''; + if (strpos($item, ' ') !== FALSE) + { + $alias = strstr($item, " "); + $item = substr($item, 0, - strlen($alias)); + } + + // Break the string apart if it contains periods, then insert the table prefix + // in the correct location, assuming the period doesn't indicate that we're dealing + // with an alias. While we're at it, we will escape the components + if (strpos($item, '.') !== FALSE) + { + $parts = explode('.', $item); + + // Does the first segment of the exploded item match + // one of the aliases previously identified? If so, + // we have nothing more to do other than escape the item + if (in_array($parts[0], $this->ar_aliased_tables)) + { + if ($protect_identifiers === TRUE) + { + foreach ($parts as $key => $val) + { + if ( ! in_array($val, $this->_reserved_identifiers)) + { + $parts[$key] = $this->_escape_identifiers($val); + } + } + + $item = implode('.', $parts); + } + return $item.$alias; + } + + // Is there a table prefix defined in the config file? If not, no need to do anything + if ($this->dbprefix != '') + { + // We now add the table prefix based on some logic. + // Do we have 4 segments (hostname.database.table.column)? + // If so, we add the table prefix to the column name in the 3rd segment. + if (isset($parts[3])) + { + $i = 2; + } + // Do we have 3 segments (database.table.column)? + // If so, we add the table prefix to the column name in 2nd position + elseif (isset($parts[2])) + { + $i = 1; + } + // Do we have 2 segments (table.column)? + // If so, we add the table prefix to the column name in 1st segment + else + { + $i = 0; + } + + // This flag is set when the supplied $item does not contain a field name. + // This can happen when this function is being called from a JOIN. + if ($field_exists == FALSE) + { + $i++; + } + + // We only add the table prefix if it does not already exist + if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) + { + $parts[$i] = $this->dbprefix.$parts[$i]; + } + + // Put the parts back together + $item = implode('.', $parts); + } + + if ($protect_identifiers === TRUE) + { + $item = $this->_escape_identifiers($item); + } + + return $item.$alias; + } + + // This is basically a bug fix for queries that use MAX, MIN, etc. + // If a parenthesis is found we know that we do not need to + // escape the data or add a prefix. There's probably a more graceful + // way to deal with this, but I'm not thinking of it -- Rick + if (strpos($item, '(') !== FALSE) + { + return $item.$alias; + } + + // Is there a table prefix? If not, no need to insert it + if ($this->dbprefix != '') + { + // Do we prefix an item with no segments? + if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) + { + $item = $this->dbprefix.$item; + } + } + + if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) + { + $item = $this->_escape_identifiers($item); + } + + return $item.$alias; + } + + +} + + +/* End of file DB_driver.php */ /* Location: ./system/database/DB_driver.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 911d3e0fdd26ebdcb7c862a2a39ddcaef935d6f7 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Mon, 15 Dec 2008 14:08:35 +0000 Subject: Fixed a bug in database escaping where a compound statement (ie: SUM()) wasn't handled correctly with database prefixes. --- system/database/DB_driver.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 4293acc91..fde0a435f 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1204,11 +1204,20 @@ class CI_DB_driver { // Basically we remove everything to the right of the first space $alias = ''; if (strpos($item, ' ') !== FALSE) - { + { $alias = strstr($item, " "); $item = substr($item, 0, - strlen($alias)); } + // This is basically a bug fix for queries that use MAX, MIN, etc. + // If a parenthesis is found we know that we do not need to + // escape the data or add a prefix. There's probably a more graceful + // way to deal with this, but I'm not thinking of it -- Rick + if (strpos($item, '(') !== FALSE) + { + return $item.$alias; + } + // Break the string apart if it contains periods, then insert the table prefix // in the correct location, assuming the period doesn't indicate that we're dealing // with an alias. While we're at it, we will escape the components @@ -1220,7 +1229,7 @@ class CI_DB_driver { // one of the aliases previously identified? If so, // we have nothing more to do other than escape the item if (in_array($parts[0], $this->ar_aliased_tables)) - { + { if ($protect_identifiers === TRUE) { foreach ($parts as $key => $val) @@ -1284,15 +1293,6 @@ class CI_DB_driver { return $item.$alias; } - // This is basically a bug fix for queries that use MAX, MIN, etc. - // If a parenthesis is found we know that we do not need to - // escape the data or add a prefix. There's probably a more graceful - // way to deal with this, but I'm not thinking of it -- Rick - if (strpos($item, '(') !== FALSE) - { - return $item.$alias; - } - // Is there a table prefix? If not, no need to insert it if ($this->dbprefix != '') { -- cgit v1.2.3-24-g4f1b From e37ab385f5c9ef8824d2ad4e31f544dbe6089095 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Tue, 3 Feb 2009 16:13:57 +0000 Subject: DB count_all() not returns an integer always Added some syntactical improvements within DB (braces) Fixed a bug when doing 'random' on order_by() (#5706). Fixed a bug where adding a primary key through Forge could fail (#5731). Fixed a bug when using DB cache on multiple databases (#5737). --- system/database/DB_driver.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index fde0a435f..dac4c8bdf 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -613,7 +613,7 @@ class CI_DB_driver { */ function is_write_type($sql) { - if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) + if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) { return FALSE; } @@ -1086,12 +1086,15 @@ class CI_DB_driver { { return TRUE; } - - if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) + + if ( ! class_exists('CI_DB_Cache')) { - return $this->cache_off(); + if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) + { + return $this->cache_off(); + } } - + $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects return TRUE; } @@ -1196,7 +1199,19 @@ class CI_DB_driver { { $protect_identifiers = $this->_protect_identifiers; } - + + if (is_array($item)) + { + $escaped_array = array(); + + foreach($item as $k => $v) + { + $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v); + } + + return $escaped_array; + } + // Convert tabs or multiple spaces into single spaces $item = preg_replace('/[\t| ]+/', ' ', $item); -- cgit v1.2.3-24-g4f1b From 7b3b96cff8d0b1b36caaaef5cd58fcfea5e3537e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 10 Feb 2009 21:01:47 +0000 Subject: fixed a bug in the regex pattern to converts tabs and spaces into a single space http://codeigniter.com/bug_tracker/bug/6662/ --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index dac4c8bdf..42b6a7577 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1213,7 +1213,7 @@ class CI_DB_driver { } // Convert tabs or multiple spaces into single spaces - $item = preg_replace('/[\t| ]+/', ' ', $item); + $item = preg_replace('/[\t ]+/', ' ', $item); // If the item has an alias declaration we remove it and set it aside. // Basically we remove everything to the right of the first space -- cgit v1.2.3-24-g4f1b From a377bdd47e57ddde51e90641d961f00af4a71fa1 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 11 Feb 2009 18:55:24 +0000 Subject: switched from gettype() to is_* for testing type in escape() --- system/database/DB_driver.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 42b6a7577..9385870e9 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -673,16 +673,19 @@ class CI_DB_driver { * @return integer */ function escape($str) - { - switch (gettype($str)) + { + if (is_string($str)) { - case 'string' : $str = "'".$this->escape_str($str)."'"; - break; - case 'boolean' : $str = ($str === FALSE) ? 0 : 1; - break; - default : $str = ($str === NULL) ? 'NULL' : $str; - break; - } + $str = "'".$this->escape_str($str)."'"; + } + elseif (is_bool($str)) + { + $str = ($str === FALSE) ? 0 : 1; + } + elseif (is_null($str)) + { + $str = 'NULL'; + } return $str; } -- cgit v1.2.3-24-g4f1b From e4ed583067095144eb20aefc61d4499d8386532a Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Fri, 20 Feb 2009 21:44:59 +0000 Subject: added LIKE condition escaping to all drivers and Active Record updated all DB drivers to accept arrays in escape_str() --- system/database/DB_driver.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 9385870e9..729af5bf6 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -670,7 +670,7 @@ class CI_DB_driver { * * @access public * @param string - * @return integer + * @return mixed */ function escape($str) { @@ -691,7 +691,24 @@ class CI_DB_driver { } // -------------------------------------------------------------------- + + /** + * Smart Escape LIKE String + * + * Calls the individual driver for platform + * specific escaping for LIKE conditions + * + * @access public + * @param string + * @return mixed + */ + function escape_like_str($str) + { + return $this->escape_str($str, TRUE); + } + // -------------------------------------------------------------------- + /** * Primary * -- cgit v1.2.3-24-g4f1b From bdc7fb907f04469f526868058103e35460f4e52d Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Fri, 20 Feb 2009 21:55:10 +0000 Subject: fixed docblock --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 729af5bf6..161569d3c 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -693,7 +693,7 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Smart Escape LIKE String + * Escape LIKE String * * Calls the individual driver for platform * specific escaping for LIKE conditions -- cgit v1.2.3-24-g4f1b From fc395a1046441cb584cbcfe42651dacece7eca3e Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 22 Apr 2009 14:15:09 +0000 Subject: updated copyrights to 2009 --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 161569d3c..54aa5ed7a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -6,7 +6,7 @@ * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 55acc8b4d980c43e4bde04f0e0b99a594dea28ff Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Sat, 11 Jul 2009 03:38:13 +0000 Subject: fixed a bug with _protect_identifiers() and db/swap prefixes. It was appending the prefix, but not accounting for the need to swap --- system/database/DB_driver.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 54aa5ed7a..014dfd44e 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1309,7 +1309,13 @@ class CI_DB_driver { { $i++; } - + + // Verify table prefix and replace if necessary + if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0) + { + $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]); + } + // We only add the table prefix if it does not already exist if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) { @@ -1331,6 +1337,12 @@ class CI_DB_driver { // Is there a table prefix? If not, no need to insert it if ($this->dbprefix != '') { + // Verify table prefix and replace if necessary + if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0) + { + $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item); + } + // Do we prefix an item with no segments? if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) { -- cgit v1.2.3-24-g4f1b From 3683f775bec4993b3777b012a087e82d6bff8d3f Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Wed, 16 Dec 2009 17:32:33 +0000 Subject: Change to CI's db->version() function to allow a list of exceptions for dbs with functions to return version string, vs SQL queries to return version strings. Currently this list only includes Oracle and SQLite. --- system/database/DB_driver.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 014dfd44e..aee06205b 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -215,14 +215,20 @@ class CI_DB_driver { } return FALSE; } - - if ($this->dbdriver == 'oci8') + + // Some DBs have functions that return the version, and don't run special + // SQL queries per se. In these instances, just return the result. + $driver_version_exceptions = array('oci8', 'sqlite'); + + if (in_array($this->dbdriver, $driver_version_exceptions)) { return $sql; } - - $query = $this->query($sql); - return $query->row('ver'); + else + { + $query = $this->query($sql); + return $query->row('ver'); + } } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 7f3719faf120dc15f3d7b45e132ab3192f60ad62 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 5 Jan 2010 13:35:37 +0000 Subject: updated copyrights --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index aee06205b..390d5570b 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -6,7 +6,7 @@ * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 1edde30e4443bdcb54a16bf220a5a359825ab549 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Tue, 26 Jan 2010 00:17:01 +0000 Subject: Fixing bug in DB Driver where identifiers could be escaped in some drivers that can't accept it in the list_fields() function. http://codeigniter.com/bug_tracker/bug/5865/ http://codeigniter.com/bug_tracker/bug/11218/ --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 390d5570b..d7f17ccb8 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -822,7 +822,7 @@ class CI_DB_driver { return FALSE; } - if (FALSE === ($sql = $this->_list_columns($this->_protect_identifiers($table, TRUE, NULL, FALSE)))) + if (FALSE === ($sql = $this->_list_columns($table))) { if ($this->db_debug) { -- cgit v1.2.3-24-g4f1b From e77922026939fed28a607338e24c1c37066a97b4 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Tue, 2 Mar 2010 17:24:46 -0600 Subject: whitespace and changes to load_class() to use core --- system/database/DB_driver.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index d7f17ccb8..dfef42757 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -261,10 +261,10 @@ class CI_DB_driver { // Verify table prefix and replace if necessary if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) ) - { + { $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); } - + // Is query caching enabled? If the query is a "read type" // we will load the caching class and return the previously // cached query if it exists @@ -679,7 +679,7 @@ class CI_DB_driver { * @return mixed */ function escape($str) - { + { if (is_string($str)) { $str = "'".$this->escape_str($str)."'"; @@ -697,7 +697,7 @@ class CI_DB_driver { } // -------------------------------------------------------------------- - + /** * Escape LIKE String * @@ -714,7 +714,7 @@ class CI_DB_driver { } // -------------------------------------------------------------------- - + /** * Primary * @@ -1155,7 +1155,7 @@ class CI_DB_driver { */ function display_error($error = '', $swap = '', $native = FALSE) { - $LANG =& load_class('Language'); + $LANG =& load_class('Lang', 'core'); $LANG->load('db'); $heading = $LANG->line('db_error_heading'); @@ -1169,7 +1169,7 @@ class CI_DB_driver { $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; } - $error =& load_class('Exceptions'); + $error =& load_class('Exceptions', 'core'); echo $error->show_error($heading, $message, 'error_db'); exit; } @@ -1315,13 +1315,13 @@ class CI_DB_driver { { $i++; } - + // Verify table prefix and replace if necessary if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0) { $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]); } - + // We only add the table prefix if it does not already exist if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) { -- cgit v1.2.3-24-g4f1b From 60f8c395f24ba6db80d510892bcc53ce5bf9f4eb Mon Sep 17 00:00:00 2001 From: Pascal Kriete Date: Wed, 25 Aug 2010 18:03:28 +0200 Subject: Modified the database driver's display_error() method to show the filename and line number of the failed query. --- system/database/DB_driver.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index dfef42757..8e6f88801 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1169,6 +1169,24 @@ class CI_DB_driver { $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; } + // Find the most likely culprit of the error by going through + // the backtrace until the source file is no longer in the + // database folder. + + $trace = debug_backtrace(); + + foreach($trace as $call) + { + if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE) + { + // Found it - use a relative path for safety + $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']); + $message[] = 'Line Number: '.$call['line']; + + break; + } + } + $error =& load_class('Exceptions', 'core'); echo $error->show_error($heading, $message, 'error_db'); exit; -- cgit v1.2.3-24-g4f1b From dd6719738936be31cdaa1758ca86d5eb14dcab3d Mon Sep 17 00:00:00 2001 From: Barry Mieny Date: Mon, 4 Oct 2010 16:33:58 +0200 Subject: Cleanup of stray spaces and tabs --- system/database/DB_driver.php | 332 +++++++++++++++++++++--------------------- 1 file changed, 166 insertions(+), 166 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 8e6f88801..cbfa33e78 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -71,13 +71,13 @@ class CI_DB_driver { var $limit_used; - + /** * Constructor. Accepts one parameter containing the database * connection settings. * * @param array - */ + */ function CI_DB_driver($params) { if (is_array($params)) @@ -90,7 +90,7 @@ class CI_DB_driver { log_message('debug', 'Database Driver Class Initialized'); } - + // -------------------------------------------------------------------- /** @@ -99,7 +99,7 @@ class CI_DB_driver { * @access private Called by the constructor * @param mixed * @return void - */ + */ function initialize() { // If an existing connection resource is available @@ -108,9 +108,9 @@ class CI_DB_driver { { return TRUE; } - + // ---------------------------------------------------------------- - + // Connect to the database and set the connection ID $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); @@ -118,7 +118,7 @@ class CI_DB_driver { if ( ! $this->conn_id) { log_message('error', 'Unable to connect to the database'); - + if ($this->db_debug) { $this->display_error('db_unable_to_connect'); @@ -134,12 +134,12 @@ class CI_DB_driver { if ( ! $this->db_select()) { log_message('error', 'Unable to select database: '.$this->database); - + if ($this->db_debug) { $this->display_error('db_unable_to_select', $this->database); } - return FALSE; + return FALSE; } else { @@ -148,14 +148,14 @@ class CI_DB_driver { { return FALSE; } - + return TRUE; } } return TRUE; } - + // -------------------------------------------------------------------- /** @@ -171,26 +171,26 @@ class CI_DB_driver { if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat)) { log_message('error', 'Unable to set database connection charset: '.$this->char_set); - + if ($this->db_debug) { $this->display_error('db_unable_to_set_charset', $this->char_set); } - + return FALSE; } - + return TRUE; } - + // -------------------------------------------------------------------- /** * The name of the platform in use (mysql, mssql, etc...) * * @access public - * @return string - */ + * @return string + */ function platform() { return $this->dbdriver; @@ -203,8 +203,8 @@ class CI_DB_driver { * version of the database being used * * @access public - * @return string - */ + * @return string + */ function version() { if (FALSE === ($sql = $this->_version())) @@ -230,7 +230,7 @@ class CI_DB_driver { return $query->row('ver'); } } - + // -------------------------------------------------------------------- /** @@ -245,8 +245,8 @@ class CI_DB_driver { * @access public * @param string An SQL query string * @param array An array of binding data - * @return mixed - */ + * @return mixed + */ function query($sql, $binds = FALSE, $return_object = TRUE) { if ($sql == '') @@ -279,7 +279,7 @@ class CI_DB_driver { } } } - + // Compile binds if needed if ($binds !== FALSE) { @@ -291,10 +291,10 @@ class CI_DB_driver { { $this->queries[] = $sql; } - + // Start the Query Timer $time_start = list($sm, $ss) = explode(' ', microtime()); - + // Run the Query if (FALSE === ($this->result_id = $this->simple_query($sql))) { @@ -302,7 +302,7 @@ class CI_DB_driver { { $this->query_times[] = 0; } - + // This will trigger a rollback if transactions are being used $this->_trans_status = FALSE; @@ -312,10 +312,10 @@ class CI_DB_driver { // additional queries before displaying the error $error_no = $this->_error_number(); $error_msg = $this->_error_message(); - + // We call this function in order to roll-back queries // if transactions are enabled. If we don't call this here - // the error message will trigger an exit, causing the + // the error message will trigger an exit, causing the // transactions to remain in limbo. $this->trans_complete(); @@ -329,10 +329,10 @@ class CI_DB_driver { ) ); } - + return FALSE; } - + // Stop and aggregate the query time results $time_end = list($em, $es) = explode(' ', microtime()); $this->benchmark += ($em + $es) - ($sm + $ss); @@ -341,10 +341,10 @@ class CI_DB_driver { { $this->query_times[] = ($em + $es) - ($sm + $ss); } - + // Increment the query counter $this->query_count++; - + // Was the query a "write" type? // If so we'll simply return true if ($this->is_write_type($sql) === TRUE) @@ -355,10 +355,10 @@ class CI_DB_driver { { $this->CACHE->delete(); } - + return TRUE; } - + // Return TRUE if we don't need to create a result object // Currently only the Oracle driver uses this when stored // procedures are used @@ -366,11 +366,11 @@ class CI_DB_driver { { return TRUE; } - - // Load and instantiate the result driver - - $driver = $this->load_rdriver(); - $RES = new $driver(); + + // Load and instantiate the result driver + + $driver = $this->load_rdriver(); + $RES = new $driver(); $RES->conn_id = $this->conn_id; $RES->result_id = $this->result_id; @@ -381,10 +381,10 @@ class CI_DB_driver { $RES->limit_used = $this->limit_used; $this->stmt_id = FALSE; } - + // oci8 vars must be set before calling this $RES->num_rows = $RES->num_rows(); - + // Is query caching enabled? If so, we'll serialize the // result object and save it to a cache file. if ($this->cache_on == TRUE AND $this->_cache_init()) @@ -396,17 +396,17 @@ class CI_DB_driver { // result object, so we'll have to compile the data // and save it) $CR = new CI_DB_result(); - $CR->num_rows = $RES->num_rows(); + $CR->num_rows = $RES->num_rows(); $CR->result_object = $RES->result_object(); $CR->result_array = $RES->result_array(); - + // Reset these since cached objects can not utilize resource IDs. $CR->conn_id = NULL; $CR->result_id = NULL; $this->CACHE->write($sql, $CR); } - + return $RES; } @@ -416,8 +416,8 @@ class CI_DB_driver { * Load the result drivers * * @access public - * @return string the name of the result class - */ + * @return string the name of the result class + */ function load_rdriver() { $driver = 'CI_DB_'.$this->dbdriver.'_result'; @@ -427,10 +427,10 @@ class CI_DB_driver { include_once(BASEPATH.'database/DB_result'.EXT); include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); } - + return $driver; } - + // -------------------------------------------------------------------- /** @@ -441,8 +441,8 @@ class CI_DB_driver { * * @access public * @param string the sql query - * @return mixed - */ + * @return mixed + */ function simple_query($sql) { if ( ! $this->conn_id) @@ -452,7 +452,7 @@ class CI_DB_driver { return $this->_execute($sql); } - + // -------------------------------------------------------------------- /** @@ -460,8 +460,8 @@ class CI_DB_driver { * This permits transactions to be disabled at run-time. * * @access public - * @return void - */ + * @return void + */ function trans_off() { $this->trans_enabled = FALSE; @@ -477,23 +477,23 @@ class CI_DB_driver { * a failure of one group will not affect any others * * @access public - * @return void - */ + * @return void + */ function trans_strict($mode = TRUE) { $this->trans_strict = is_bool($mode) ? $mode : TRUE; } - + // -------------------------------------------------------------------- /** * Start Transaction * * @access public - * @return void - */ + * @return void + */ function trans_start($test_mode = FALSE) - { + { if ( ! $this->trans_enabled) { return FALSE; @@ -505,7 +505,7 @@ class CI_DB_driver { $this->_trans_depth += 1; return; } - + $this->trans_begin($test_mode); } @@ -515,27 +515,27 @@ class CI_DB_driver { * Complete Transaction * * @access public - * @return bool - */ + * @return bool + */ function trans_complete() { if ( ! $this->trans_enabled) { return FALSE; } - + // When transactions are nested we only begin/commit/rollback the outermost ones if ($this->_trans_depth > 1) { $this->_trans_depth -= 1; return TRUE; } - + // The query() function will set this flag to FALSE in the event that a query failed if ($this->_trans_status === FALSE) { $this->trans_rollback(); - + // If we are NOT running in strict mode, we will reset // the _trans_status flag so that subsequent groups of transactions // will be permitted. @@ -547,7 +547,7 @@ class CI_DB_driver { log_message('debug', 'DB Transaction Failure'); return FALSE; } - + $this->trans_commit(); return TRUE; } @@ -558,8 +558,8 @@ class CI_DB_driver { * Lets you retrieve the transaction flag to determine if it has failed * * @access public - * @return bool - */ + * @return bool + */ function trans_status() { return $this->_trans_status; @@ -573,20 +573,20 @@ class CI_DB_driver { * @access public * @param string the sql statement * @param array an array of bind data - * @return string - */ + * @return string + */ function compile_binds($sql, $binds) { if (strpos($sql, $this->bind_marker) === FALSE) { return $sql; } - + if ( ! is_array($binds)) { $binds = array($binds); } - + // Get the sql segments around the bind markers $segments = explode($this->bind_marker, $sql); @@ -607,7 +607,7 @@ class CI_DB_driver { return $result; } - + // -------------------------------------------------------------------- /** @@ -615,8 +615,8 @@ class CI_DB_driver { * * @access public * @param string An SQL query string - * @return boolean - */ + * @return boolean + */ function is_write_type($sql) { if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) @@ -625,7 +625,7 @@ class CI_DB_driver { } return TRUE; } - + // -------------------------------------------------------------------- /** @@ -633,34 +633,34 @@ class CI_DB_driver { * * @access public * @param integer The number of decimal places - * @return integer - */ + * @return integer + */ function elapsed_time($decimals = 6) { return number_format($this->benchmark, $decimals); } - + // -------------------------------------------------------------------- /** * Returns the total number of queries * * @access public - * @return integer - */ + * @return integer + */ function total_queries() { return $this->query_count; } - + // -------------------------------------------------------------------- /** * Returns the last query that was executed * * @access public - * @return void - */ + * @return void + */ function last_query() { return end($this->queries); @@ -676,10 +676,10 @@ class CI_DB_driver { * * @access public * @param string - * @return mixed - */ + * @return mixed + */ function escape($str) - { + { if (is_string($str)) { $str = "'".$this->escape_str($str)."'"; @@ -703,14 +703,14 @@ class CI_DB_driver { * * Calls the individual driver for platform * specific escaping for LIKE conditions - * + * * @access public * @param string * @return mixed */ - function escape_like_str($str) - { - return $this->escape_str($str, TRUE); + function escape_like_str($str) + { + return $this->escape_str($str, TRUE); } // -------------------------------------------------------------------- @@ -723,12 +723,12 @@ class CI_DB_driver { * * @access public * @param string the table name - * @return string - */ + * @return string + */ function primary($table = '') - { + { $fields = $this->list_fields($table); - + if ( ! is_array($fields)) { return FALSE; @@ -743,8 +743,8 @@ class CI_DB_driver { * Returns an array of table names * * @access public - * @return array - */ + * @return array + */ function list_tables($constrain_by_prefix = FALSE) { // Is there a cached result? @@ -752,7 +752,7 @@ class CI_DB_driver { { return $this->data_cache['table_names']; } - + if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) { if ($this->db_debug) @@ -764,7 +764,7 @@ class CI_DB_driver { $retval = array(); $query = $this->query($sql); - + if ($query->num_rows() > 0) { foreach($query->result_array() as $row) @@ -783,7 +783,7 @@ class CI_DB_driver { $this->data_cache['table_names'] = $retval; return $this->data_cache['table_names']; } - + // -------------------------------------------------------------------- /** @@ -792,10 +792,10 @@ class CI_DB_driver { * @return boolean */ function table_exists($table_name) - { + { return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; } - + // -------------------------------------------------------------------- /** @@ -803,7 +803,7 @@ class CI_DB_driver { * * @access public * @param string the table name - * @return array + * @return array */ function list_fields($table = '') { @@ -812,7 +812,7 @@ class CI_DB_driver { { return $this->data_cache['field_names'][$table]; } - + if ($table == '') { if ($this->db_debug) @@ -821,7 +821,7 @@ class CI_DB_driver { } return FALSE; } - + if (FALSE === ($sql = $this->_list_columns($table))) { if ($this->db_debug) @@ -830,9 +830,9 @@ class CI_DB_driver { } return FALSE; } - + $query = $this->query($sql); - + $retval = array(); foreach($query->result_array() as $row) { @@ -843,9 +843,9 @@ class CI_DB_driver { else { $retval[] = current($row); - } + } } - + $this->data_cache['field_names'][$table] = $retval; return $this->data_cache['field_names'][$table]; } @@ -860,10 +860,10 @@ class CI_DB_driver { * @return boolean */ function field_exists($field_name, $table_name) - { + { return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; } - + // -------------------------------------------------------------------- /** @@ -871,8 +871,8 @@ class CI_DB_driver { * * @access public * @param string the table name - * @return object - */ + * @return object + */ function field_data($table = '') { if ($table == '') @@ -883,36 +883,36 @@ class CI_DB_driver { } return FALSE; } - + $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); return $query->field_data(); - } + } // -------------------------------------------------------------------- - + /** * Generate an insert string * * @access public * @param string the table upon which the query will be performed * @param array an associative array data of key/values - * @return string - */ + * @return string + */ function insert_string($table, $data) { $fields = array(); $values = array(); - + foreach($data as $key => $val) { $fields[] = $this->_escape_identifiers($key); $values[] = $this->escape($val); } - + return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); - } - + } + // -------------------------------------------------------------------- /** @@ -922,15 +922,15 @@ class CI_DB_driver { * @param string the table upon which the query will be performed * @param array an associative array data of key/values * @param mixed the "where" statement - * @return string - */ + * @return string + */ function update_string($table, $data, $where) { if ($where == '') { return false; } - + $fields = array(); foreach($data as $key => $val) { @@ -947,23 +947,23 @@ class CI_DB_driver { foreach ($where as $key => $val) { $prefix = (count($dest) == 0) ? '' : ' AND '; - + if ($val !== '') { if ( ! $this->_has_operator($key)) { $key .= ' ='; } - + $val = ' '.$this->escape($val); } - + $dest[] = $prefix.$key.$val; } - } + } return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest); - } + } // -------------------------------------------------------------------- @@ -993,17 +993,17 @@ class CI_DB_driver { * @access public * @param string the function name * @param mixed any parameters needed by the function - * @return mixed - */ + * @return mixed + */ function call_function($function) { $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; - + if (FALSE === strpos($driver, $function)) { $function = $driver.$function; } - + if ( ! function_exists($function)) { if ($this->db_debug) @@ -1028,7 +1028,7 @@ class CI_DB_driver { * @access public * @param string the path to the cache directory * @return void - */ + */ function cache_set_path($path = '') { $this->cachedir = $path; @@ -1041,7 +1041,7 @@ class CI_DB_driver { * * @access public * @return void - */ + */ function cache_on() { $this->cache_on = TRUE; @@ -1055,13 +1055,13 @@ class CI_DB_driver { * * @access public * @return void - */ + */ function cache_off() { $this->cache_on = FALSE; return FALSE; } - + // -------------------------------------------------------------------- @@ -1070,7 +1070,7 @@ class CI_DB_driver { * * @access public * @return void - */ + */ function cache_delete($segment_one = '', $segment_two = '') { if ( ! $this->_cache_init()) @@ -1087,7 +1087,7 @@ class CI_DB_driver { * * @access public * @return void - */ + */ function cache_delete_all() { if ( ! $this->_cache_init()) @@ -1105,7 +1105,7 @@ class CI_DB_driver { * * @access private * @return void - */ + */ function _cache_init() { if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) @@ -1131,8 +1131,8 @@ class CI_DB_driver { * Close DB Connection * * @access public - * @return void - */ + * @return void + */ function close() { if (is_resource($this->conn_id) OR is_object($this->conn_id)) @@ -1141,7 +1141,7 @@ class CI_DB_driver { } $this->conn_id = FALSE; } - + // -------------------------------------------------------------------- /** @@ -1151,8 +1151,8 @@ class CI_DB_driver { * @param string the error message * @param string any "swap" values * @param boolean whether to localize the message - * @return string sends the application/error_db.php template - */ + * @return string sends the application/error_db.php template + */ function display_error($error = '', $swap = '', $native = FALSE) { $LANG =& load_class('Lang', 'core'); @@ -1168,11 +1168,11 @@ class CI_DB_driver { { $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; } - + // Find the most likely culprit of the error by going through // the backtrace until the source file is no longer in the // database folder. - + $trace = debug_backtrace(); foreach($trace as $call) @@ -1182,11 +1182,11 @@ class CI_DB_driver { // Found it - use a relative path for safety $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']); $message[] = 'Line Number: '.$call['line']; - + break; } } - + $error =& load_class('Exceptions', 'core'); echo $error->show_error($heading, $message, 'error_db'); exit; @@ -1214,7 +1214,7 @@ class CI_DB_driver { * Protect Identifiers * * This function is used extensively by the Active Record class, and by - * a couple functions in this class. + * a couple functions in this class. * It takes a column or table name (optionally with an alias) and inserts * the table prefix onto it. Some logic is necessary in order to deal with * column names that include the path. Consider a query like this: @@ -1236,7 +1236,7 @@ class CI_DB_driver { * @param mixed * @param bool * @return string - */ + */ function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) { if ( ! is_bool($protect_identifiers)) @@ -1258,7 +1258,7 @@ class CI_DB_driver { // Convert tabs or multiple spaces into single spaces $item = preg_replace('/[\t ]+/', ' ', $item); - + // If the item has an alias declaration we remove it and set it aside. // Basically we remove everything to the right of the first space $alias = ''; @@ -1269,7 +1269,7 @@ class CI_DB_driver { } // This is basically a bug fix for queries that use MAX, MIN, etc. - // If a parenthesis is found we know that we do not need to + // If a parenthesis is found we know that we do not need to // escape the data or add a prefix. There's probably a more graceful // way to deal with this, but I'm not thinking of it -- Rick if (strpos($item, '(') !== FALSE) @@ -1283,7 +1283,7 @@ class CI_DB_driver { if (strpos($item, '.') !== FALSE) { $parts = explode('.', $item); - + // Does the first segment of the exploded item match // one of the aliases previously identified? If so, // we have nothing more to do other than escape the item @@ -1298,12 +1298,12 @@ class CI_DB_driver { $parts[$key] = $this->_escape_identifiers($val); } } - + $item = implode('.', $parts); - } + } return $item.$alias; } - + // Is there a table prefix defined in the config file? If not, no need to do anything if ($this->dbprefix != '') { @@ -1326,35 +1326,35 @@ class CI_DB_driver { { $i = 0; } - + // This flag is set when the supplied $item does not contain a field name. // This can happen when this function is being called from a JOIN. if ($field_exists == FALSE) { $i++; } - + // Verify table prefix and replace if necessary if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0) { $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]); } - + // We only add the table prefix if it does not already exist if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) { $parts[$i] = $this->dbprefix.$parts[$i]; } - + // Put the parts back together $item = implode('.', $parts); } - + if ($protect_identifiers === TRUE) { $item = $this->_escape_identifiers($item); } - + return $item.$alias; } @@ -1371,14 +1371,14 @@ class CI_DB_driver { if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) { $item = $this->dbprefix.$item; - } + } } if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) { $item = $this->_escape_identifiers($item); } - + return $item.$alias; } -- cgit v1.2.3-24-g4f1b From 741de1c1319dd13de75348863cca591713dd46ce Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Wed, 10 Nov 2010 14:52:57 -0600 Subject: Updating PHP requirements in files 5.1.6 --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index cbfa33e78..7fe5695b0 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -2,7 +2,7 @@ /** * CodeIgniter * - * An open source application development framework for PHP 4.3.2 or newer + * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team -- cgit v1.2.3-24-g4f1b From 0711dc87d98ce20d3a87f7ac43d78af8fba1dca7 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Wed, 5 Jan 2011 10:49:40 -0600 Subject: Hey look, it's 2011 --- system/database/DB_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 7fe5695b0..2d8f592e3 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -6,7 +6,7 @@ * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From c3a4a8d973b9c0a7cc935d150b8b1c6898037c45 Mon Sep 17 00:00:00 2001 From: Pascal Kriete Date: Mon, 14 Feb 2011 13:40:08 -0500 Subject: Whitespace cleanup in db classes --- system/database/DB_driver.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 2d8f592e3..e7a9de475 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -767,7 +767,7 @@ class CI_DB_driver { if ($query->num_rows() > 0) { - foreach($query->result_array() as $row) + foreach ($query->result_array() as $row) { if (isset($row['TABLE_NAME'])) { @@ -834,7 +834,7 @@ class CI_DB_driver { $query = $this->query($sql); $retval = array(); - foreach($query->result_array() as $row) + foreach ($query->result_array() as $row) { if (isset($row['COLUMN_NAME'])) { @@ -904,7 +904,7 @@ class CI_DB_driver { $fields = array(); $values = array(); - foreach($data as $key => $val) + foreach ($data as $key => $val) { $fields[] = $this->_escape_identifiers($key); $values[] = $this->escape($val); @@ -932,7 +932,7 @@ class CI_DB_driver { } $fields = array(); - foreach($data as $key => $val) + foreach ($data as $key => $val) { $fields[$this->_protect_identifiers($key)] = $this->escape($val); } @@ -1175,7 +1175,7 @@ class CI_DB_driver { $trace = debug_backtrace(); - foreach($trace as $call) + foreach ($trace as $call) { if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE) { @@ -1248,7 +1248,7 @@ class CI_DB_driver { { $escaped_array = array(); - foreach($item as $k => $v) + foreach ($item as $k => $v) { $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v); } -- cgit v1.2.3-24-g4f1b From 3a746655e92ec59ee7e731c3535673a9aedc5d3e Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Tue, 19 Apr 2011 10:59:47 -0500 Subject: Removing internal references to the EXT constant. Additionally, marked the constant as deprecated. Use ".php" instead. Also adding upgrade notes from 2.0.2 to 2.0.3. --- system/database/DB_driver.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index e7a9de475..10e8ed0c0 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -424,8 +424,8 @@ class CI_DB_driver { if ( ! class_exists($driver)) { - include_once(BASEPATH.'database/DB_result'.EXT); - include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); + include_once(BASEPATH.'database/DB_result.php'); + include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php'); } return $driver; @@ -1115,7 +1115,7 @@ class CI_DB_driver { if ( ! class_exists('CI_DB_Cache')) { - if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) + if ( ! @include(BASEPATH.'database/DB_cache.php')) { return $this->cache_off(); } -- cgit v1.2.3-24-g4f1b From 114ab0988e20ac6be39ad363ff897a1a3b85e565 Mon Sep 17 00:00:00 2001 From: Razican Date: Mon, 25 Apr 2011 17:26:45 +0200 Subject: Fixed double-space typo. --- system/database/DB_driver.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 10e8ed0c0..40be2f903 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1,4 +1,4 @@ -conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - // No connection resource? Throw an error + // No connection resource? Throw an error if ( ! $this->conn_id) { log_message('error', 'Unable to connect to the database'); @@ -199,7 +199,7 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Database Version Number. Returns a string containing the + * Database Version Number. Returns a string containing the * version of the database being used * * @access public @@ -237,7 +237,7 @@ class CI_DB_driver { * Execute the query * * Accepts an SQL string as input and returns a result object upon - * successful execution of a "read" type query. Returns boolean TRUE + * successful execution of a "read" type query. Returns boolean TRUE * upon successful execution of a "write" type query. Returns boolean * FALSE upon failure, and if the $db_debug variable is set to TRUE * will raise an error. @@ -265,7 +265,7 @@ class CI_DB_driver { $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); } - // Is query caching enabled? If the query is a "read type" + // Is query caching enabled? If the query is a "read type" // we will load the caching class and return the previously // cached query if it exists if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) @@ -286,7 +286,7 @@ class CI_DB_driver { $sql = $this->compile_binds($sql, $binds); } - // Save the query for debugging + // Save the query for debugging if ($this->save_queries == TRUE) { $this->queries[] = $sql; @@ -314,7 +314,7 @@ class CI_DB_driver { $error_msg = $this->_error_message(); // We call this function in order to roll-back queries - // if transactions are enabled. If we don't call this here + // if transactions are enabled. If we don't call this here // the error message will trigger an exit, causing the // transactions to remain in limbo. $this->trans_complete(); @@ -385,7 +385,7 @@ class CI_DB_driver { // oci8 vars must be set before calling this $RES->num_rows = $RES->num_rows(); - // Is query caching enabled? If so, we'll serialize the + // Is query caching enabled? If so, we'll serialize the // result object and save it to a cache file. if ($this->cache_on == TRUE AND $this->_cache_init()) { @@ -435,7 +435,7 @@ class CI_DB_driver { /** * Simple Query - * This is a simplified version of the query() function. Internally + * This is a simplified version of the query() function. Internally * we only use it when running transaction commands since they do * not require all the features of the main query() function. * @@ -718,7 +718,7 @@ class CI_DB_driver { /** * Primary * - * Retrieves the primary key. It assumes that the row in the first + * Retrieves the primary key. It assumes that the row in the first * position is the primary key * * @access public @@ -1216,8 +1216,8 @@ class CI_DB_driver { * This function is used extensively by the Active Record class, and by * a couple functions in this class. * It takes a column or table name (optionally with an alias) and inserts - * the table prefix onto it. Some logic is necessary in order to deal with - * column names that include the path. Consider a query like this: + * the table prefix onto it. Some logic is necessary in order to deal with + * column names that include the path. Consider a query like this: * * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table * @@ -1270,7 +1270,7 @@ class CI_DB_driver { // This is basically a bug fix for queries that use MAX, MIN, etc. // If a parenthesis is found we know that we do not need to - // escape the data or add a prefix. There's probably a more graceful + // escape the data or add a prefix. There's probably a more graceful // way to deal with this, but I'm not thinking of it -- Rick if (strpos($item, '(') !== FALSE) { @@ -1285,7 +1285,7 @@ class CI_DB_driver { $parts = explode('.', $item); // Does the first segment of the exploded item match - // one of the aliases previously identified? If so, + // one of the aliases previously identified? If so, // we have nothing more to do other than escape the item if (in_array($parts[0], $this->ar_aliased_tables)) { @@ -1304,7 +1304,7 @@ class CI_DB_driver { return $item.$alias; } - // Is there a table prefix defined in the config file? If not, no need to do anything + // Is there a table prefix defined in the config file? If not, no need to do anything if ($this->dbprefix != '') { // We now add the table prefix based on some logic. @@ -1358,7 +1358,7 @@ class CI_DB_driver { return $item.$alias; } - // Is there a table prefix? If not, no need to insert it + // Is there a table prefix? If not, no need to insert it if ($this->dbprefix != '') { // Verify table prefix and replace if necessary -- cgit v1.2.3-24-g4f1b From 4b9c62980599228f070b401c7673dce8085b0c61 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Fri, 1 Jul 2011 17:40:48 -0500 Subject: backed out 648b42a75739, which was a NON-trivial whitespace commit. It broke the Typography class's string replacements, for instance --- system/database/DB_driver.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 40be2f903..10e8ed0c0 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1,4 +1,4 @@ -conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - // No connection resource? Throw an error + // No connection resource? Throw an error if ( ! $this->conn_id) { log_message('error', 'Unable to connect to the database'); @@ -199,7 +199,7 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Database Version Number. Returns a string containing the + * Database Version Number. Returns a string containing the * version of the database being used * * @access public @@ -237,7 +237,7 @@ class CI_DB_driver { * Execute the query * * Accepts an SQL string as input and returns a result object upon - * successful execution of a "read" type query. Returns boolean TRUE + * successful execution of a "read" type query. Returns boolean TRUE * upon successful execution of a "write" type query. Returns boolean * FALSE upon failure, and if the $db_debug variable is set to TRUE * will raise an error. @@ -265,7 +265,7 @@ class CI_DB_driver { $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); } - // Is query caching enabled? If the query is a "read type" + // Is query caching enabled? If the query is a "read type" // we will load the caching class and return the previously // cached query if it exists if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) @@ -286,7 +286,7 @@ class CI_DB_driver { $sql = $this->compile_binds($sql, $binds); } - // Save the query for debugging + // Save the query for debugging if ($this->save_queries == TRUE) { $this->queries[] = $sql; @@ -314,7 +314,7 @@ class CI_DB_driver { $error_msg = $this->_error_message(); // We call this function in order to roll-back queries - // if transactions are enabled. If we don't call this here + // if transactions are enabled. If we don't call this here // the error message will trigger an exit, causing the // transactions to remain in limbo. $this->trans_complete(); @@ -385,7 +385,7 @@ class CI_DB_driver { // oci8 vars must be set before calling this $RES->num_rows = $RES->num_rows(); - // Is query caching enabled? If so, we'll serialize the + // Is query caching enabled? If so, we'll serialize the // result object and save it to a cache file. if ($this->cache_on == TRUE AND $this->_cache_init()) { @@ -435,7 +435,7 @@ class CI_DB_driver { /** * Simple Query - * This is a simplified version of the query() function. Internally + * This is a simplified version of the query() function. Internally * we only use it when running transaction commands since they do * not require all the features of the main query() function. * @@ -718,7 +718,7 @@ class CI_DB_driver { /** * Primary * - * Retrieves the primary key. It assumes that the row in the first + * Retrieves the primary key. It assumes that the row in the first * position is the primary key * * @access public @@ -1216,8 +1216,8 @@ class CI_DB_driver { * This function is used extensively by the Active Record class, and by * a couple functions in this class. * It takes a column or table name (optionally with an alias) and inserts - * the table prefix onto it. Some logic is necessary in order to deal with - * column names that include the path. Consider a query like this: + * the table prefix onto it. Some logic is necessary in order to deal with + * column names that include the path. Consider a query like this: * * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table * @@ -1270,7 +1270,7 @@ class CI_DB_driver { // This is basically a bug fix for queries that use MAX, MIN, etc. // If a parenthesis is found we know that we do not need to - // escape the data or add a prefix. There's probably a more graceful + // escape the data or add a prefix. There's probably a more graceful // way to deal with this, but I'm not thinking of it -- Rick if (strpos($item, '(') !== FALSE) { @@ -1285,7 +1285,7 @@ class CI_DB_driver { $parts = explode('.', $item); // Does the first segment of the exploded item match - // one of the aliases previously identified? If so, + // one of the aliases previously identified? If so, // we have nothing more to do other than escape the item if (in_array($parts[0], $this->ar_aliased_tables)) { @@ -1304,7 +1304,7 @@ class CI_DB_driver { return $item.$alias; } - // Is there a table prefix defined in the config file? If not, no need to do anything + // Is there a table prefix defined in the config file? If not, no need to do anything if ($this->dbprefix != '') { // We now add the table prefix based on some logic. @@ -1358,7 +1358,7 @@ class CI_DB_driver { return $item.$alias; } - // Is there a table prefix? If not, no need to insert it + // Is there a table prefix? If not, no need to insert it if ($this->dbprefix != '') { // Verify table prefix and replace if necessary -- cgit v1.2.3-24-g4f1b