init_class('CI_DB_utility', 'dbutility'); // ------------------------------------------------------------------------ /** * Database Utility Class * * @category Database * @author Rick Ellis * @link http://www.codeigniter.com/user_guide/database/ */ class CI_DB_utility { var $db; /** * Constructor * * Grabs the CI super object instance so we can access it. * */ function CI_DB_utility() { // Assign the main database object to $this->db $obj =& get_instance(); $this->db =& $obj->db; log_message('debug', "Database Utility Class Initialized"); } // -------------------------------------------------------------------- /** * Create database * * @access public * @param string the database name * @return bool */ function create_database($db_name) { $sql = $this->_create_database($db_name); if (is_bool($sql)) { return $sql; } return $this->db->query($sql); } // -------------------------------------------------------------------- /** * Drop database * * @access public * @param string the database name * @return bool */ function drop_database($db_name) { $sql = $this->_drop_database($db_name); if (is_bool($sql)) { return $sql; } return $this->db->query($sql); } // -------------------------------------------------------------------- /** * List databases * * @access public * @return bool */ function list_databases() { $query = $this->db->query($this->_list_database()); $dbs = array(); if ($query->num_rows() > 0) { foreach ($query->result_array() as $row) { $dbs[] = current($row); } } return $dbs; } // -------------------------------------------------------------------- /** * Returns an array of table names * * @access public * @return array */ function list_tables() { if (FALSE === ($sql = $this->_list_tables())) { if ($this->db->db_debug) { return $this->db->display_error('db_unsupported_function'); } return FALSE; } $retval = array(); $query = $this->db->query($sql); if ($query->num_rows() > 0) { foreach($query->result_array() as $row) { if (isset($row['TABLE_NAME'])) { $retval[] = $row['TABLE_NAME']; } else { $retval[] = array_shift($row); } } } return $retval; } // -------------------------------------------------------------------- /** * Determine if a particular table exists * @access public * @return boolean */ function table_exists($table_name) { return ( ! in_array($this->db->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; } // -------------------------------------------------------------------- /** * Optimize Table * * @access public * @param string the table name * @return bool */ function optimize_table($table_name) { $sql = $this->_optimize_table($table_name); if (is_bool($sql)) { return $sql; } $query = $this->db->query($sql); return current($query->result_array()); } // -------------------------------------------------------------------- /** * Optimize Database * * @access public * @return array */ function optimize_database() { $result = array(); foreach ($this->list_tables() as $table_name) { $sql = $this->_optimize_table($table_name); if (is_bool($sql)) { return $sql; } $query = $this->db->query($sql); // Build the result array... $res = current($query->result_array()); $key = str_replace($this->db->database.'.', '', current($res)); $keys = array_keys($res); unset($res[$keys[0]]); $result[$key] = $res; } return $result; } // -------------------------------------------------------------------- /** * Optimize Table * * @access public * @param string the table name * @return bool */ function repair_table($table_name) { $sql = $this->_repair_table($table_name); if (is_bool($sql)) { return $sql; } $query = $this->db->query($sql); return current($query->result_array()); } // -------------------------------------------------------------------- /** * Drop Table * * @access public * @param string the table name * @return bool */ function drop_table($table_name) { $sql = $this->_drop_table($table_name); if (is_bool($sql)) { return $sql; } return $this->db->query($sql); } } ?>