From b457a4001ce2380e97f36b0a983b477c3e31de69 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 9 Apr 2012 16:11:56 +0300 Subject: DB Utility improvements - Replaced driver methods _list_databases(), _optimize_table() and _repair_table() with properties - Added defaults for optimize_table() and repair_table() SQL strings (FALSE, as those are mostly MySQL-specific) - Added MSSQL/SQLSRV support for optimize_table() (actually rebuilds table indexes) - Switched public driver methods to protected - Improved CUBRID support for list_databases() as it used to only return the currently used database - Minor speed improvements --- system/database/DB_utility.php | 120 ++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 60 deletions(-) (limited to 'system/database/DB_utility.php') diff --git a/system/database/DB_utility.php b/system/database/DB_utility.php index 642a004bd..587dfdc01 100644 --- a/system/database/DB_utility.php +++ b/system/database/DB_utility.php @@ -37,6 +37,11 @@ abstract class CI_DB_utility extends CI_DB_forge { public $db; public $data_cache = array(); + // Platform specific SQL strings + // Just setting those defaults to FALSE as they are mostly MySQL-specific + protected $_optimize_table = FALSE; + protected $_repair_table = FALSE; + public function __construct() { // Assign the main database object to $this->db @@ -50,7 +55,7 @@ abstract class CI_DB_utility extends CI_DB_forge { /** * List databases * - * @return bool + * @return array */ public function list_databases() { @@ -59,18 +64,25 @@ abstract class CI_DB_utility extends CI_DB_forge { { return $this->data_cache['db_names']; } + elseif ($this->_list_databases === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } + + $this->data_cache['db_names'] = array(); - $query = $this->db->query($this->_list_databases()); - $dbs = array(); - if ($query->num_rows() > 0) + $query = $this->db->query($this->_list_databases); + if ($query === FALSE) { - foreach ($query->result_array() as $row) - { - $dbs[] = current($row); - } + return $this->data_cache['db_names']; + } + + for ($i = 0, $c = count($query); $i < $c; $i++) + { + $this->data_cache['db_names'] = current($query[$i]); } - return $this->data_cache['db_names'] = $dbs; + return $this->data_cache['db_names']; } // -------------------------------------------------------------------- @@ -79,48 +91,36 @@ abstract class CI_DB_utility extends CI_DB_forge { * Determine if a particular database exists * * @param string - * @return boolean + * @return bool */ public function database_exists($database_name) { - // Some databases won't have access to the list_databases() function, so - // this is intended to allow them to override with their own functions as - // defined in $driver_utility.php - if (method_exists($this, '_database_exists')) - { - return $this->_database_exists($database_name); - } - else - { - return ( ! in_array($database_name, $this->list_databases())) ? FALSE : TRUE; - } + return in_array($database_name, $this->list_databases()); } - // -------------------------------------------------------------------- /** * Optimize Table * * @param string the table name - * @return bool + * @return mixed */ public function optimize_table($table_name) { - $sql = $this->_optimize_table($table_name); - - if (is_bool($sql)) + if ($this->_optimize_table === FALSE) { - show_error('db_must_use_set'); - return FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; } - $query = $this->db->query($sql); - $res = $query->result_array(); + $query = $this->db->query(sprintf($this->_optimize_table, $this->db->escape_identifiers($table_name))); + if ($query !== FALSE) + { + $query = $query->result_array(); + return current($res); + } - // Note: Due to a bug in current() that affects some versions - // of PHP we can not pass function call directly into it - return current($res); + return FALSE; } // -------------------------------------------------------------------- @@ -128,26 +128,26 @@ abstract class CI_DB_utility extends CI_DB_forge { /** * Optimize Database * - * @return array + * @return mixed */ public function optimize_database() { + if ($this->_optimize_table === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } + $result = array(); foreach ($this->db->list_tables() as $table_name) { - $sql = $this->_optimize_table($table_name); - - if (is_bool($sql)) + $res = $this->db->query(sprintf($this->_optimize_table, $this->db->escape_identifiers($table_name))); + if (is_bool($res)) { - return $sql; + return $res; } - $query = $this->db->query($sql); - // Build the result array... - // Note: Due to a bug in current() that affects some versions - // of PHP we can not pass function call directly into it - $res = $query->result_array(); + $res = $res->result_array(); $res = current($res); $key = str_replace($this->db->database.'.', '', current($res)); $keys = array_keys($res); @@ -165,23 +165,23 @@ abstract class CI_DB_utility extends CI_DB_forge { * Repair Table * * @param string the table name - * @return bool + * @return mixed */ public function repair_table($table_name) { - $sql = $this->_repair_table($table_name); - - if (is_bool($sql)) + if ($this->_repair_table === FALSE) { - return $sql; + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; } - $query = $this->db->query($sql); + $query = $this->db->query(sprintf($this->_repair_table, $this->db->escape_identifiers($table_name))); + if (is_bool($query)) + { + return $query; + } - // Note: Due to a bug in current() that affects some versions - // of PHP we can not pass function call directly into it - $res = $query->result_array(); - return current($res); + $query = $query->result_array(); + return current($query); } // -------------------------------------------------------------------- @@ -257,18 +257,18 @@ abstract class CI_DB_utility extends CI_DB_forge { $CI->load->helper('xml'); // Generate the result - $xml = "<{$root}>".$newline; + $xml = '<'.$root.'>'.$newline; foreach ($query->result_array() as $row) { - $xml .= $tab."<{$element}>".$newline; + $xml .= $tab.'<'.$element.'>'.$newline; foreach ($row as $key => $val) { - $xml .= $tab.$tab."<{$key}>".xml_convert($val)."".$newline; + $xml .= $tab.$tab.'<'.$key.'>'.xml_convert($val).''.$newline; } - $xml .= $tab."".$newline; + $xml .= $tab.''.$newline; } - return $xml .= "".$newline; + return $xml.''.$newline; } // -------------------------------------------------------------------- @@ -328,8 +328,8 @@ abstract class CI_DB_utility extends CI_DB_forge { // Is the encoder supported? If not, we'll either issue an // error or use plain text depending on the debug settings - if (($prefs['format'] === 'gzip' AND ! @function_exists('gzencode')) - OR ($prefs['format'] === 'zip' AND ! @function_exists('gzcompress'))) + if (($prefs['format'] === 'gzip' && ! @function_exists('gzencode')) + OR ($prefs['format'] === 'zip' && ! @function_exists('gzcompress'))) { if ($this->db->db_debug) { -- cgit v1.2.3-24-g4f1b