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