From 24abcb98d4281dcf857cb08a86a58af286a2f94a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 5 Jan 2012 20:40:15 +0200 Subject: Numerous improvements to the Oracle (oci8) driver and DB_driver --- system/database/DB_driver.php | 242 +++++++++++++++++------------------------- 1 file changed, 95 insertions(+), 147 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 661b42ced..41d170875 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1,13 +1,13 @@ -_db_set_charset($this->char_set, $this->dbcollat)) { @@ -227,10 +222,9 @@ class CI_DB_driver { /** * The name of the platform in use (mysql, mssql, etc...) * - * @access public * @return string */ - function platform() + public function platform() { return $this->dbdriver; } @@ -241,10 +235,9 @@ class CI_DB_driver { * Database Version Number. Returns a string containing the * version of the database being used * - * @access public * @return string */ - function version() + public function version() { if (FALSE === ($sql = $this->_version())) { @@ -281,12 +274,11 @@ class CI_DB_driver { * 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) + public function query($sql, $binds = FALSE, $return_object = TRUE) { if ($sql == '') { @@ -410,23 +402,21 @@ class CI_DB_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; + $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->curs_id = $this->curs_id; $RES->limit_used = $this->limit_used; + // Passing the next one by reference to make sure it's updated, if needed: + $RES->commit_mode = &$this->_commit; $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()) @@ -438,9 +428,9 @@ 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->result_object = $RES->result_object(); $CR->result_array = $RES->result_array(); + $CR->num_rows = $RES->num_rows(); // Reset these since cached objects can not utilize resource IDs. $CR->conn_id = NULL; @@ -457,10 +447,9 @@ class CI_DB_driver { /** * Load the result drivers * - * @access public * @return string the name of the result class */ - function load_rdriver() + public function load_rdriver() { $driver = 'CI_DB_'.$this->dbdriver.'_result'; @@ -481,11 +470,10 @@ class CI_DB_driver { * 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) + public function simple_query($sql) { if ( ! $this->conn_id) { @@ -504,7 +492,7 @@ class CI_DB_driver { * @access public * @return void */ - function trans_off() + public function trans_off() { $this->trans_enabled = FALSE; } @@ -521,7 +509,7 @@ class CI_DB_driver { * @access public * @return void */ - function trans_strict($mode = TRUE) + public function trans_strict($mode = TRUE) { $this->trans_strict = is_bool($mode) ? $mode : TRUE; } @@ -531,10 +519,9 @@ class CI_DB_driver { /** * Start Transaction * - * @access public * @return void */ - function trans_start($test_mode = FALSE) + public function trans_start($test_mode = FALSE) { if ( ! $this->trans_enabled) { @@ -557,10 +544,9 @@ class CI_DB_driver { /** * Complete Transaction * - * @access public * @return bool */ - function trans_complete() + public function trans_complete() { if ( ! $this->trans_enabled) { @@ -604,10 +590,9 @@ class CI_DB_driver { /** * Lets you retrieve the transaction flag to determine if it has failed * - * @access public * @return bool */ - function trans_status() + public function trans_status() { return $this->_trans_status; } @@ -617,12 +602,11 @@ class CI_DB_driver { /** * Compile Bindings * - * @access public * @param string the sql statement * @param array an array of bind data * @return string */ - function compile_binds($sql, $binds) + public function compile_binds($sql, $binds) { if (strpos($sql, $this->bind_marker) === FALSE) { @@ -660,11 +644,10 @@ class CI_DB_driver { /** * Determines if a query is a "write" type. * - * @access public * @param string An SQL query string * @return boolean */ - function is_write_type($sql) + public 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)) { @@ -678,11 +661,10 @@ class CI_DB_driver { /** * Calculate the aggregate query elapsed time * - * @access public * @param integer The number of decimal places * @return integer */ - function elapsed_time($decimals = 6) + public function elapsed_time($decimals = 6) { return number_format($this->benchmark, $decimals); } @@ -692,10 +674,9 @@ class CI_DB_driver { /** * Returns the total number of queries * - * @access public * @return integer */ - function total_queries() + public function total_queries() { return $this->query_count; } @@ -705,10 +686,9 @@ class CI_DB_driver { /** * Returns the last query that was executed * - * @access public * @return void */ - function last_query() + public function last_query() { return end($this->queries); } @@ -721,11 +701,10 @@ class CI_DB_driver { * Escapes data based on type * Sets boolean and null types * - * @access public * @param string * @return mixed */ - function escape($str) + public function escape($str) { if (is_string($str)) { @@ -751,11 +730,10 @@ 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) + public function escape_like_str($str) { return $this->escape_str($str, TRUE); } @@ -768,11 +746,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 */ - function primary($table = '') + public function primary($table = '') { $fields = $this->list_fields($table); @@ -789,10 +766,9 @@ class CI_DB_driver { /** * Returns an array of table names * - * @access public * @return array */ - function list_tables($constrain_by_prefix = FALSE) + public function list_tables($constrain_by_prefix = FALSE) { // Is there a cached result? if (isset($this->data_cache['table_names'])) @@ -835,12 +811,12 @@ class CI_DB_driver { /** * Determine if a particular table exists - * @access public + * * @return boolean */ - function table_exists($table_name) + public function table_exists($table_name) { - return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; + return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables()); } // -------------------------------------------------------------------- @@ -848,11 +824,10 @@ class CI_DB_driver { /** * Fetch MySQL Field Names * - * @access public * @param string the table name * @return array */ - function list_fields($table = '') + public function list_fields($table = '') { // Is there a cached result? if (isset($this->data_cache['field_names'][$table])) @@ -901,14 +876,14 @@ class CI_DB_driver { /** * Determine if a particular field exists - * @access public + * * @param string * @param string * @return boolean */ - function field_exists($field_name, $table_name) + public 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)); } // -------------------------------------------------------------------- @@ -916,11 +891,10 @@ class CI_DB_driver { /** * Returns an object with field data * - * @access public * @param string the table name * @return object */ - function field_data($table = '') + public function field_data($table = '') { if ($table == '') { @@ -932,7 +906,6 @@ class CI_DB_driver { } $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); - return $query->field_data(); } @@ -941,12 +914,11 @@ 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 */ - function insert_string($table, $data) + public function insert_string($table, $data) { $fields = array(); $values = array(); @@ -965,13 +937,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 */ - function update_string($table, $data, $where) + public function update_string($table, $data, $where) { if ($where == '') { @@ -1018,19 +989,12 @@ class CI_DB_driver { /** * Tests whether the string has an SQL operator * - * @access private * @param string * @return bool */ - function _has_operator($str) + protected function _has_operator($str) { - $str = trim($str); - if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) - { - return FALSE; - } - - return TRUE; + return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str)); } // -------------------------------------------------------------------- @@ -1038,12 +1002,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 */ - function call_function($function) + public function call_function($function) { $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; @@ -1080,11 +1043,10 @@ class CI_DB_driver { /** * Set Cache Directory Path * - * @access public * @param string the path to the cache directory * @return void */ - function cache_set_path($path = '') + public function cache_set_path($path = '') { $this->cachedir = $path; } @@ -1094,13 +1056,11 @@ class CI_DB_driver { /** * Enable Query Caching * - * @access public * @return void */ - function cache_on() + public function cache_on() { - $this->cache_on = TRUE; - return TRUE; + return $this->cache_on = TRUE; } // -------------------------------------------------------------------- @@ -1108,13 +1068,11 @@ class CI_DB_driver { /** * Disable Query Caching * - * @access public * @return void */ - function cache_off() + public function cache_off() { - $this->cache_on = FALSE; - return FALSE; + return $this->cache_on = FALSE; } @@ -1123,10 +1081,9 @@ class CI_DB_driver { /** * Delete the cache files associated with a particular URI * - * @access public * @return void */ - function cache_delete($segment_one = '', $segment_two = '') + public function cache_delete($segment_one = '', $segment_two = '') { if ( ! $this->_cache_init()) { @@ -1140,10 +1097,9 @@ class CI_DB_driver { /** * Delete All cache files * - * @access public * @return void */ - function cache_delete_all() + public function cache_delete_all() { if ( ! $this->_cache_init()) { @@ -1158,22 +1114,18 @@ class CI_DB_driver { /** * Initialize the Cache Class * - * @access private * @return void */ - function _cache_init() + private function _cache_init() { if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) { return TRUE; } - if ( ! class_exists('CI_DB_Cache')) + if ( ! class_exists('CI_DB_Cache') AND ! @include(BASEPATH.'database/DB_cache.php')) { - if ( ! @include(BASEPATH.'database/DB_cache.php')) - { - return $this->cache_off(); - } + return $this->cache_off(); } $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects @@ -1185,10 +1137,9 @@ class CI_DB_driver { /** * Close DB Connection * - * @access public * @return void */ - function close() + public function close() { if (is_resource($this->conn_id) OR is_object($this->conn_id)) { @@ -1202,13 +1153,12 @@ 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 */ - function display_error($error = '', $swap = '', $native = FALSE) + public function display_error($error = '', $swap = '', $native = FALSE) { $LANG =& load_class('Lang', 'core'); $LANG->load('db'); @@ -1254,11 +1204,10 @@ class CI_DB_driver { * * 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) + public function protect_identifiers($item, $prefix_single = FALSE) { return $this->_protect_identifiers($item, $prefix_single); } @@ -1285,14 +1234,14 @@ class CI_DB_driver { * insert the table prefix (if it exists) in the proper position, and escape only * the correct identifiers. * - * @access private + * @access public (DB Forge needs it to be public!) * @param string * @param bool * @param mixed * @param bool * @return string */ - function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) + public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) { if ( ! is_bool($protect_identifiers)) { @@ -1440,6 +1389,5 @@ class CI_DB_driver { } - /* End of file DB_driver.php */ /* Location: ./system/database/DB_driver.php */ -- cgit v1.2.3-24-g4f1b From 7b62bff30cd80b6ddde4489f8a59948766334c20 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 5 Jan 2012 20:43:57 +0200 Subject: Fix a method description --- 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 41d170875..e667c25d3 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -822,7 +822,7 @@ class CI_DB_driver { // -------------------------------------------------------------------- /** - * Fetch MySQL Field Names + * Fetch Field Names * * @param string the table name * @return array -- cgit v1.2.3-24-g4f1b From 1ab62aedc09cc75ac5619d5e881076cc9ca5e090 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 20 Jan 2012 13:04:10 +0200 Subject: Replaced AND with && --- system/database/DB_driver.php | 53 ++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 29 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index e667c25d3..7f5ec1e20 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -106,7 +106,7 @@ class CI_DB_driver { * Initialize Database Settings * * @param mixed - * @return void + * @return bool */ public function initialize() { @@ -198,7 +198,7 @@ class CI_DB_driver { * * @param string * @param string - * @return resource + * @return bool */ public function db_set_charset($charset, $collation) { @@ -292,15 +292,15 @@ class CI_DB_driver { } // Verify table prefix and replace if necessary - if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) ) + if (($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre)) { - $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); + $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_on == TRUE && stristr($sql, 'SELECT')) { if ($this->_cache_init()) { @@ -385,7 +385,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_autodel == TRUE AND $this->_cache_init()) + if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init()) { $this->CACHE->delete(); } @@ -419,7 +419,7 @@ 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 AND $this->_cache_init()) + if ($this->cache_on == TRUE && $this->_cache_init()) { // We'll create a new instance of the result object // only without the platform specific driver since @@ -489,7 +489,6 @@ class CI_DB_driver { * Disable Transactions * This permits transactions to be disabled at run-time. * - * @access public * @return void */ public function trans_off() @@ -501,12 +500,12 @@ 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 */ public function trans_strict($mode = TRUE) @@ -645,15 +644,11 @@ class CI_DB_driver { * Determines if a query is a "write" type. * * @param string An SQL query string - * @return boolean + * @return bool */ public 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)) - { - return FALSE; - } - return TRUE; + return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql); } // -------------------------------------------------------------------- @@ -661,8 +656,8 @@ class CI_DB_driver { /** * Calculate the aggregate query elapsed time * - * @param integer The number of decimal places - * @return integer + * @param int The number of decimal places + * @return string */ public function elapsed_time($decimals = 6) { @@ -743,7 +738,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 * * @param string the table name @@ -946,7 +941,7 @@ class CI_DB_driver { { if ($where == '') { - return false; + return FALSE; } $fields = array(); @@ -1118,12 +1113,12 @@ class CI_DB_driver { */ private function _cache_init() { - if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) + if (is_object($this->CACHE) && class_exists('CI_DB_Cache')) { return TRUE; } - if ( ! class_exists('CI_DB_Cache') AND ! @include(BASEPATH.'database/DB_cache.php')) + if ( ! class_exists('CI_DB_Cache') && ! @include(BASEPATH.'database/DB_cache.php')) { return $this->cache_off(); } @@ -1221,7 +1216,7 @@ class CI_DB_driver { * 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: + * column names that include the path. Consider a query like this: * * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table * @@ -1289,7 +1284,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)) { @@ -1308,7 +1303,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. @@ -1341,7 +1336,7 @@ class CI_DB_driver { // 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]); + $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 @@ -1362,23 +1357,23 @@ 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 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); + $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) + if ($prefix_single == TRUE && substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) { $item = $this->dbprefix.$item; } } - if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) + if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers)) { $item = $this->_escape_identifiers($item); } -- cgit v1.2.3-24-g4f1b From af5d5586a5040de24335e483edb17a2657ddeb21 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 25 Jan 2012 21:34:47 +0200 Subject: Improve the base database driver class --- system/database/DB_driver.php | 502 ++++++++++++++++-------------------------- 1 file changed, 189 insertions(+), 313 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 661b42ced..d9d83d55d 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1,13 +1,13 @@ -conn_id) OR is_object($this->conn_id)) + /* If an established connection is available, then there's + * no need to connect and select the database. + * + * Depending on the database driver, conn_id can be either + * boolean TRUE, a resource or an object. + */ + if ($this->conn_id) { return TRUE; } @@ -126,7 +123,7 @@ class CI_DB_driver { // Connect to the database and set the connection ID $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); - // No connection resource? Check if there is a failover else throw an error + // No connection resource? Check if there is a failover else throw an error if ( ! $this->conn_id) { // Check if there is a failover set @@ -200,12 +197,11 @@ class CI_DB_driver { /** * Set client character set * - * @access public * @param string * @param string - * @return resource + * @return bool */ - function db_set_charset($charset, $collation) + public function db_set_charset($charset, $collation) { if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat)) { @@ -227,10 +223,9 @@ class CI_DB_driver { /** * The name of the platform in use (mysql, mssql, etc...) * - * @access public * @return string */ - function platform() + public function platform() { return $this->dbdriver; } @@ -238,21 +233,16 @@ 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 * @return string */ - function version() + public function version() { if (FALSE === ($sql = $this->_version())) { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; } // Some DBs have functions that return the version, and don't run special @@ -266,7 +256,8 @@ class CI_DB_driver { else { $query = $this->query($sql); - return $query->row('ver'); + $query = $query->row(); + return $query->ver; } } @@ -281,42 +272,34 @@ class CI_DB_driver { * 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) + public function query($sql, $binds = FALSE, $return_object = TRUE) { if ($sql == '') { log_message('error', 'Invalid query: '.$sql); - if ($this->db_debug) - { - return $this->display_error('db_invalid_query'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_invalid_query') : FALSE; } // Verify table prefix and replace if necessary - if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) ) + if ( ($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre) ) { - $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); + $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')) + if ($this->cache_on == TRUE && stripos($sql, 'SELECT') !== FALSE && $this->_cache_init()) { - if ($this->_cache_init()) + $this->load_rdriver(); + if (FALSE !== ($cache = $this->CACHE->read($sql))) { - $this->load_rdriver(); - if (FALSE !== ($cache = $this->CACHE->read($sql))) - { - return $cache; - } + return $cache; } } @@ -326,7 +309,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; @@ -357,19 +340,13 @@ class CI_DB_driver { 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 + // 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(); // Display errors - return $this->display_error( - array( - 'Error Number: '.$error_no, - $error_msg, - $sql - ) - ); + return $this->display_error(array('Error Number: '.$error_no, $error_msg, $sql)); } return FALSE; @@ -393,7 +370,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_autodel == TRUE AND $this->_cache_init()) + if ($this->cache_on == TRUE && $this->cache_autodel == TRUE && $this->_cache_init()) { $this->CACHE->delete(); } @@ -410,13 +387,12 @@ 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->result_id = $this->result_id; - if ($this->dbdriver == 'oci8') + if ($this->dbdriver === 'oci8') { $RES->stmt_id = $this->stmt_id; $RES->curs_id = NULL; @@ -427,9 +403,9 @@ 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()) + if ($this->cache_on == TRUE && $this->_cache_init()) { // We'll create a new instance of the result object // only without the platform specific driver since @@ -438,9 +414,9 @@ 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->result_object = $RES->result_object(); $CR->result_array = $RES->result_array(); + $CR->num_rows = $RES->num_rows(); // Reset these since cached objects can not utilize resource IDs. $CR->conn_id = NULL; @@ -457,10 +433,9 @@ class CI_DB_driver { /** * Load the result drivers * - * @access public * @return string the name of the result class */ - function load_rdriver() + public function load_rdriver() { $driver = 'CI_DB_'.$this->dbdriver.'_result'; @@ -477,15 +452,14 @@ 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. * - * @access public * @param string the sql query * @return mixed */ - function simple_query($sql) + public function simple_query($sql) { if ( ! $this->conn_id) { @@ -501,10 +475,9 @@ class CI_DB_driver { * Disable Transactions * This permits transactions to be disabled at run-time. * - * @access public * @return void */ - function trans_off() + public function trans_off() { $this->trans_enabled = FALSE; } @@ -518,10 +491,9 @@ class CI_DB_driver { * 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) + public function trans_strict($mode = TRUE) { $this->trans_strict = is_bool($mode) ? $mode : TRUE; } @@ -531,10 +503,9 @@ class CI_DB_driver { /** * Start Transaction * - * @access public * @return void */ - function trans_start($test_mode = FALSE) + public function trans_start($test_mode = FALSE) { if ( ! $this->trans_enabled) { @@ -557,10 +528,9 @@ class CI_DB_driver { /** * Complete Transaction * - * @access public * @return bool */ - function trans_complete() + public function trans_complete() { if ( ! $this->trans_enabled) { @@ -604,10 +574,9 @@ class CI_DB_driver { /** * Lets you retrieve the transaction flag to determine if it has failed * - * @access public * @return bool */ - function trans_status() + public function trans_status() { return $this->_trans_status; } @@ -617,12 +586,11 @@ class CI_DB_driver { /** * Compile Bindings * - * @access public * @param string the sql statement * @param array an array of bind data * @return string */ - function compile_binds($sql, $binds) + public function compile_binds($sql, $binds) { if (strpos($sql, $this->bind_marker) === FALSE) { @@ -639,7 +607,8 @@ class CI_DB_driver { // 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)) { + if (count($binds) >= count($segments)) + { $binds = array_slice($binds, 0, count($segments)-1); } @@ -648,8 +617,7 @@ class CI_DB_driver { $i = 0; foreach ($binds as $bind) { - $result .= $this->escape($bind); - $result .= $segments[++$i]; + $result .= $this->escape($bind).$segments[++$i]; } return $result; @@ -660,17 +628,12 @@ class CI_DB_driver { /** * Determines if a query is a "write" type. * - * @access public * @param string An SQL query string - * @return boolean + * @return bool */ - function is_write_type($sql) + public 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)) - { - return FALSE; - } - return TRUE; + return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql); } // -------------------------------------------------------------------- @@ -678,11 +641,10 @@ class CI_DB_driver { /** * Calculate the aggregate query elapsed time * - * @access public - * @param integer The number of decimal places - * @return integer + * @param int The number of decimal places + * @return int */ - function elapsed_time($decimals = 6) + public function elapsed_time($decimals = 6) { return number_format($this->benchmark, $decimals); } @@ -692,10 +654,9 @@ class CI_DB_driver { /** * Returns the total number of queries * - * @access public - * @return integer + * @return int */ - function total_queries() + public function total_queries() { return $this->query_count; } @@ -705,10 +666,9 @@ class CI_DB_driver { /** * Returns the last query that was executed * - * @access public - * @return void + * @return string */ - function last_query() + public function last_query() { return end($this->queries); } @@ -721,23 +681,22 @@ class CI_DB_driver { * Escapes data based on type * Sets boolean and null types * - * @access public * @param string * @return mixed */ - function escape($str) + public function escape($str) { if (is_string($str)) { - $str = "'".$this->escape_str($str)."'"; + return "'".$this->escape_str($str)."'"; } elseif (is_bool($str)) { - $str = ($str === FALSE) ? 0 : 1; + return ($str === FALSE) ? 0 : 1; } elseif (is_null($str)) { - $str = 'NULL'; + return 'NULL'; } return $str; @@ -751,11 +710,10 @@ 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) + public function escape_like_str($str) { return $this->escape_str($str, TRUE); } @@ -765,23 +723,16 @@ 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 * @param string the table name * @return string */ - function primary($table = '') + public function primary($table = '') { $fields = $this->list_fields($table); - - if ( ! is_array($fields)) - { - return FALSE; - } - - return current($fields); + return is_array($fields) ? current($fields) : FALSE; } // -------------------------------------------------------------------- @@ -789,10 +740,9 @@ class CI_DB_driver { /** * Returns an array of table names * - * @access public * @return array */ - function list_tables($constrain_by_prefix = FALSE) + public function list_tables($constrain_by_prefix = FALSE) { // Is there a cached result? if (isset($this->data_cache['table_names'])) @@ -802,32 +752,17 @@ class CI_DB_driver { if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; } - $retval = array(); + $this->data_cache['table_names'] = array(); $query = $this->query($sql); - if ($query->num_rows() > 0) + foreach ($query->result_array() as $row) { - 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'][] = isset($row['TABLE_NAME']) ? $row['TABLE_NAME'] : array_shift($row); } - $this->data_cache['table_names'] = $retval; return $this->data_cache['table_names']; } @@ -835,12 +770,12 @@ class CI_DB_driver { /** * Determine if a particular table exists - * @access public - * @return boolean + * + * @return bool */ - function table_exists($table_name) + public function table_exists($table_name) { - return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; + return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables()); } // -------------------------------------------------------------------- @@ -848,11 +783,10 @@ class CI_DB_driver { /** * Fetch MySQL Field Names * - * @access public * @param string the table name * @return array */ - function list_fields($table = '') + public function list_fields($table = '') { // Is there a cached result? if (isset($this->data_cache['field_names'][$table])) @@ -862,38 +796,22 @@ class CI_DB_driver { if ($table == '') { - if ($this->db_debug) - { - return $this->display_error('db_field_param_missing'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; } if (FALSE === ($sql = $this->_list_columns($table))) { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; } $query = $this->query($sql); + $this->data_cache['field_names'][$table] = array(); - $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][] = isset($row['COLUMN_NAME']) ? $row['COLUMN_NAME'] : current($row); } - $this->data_cache['field_names'][$table] = $retval; return $this->data_cache['field_names'][$table]; } @@ -901,14 +819,14 @@ class CI_DB_driver { /** * Determine if a particular field exists - * @access public + * * @param string * @param string - * @return boolean + * @return bool */ - function field_exists($field_name, $table_name) + public 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)); } // -------------------------------------------------------------------- @@ -916,23 +834,17 @@ class CI_DB_driver { /** * Returns an object with field data * - * @access public * @param string the table name - * @return object + * @return mixed */ - function field_data($table = '') + public function field_data($table = '') { if ($table == '') { - if ($this->db_debug) - { - return $this->display_error('db_field_param_missing'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; } $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); - return $query->field_data(); } @@ -941,15 +853,13 @@ 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 */ - function insert_string($table, $data) + public function insert_string($table, $data) { - $fields = array(); - $values = array(); + $fields = $values = array(); foreach ($data as $key => $val) { @@ -965,17 +875,16 @@ 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 */ - function update_string($table, $data, $where) + public function update_string($table, $data, $where) { if ($where == '') { - return false; + return FALSE; } $fields = array(); @@ -993,7 +902,7 @@ class CI_DB_driver { $dest = array(); foreach ($where as $key => $val) { - $prefix = (count($dest) == 0) ? '' : ' AND '; + $prefix = (count($dest) === 0) ? '' : ' AND '; $key = $this->_protect_identifiers($key); if ($val !== '') @@ -1018,19 +927,12 @@ class CI_DB_driver { /** * Tests whether the string has an SQL operator * - * @access private * @param string * @return bool */ - function _has_operator($str) + protected function _has_operator($str) { - $str = trim($str); - if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) - { - return FALSE; - } - - return TRUE; + return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str)); } // -------------------------------------------------------------------- @@ -1038,12 +940,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 */ - function call_function($function) + public function call_function($function) { $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; @@ -1054,25 +955,12 @@ class CI_DB_driver { if ( ! function_exists($function)) { - if ($this->db_debug) - { - return $this->display_error('db_unsupported_function'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_unsupported_function') : FALSE; } - else - { - $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; - if (is_null($args)) - { - return call_user_func($function); - } - else - { - return call_user_func_array($function, $args); - } - } + return (func_num_args() > 1) + ? call_user_func_array($function, array_splice(func_get_args(), 1)) + : call_user_func($function); } // -------------------------------------------------------------------- @@ -1080,11 +968,10 @@ class CI_DB_driver { /** * Set Cache Directory Path * - * @access public * @param string the path to the cache directory * @return void */ - function cache_set_path($path = '') + public function cache_set_path($path = '') { $this->cachedir = $path; } @@ -1094,13 +981,11 @@ class CI_DB_driver { /** * Enable Query Caching * - * @access public - * @return void + * @return bool cache_on value */ - function cache_on() + public function cache_on() { - $this->cache_on = TRUE; - return TRUE; + return $this->cache_on = TRUE; } // -------------------------------------------------------------------- @@ -1108,13 +993,11 @@ class CI_DB_driver { /** * Disable Query Caching * - * @access public - * @return void + * @return bool cache_on value */ - function cache_off() + public function cache_off() { - $this->cache_on = FALSE; - return FALSE; + return $this->cache_on = FALSE; } @@ -1123,15 +1006,15 @@ class CI_DB_driver { /** * Delete the cache files associated with a particular URI * - * @access public - * @return void + * @return bool */ - function cache_delete($segment_one = '', $segment_two = '') + public function cache_delete($segment_one = '', $segment_two = '') { if ( ! $this->_cache_init()) { return FALSE; } + return $this->CACHE->delete($segment_one, $segment_two); } @@ -1140,10 +1023,9 @@ class CI_DB_driver { /** * Delete All cache files * - * @access public - * @return void + * @return bool */ - function cache_delete_all() + public function cache_delete_all() { if ( ! $this->_cache_init()) { @@ -1158,23 +1040,21 @@ class CI_DB_driver { /** * Initialize the Cache Class * - * @access private * @return void */ - function _cache_init() + protected function _cache_init() { - if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) + if (class_exists('CI_DB_Cache')) { - return TRUE; - } - - if ( ! class_exists('CI_DB_Cache')) - { - if ( ! @include(BASEPATH.'database/DB_cache.php')) + if (is_object($this->CACHE)) { - return $this->cache_off(); + return TRUE; } } + elseif ( ! @include_once(BASEPATH.'database/DB_cache.php')) + { + 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; @@ -1185,16 +1065,15 @@ class CI_DB_driver { /** * Close DB Connection * - * @access public * @return void */ - function close() + public function close() { - if (is_resource($this->conn_id) OR is_object($this->conn_id)) + if ($this->conn_id) { $this->_close($this->conn_id); + $this->conn_id = FALSE; } - $this->conn_id = FALSE; } // -------------------------------------------------------------------- @@ -1202,13 +1081,12 @@ 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 + * @param bool whether to localize the message * @return string sends the application/error_db.php template */ - function display_error($error = '', $swap = '', $native = FALSE) + public function display_error($error = '', $swap = '', $native = FALSE) { $LANG =& load_class('Lang', 'core'); $LANG->load('db'); @@ -1227,9 +1105,7 @@ class CI_DB_driver { // 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) @@ -1237,7 +1113,6 @@ 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; } } @@ -1254,11 +1129,10 @@ class CI_DB_driver { * * 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) + public function protect_identifiers($item, $prefix_single = FALSE) { return $this->_protect_identifiers($item, $prefix_single); } @@ -1271,8 +1145,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 * @@ -1285,14 +1159,16 @@ class CI_DB_driver { * insert the table prefix (if it exists) in the proper position, and escape only * the correct identifiers. * - * @access private + * NOTE: This is used by DB_forge drivers and therefore needs to be public. + * (until a better solution is implemented) + * * @param string * @param bool * @param mixed * @param bool * @return string */ - function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) + public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) { if ( ! is_bool($protect_identifiers)) { @@ -1302,7 +1178,6 @@ class CI_DB_driver { if (is_array($item)) { $escaped_array = array(); - foreach ($item as $k => $v) { $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v); @@ -1316,16 +1191,19 @@ class CI_DB_driver { // 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, " "); + $alias = strstr($item, ' '); $item = substr($item, 0, - strlen($alias)); } + else + { + $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 + // 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) { @@ -1340,7 +1218,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)) { @@ -1356,10 +1234,11 @@ class CI_DB_driver { $item = implode('.', $parts); } + 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. @@ -1390,13 +1269,12 @@ class CI_DB_driver { } // Verify table prefix and replace if necessary - if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0) + if ($this->swap_pre != '' && strpos($parts[$i], $this->swap_pre) === 0) { - $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]); + $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) + elseif (strpos($parts[$i], $this->dbprefix) !== 0) { $parts[$i] = $this->dbprefix.$parts[$i]; } @@ -1417,19 +1295,18 @@ class CI_DB_driver { if ($this->dbprefix != '') { // Verify table prefix and replace if necessary - if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0) + if ($this->swap_pre != '' && strpos($item, $this->swap_pre) === 0) { - $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item); + $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) + elseif ($prefix_single == TRUE && strpos($item, $this->dbprefix) !== 0) { $item = $this->dbprefix.$item; } } - if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) + if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers)) { $item = $this->_escape_identifiers($item); } @@ -1440,6 +1317,5 @@ class CI_DB_driver { } - /* End of file DB_driver.php */ /* Location: ./system/database/DB_driver.php */ -- cgit v1.2.3-24-g4f1b From a5f2f694c6fb0cf3286a4ae44af47ac77684a36a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 25 Jan 2012 21:44:56 +0200 Subject: Pass CI_DB_driver::curs_id to CI_DB_oci8_result ... --- 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 d9d83d55d..117db68e8 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -395,7 +395,7 @@ class CI_DB_driver { if ($this->dbdriver === 'oci8') { $RES->stmt_id = $this->stmt_id; - $RES->curs_id = NULL; + $RES->curs_id = $this->curs_id; $RES->limit_used = $this->limit_used; $this->stmt_id = FALSE; } -- cgit v1.2.3-24-g4f1b From 12567e8263be2d007dc50fc94e7456c7183f8098 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 25 Jan 2012 22:50:33 +0200 Subject: Add better key/index detection for list_tables() and list_fields() --- system/database/DB_driver.php | 50 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 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 117db68e8..eb6ab7430 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -760,7 +760,30 @@ class CI_DB_driver { foreach ($query->result_array() as $row) { - $this->data_cache['table_names'][] = isset($row['TABLE_NAME']) ? $row['TABLE_NAME'] : array_shift($row); + // Do we know from which column to get the table name? + if ( ! isset($key)) + { + if (array_key_exists('table_name', $row)) + { + $key = 'table_name'; + } + elseif (array_key_exists('TABLE_NAME', $row)) + { + $key = 'TABLE_NAME'; + } + else + { + /* We have no other choice but to just get the first element's key. + * Due to array_shift() accepting it's argument by reference, if + * E_STRICT is on, this would trigger a warning. So we'll have to + * assign it first. + */ + $key = array_keys($row); + $key = array_shift($key); + } + } + + $this->data_cache['table_names'][] = $row[$key]; } return $this->data_cache['table_names']; @@ -809,7 +832,30 @@ class CI_DB_driver { foreach ($query->result_array() as $row) { - $this->data_cache['field_names'][$table][] = isset($row['COLUMN_NAME']) ? $row['COLUMN_NAME'] : current($row); + // Do we know from where to get the column's name? + if ( ! isset($key)) + { + if (array_key_exists('column_name', $row)) + { + $key = 'column_name'; + } + elseif (array_key_exists('COLUMN_NAME', $row)) + { + $key = 'COLUMN_NAME'; + } + else + { + /* We have no other choice but to just get the first element's key. + * Due to array_shift() accepting it's argument by reference, if + * E_STRICT is on, this would trigger a warning. So we'll have to + * assign it first. + */ + $key = array_keys($row); + $key = array_shift($key); + } + } + + $this->data_cache['field_names'][$table][] = $row[$key]; } return $this->data_cache['field_names'][$table]; -- cgit v1.2.3-24-g4f1b From f32110a952f8e1f58c4c0f13715681c248bc8ace Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 26 Jan 2012 11:04:11 +0200 Subject: Fix a comment typo --- 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 eb6ab7430..113c3d3fe 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -74,7 +74,7 @@ class CI_DB_driver { protected $_protect_identifiers = TRUE; protected $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped - // These are use with Oracle + // These are used with Oracle public $stmt_id; public $curs_id; public $limit_used; -- cgit v1.2.3-24-g4f1b From a7c0656a530ca405d2ed18b8e9bd252975bb8505 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 27 Jan 2012 21:18:48 +0200 Subject: Removed a few more unnecessary lines --- system/database/DB_driver.php | 11 ++--------- 1 file changed, 2 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 03fc8a698..39eb1cc8a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -180,12 +180,7 @@ class CI_DB_driver { 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 $this->db_set_charset($this->char_set, $this->dbcollat); } } @@ -247,9 +242,7 @@ class CI_DB_driver { // 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', 'cubrid', 'pdo', 'mysqli'); - - if (in_array($this->dbdriver, $driver_version_exceptions)) + if (in_array($this->dbdriver, array('oci8', 'sqlite', 'cubrid', 'pdo', 'mysqli')) { return $sql; } -- cgit v1.2.3-24-g4f1b From 569091cf6ad7101059e6a4ffbd93f227aad9937f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 9 Feb 2012 00:16:17 +0200 Subject: Revert CI_DB_driver changes (see #971) --- system/database/DB_driver.php | 287 +++++++++++++++++++++++++----------------- 1 file changed, 171 insertions(+), 116 deletions(-) (limited to 'system/database/DB_driver.php') diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index a30c8226d..daca08f0f 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1,13 +1,13 @@ -_db_set_charset($this->char_set, $this->dbcollat)) { @@ -222,9 +227,10 @@ class CI_DB_driver { /** * The name of the platform in use (mysql, mssql, etc...) * + * @access public * @return string */ - public function platform() + function platform() { return $this->dbdriver; } @@ -235,9 +241,10 @@ class CI_DB_driver { * Database Version Number. Returns a string containing the * version of the database being used * + * @access public * @return string */ - public function version() + function version() { if (FALSE === ($sql = $this->_version())) { @@ -274,11 +281,12 @@ class CI_DB_driver { * 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 */ - public function query($sql, $binds = FALSE, $return_object = TRUE) + function query($sql, $binds = FALSE, $return_object = TRUE) { if ($sql == '') { @@ -292,15 +300,15 @@ class CI_DB_driver { } // Verify table prefix and replace if necessary - if (($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre)) + 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); + $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 && stristr($sql, 'SELECT')) + if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) { if ($this->_cache_init()) { @@ -385,7 +393,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 && $this->cache_autodel == TRUE && $this->_cache_init()) + if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) { $this->CACHE->delete(); } @@ -404,10 +412,10 @@ class CI_DB_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; + $RES->conn_id = $this->conn_id; + $RES->result_id = $this->result_id; - if ($this->dbdriver == 'oci8') + if ($this->dbdriver === 'oci8') { $RES->stmt_id = $this->stmt_id; $RES->curs_id = $this->curs_id; @@ -419,7 +427,7 @@ 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 && $this->_cache_init()) + 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 @@ -447,9 +455,10 @@ class CI_DB_driver { /** * Load the result drivers * + * @access public * @return string the name of the result class */ - public function load_rdriver() + function load_rdriver() { $driver = 'CI_DB_'.$this->dbdriver.'_result'; @@ -470,10 +479,11 @@ class CI_DB_driver { * 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 */ - public function simple_query($sql) + function simple_query($sql) { if ( ! $this->conn_id) { @@ -489,9 +499,10 @@ class CI_DB_driver { * Disable Transactions * This permits transactions to be disabled at run-time. * + * @access public * @return void */ - public function trans_off() + function trans_off() { $this->trans_enabled = FALSE; } @@ -500,15 +511,15 @@ 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 */ - public function trans_strict($mode = TRUE) + function trans_strict($mode = TRUE) { $this->trans_strict = is_bool($mode) ? $mode : TRUE; } @@ -518,9 +529,10 @@ class CI_DB_driver { /** * Start Transaction * + * @access public * @return void */ - public function trans_start($test_mode = FALSE) + function trans_start($test_mode = FALSE) { if ( ! $this->trans_enabled) { @@ -543,9 +555,10 @@ class CI_DB_driver { /** * Complete Transaction * + * @access public * @return bool */ - public function trans_complete() + function trans_complete() { if ( ! $this->trans_enabled) { @@ -589,9 +602,10 @@ class CI_DB_driver { /** * Lets you retrieve the transaction flag to determine if it has failed * + * @access public * @return bool */ - public function trans_status() + function trans_status() { return $this->_trans_status; } @@ -601,11 +615,12 @@ class CI_DB_driver { /** * Compile Bindings * + * @access public * @param string the sql statement * @param array an array of bind data * @return string */ - public function compile_binds($sql, $binds) + function compile_binds($sql, $binds) { if (strpos($sql, $this->bind_marker) === FALSE) { @@ -643,12 +658,17 @@ class CI_DB_driver { /** * Determines if a query is a "write" type. * + * @access public * @param string An SQL query string - * @return bool + * @return boolean */ - public function is_write_type($sql) + function is_write_type($sql) { - return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|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; + } + return TRUE; } // -------------------------------------------------------------------- @@ -656,10 +676,11 @@ class CI_DB_driver { /** * Calculate the aggregate query elapsed time * - * @param int The number of decimal places - * @return string + * @access public + * @param integer The number of decimal places + * @return integer */ - public function elapsed_time($decimals = 6) + function elapsed_time($decimals = 6) { return number_format($this->benchmark, $decimals); } @@ -669,9 +690,10 @@ class CI_DB_driver { /** * Returns the total number of queries * + * @access public * @return integer */ - public function total_queries() + function total_queries() { return $this->query_count; } @@ -681,9 +703,10 @@ class CI_DB_driver { /** * Returns the last query that was executed * + * @access public * @return void */ - public function last_query() + function last_query() { return end($this->queries); } @@ -696,10 +719,11 @@ class CI_DB_driver { * Escapes data based on type * Sets boolean and null types * + * @access public * @param string * @return mixed */ - public function escape($str) + function escape($str) { if (is_string($str)) { @@ -725,10 +749,11 @@ class CI_DB_driver { * Calls the individual driver for platform * specific escaping for LIKE conditions * + * @access public * @param string * @return mixed */ - public function escape_like_str($str) + function escape_like_str($str) { return $this->escape_str($str, TRUE); } @@ -738,13 +763,14 @@ 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 * @param string the table name * @return string */ - public function primary($table = '') + function primary($table = '') { $fields = $this->list_fields($table); @@ -761,9 +787,10 @@ class CI_DB_driver { /** * Returns an array of table names * + * @access public * @return array */ - public function list_tables($constrain_by_prefix = FALSE) + function list_tables($constrain_by_prefix = FALSE) { // Is there a cached result? if (isset($this->data_cache['table_names'])) @@ -806,23 +833,24 @@ class CI_DB_driver { /** * Determine if a particular table exists - * + * @access public * @return boolean */ - public function table_exists($table_name) + function table_exists($table_name) { - return in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables()); + return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; } // -------------------------------------------------------------------- /** - * Fetch Field Names + * Fetch MySQL Field Names * + * @access public * @param string the table name * @return array */ - public function list_fields($table = '') + function list_fields($table = '') { // Is there a cached result? if (isset($this->data_cache['field_names'][$table])) @@ -871,14 +899,14 @@ class CI_DB_driver { /** * Determine if a particular field exists - * + * @access public * @param string * @param string * @return boolean */ - public function field_exists($field_name, $table_name) + function field_exists($field_name, $table_name) { - return in_array($field_name, $this->list_fields($table_name)); + return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; } // -------------------------------------------------------------------- @@ -886,10 +914,11 @@ class CI_DB_driver { /** * Returns an object with field data * + * @access public * @param string the table name * @return object */ - public function field_data($table = '') + function field_data($table = '') { if ($table == '') { @@ -901,6 +930,7 @@ class CI_DB_driver { } $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); + return $query->field_data(); } @@ -909,11 +939,12 @@ 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 */ - public function insert_string($table, $data) + function insert_string($table, $data) { $fields = array(); $values = array(); @@ -932,16 +963,17 @@ 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 */ - public function update_string($table, $data, $where) + function update_string($table, $data, $where) { if ($where == '') { - return FALSE; + return false; } $fields = array(); @@ -984,12 +1016,19 @@ class CI_DB_driver { /** * Tests whether the string has an SQL operator * + * @access private * @param string * @return bool */ - protected function _has_operator($str) + function _has_operator($str) { - return (bool) preg_match('/(\s|<|>|!|=|is null|is not null)/i', trim($str)); + $str = trim($str); + if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) + { + return FALSE; + } + + return TRUE; } // -------------------------------------------------------------------- @@ -997,11 +1036,12 @@ 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 */ - public function call_function($function) + function call_function($function) { $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; @@ -1038,10 +1078,11 @@ class CI_DB_driver { /** * Set Cache Directory Path * + * @access public * @param string the path to the cache directory * @return void */ - public function cache_set_path($path = '') + function cache_set_path($path = '') { $this->cachedir = $path; } @@ -1051,11 +1092,13 @@ class CI_DB_driver { /** * Enable Query Caching * + * @access public * @return void */ - public function cache_on() + function cache_on() { - return $this->cache_on = TRUE; + $this->cache_on = TRUE; + return TRUE; } // -------------------------------------------------------------------- @@ -1063,11 +1106,13 @@ class CI_DB_driver { /** * Disable Query Caching * + * @access public * @return void */ - public function cache_off() + function cache_off() { - return $this->cache_on = FALSE; + $this->cache_on = FALSE; + return FALSE; } @@ -1076,9 +1121,10 @@ class CI_DB_driver { /** * Delete the cache files associated with a particular URI * + * @access public * @return void */ - public function cache_delete($segment_one = '', $segment_two = '') + function cache_delete($segment_one = '', $segment_two = '') { if ( ! $this->_cache_init()) { @@ -1092,9 +1138,10 @@ class CI_DB_driver { /** * Delete All cache files * + * @access public * @return void */ - public function cache_delete_all() + function cache_delete_all() { if ( ! $this->_cache_init()) { @@ -1109,18 +1156,22 @@ class CI_DB_driver { /** * Initialize the Cache Class * + * @access private * @return void */ - private function _cache_init() + function _cache_init() { - if (is_object($this->CACHE) && class_exists('CI_DB_Cache')) + if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) { return TRUE; } - if ( ! class_exists('CI_DB_Cache') && ! @include(BASEPATH.'database/DB_cache.php')) + if ( ! class_exists('CI_DB_Cache')) { - return $this->cache_off(); + if ( ! @include(BASEPATH.'database/DB_cache.php')) + { + return $this->cache_off(); + } } $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects @@ -1132,9 +1183,10 @@ class CI_DB_driver { /** * Close DB Connection * + * @access public * @return void */ - public function close() + function close() { if (is_resource($this->conn_id) OR is_object($this->conn_id)) { @@ -1148,12 +1200,13 @@ 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 */ - public function display_error($error = '', $swap = '', $native = FALSE) + function display_error($error = '', $swap = '', $native = FALSE) { $LANG =& load_class('Lang', 'core'); $LANG->load('db'); @@ -1199,10 +1252,11 @@ class CI_DB_driver { * * This function adds backticks if appropriate based on db type * + * @access private * @param mixed the item to escape * @return mixed the item with backticks */ - public function protect_identifiers($item, $prefix_single = FALSE) + function protect_identifiers($item, $prefix_single = FALSE) { return $this->_protect_identifiers($item, $prefix_single); } @@ -1216,7 +1270,7 @@ class CI_DB_driver { * 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: + * column names that include the path. Consider a query like this: * * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table * @@ -1229,14 +1283,14 @@ class CI_DB_driver { * insert the table prefix (if it exists) in the proper position, and escape only * the correct identifiers. * - * @access public (DB Forge needs it to be public!) + * @access private * @param string * @param bool * @param mixed * @param bool * @return string */ - public function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) + function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE) { if ( ! is_bool($protect_identifiers)) { @@ -1284,7 +1338,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)) { @@ -1303,7 +1357,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. @@ -1336,7 +1390,7 @@ class CI_DB_driver { // 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]); + $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 @@ -1357,23 +1411,23 @@ 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 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); + $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item); } // Do we prefix an item with no segments? - if ($prefix_single == TRUE && substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) + if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) { $item = $this->dbprefix.$item; } } - if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers)) + if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) { $item = $this->_escape_identifiers($item); } @@ -1384,5 +1438,6 @@ class CI_DB_driver { } + /* End of file DB_driver.php */ /* Location: ./system/database/DB_driver.php */ -- cgit v1.2.3-24-g4f1b From 45ba4f73a13660fa05f30d2ec21965620b0e5be5 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 13 Feb 2012 01:24:39 +0200 Subject: Fix a possible bug in DB() and renamed a variable --- 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 c4aa0beaf..30149193a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -40,6 +40,7 @@ */ class CI_DB_driver { + public $dsn; public $username; public $password; public $hostname; -- cgit v1.2.3-24-g4f1b From 8ad2ec7b0a8e6ea385b13ad2fd782fe775ef92db Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 13 Feb 2012 12:25:42 +0200 Subject: Fix issue #1036 --- 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 30149193a..a3171e395 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -627,7 +627,7 @@ class CI_DB_driver { */ public function is_write_type($sql) { - return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql); + return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 5d93e13326b86344610cf13008726e40bef0a5b8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 6 Mar 2012 20:36:21 +0200 Subject: Revert a comment change --- 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 1dc80a104..482a6c8f3 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -854,7 +854,7 @@ class CI_DB_driver { * Returns an object with field data * * @param string the table name - * @return mixed + * @return object */ public function field_data($table = '') { -- cgit v1.2.3-24-g4f1b From 07c1ac830b4e98aa40f48baef3dd05fb68c0a836 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Fri, 9 Mar 2012 17:03:37 +0000 Subject: Bumped CodeIgniter's PHP requirement to 5.2.4. Yes I know PHP 5.4 just came out, and yes I know PHP 5.3 has lovely features, but there are plenty of corporate systems running on CodeIgniter and PHP 5.3 still is not widely supported enough. CodeIgniter is great for distributed applications, and this is the highest we can reasonably go without breaking support. PHP 5.3 will most likely happen in another year or so. Fingers crossed on that one anyway... --- 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 a61450d4c..59018cc91 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 5.1.6 or newer + * An open source application development framework for PHP 5.2.4 or newer * * NOTICE OF LICENSE * -- cgit v1.2.3-24-g4f1b From 10aa8e660c6f439958b79fce5d85ce7e8eecf028 Mon Sep 17 00:00:00 2001 From: Joel Kallman Date: Fri, 9 Mar 2012 14:54:53 -0500 Subject: Adding Support to Properly Escape Objects that have __toString() magic method so that the object can be passed directly as a parameter in a condition without having to manually convert to a string Signed-off-by: Joel Kallman --- 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 9d92f2f87..a72bf3101 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -727,7 +727,7 @@ class CI_DB_driver { */ function escape($str) { - if (is_string($str)) + if (is_string($str) OR method_exists($str, '__toString')) { $str = "'".$this->escape_str($str)."'"; } -- cgit v1.2.3-24-g4f1b From 95bd1d1a5f5bdccfde53cc27d7d5c20991112643 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 12 Mar 2012 16:22:28 +0200 Subject: Remove collation parameter from db_set_charset() (no longer needed) --- 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 a04a65eeb..12cd3917c 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -165,7 +165,7 @@ class CI_DB_driver { } // Now we set the character set and that's all - return $this->db_set_charset($this->char_set, $this->dbcollat); + return $this->db_set_charset($this->char_set); } // -------------------------------------------------------------------- @@ -177,9 +177,9 @@ class CI_DB_driver { * @param string * @return bool */ - public function db_set_charset($charset, $collation = '') + public function db_set_charset($charset) { - if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset, $collation)) + if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset)) { log_message('error', 'Unable to set database connection charset: '.$charset); -- cgit v1.2.3-24-g4f1b From 7eeda537a1fa8dc3a60bbdb88a7e473cc909f590 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 19 Mar 2012 16:01:55 -0400 Subject: Made database parent classes and methods abstract --- system/database/DB_driver.php | 6 ++---- 1 file changed, 2 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 bcff43392..79b7285bd 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -38,7 +38,7 @@ * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/database/ */ -class CI_DB_driver { +abstract class CI_DB_driver { public $dsn; public $username; @@ -1357,9 +1357,7 @@ class CI_DB_driver { * * @return void */ - protected function _reset_select() - { - } + abstract protected function _reset_select(); } -- cgit v1.2.3-24-g4f1b From d2ff0bc1336e106e4b45abe7ee176bf6b9496b6e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 19 Mar 2012 16:52:10 -0400 Subject: Removed pointless _prep_sql methods --- 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 79b7285bd..42b1b35aa 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1362,4 +1362,4 @@ abstract class CI_DB_driver { } /* End of file DB_driver.php */ -/* Location: ./system/database/DB_driver.php */ +/* Location: ./system/database/DB_driver.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 65b568fcd444fbeaf6fa9130289c254e34e5bbbd Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 20 Mar 2012 15:33:15 +0200 Subject: Remove EOF newlines --- 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 73f3e44d7..7fc569e9a 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1327,4 +1327,4 @@ class CI_DB_driver { } /* End of file DB_driver.php */ -/* Location: ./system/database/DB_driver.php */ +/* Location: ./system/database/DB_driver.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 35bbb1ab4f7ee09d75fb407c6d9d9637b4404698 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 20 Mar 2012 15:33:55 +0200 Subject: Remove EOF newlines --- 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 162d3bbb2..1fce76cb5 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1327,4 +1327,4 @@ class CI_DB_driver { } /* End of file DB_driver.php */ -/* Location: ./system/database/DB_driver.php */ +/* Location: ./system/database/DB_driver.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 2cae193647045a83014972d2aae908e7ea0ac8f3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 23 Mar 2012 16:08:41 +0200 Subject: Add dummy method reconnect() method to CI_DB_driver and remove it from drivers that don't support it --- 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 42b1b35aa..9f1a0b895 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -170,6 +170,23 @@ abstract class CI_DB_driver { // -------------------------------------------------------------------- + /** + * Reconnect + * + * Keep / reestablish the db connection if no queries have been + * sent for a length of time exceeding the server's idle timeout. + * + * This is just a dummy method to allow drivers without such + * functionality to not declare it, while others will override it. + * + * @return void + */ + public function reconnect() + { + } + + // -------------------------------------------------------------------- + /** * Set client character set * -- cgit v1.2.3-24-g4f1b From 6202a3c822a7b68040de2c5e0528bc1ec1c50758 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 28 Mar 2012 14:46:24 +0300 Subject: Some more unneeded brackets removed --- 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 f0b8e7789..553f70c45 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -291,7 +291,7 @@ abstract class CI_DB_driver { } // Verify table prefix and replace if necessary - if ( ($this->dbprefix != '' && $this->swap_pre != '') && ($this->dbprefix != $this->swap_pre) ) + if ($this->dbprefix != '' && $this->swap_pre != '' && $this->dbprefix != $this->swap_pre) { $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql); } -- cgit v1.2.3-24-g4f1b From 6781a5f64dc0c75110f3100eb281276f00fe9dbf Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 28 Mar 2012 14:50:51 +0300 Subject: A replace array_key_exists() with isset() --- 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 553f70c45..d3359e13e 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -755,11 +755,11 @@ abstract class CI_DB_driver { // Do we know from which column to get the table name? if ( ! isset($key)) { - if (array_key_exists('table_name', $row)) + if (isset($row['table_name'])) { $key = 'table_name'; } - elseif (array_key_exists('TABLE_NAME', $row)) + elseif (isset($row['TABLE_NAME'])) { $key = 'TABLE_NAME'; } @@ -827,11 +827,11 @@ abstract class CI_DB_driver { // Do we know from where to get the column's name? if ( ! isset($key)) { - if (array_key_exists('column_name', $row)) + if (isset($row['column_name'])) { $key = 'column_name'; } - elseif (array_key_exists('COLUMN_NAME', $row)) + elseif (isset($row['COLUMN_NAME'])) { $key = 'COLUMN_NAME'; } -- cgit v1.2.3-24-g4f1b From bee6644ce280d8e72cc2db23c86d5f5f6973acf3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 28 Mar 2012 15:28:19 +0300 Subject: Add a dummy db_select() method to CI_DB_driver and remove it from drivers that don't have such functionality --- system/database/DB_driver.php | 16 +++++++++++++++- 1 file changed, 15 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 d3359e13e..594fcced9 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -191,11 +191,25 @@ abstract class CI_DB_driver { // -------------------------------------------------------------------- + /** + * Select database + * + * This is just a dummy method to allow drivers without such + * functionality to not declare it, while others will override it. + * + * @return bool + */ + public function db_select() + { + return TRUE; + } + + // -------------------------------------------------------------------- + /** * Set client character set * * @param string - * @param string * @return bool */ public function db_set_charset($charset) -- cgit v1.2.3-24-g4f1b From da44bacbad76b9a898accdd533b48c04c6b0b12f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 28 Mar 2012 15:45:18 +0300 Subject: Revert CI_DB_driver::_reset_select() to a regular method instead of abstract.abstract methods are required to be implemented by their child classes and this is not what's intended in our case. --- 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 594fcced9..45f17e8ec 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -1351,7 +1351,9 @@ abstract class CI_DB_driver { * * @return void */ - abstract protected function _reset_select(); + protected function _reset_select() + { + } } -- cgit v1.2.3-24-g4f1b From ea09a8a5552f2aacdeab0c88a605fe44047ebd0a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 6 Apr 2012 20:50:07 +0300 Subject: Renamed _escape_identifiers() to escape_identifiers() and moved it to CI_DB_driver --- system/database/DB_driver.php | 45 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 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 dea705054..8b030af77 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -900,6 +900,43 @@ abstract class CI_DB_driver { // -------------------------------------------------------------------- + /** + * Escape the SQL Identifiers + * + * This function escapes column and table names + * + * @param string + * @return string + */ + public function escape_identifiers($item) + { + if ($this->_escape_char == '') + { + return $item; + } + + foreach ($this->_reserved_identifiers as $id) + { + if (strpos($item, '.'.$id) !== FALSE) + { + $item = str_replace('.', $this->_escape_char.'.', $item); + + // remove duplicates if the user already included the escape + return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); + } + } + + if (strpos($item, '.') !== FALSE) + { + $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); + } + + // remove duplicates if the user already included the escape + return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); + } + + // -------------------------------------------------------------------- + /** * Generate an insert string * @@ -913,7 +950,7 @@ abstract class CI_DB_driver { foreach ($data as $key => $val) { - $fields[] = $this->_escape_identifiers($key); + $fields[] = $this->escape_identifiers($key); $values[] = $this->escape($val); } @@ -1254,7 +1291,7 @@ abstract class CI_DB_driver { { if ( ! in_array($val, $this->_reserved_identifiers)) { - $parts[$key] = $this->_escape_identifiers($val); + $parts[$key] = $this->escape_identifiers($val); } } @@ -1311,7 +1348,7 @@ abstract class CI_DB_driver { if ($protect_identifiers === TRUE) { - $item = $this->_escape_identifiers($item); + $item = $this->escape_identifiers($item); } return $item.$alias; @@ -1334,7 +1371,7 @@ abstract class CI_DB_driver { if ($protect_identifiers === TRUE && ! in_array($item, $this->_reserved_identifiers)) { - $item = $this->_escape_identifiers($item); + $item = $this->escape_identifiers($item); } return $item.$alias; -- cgit v1.2.3-24-g4f1b From b457a4001ce2380e97f36b0a983b477c3e31de69 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 9 Apr 2012 16:11:56 +0300 Subject: DB Utility improvements - Replaced driver methods _list_databases(), _optimize_table() and _repair_table() with properties - Added defaults for optimize_table() and repair_table() SQL strings (FALSE, as those are mostly MySQL-specific) - Added MSSQL/SQLSRV support for optimize_table() (actually rebuilds table indexes) - Switched public driver methods to protected - Improved CUBRID support for list_databases() as it used to only return the currently used database - Minor speed improvements --- 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 8b030af77..61b05d52b 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -637,7 +637,7 @@ abstract class CI_DB_driver { */ public function is_write_type($sql) { - return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|OPTIMIZE|REINDEX)\s+/i', $sql); + return (bool) preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From ab3cab5a8f2afdd45ec8c6c365e43722781e54e6 Mon Sep 17 00:00:00 2001 From: dixy Date: Sat, 21 Apr 2012 00:58:00 +0200 Subject: Use parameter 'get_as_float' of microtime() --- 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 61b05d52b..cb04c7103 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -335,7 +335,7 @@ abstract class CI_DB_driver { } // Start the Query Timer - $time_start = list($sm, $ss) = explode(' ', microtime()); + $time_start = microtime(TRUE); // Run the Query if (FALSE === ($this->result_id = $this->simple_query($sql))) @@ -370,12 +370,12 @@ abstract class CI_DB_driver { } // Stop and aggregate the query time results - $time_end = list($em, $es) = explode(' ', microtime()); - $this->benchmark += ($em + $es) - ($sm + $ss); + $time_end = microtime(TRUE); + $this->benchmark += $time_end - $time_start; if ($this->save_queries == TRUE) { - $this->query_times[] = ($em + $es) - ($sm + $ss); + $this->query_times[] = $time_end - $time_start; } // Increment the query counter -- cgit v1.2.3-24-g4f1b