summaryrefslogtreecommitdiffstats
path: root/system
diff options
context:
space:
mode:
authorAndrey Andreev <narf@bofh.bg>2012-03-28 13:54:09 +0200
committerAndrey Andreev <narf@bofh.bg>2012-03-28 13:54:09 +0200
commit8e2478e7310b5cfff88b64ff5d84b71f1c124b58 (patch)
tree8e4db92c0a8211c2e5ce1fd879e69b60a1341215 /system
parent8f50b6b36cb743be8b70809279e501c20b8e38e2 (diff)
parent6781a5f64dc0c75110f3100eb281276f00fe9dbf (diff)
Merge pull request #971 from narfbg/develop-db-driver
Improve the base database driver class
Diffstat (limited to 'system')
-rw-r--r--system/database/DB_driver.php265
1 files changed, 114 insertions, 151 deletions
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php
index 9f1a0b895..d3359e13e 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;
}
@@ -283,17 +287,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 +305,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 +349,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 +373,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 +395,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 +406,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 +599,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 +609,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 +680,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 +724,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 +744,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 +811,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']))
- {
- $retval[] = $row['COLUMN_NAME'];
- }
- else
+ // Do we know from where to get the column's name?
+ if ( ! isset($key))
{
- $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 +864,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 +879,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 +897,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 +970,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 +993,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 +1023,7 @@ abstract class CI_DB_driver {
*/
public function cache_on()
{
- $this->cache_on = TRUE;
- return TRUE;
+ return $this->cache_on = TRUE;
}
// --------------------------------------------------------------------
@@ -1064,8 +1035,7 @@ abstract class CI_DB_driver {
*/
public function cache_off()
{
- $this->cache_on = FALSE;
- return FALSE;
+ return $this->cache_on = FALSE;
}
@@ -1078,11 +1048,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 +1062,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 +1076,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 +1101,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 +1137,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 +1145,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 +1162,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 +1205,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 +1248,7 @@ abstract class CI_DB_driver {
$item = implode('.', $parts);
}
+
return $item.$alias;
}
@@ -1318,13 +1283,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 +1309,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);
}