blob: f98448adf6bbcde2047838fe77fea24d63095dc3 [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;
34 }
35
admin7b613c72006-09-24 18:05:17 +000036 // --------------------------------------------------------------------
37
38 /**
admin6cec6a52006-09-25 06:56:49 +000039 * Create database
40 *
41 * @access public
42 * @param string the database name
43 * @return bool
44 */
adminab4f61b2006-09-25 22:12:32 +000045 function create_database($db_name)
admin6cec6a52006-09-25 06:56:49 +000046 {
adminab4f61b2006-09-25 22:12:32 +000047 $sql = $this->_create_database($db_name);
admin6cec6a52006-09-25 06:56:49 +000048
49 if (is_bool($sql))
50 {
51 return $sql;
52 }
53
54 return $this->db->query($sql);
55 }
56
57 // --------------------------------------------------------------------
58
admin83b05a82006-09-25 21:06:46 +000059 /**
60 * Drop database
61 *
62 * @access public
63 * @param string the database name
64 * @return bool
65 */
adminab4f61b2006-09-25 22:12:32 +000066 function drop_database($db_name)
adminbb1d4392006-09-24 20:14:38 +000067 {
adminab4f61b2006-09-25 22:12:32 +000068 $sql = $this->_drop_database($db_name);
admin83b05a82006-09-25 21:06:46 +000069
70 if (is_bool($sql))
71 {
72 return $sql;
73 }
74
75 return $this->db->query($sql);
adminbb1d4392006-09-24 20:14:38 +000076 }
admin83b05a82006-09-25 21:06:46 +000077
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 }
admin9cd4e8e2006-09-25 23:26:25 +0000100
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 }
adminab4f61b2006-09-25 22:12:32 +0000152
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 }
admin83b05a82006-09-25 21:06:46 +0000197
198 // --------------------------------------------------------------------
199
200 /**
201 * Drop Table
202 *
203 * @access public
204 * @param string the table name
205 * @return bool
206 */
adminab4f61b2006-09-25 22:12:32 +0000207 function drop_table($table_name)
admin83b05a82006-09-25 21:06:46 +0000208 {
adminab4f61b2006-09-25 22:12:32 +0000209 $sql = $this->_drop_table($table_name);
admin83b05a82006-09-25 21:06:46 +0000210
211 if (is_bool($sql))
212 {
213 return $sql;
214 }
215
216 return $this->db->query($sql);
217 }
218
219
admin7b613c72006-09-24 18:05:17 +0000220
221
222}
223
224?>