blob: 41941ae35c54a24957fef47fb5aa87de75af2aba [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
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 */
25class CI_DB_utility {
26
admina5e812c2006-09-25 02:17:30 +000027 var $db;
admin7b613c72006-09-24 18:05:17 +000028
admina5e812c2006-09-25 02:17:30 +000029 function CI_DB_utility()
30 {
31 // Assign the main database object to $this->db
32 $obj =& get_instance();
33 $this->db =& $obj->db;
admin910d8622006-09-26 02:09:05 +000034
35 log_message('debug', "Database Utility Class Initialized");
admina5e812c2006-09-25 02:17:30 +000036 }
37
admin7b613c72006-09-24 18:05:17 +000038 // --------------------------------------------------------------------
39
40 /**
admin6cec6a52006-09-25 06:56:49 +000041 * Create database
42 *
43 * @access public
44 * @param string the database name
45 * @return bool
46 */
adminab4f61b2006-09-25 22:12:32 +000047 function create_database($db_name)
admin6cec6a52006-09-25 06:56:49 +000048 {
adminab4f61b2006-09-25 22:12:32 +000049 $sql = $this->_create_database($db_name);
admin6cec6a52006-09-25 06:56:49 +000050
51 if (is_bool($sql))
52 {
53 return $sql;
54 }
55
56 return $this->db->query($sql);
57 }
58
59 // --------------------------------------------------------------------
60
admin83b05a82006-09-25 21:06:46 +000061 /**
62 * Drop database
63 *
64 * @access public
65 * @param string the database name
66 * @return bool
67 */
adminab4f61b2006-09-25 22:12:32 +000068 function drop_database($db_name)
adminbb1d4392006-09-24 20:14:38 +000069 {
adminab4f61b2006-09-25 22:12:32 +000070 $sql = $this->_drop_database($db_name);
admin83b05a82006-09-25 21:06:46 +000071
72 if (is_bool($sql))
73 {
74 return $sql;
75 }
76
77 return $this->db->query($sql);
adminbb1d4392006-09-24 20:14:38 +000078 }
admin83b05a82006-09-25 21:06:46 +000079
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 }
admin9cd4e8e2006-09-25 23:26:25 +0000102
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 }
adminab4f61b2006-09-25 22:12:32 +0000154
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 }
admin83b05a82006-09-25 21:06:46 +0000199
200 // --------------------------------------------------------------------
201
202 /**
203 * Drop Table
204 *
205 * @access public
206 * @param string the table name
207 * @return bool
208 */
adminab4f61b2006-09-25 22:12:32 +0000209 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000210 {
adminab4f61b2006-09-25 22:12:32 +0000211 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000212
213 if (is_bool($sql))
214 {
215 return $sql;
216 }
217
218 return $this->db->query($sql);
219 }
220
221
admin7b613c72006-09-24 18:05:17 +0000222
223
224}
225
226?>