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') 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') 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') 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') 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') 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 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.php | 23 +++++++++++++---------- system/database/DB_driver.php | 1 + 2 files changed, 14 insertions(+), 10 deletions(-) (limited to 'system/database') diff --git a/system/database/DB.php b/system/database/DB.php index d06ffb40e..8f9cb3743 100755 --- a/system/database/DB.php +++ b/system/database/DB.php @@ -74,24 +74,24 @@ function &DB($params = '', $active_record_override = NULL) * parameter. DSNs must have this prototype: * $dsn = 'driver://username:password@hostname/database'; */ - if (($dns = @parse_url($params)) === FALSE) + if (($dsn = @parse_url($params)) === FALSE) { show_error('Invalid DB Connection String'); } $params = array( - 'dbdriver' => $dns['scheme'], - 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '', - 'port' => (isset($dns['port'])) ? rawurldecode($dns['port']) : '', - 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '', - 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '', - 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : '' + 'dbdriver' => $dsn['scheme'], + 'hostname' => (isset($dsn['host'])) ? rawurldecode($dsn['host']) : '', + 'port' => (isset($dsn['port'])) ? rawurldecode($dsn['port']) : '', + 'username' => (isset($dsn['user'])) ? rawurldecode($dsn['user']) : '', + 'password' => (isset($dsn['pass'])) ? rawurldecode($dsn['pass']) : '', + 'database' => (isset($dsn['path'])) ? rawurldecode(substr($dsn['path'], 1)) : '' ); // were additional config items set? - if (isset($dns['query'])) + if (isset($dsn['query'])) { - parse_str($dns['query'], $extra); + parse_str($dsn['query'], $extra); foreach ($extra as $key => $val) { // booleans please @@ -104,7 +104,10 @@ function &DB($params = '', $active_record_override = NULL) $val = FALSE; } - $params[$key] = $val; + if ( ! isset($params[$key])) + { + $params[$key] = $val; + } } } } 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') 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') 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 992f17568dc59af9fcc243a19dd8fcafeeff7aaa Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 19 Mar 2012 16:58:43 +0200 Subject: Switched MySQLi driver to use OOP --- system/database/drivers/mysqli/mysqli_driver.php | 29 ++++++++++++------------ system/database/drivers/mysqli/mysqli_result.php | 16 ++++++------- 2 files changed, 23 insertions(+), 22 deletions(-) (limited to 'system/database') diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index f38b94c13..43e8ac756 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -72,8 +72,8 @@ class CI_DB_mysqli_driver extends CI_DB { public function db_connect() { return ($this->port != '') - ? @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port) - : @mysqli_connect($this->hostname, $this->username, $this->password, $this->database); + ? @new mysqli($this->hostname, $this->username, $this->password, $this->database, $this->port) + : @new mysqli($this->hostname, $this->username, $this->password, $this->database); } // -------------------------------------------------------------------- @@ -92,8 +92,8 @@ class CI_DB_mysqli_driver extends CI_DB { } return ($this->port != '') - ? @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port) - : @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database); + ? @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port) + : @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database); } // -------------------------------------------------------------------- @@ -108,7 +108,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ public function reconnect() { - if (mysqli_ping($this->conn_id) === FALSE) + if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE) { $this->conn_id = FALSE; } @@ -129,7 +129,7 @@ class CI_DB_mysqli_driver extends CI_DB { $database = $this->database; } - if (@mysqli_select_db($this->conn_id, $database)) + if (@$this->conn_id->select_db($database)) { $this->database = $database; return TRUE; @@ -148,7 +148,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ protected function _db_set_charset($charset) { - return @mysqli_set_charset($this->conn_id, $charset); + return @$this->conn_id->set_charset($charset); } // -------------------------------------------------------------------- @@ -162,7 +162,7 @@ class CI_DB_mysqli_driver extends CI_DB { { return isset($this->data_cache['version']) ? $this->data_cache['version'] - : $this->data_cache['version'] = @mysqli_get_server_info($this->conn_id); + : $this->data_cache['version'] = $this->conn_id->server_info; } // -------------------------------------------------------------------- @@ -175,7 +175,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ protected function _execute($sql) { - return @mysqli_query($this->conn_id, $this->_prep_query($sql)); + return @$this->conn_id->query($this->_prep_query($sql)); } // -------------------------------------------------------------------- @@ -286,7 +286,7 @@ class CI_DB_mysqli_driver extends CI_DB { return $str; } - $str = is_object($this->conn_id) ? mysqli_real_escape_string($this->conn_id, $str) : addslashes($str); + $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str); // escape LIKE condition wildcards if ($like === TRUE) @@ -306,7 +306,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ public function affected_rows() { - return @mysqli_affected_rows($this->conn_id); + return $this->conn_id->affected_rows; } // -------------------------------------------------------------------- @@ -318,7 +318,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ public function insert_id() { - return @mysqli_insert_id($this->conn_id); + return $this->conn_id->insert_id; } // -------------------------------------------------------------------- @@ -434,7 +434,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ public function error() { - return array('code' => mysqli_errno($this->conn_id), 'message' => mysqli_error($this->conn_id)); + return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error); } // -------------------------------------------------------------------- @@ -691,7 +691,8 @@ class CI_DB_mysqli_driver extends CI_DB { */ protected function _close($conn_id) { - @mysqli_close($conn_id); + $this->conn_id->close(); + $this->conn_id = FALSE; } } diff --git a/system/database/drivers/mysqli/mysqli_result.php b/system/database/drivers/mysqli/mysqli_result.php index f135f4d46..dc7d9a793 100644 --- a/system/database/drivers/mysqli/mysqli_result.php +++ b/system/database/drivers/mysqli/mysqli_result.php @@ -43,7 +43,7 @@ class CI_DB_mysqli_result extends CI_DB_result { */ public function num_rows() { - return @mysqli_num_rows($this->result_id); + return $this->result_id->num_rows; } // -------------------------------------------------------------------- @@ -55,7 +55,7 @@ class CI_DB_mysqli_result extends CI_DB_result { */ public function num_fields() { - return @mysqli_num_fields($this->result_id); + return $this->result_id->field_count; } // -------------------------------------------------------------------- @@ -70,7 +70,7 @@ class CI_DB_mysqli_result extends CI_DB_result { public function list_fields() { $field_names = array(); - while ($field = mysqli_fetch_field($this->result_id)) + while ($field = $this->result_id->fetch_field()) { $field_names[] = $field->name; } @@ -90,7 +90,7 @@ class CI_DB_mysqli_result extends CI_DB_result { public function field_data() { $retval = array(); - $field_data = mysqli_fetch_fields($this->result_id); + $field_data = $this->result_id->fetch_fields(); for ($i = 0, $c = count($field_data); $i < $c; $i++) { $retval[$i] = new stdClass(); @@ -115,7 +115,7 @@ class CI_DB_mysqli_result extends CI_DB_result { { if (is_object($this->result_id)) { - mysqli_free_result($this->result_id); + $this->result_id->free(); $this->result_id = FALSE; } } @@ -133,7 +133,7 @@ class CI_DB_mysqli_result extends CI_DB_result { */ protected function _data_seek($n = 0) { - return mysqli_data_seek($this->result_id, $n); + return $this->result_id->data_seek($n); } // -------------------------------------------------------------------- @@ -147,7 +147,7 @@ class CI_DB_mysqli_result extends CI_DB_result { */ protected function _fetch_assoc() { - return mysqli_fetch_assoc($this->result_id); + return $this->result_id->fetch_assoc(); } // -------------------------------------------------------------------- @@ -161,7 +161,7 @@ class CI_DB_mysqli_result extends CI_DB_result { */ protected function _fetch_object() { - return mysqli_fetch_object($this->result_id); + return $this->result_id->fetch_object(); } } -- 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.php | 2 +- system/database/DB_driver.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database') diff --git a/system/database/DB.php b/system/database/DB.php index 8f9cb3743..e9d70ccc2 100755 --- a/system/database/DB.php +++ b/system/database/DB.php @@ -161,4 +161,4 @@ function &DB($params = '', $active_record_override = NULL) } /* End of file DB.php */ -/* Location: ./system/database/DB.php */ +/* Location: ./system/database/DB.php */ \ No newline at end of file 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.php | 2 +- system/database/DB_driver.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database') diff --git a/system/database/DB.php b/system/database/DB.php index be6ee9c2f..7c12e562b 100755 --- a/system/database/DB.php +++ b/system/database/DB.php @@ -161,4 +161,4 @@ function &DB($params = '', $active_record_override = NULL) } /* End of file DB.php */ -/* Location: ./system/database/DB.php */ +/* Location: ./system/database/DB.php */ \ No newline at end of file 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 c6a68e04169802c8aa82e41634ce350eafd1ec1e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 26 Mar 2012 14:30:10 +0300 Subject: Add visibility declarations where they remain missing --- system/database/drivers/mssql/mssql_driver.php | 3 +-- system/database/drivers/pdo/pdo_driver.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'system/database') diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index f2933fe43..912808631 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -539,13 +539,12 @@ class CI_DB_mssql_driver extends CI_DB { * * Generates a platform-specific delete string from the supplied data * - * @access public * @param string the table name * @param array the where clause * @param string the limit clause * @return string */ - function _delete($table, $where = array(), $like = array(), $limit = FALSE) + protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) { $conditions = ''; diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index 19338e30f..f336eb0ab 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -484,7 +484,7 @@ class CI_DB_pdo_driver extends CI_DB { * @param string * @return string */ - function count_all($table = '') + public function count_all($table = '') { if ($table == '') { -- cgit v1.2.3-24-g4f1b From a8bb4be3b2aa5984c465bbcf1ef51fd3eba92208 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 26 Mar 2012 15:54:23 +0300 Subject: Remove remaining access description lines from database classes and styleguide example --- system/database/DB_active_rec.php | 40 +++++++++--------------- system/database/drivers/mysqli/mysqli_driver.php | 1 - 2 files changed, 15 insertions(+), 26 deletions(-) (limited to 'system/database') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index fe591dda1..b324226ab 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * Active Record Class * @@ -422,7 +420,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { foreach ($key as $k => $v) { - $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type; + $prefix = (count($this->ar_where) === 0 && count($this->ar_cache_where) === 0) ? '' : $type; if (is_null($v) && ! $this->_has_operator($k)) { @@ -537,7 +535,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * @param string The field to search * @param array The values searched on - * @param boolean If the statement would be IN or NOT IN + * @param bool If the statement would be IN or NOT IN * @param string * @return object */ @@ -719,7 +717,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { { $type = $this->_group_get_type($type); $this->ar_where_group_started = TRUE; - $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type; + $prefix = (count($this->ar_where) === 0 && count($this->ar_cache_where) === 0) ? '' : $type; $this->ar_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->ar_where_group_count).' ('; if ($this->ar_caching) @@ -984,8 +982,8 @@ abstract class CI_DB_active_record extends CI_DB_driver { /** * Sets the LIMIT value * - * @param integer the limit value - * @param integer the offset value + * @param int the limit value + * @param int the offset value * @return object */ public function limit($value, $offset = NULL) @@ -1005,7 +1003,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { /** * Sets the OFFSET value * - * @param integer the offset value + * @param int the offset value * @return object */ public function offset($offset) @@ -1021,7 +1019,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * @param mixed * @param string - * @param boolean + * @param bool * @return object */ public function set($key, $value = '', $escape = TRUE) @@ -1055,9 +1053,8 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * Compiles a SELECT query string and returns the sql. * - * @access public * @param string the table name to select from (optional) - * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone + * @param bool TRUE: resets AR values; FALSE: leave AR vaules alone * @return string */ public function get_compiled_select($table = '', $reset = TRUE) @@ -1226,7 +1223,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * @param mixed * @param string - * @param boolean + * @param bool * @return object */ public function set_insert_batch($key, $value = '', $escape = TRUE) @@ -1283,9 +1280,8 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * Compiles an insert query and returns the sql * - * @access public * @param string the table to insert into - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @param bool TRUE: reset AR values; FALSE: leave AR values alone * @return string */ public function get_compiled_insert($table = '', $reset = TRUE) @@ -1316,7 +1312,6 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * Compiles an insert string and runs the query * - * @access public * @param string the table to insert data into * @param array an associative array of insert values * @return object @@ -1352,7 +1347,6 @@ abstract class CI_DB_active_record extends CI_DB_driver { * validate that the there data is actually being set and that table * has been chosen to be inserted into. * - * @access public * @param string the table to insert data into * @return string */ @@ -1423,9 +1417,8 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * Compiles an update query and returns the sql * - * @access public * @param string the table to update - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @param bool TRUE: reset AR values; FALSE: leave AR values alone * @return string */ public function get_compiled_update($table = '', $reset = TRUE) @@ -1499,7 +1492,6 @@ abstract class CI_DB_active_record extends CI_DB_driver { * validate that data is actually being set and that a table has been * chosen to be update. * - * @access public * @param string the table to update data on * @return bool */ @@ -1584,7 +1576,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * @param array * @param string - * @param boolean + * @param bool * @return object */ public function set_update_batch($key, $index = '', $escape = TRUE) @@ -1696,9 +1688,8 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * Compiles a delete query string and returns the sql * - * @access public * @param string the table to delete from - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @param bool TRUE: reset AR values; FALSE: leave AR values alone * @return string */ public function get_compiled_delete($table = '', $reset = TRUE) @@ -1719,7 +1710,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { * @param mixed the table(s) to delete from. String or array * @param mixed the where clause * @param mixed the limit clause - * @param boolean + * @param bool * @return object */ public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) @@ -2062,7 +2053,6 @@ abstract class CI_DB_active_record extends CI_DB_driver { * * Empties the AR cache * - * @access public * @return void */ public function flush_cache() @@ -2114,7 +2104,7 @@ abstract class CI_DB_active_record extends CI_DB_driver { // If we are "protecting identifiers" we need to examine the "from" // portion of the query to determine if there are any aliases - if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0) + if ($this->_protect_identifiers === TRUE && count($this->ar_cache_from) > 0) { $this->_track_aliases($this->ar_from); } diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index 4c5d52127..041daf710 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -357,7 +357,6 @@ class CI_DB_mysqli_driver extends CI_DB { * * Generates a platform-specific query string so that the table names can be fetched * - * @access private * @param bool * @return string */ -- cgit v1.2.3-24-g4f1b From 67c287192b5ff414753ae50a834932f676a0db9e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 27 Mar 2012 09:20:54 +0300 Subject: Remove EOF newlines from MySQLi driver and result classes --- system/database/drivers/mysqli/mysqli_driver.php | 2 +- system/database/drivers/mysqli/mysqli_result.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database') diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index a6ac14f12..47b0449d6 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -697,4 +697,4 @@ class CI_DB_mysqli_driver extends CI_DB { } /* End of file mysqli_driver.php */ -/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */ +/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */ \ No newline at end of file diff --git a/system/database/drivers/mysqli/mysqli_result.php b/system/database/drivers/mysqli/mysqli_result.php index dc7d9a793..cf0362217 100644 --- a/system/database/drivers/mysqli/mysqli_result.php +++ b/system/database/drivers/mysqli/mysqli_result.php @@ -167,4 +167,4 @@ class CI_DB_mysqli_result extends CI_DB_result { } /* End of file mysqli_result.php */ -/* Location: ./system/database/drivers/mysqli/mysqli_result.php */ +/* Location: ./system/database/drivers/mysqli/mysqli_result.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 3b2587e1559d2cbe751d04f801f999ef3fa4e74c Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 28 Mar 2012 13:39:34 +0300 Subject: Added random ordering support for MSSQL and SQLSRV drivers and removed an unused method --- system/database/drivers/mssql/mssql_driver.php | 4 ++-- system/database/drivers/sqlsrv/sqlsrv_driver.php | 29 +++++------------------- 2 files changed, 8 insertions(+), 25 deletions(-) (limited to 'system/database') diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index 912808631..81af6cd72 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -52,10 +52,10 @@ class CI_DB_mssql_driver extends CI_DB { /** * The syntax to count rows is slightly different across different * database engines, so this string appears in each driver and is - * used for the count_all() and count_all_results() functions. + * used for the count_all() and count_all_results() methods. */ protected $_count_string = 'SELECT COUNT(*) AS '; - protected $_random_keyword = ' ASC'; // not currently supported + protected $_random_keyword = ' NEWID()'; /** * Non-persistent database connection diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index 9f2f88699..bb4f009bc 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -55,7 +55,7 @@ class CI_DB_sqlsrv_driver extends CI_DB { * used for the count_all() and count_all_results() functions. */ protected $_count_string = 'SELECT COUNT(*) AS '; - protected $_random_keyword = ' ASC'; // not currently supported + protected $_random_keyword = ' NEWID()'; // not currently supported /** * Non-persistent database connection @@ -68,12 +68,12 @@ class CI_DB_sqlsrv_driver extends CI_DB { $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set; $connection = array( - 'UID' => empty($this->username) ? '' : $this->username, - 'PWD' => empty($this->password) ? '' : $this->password, - 'Database' => $this->database, - 'ConnectionPooling' => $pooling ? 1 : 0, + 'UID' => empty($this->username) ? '' : $this->username, + 'PWD' => empty($this->password) ? '' : $this->password, + 'Database' => $this->database, + 'ConnectionPooling' => $pooling ? 1 : 0, 'CharacterSet' => $character_set, - 'ReturnDatesAsStrings' => 1 + 'ReturnDatesAsStrings' => 1 ); // If the username and password are both empty, assume this is a @@ -258,23 +258,6 @@ class CI_DB_sqlsrv_driver extends CI_DB { // -------------------------------------------------------------------- - /** - * Parse major version - * - * Grabs the major version number from the - * database server version string passed in. - * - * @param string $version - * @return int major version number - */ - protected function _parse_major_version($version) - { - preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info); - return $ver_info[1]; // return the major version b/c that's all we're interested in. - } - - // -------------------------------------------------------------------- - /** * Database version number * -- 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') 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') 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