admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 16 | // INITIALIZE THE CLASS --------------------------------------------------- |
| 17 | |
| 18 | $obj =& get_instance(); |
admin | 7981a9a | 2006-09-26 07:52:09 +0000 | [diff] [blame] | 19 | $obj->init_class('CI_DB_utility', 'dbutility'); |
admin | e79dc71 | 2006-09-26 03:52:45 +0000 | [diff] [blame] | 20 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 21 | // ------------------------------------------------------------------------ |
| 22 | |
| 23 | /** |
| 24 | * Database Utility Class |
| 25 | * |
| 26 | * @category Database |
| 27 | * @author Rick Ellis |
| 28 | * @link http://www.codeigniter.com/user_guide/database/ |
| 29 | */ |
| 30 | class CI_DB_utility { |
| 31 | |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 32 | var $db; |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 33 | |
admin | 41a1685 | 2006-09-26 18:17:19 +0000 | [diff] [blame] | 34 | /** |
| 35 | * Constructor |
| 36 | * |
| 37 | * Grabs the CI super object instance so we can access it. |
| 38 | * |
| 39 | */ |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 40 | function CI_DB_utility() |
| 41 | { |
| 42 | // Assign the main database object to $this->db |
| 43 | $obj =& get_instance(); |
| 44 | $this->db =& $obj->db; |
admin | 910d862 | 2006-09-26 02:09:05 +0000 | [diff] [blame] | 45 | |
| 46 | log_message('debug', "Database Utility Class Initialized"); |
admin | a5e812c | 2006-09-25 02:17:30 +0000 | [diff] [blame] | 47 | } |
| 48 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 49 | // -------------------------------------------------------------------- |
| 50 | |
| 51 | /** |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 52 | * Create database |
| 53 | * |
| 54 | * @access public |
| 55 | * @param string the database name |
| 56 | * @return bool |
| 57 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 58 | function create_database($db_name) |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 59 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 60 | $sql = $this->_create_database($db_name); |
admin | 6cec6a5 | 2006-09-25 06:56:49 +0000 | [diff] [blame] | 61 | |
| 62 | if (is_bool($sql)) |
| 63 | { |
| 64 | return $sql; |
| 65 | } |
| 66 | |
| 67 | return $this->db->query($sql); |
| 68 | } |
| 69 | |
| 70 | // -------------------------------------------------------------------- |
| 71 | |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Drop database |
| 74 | * |
| 75 | * @access public |
| 76 | * @param string the database name |
| 77 | * @return bool |
| 78 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 79 | function drop_database($db_name) |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 80 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 81 | $sql = $this->_drop_database($db_name); |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 82 | |
| 83 | if (is_bool($sql)) |
| 84 | { |
| 85 | return $sql; |
| 86 | } |
| 87 | |
| 88 | return $this->db->query($sql); |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 89 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 90 | |
| 91 | // -------------------------------------------------------------------- |
| 92 | |
| 93 | /** |
| 94 | * List databases |
| 95 | * |
| 96 | * @access public |
| 97 | * @return bool |
| 98 | */ |
| 99 | function list_databases() |
| 100 | { |
| 101 | $query = $this->db->query($this->_list_database()); |
| 102 | $dbs = array(); |
| 103 | if ($query->num_rows() > 0) |
| 104 | { |
| 105 | foreach ($query->result_array() as $row) |
| 106 | { |
| 107 | $dbs[] = current($row); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return $dbs; |
| 112 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 113 | |
| 114 | // -------------------------------------------------------------------- |
| 115 | |
| 116 | /** |
| 117 | * Returns an array of table names |
| 118 | * |
| 119 | * @access public |
| 120 | * @return array |
| 121 | */ |
| 122 | function list_tables() |
| 123 | { |
| 124 | if (FALSE === ($sql = $this->_list_tables())) |
| 125 | { |
| 126 | if ($this->db->db_debug) |
| 127 | { |
| 128 | return $this->db->display_error('db_unsupported_function'); |
| 129 | } |
| 130 | return FALSE; |
| 131 | } |
| 132 | |
| 133 | $retval = array(); |
| 134 | $query = $this->db->query($sql); |
| 135 | |
| 136 | if ($query->num_rows() > 0) |
| 137 | { |
| 138 | foreach($query->result_array() as $row) |
| 139 | { |
| 140 | if (isset($row['TABLE_NAME'])) |
| 141 | { |
| 142 | $retval[] = $row['TABLE_NAME']; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | $retval[] = array_shift($row); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return $retval; |
| 152 | } |
| 153 | |
| 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
| 157 | * Determine if a particular table exists |
| 158 | * @access public |
| 159 | * @return boolean |
| 160 | */ |
| 161 | function table_exists($table_name) |
| 162 | { |
| 163 | return ( ! in_array($this->db->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE; |
| 164 | } |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 165 | |
| 166 | // -------------------------------------------------------------------- |
| 167 | |
| 168 | /** |
| 169 | * Optimize Table |
| 170 | * |
| 171 | * @access public |
| 172 | * @param string the table name |
| 173 | * @return bool |
| 174 | */ |
| 175 | function optimize_table($table_name) |
| 176 | { |
| 177 | $sql = $this->_optimize_table($table_name); |
| 178 | |
| 179 | if (is_bool($sql)) |
| 180 | { |
| 181 | return $sql; |
| 182 | } |
| 183 | |
| 184 | $query = $this->db->query($sql); |
| 185 | return current($query->result_array()); |
| 186 | } |
| 187 | |
| 188 | // -------------------------------------------------------------------- |
| 189 | |
| 190 | /** |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 191 | * Optimize Database |
| 192 | * |
| 193 | * @access public |
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 194 | * @return array |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 195 | */ |
| 196 | function optimize_database() |
| 197 | { |
| 198 | $result = array(); |
| 199 | foreach ($this->list_tables() as $table_name) |
| 200 | { |
| 201 | $sql = $this->_optimize_table($table_name); |
| 202 | |
| 203 | if (is_bool($sql)) |
| 204 | { |
| 205 | return $sql; |
| 206 | } |
| 207 | |
| 208 | $query = $this->db->query($sql); |
admin | e5bb936 | 2006-09-27 00:31:22 +0000 | [diff] [blame] | 209 | |
| 210 | // Build the result array... |
admin | 31075cf | 2006-09-26 18:50:02 +0000 | [diff] [blame] | 211 | $res = current($query->result_array()); |
| 212 | $key = str_replace($this->db->database.'.', '', current($res)); |
| 213 | $keys = array_keys($res); |
| 214 | unset($res[$keys[0]]); |
| 215 | |
| 216 | $result[$key] = $res; |
| 217 | } |
| 218 | |
| 219 | return $result; |
| 220 | } |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
| 223 | |
| 224 | /** |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 225 | * Optimize Table |
| 226 | * |
| 227 | * @access public |
| 228 | * @param string the table name |
| 229 | * @return bool |
| 230 | */ |
| 231 | |
| 232 | function repair_table($table_name) |
| 233 | { |
| 234 | $sql = $this->_repair_table($table_name); |
| 235 | |
| 236 | if (is_bool($sql)) |
| 237 | { |
| 238 | return $sql; |
| 239 | } |
| 240 | |
| 241 | $query = $this->db->query($sql); |
| 242 | return current($query->result_array()); |
| 243 | } |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 244 | |
| 245 | // -------------------------------------------------------------------- |
| 246 | |
| 247 | /** |
| 248 | * Drop Table |
| 249 | * |
| 250 | * @access public |
| 251 | * @param string the table name |
| 252 | * @return bool |
| 253 | */ |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 254 | function drop_table($table_name) |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 255 | { |
admin | ab4f61b | 2006-09-25 22:12:32 +0000 | [diff] [blame] | 256 | $sql = $this->_drop_table($table_name); |
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 257 | |
| 258 | if (is_bool($sql)) |
| 259 | { |
| 260 | return $sql; |
| 261 | } |
| 262 | |
| 263 | return $this->db->query($sql); |
| 264 | } |
| 265 | |
| 266 | |
admin | 7b613c7 | 2006-09-24 18:05:17 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | } |
| 270 | |
| 271 | ?> |