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