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