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