Source code: javatools/db/DbDatabaseAdmin.java
1 /*
2 * DbDatabaseAdmin.java
3 *
4 * Created on 8 gennaio 2002, 11.55
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.db;
26
27 import java.lang.*;
28 import java.sql.*;
29 import java.util.*;
30 import javatools.db.util.*;
31 import javatools.util.*;
32
33 /**
34 * Class to manage administrator functions.
35 * @author Antonio Petrelli
36 * @version 0.1.10
37 */
38 public class DbDatabaseAdmin extends DbDatabase {
39
40 /** Creates new DbDatabaseAdmin
41 * @param manager The manager for getting properties file.
42 * @param name Name of DBMS.
43 * @param driver Driver used to connect to DBMS.
44 * @param connectString Connection string to connect to DBMS.
45 * @param userName User name to connect to DBMS. It must not be included into connectString!
46 * @param password Password to connect to DBMS. It must not be included into connectString!
47 */
48 public DbDatabaseAdmin(DbManager manager, String name, String driver,
49 String connectString, String userName, String password) {
50
51 super(manager, name, driver, connectString, userName, password);
52 initCreatorMap();
53 }
54
55 /** Creates new DbDatabaseAdmin
56 * @param base The database to use.
57 */
58 public DbDatabaseAdmin(DbDatabase base) {
59 super(base.manager, base.name, base.driver, base.connectString,
60 base.userName, base.password);
61 initCreatorMap();
62 }
63
64 /** Creates a database.
65 * @param dbName Name of database to be created.
66 * @throws DbException If something goes wrong.
67 */
68 public void createDatabase(String dbAddress, int port, String dbName) throws DbException {
69 DbCreator creator;
70
71 creator = (DbCreator) dbms2creator.get(name);
72 if (creator != null)
73 creator.create(this, dbAddress, port, dbName);
74 else
75 throw new DbException("[DbDatabaseAdmin] Cannot find database creator");
76 }
77
78 public void createUser(String userName, String password) throws DbException {
79 UserCreator creator;
80
81 creator = (UserCreator) dbms2userCreator.get(name);
82 if (creator != null)
83 creator.create(this, userName, password);
84 else
85 throw new DbException("[DbDatabaseAdmin] Cannot find user creator");
86 }
87
88 /** Destroys a database.
89 * @param dbName Database to be destroyed.
90 * @throws DbException If something goes wrong.
91 */
92 public void dropDatabase(String dbName) throws DbException {
93 DbConnection conn;
94 Connection sqlConn;
95 String sqlString;
96 Statement st;
97
98 try {
99 conn = getNewConnection();
100 }
101 catch (DbException e) {
102 throw e;
103 }
104 sqlString = SubstituteVariable.substitute(getProperty("dropDatabaseString"), "$(dbName)", dbName);
105 try {
106 sqlConn = conn.getSqlConnection();
107 st = sqlConn.createStatement();
108 st.executeUpdate(sqlString);
109 conn.close();
110 }
111 catch (SQLException e) {
112 throw new DbException("Cannot create database");
113 }
114 }
115
116 /** Deletes a table from a database.
117 * @param table The table to remove.
118 * @throws DbException If something goes wrong.
119 */
120 public void dropTable(DbAbstractTable table) throws DbException {
121 DbConnection conn;
122 Connection sqlConn;
123 String sqlString;
124 Statement st;
125
126 conn = getNewConnection();
127 sqlString = getProperty("dropTableString");
128 sqlString = SubstituteVariable.substitute(sqlString, "${tableName}",
129 table.tableName);
130 try {
131 sqlConn = conn.getSqlConnection();
132 st = sqlConn.createStatement();
133 st.execute(sqlString);
134 }
135 catch (SQLException e) {
136 System.out.println(e.getMessage());
137 throw new DbException("Cannot drop table");
138 }
139 }
140
141 /** Make table optimization.
142 * @param table The table to optimize.
143 * @throws DbException If something goes wrong.
144 */
145 public void optimizeTable(DbAbstractTable table) throws DbException {
146 DbConnection conn;
147 Connection sqlConn;
148 String sqlString;
149 Statement st;
150
151 conn = getNewConnection();
152 sqlString = getProperty("optimizeTableString");
153 sqlString = SubstituteVariable.substitute(sqlString, "${tableName}",
154 table.tableName);
155 try {
156 sqlConn = conn.getSqlConnection();
157 st = sqlConn.createStatement();
158 st.execute(sqlString);
159 }
160 catch (SQLException e) {
161 System.out.println(e.getMessage());
162 throw new DbException("Cannot optimize table");
163 }
164 }
165
166 /** Optimizes an index for the database.
167 * @param indexName The index name.
168 * @throws DbException If something goes wrong.
169 */
170 public void optimizeIndex(String indexName) throws DbException {
171 DbConnection conn;
172 Connection sqlConn;
173 String sqlString;
174 Statement st;
175
176 conn = getNewConnection();
177 sqlString = getProperty("optimizeIndexString");
178 sqlString = SubstituteVariable.substitute(sqlString, "${indexName}", indexName);
179 try {
180 sqlConn = conn.getSqlConnection();
181 st = sqlConn.createStatement();
182 st.executeUpdate(sqlString);
183 }
184 catch (SQLException e) {
185 throw new DbException("Cannot optimize index");
186 }
187 }
188
189 /** Creates an index for a database.
190 * @param indexName The index name.
191 * @param table The table to use.
192 * @param cols The columns to be indexed.
193 * @throws DbException If something goes wrong.
194 */
195 public void createIndex(String indexName, DbAbstractTable table, DbColumn[] cols) throws DbException {
196 int i, numCols;
197 DbConnection conn;
198 Connection sqlConn;
199 String sqlString, tempString;
200 Statement st;
201
202 conn = getNewConnection();
203 sqlString = getProperty("createIndexString");
204 sqlString = SubstituteVariable.substitute(sqlString, "${indexName}", indexName);
205 sqlString = SubstituteVariable.substitute(sqlString, "${tableName}", table.tableName);
206 numCols = cols.length;
207 tempString = null;
208 if (numCols > 0) {
209 tempString = cols[0].getName();
210 for (i=1; i < numCols; i++)
211 tempString += ", " + cols[i].getName();
212 }
213 if (tempString != null) {
214 sqlString = SubstituteVariable.substitute(sqlString, "${colList}", tempString);
215 try {
216 sqlConn = conn.getSqlConnection();
217 st = sqlConn.createStatement();
218 st.executeUpdate(sqlString);
219 }
220 catch (SQLException e) {
221 throw new DbException(e.getMessage());
222 }
223 }
224 }
225
226 private HashMap dbms2creator, dbms2userCreator;
227
228 private void initCreatorMap() {
229 dbms2creator = new HashMap();
230 dbms2creator.put("mysql", new MySQLDbCreator());
231 dbms2creator.put("firebird", new FirebirdDbCreator());
232 dbms2creator.put("firebird10", new Firebird10DbCreator());
233 dbms2userCreator = new HashMap();
234 dbms2userCreator.put("firebird", new FirebirdUserCreator());
235 dbms2userCreator.put("firebird10", new Firebird10UserCreator());
236 }
237 }