blob: 764f10cb7cde8c7ab93a4f6557270df8cbfc7830 [file] [log] [blame]
admin7b613c72006-09-24 18:05:17 +00001<?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 */
admine79dc712006-09-26 03:52:45 +000015
admin7b613c72006-09-24 18:05:17 +000016// ------------------------------------------------------------------------
17
18/**
19 * Database Utility Class
20 *
21 * @category Database
22 * @author Rick Ellis
23 * @link http://www.codeigniter.com/user_guide/database/
24 */
25class CI_DB_utility {
26
admina5e812c2006-09-25 02:17:30 +000027 var $db;
admin7b613c72006-09-24 18:05:17 +000028
admin41a16852006-09-26 18:17:19 +000029 /**
30 * Constructor
31 *
32 * Grabs the CI super object instance so we can access it.
33 *
34 */
admina5e812c2006-09-25 02:17:30 +000035 function CI_DB_utility()
36 {
37 // Assign the main database object to $this->db
38 $obj =& get_instance();
39 $this->db =& $obj->db;
admin910d8622006-09-26 02:09:05 +000040
41 log_message('debug', "Database Utility Class Initialized");
admina5e812c2006-09-25 02:17:30 +000042 }
43
admin7b613c72006-09-24 18:05:17 +000044 // --------------------------------------------------------------------
45
46 /**
admin6cec6a52006-09-25 06:56:49 +000047 * Create database
48 *
49 * @access public
50 * @param string the database name
51 * @return bool
52 */
adminab4f61b2006-09-25 22:12:32 +000053 function create_database($db_name)
admin6cec6a52006-09-25 06:56:49 +000054 {
adminab4f61b2006-09-25 22:12:32 +000055 $sql = $this->_create_database($db_name);
admin6cec6a52006-09-25 06:56:49 +000056
57 if (is_bool($sql))
58 {
59 return $sql;
60 }
61
62 return $this->db->query($sql);
63 }
64
65 // --------------------------------------------------------------------
66
admin83b05a82006-09-25 21:06:46 +000067 /**
68 * Drop database
69 *
70 * @access public
71 * @param string the database name
72 * @return bool
73 */
adminab4f61b2006-09-25 22:12:32 +000074 function drop_database($db_name)
adminbb1d4392006-09-24 20:14:38 +000075 {
adminab4f61b2006-09-25 22:12:32 +000076 $sql = $this->_drop_database($db_name);
admin83b05a82006-09-25 21:06:46 +000077
78 if (is_bool($sql))
79 {
80 return $sql;
81 }
82
83 return $this->db->query($sql);
adminbb1d4392006-09-24 20:14:38 +000084 }
admin83b05a82006-09-25 21:06:46 +000085
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 }
admin9cd4e8e2006-09-25 23:26:25 +0000108
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 }
adminab4f61b2006-09-25 22:12:32 +0000160
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 /**
admin31075cf2006-09-26 18:50:02 +0000186 * Optimize Database
187 *
188 * @access public
admine5bb9362006-09-27 00:31:22 +0000189 * @return array
admin31075cf2006-09-26 18:50:02 +0000190 */
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);
admine5bb9362006-09-27 00:31:22 +0000204
205 // Build the result array...
admin31075cf2006-09-26 18:50:02 +0000206 $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 /**
adminab4f61b2006-09-25 22:12:32 +0000220 * 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 }
admin83b05a82006-09-25 21:06:46 +0000239
240 // --------------------------------------------------------------------
241
242 /**
243 * Drop Table
244 *
245 * @access public
246 * @param string the table name
247 * @return bool
248 */
adminab4f61b2006-09-25 22:12:32 +0000249 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000250 {
adminab4f61b2006-09-25 22:12:32 +0000251 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000252
253 if (is_bool($sql))
254 {
255 return $sql;
256 }
257
258 return $this->db->query($sql);
259 }
260
261
admin7b613c72006-09-24 18:05:17 +0000262
263
264}
265
266?>