diff options
Diffstat (limited to 'system/database')
-rw-r--r-- | system/database/DB_driver.php | 285 | ||||
-rw-r--r-- | system/database/drivers/cubrid/cubrid_driver.php | 16 | ||||
-rw-r--r-- | system/database/drivers/interbase/interbase_driver.php | 13 | ||||
-rw-r--r-- | system/database/drivers/oci8/oci8_driver.php | 13 | ||||
-rw-r--r-- | system/database/drivers/odbc/odbc_driver.php | 13 | ||||
-rw-r--r-- | system/database/drivers/pdo/pdo_driver.php | 13 | ||||
-rw-r--r-- | system/database/drivers/postgre/postgre_driver.php | 13 | ||||
-rw-r--r-- | system/database/drivers/sqlite/sqlite_driver.php | 12 |
8 files changed, 132 insertions, 246 deletions
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 9f1a0b895..45f17e8ec 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -99,9 +99,13 @@ abstract class CI_DB_driver { */ public function initialize() { - // If an existing connection resource is available - // there is no need to connect and select the database - if (is_resource($this->conn_id) OR is_object($this->conn_id)) + /* 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; } @@ -188,10 +192,24 @@ 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) @@ -283,17 +301,13 @@ abstract class CI_DB_driver { { 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); } // Compile binds if needed @@ -305,15 +319,12 @@ abstract class CI_DB_driver { // 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; } } @@ -352,13 +363,7 @@ abstract class CI_DB_driver { $this->trans_complete(); // Display errors - return $this->display_error( - array( - 'Error Number: '.$error['code'], - $error['message'], - $sql - ) - ); + return $this->display_error(array('Error Number: '.$error['code'], $error['message'], $sql)); } return FALSE; @@ -382,7 +387,7 @@ abstract 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(); } @@ -404,9 +409,9 @@ abstract class CI_DB_driver { $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 @@ -415,9 +420,9 @@ abstract 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; @@ -608,7 +613,8 @@ abstract 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); } @@ -617,8 +623,7 @@ abstract 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; @@ -689,15 +694,15 @@ abstract class CI_DB_driver { { if (is_string($str) OR method_exists($str, '__toString')) { - $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; @@ -733,13 +738,7 @@ abstract class CI_DB_driver { 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; } // -------------------------------------------------------------------- @@ -759,35 +758,40 @@ abstract 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) { - $table = FALSE; - $rows = $query->result_array(); - $key = (($row = current($rows)) && in_array('table_name', array_map('strtolower', array_keys($row)))); - - if ($key) + // Do we know from which column to get the table name? + if ( ! isset($key)) { - $table = array_key_exists('TABLE_NAME', $row) ? 'TABLE_NAME' : 'table_name'; + if (isset($row['table_name'])) + { + $key = 'table_name'; + } + elseif (isset($row['TABLE_NAME'])) + { + $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); + } } - foreach ($rows as $row) - { - $retval[] = ( ! $table) ? current($row) : $row[$table]; - } + $this->data_cache['table_names'][] = $row[$key]; } - $this->data_cache['table_names'] = $retval; - return $this->data_cache['table_names']; } @@ -821,38 +825,45 @@ abstract 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'])) + // Do we know from where to get the column's name? + if ( ! isset($key)) { - $retval[] = $row['COLUMN_NAME']; - } - else - { - $retval[] = current($row); + if (isset($row['column_name'])) + { + $key = 'column_name'; + } + elseif (isset($row['COLUMN_NAME'])) + { + $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]; } - $this->data_cache['field_names'][$table] = $retval; return $this->data_cache['field_names'][$table]; } @@ -867,7 +878,7 @@ abstract class CI_DB_driver { */ 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)); } // -------------------------------------------------------------------- @@ -882,15 +893,10 @@ abstract 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; } $query = $this->query($this->_field_data($this->protect_identifiers($table, TRUE, NULL, FALSE))); - return $query->field_data(); } @@ -905,8 +911,7 @@ abstract class CI_DB_driver { */ public function insert_string($table, $data) { - $fields = array(); - $values = array(); + $fields = $values = array(); foreach ($data as $key => $val) { @@ -979,13 +984,7 @@ abstract class CI_DB_driver { */ 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)); } // -------------------------------------------------------------------- @@ -1008,25 +1007,12 @@ abstract 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); } // -------------------------------------------------------------------- @@ -1051,8 +1037,7 @@ abstract class CI_DB_driver { */ public function cache_on() { - $this->cache_on = TRUE; - return TRUE; + return $this->cache_on = TRUE; } // -------------------------------------------------------------------- @@ -1064,8 +1049,7 @@ abstract class CI_DB_driver { */ public function cache_off() { - $this->cache_on = FALSE; - return FALSE; + return $this->cache_on = FALSE; } @@ -1078,11 +1062,9 @@ abstract class CI_DB_driver { */ public function cache_delete($segment_one = '', $segment_two = '') { - if ( ! $this->_cache_init()) - { - return FALSE; - } - return $this->CACHE->delete($segment_one, $segment_two); + return ($this->_cache_init()) + ? $this->CACHE->delete($segment_one, $segment_two) + : FALSE; } // -------------------------------------------------------------------- @@ -1094,12 +1076,9 @@ abstract class CI_DB_driver { */ public function cache_delete_all() { - if ( ! $this->_cache_init()) - { - return FALSE; - } - - return $this->CACHE->delete_all(); + return ($this->_cache_init()) + ? $this->CACHE->delete_all() + : FALSE; } // -------------------------------------------------------------------- @@ -1111,18 +1090,17 @@ abstract class CI_DB_driver { */ protected 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')) { - 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; @@ -1137,11 +1115,11 @@ abstract class CI_DB_driver { */ 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; } // -------------------------------------------------------------------- @@ -1173,9 +1151,7 @@ abstract 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) @@ -1183,7 +1159,6 @@ abstract 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; } } @@ -1201,7 +1176,7 @@ abstract 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 + * 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 @@ -1244,12 +1219,15 @@ abstract 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 @@ -1284,6 +1262,7 @@ abstract class CI_DB_driver { $item = implode('.', $parts); } + return $item.$alias; } @@ -1318,13 +1297,12 @@ abstract 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]; } @@ -1345,19 +1323,18 @@ abstract 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); } @@ -1374,7 +1351,9 @@ abstract class CI_DB_driver { * * @return void */ - abstract protected function _reset_select(); + protected function _reset_select() + { + } } diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index bed3d8685..0f9c427e6 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -159,22 +159,6 @@ class CI_DB_cubrid_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Select the database - * - * @return resource - */ - public function db_select() - { - // In CUBRID there is no need to select a database as the database - // is chosen at the connection time. - // So, to determine if the database is "selected", all we have to - // do is ping the server and return that value. - return cubrid_ping($this->conn_id); - } - - // -------------------------------------------------------------------- - - /** * Database version number * * @return string diff --git a/system/database/drivers/interbase/interbase_driver.php b/system/database/drivers/interbase/interbase_driver.php index d8b6ae571..6d3346292 100644 --- a/system/database/drivers/interbase/interbase_driver.php +++ b/system/database/drivers/interbase/interbase_driver.php @@ -85,19 +85,6 @@ class CI_DB_interbase_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Select the database - * - * @return bool - */ - public function db_select() - { - // Connection selects the database - return TRUE; - } - - // -------------------------------------------------------------------- - - /** * Database version number * * @return string diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php index 3bc8c114c..45b0198eb 100644 --- a/system/database/drivers/oci8/oci8_driver.php +++ b/system/database/drivers/oci8/oci8_driver.php @@ -186,19 +186,6 @@ class CI_DB_oci8_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Select the database - * - * @return resource - */ - public function db_select() - { - // Not in Oracle - schemas are actually usernames - return TRUE; - } - - // -------------------------------------------------------------------- - - /** * Database version number * * @return string diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php index ad773117f..ed901bd81 100644 --- a/system/database/drivers/odbc/odbc_driver.php +++ b/system/database/drivers/odbc/odbc_driver.php @@ -95,19 +95,6 @@ class CI_DB_odbc_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Select the database - * - * @return resource - */ - public function db_select() - { - // Not needed for ODBC - return TRUE; - } - - // -------------------------------------------------------------------- - - /** * Execute the query * * @param string an SQL query diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index f336eb0ab..a9bed367e 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -247,19 +247,6 @@ class CI_DB_pdo_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Select the database - * - * @return resource - */ - public function db_select() - { - // Not needed for PDO - return TRUE; - } - - // -------------------------------------------------------------------- - - /** * Database version number * * @return string diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 3e2d05ce8..a033d74f3 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -162,19 +162,6 @@ class CI_DB_postgre_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Select the database - * - * @return resource - */ - public function db_select() - { - // Not needed for Postgre so we'll return TRUE - return TRUE; - } - - // -------------------------------------------------------------------- - - /** * Set client character set * * @param string diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php index 102c79bb3..ef543c9b5 100644 --- a/system/database/drivers/sqlite/sqlite_driver.php +++ b/system/database/drivers/sqlite/sqlite_driver.php @@ -106,18 +106,6 @@ class CI_DB_sqlite_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Select the database - * - * @return resource - */ - public function db_select() - { - return TRUE; - } - - // -------------------------------------------------------------------- - - /** * Database version number * * @return string |