blob: 3b4b09d6e1750eec7e9a1355712bc11eafc04bb6 [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
admina5e812c2006-09-25 02:17:30 +000034 function CI_DB_utility()
35 {
36 // Assign the main database object to $this->db
37 $obj =& get_instance();
38 $this->db =& $obj->db;
admin910d8622006-09-26 02:09:05 +000039
40 log_message('debug', "Database Utility Class Initialized");
admina5e812c2006-09-25 02:17:30 +000041 }
42
admin7b613c72006-09-24 18:05:17 +000043 // --------------------------------------------------------------------
44
45 /**
admin6cec6a52006-09-25 06:56:49 +000046 * Create database
47 *
48 * @access public
49 * @param string the database name
50 * @return bool
51 */
adminab4f61b2006-09-25 22:12:32 +000052 function create_database($db_name)
admin6cec6a52006-09-25 06:56:49 +000053 {
adminab4f61b2006-09-25 22:12:32 +000054 $sql = $this->_create_database($db_name);
admin6cec6a52006-09-25 06:56:49 +000055
56 if (is_bool($sql))
57 {
58 return $sql;
59 }
60
61 return $this->db->query($sql);
62 }
63
64 // --------------------------------------------------------------------
65
admin83b05a82006-09-25 21:06:46 +000066 /**
67 * Drop database
68 *
69 * @access public
70 * @param string the database name
71 * @return bool
72 */
adminab4f61b2006-09-25 22:12:32 +000073 function drop_database($db_name)
adminbb1d4392006-09-24 20:14:38 +000074 {
adminab4f61b2006-09-25 22:12:32 +000075 $sql = $this->_drop_database($db_name);
admin83b05a82006-09-25 21:06:46 +000076
77 if (is_bool($sql))
78 {
79 return $sql;
80 }
81
82 return $this->db->query($sql);
adminbb1d4392006-09-24 20:14:38 +000083 }
admin83b05a82006-09-25 21:06:46 +000084
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 }
admin9cd4e8e2006-09-25 23:26:25 +0000107
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 }
adminab4f61b2006-09-25 22:12:32 +0000159
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 }
admin83b05a82006-09-25 21:06:46 +0000204
205 // --------------------------------------------------------------------
206
207 /**
208 * Drop Table
209 *
210 * @access public
211 * @param string the table name
212 * @return bool
213 */
adminab4f61b2006-09-25 22:12:32 +0000214 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000215 {
adminab4f61b2006-09-25 22:12:32 +0000216 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000217
218 if (is_bool($sql))
219 {
220 return $sql;
221 }
222
223 return $this->db->query($sql);
224 }
225
226
admin7b613c72006-09-24 18:05:17 +0000227
228
229}
230
231?>