blob: 281a3b87dce121acfd3233a616c7db95472f570c [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 */
15
admine79dc712006-09-26 03:52:45 +000016// INITIALIZE THE CLASS ---------------------------------------------------
17
18$obj =& get_instance();
admin7981a9a2006-09-26 07:52:09 +000019$obj->init_class('CI_DB_utility', 'dbutility');
admine79dc712006-09-26 03:52:45 +000020
admin7b613c72006-09-24 18:05:17 +000021// ------------------------------------------------------------------------
22
23/**
24 * Database Utility Class
25 *
26 * @category Database
27 * @author Rick Ellis
28 * @link http://www.codeigniter.com/user_guide/database/
29 */
30class CI_DB_utility {
31
admina5e812c2006-09-25 02:17:30 +000032 var $db;
admin7b613c72006-09-24 18:05:17 +000033
admin41a16852006-09-26 18:17:19 +000034 /**
35 * Constructor
36 *
37 * Grabs the CI super object instance so we can access it.
38 *
39 */
admina5e812c2006-09-25 02:17:30 +000040 function CI_DB_utility()
41 {
42 // Assign the main database object to $this->db
43 $obj =& get_instance();
44 $this->db =& $obj->db;
admin910d8622006-09-26 02:09:05 +000045
46 log_message('debug', "Database Utility Class Initialized");
admina5e812c2006-09-25 02:17:30 +000047 }
48
admin7b613c72006-09-24 18:05:17 +000049 // --------------------------------------------------------------------
50
51 /**
admin6cec6a52006-09-25 06:56:49 +000052 * Create database
53 *
54 * @access public
55 * @param string the database name
56 * @return bool
57 */
adminab4f61b2006-09-25 22:12:32 +000058 function create_database($db_name)
admin6cec6a52006-09-25 06:56:49 +000059 {
adminab4f61b2006-09-25 22:12:32 +000060 $sql = $this->_create_database($db_name);
admin6cec6a52006-09-25 06:56:49 +000061
62 if (is_bool($sql))
63 {
64 return $sql;
65 }
66
67 return $this->db->query($sql);
68 }
69
70 // --------------------------------------------------------------------
71
admin83b05a82006-09-25 21:06:46 +000072 /**
73 * Drop database
74 *
75 * @access public
76 * @param string the database name
77 * @return bool
78 */
adminab4f61b2006-09-25 22:12:32 +000079 function drop_database($db_name)
adminbb1d4392006-09-24 20:14:38 +000080 {
adminab4f61b2006-09-25 22:12:32 +000081 $sql = $this->_drop_database($db_name);
admin83b05a82006-09-25 21:06:46 +000082
83 if (is_bool($sql))
84 {
85 return $sql;
86 }
87
88 return $this->db->query($sql);
adminbb1d4392006-09-24 20:14:38 +000089 }
admin83b05a82006-09-25 21:06:46 +000090
91 // --------------------------------------------------------------------
92
93 /**
94 * List databases
95 *
96 * @access public
97 * @return bool
98 */
99 function list_databases()
100 {
101 $query = $this->db->query($this->_list_database());
102 $dbs = array();
103 if ($query->num_rows() > 0)
104 {
105 foreach ($query->result_array() as $row)
106 {
107 $dbs[] = current($row);
108 }
109 }
110
111 return $dbs;
112 }
admin9cd4e8e2006-09-25 23:26:25 +0000113
114 // --------------------------------------------------------------------
115
116 /**
117 * Returns an array of table names
118 *
119 * @access public
120 * @return array
121 */
122 function list_tables()
123 {
124 if (FALSE === ($sql = $this->_list_tables()))
125 {
126 if ($this->db->db_debug)
127 {
128 return $this->db->display_error('db_unsupported_function');
129 }
130 return FALSE;
131 }
132
133 $retval = array();
134 $query = $this->db->query($sql);
135
136 if ($query->num_rows() > 0)
137 {
138 foreach($query->result_array() as $row)
139 {
140 if (isset($row['TABLE_NAME']))
141 {
142 $retval[] = $row['TABLE_NAME'];
143 }
144 else
145 {
146 $retval[] = array_shift($row);
147 }
148 }
149 }
150
151 return $retval;
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Determine if a particular table exists
158 * @access public
159 * @return boolean
160 */
161 function table_exists($table_name)
162 {
163 return ( ! in_array($this->db->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE;
164 }
adminab4f61b2006-09-25 22:12:32 +0000165
166 // --------------------------------------------------------------------
167
168 /**
169 * Optimize Table
170 *
171 * @access public
172 * @param string the table name
173 * @return bool
174 */
175 function optimize_table($table_name)
176 {
177 $sql = $this->_optimize_table($table_name);
178
179 if (is_bool($sql))
180 {
181 return $sql;
182 }
183
184 $query = $this->db->query($sql);
185 return current($query->result_array());
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
admin31075cf2006-09-26 18:50:02 +0000191 * Optimize Database
192 *
193 * @access public
194 * @param string the table name
195 * @return bool
196 */
197 function optimize_database()
198 {
199 $result = array();
200 foreach ($this->list_tables() as $table_name)
201 {
202 $sql = $this->_optimize_table($table_name);
203
204 if (is_bool($sql))
205 {
206 return $sql;
207 }
208
209 $query = $this->db->query($sql);
210 $res = current($query->result_array());
211 $key = str_replace($this->db->database.'.', '', current($res));
212 $keys = array_keys($res);
213 unset($res[$keys[0]]);
214
215 $result[$key] = $res;
216 }
217
218 return $result;
219 }
220
221 // --------------------------------------------------------------------
222
223 /**
adminab4f61b2006-09-25 22:12:32 +0000224 * Optimize Table
225 *
226 * @access public
227 * @param string the table name
228 * @return bool
229 */
230
231 function repair_table($table_name)
232 {
233 $sql = $this->_repair_table($table_name);
234
235 if (is_bool($sql))
236 {
237 return $sql;
238 }
239
240 $query = $this->db->query($sql);
241 return current($query->result_array());
242 }
admin83b05a82006-09-25 21:06:46 +0000243
244 // --------------------------------------------------------------------
245
246 /**
247 * Drop Table
248 *
249 * @access public
250 * @param string the table name
251 * @return bool
252 */
adminab4f61b2006-09-25 22:12:32 +0000253 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000254 {
adminab4f61b2006-09-25 22:12:32 +0000255 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000256
257 if (is_bool($sql))
258 {
259 return $sql;
260 }
261
262 return $this->db->query($sql);
263 }
264
265
admin7b613c72006-09-24 18:05:17 +0000266
267
268}
269
270?>