KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > internaldb > RunSQLHelper


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.internaldb;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.io.File JavaDoc;
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27
28 public class RunSQLHelper {
29
30     private final static Log log = LogFactory.getLog(RunSQLHelper.class);
31
32     public static final String JavaDoc SQL_SUCCESS_MSG = "SQL command/s successful";
33
34     public static final String JavaDoc SQL_EMPTY_MSG = "SQL Command/s can't be empty";
35
36     private static final String JavaDoc DB_CREATED_MSG = "Database created";
37
38     private static final String JavaDoc DB_DELETED_MSG = "Database deleted";
39
40     private static final String JavaDoc DERBY_BACKUP_FOLDER = "derby.backup";
41
42     private static final String JavaDoc PARENT_FOLDER = "..";
43
44     private static final String JavaDoc BAK_EXTENSION = ".bak";
45
46     private static final String JavaDoc BAK_PREFIX = "BAK_";
47
48     public String JavaDoc createDB(String JavaDoc dbName) {
49         String JavaDoc result = DB_CREATED_MSG + ": " + dbName;
50
51         Connection JavaDoc conn = null;
52         try {
53             conn = DerbyConnectionUtil.getDerbyConnection(dbName,
54                     DerbyConnectionUtil.CREATE_DB_PROP);
55         } catch (Throwable JavaDoc e) {
56             if (e instanceof SQLException JavaDoc) {
57                 result = getSQLError((SQLException JavaDoc) e);
58             } else {
59                 result = e.getMessage();
60             }
61         } finally {
62             // close DB connection
63
try {
64                 if (conn != null) {
65                     conn.close();
66                 }
67             } catch (SQLException JavaDoc e) {
68                 result = "Problem closing DB connection";
69             }
70         }
71
72         return result;
73     }
74
75     public String JavaDoc backupDB(String JavaDoc derbyHome, String JavaDoc dbName) {
76         return "";
77     }
78
79     public String JavaDoc restoreDB(String JavaDoc derbyHome, String JavaDoc dbName) {
80         return "";
81     }
82
83     public String JavaDoc deleteDB(String JavaDoc derbyHome, String JavaDoc dbName) {
84         String JavaDoc result = DB_DELETED_MSG + ": " + dbName;
85
86         // shutdown database before deleting it
87
if (!shutdownDB(dbName)) {
88             result = "Database not deleted: " + dbName
89                     + " Couldn't shutdown db: " + dbName;
90             return result;
91         }
92
93         try {
94             // create backup folder if not created
95
File JavaDoc derbyBackupFolder = new File JavaDoc(derbyHome + File.separatorChar
96                     + PARENT_FOLDER + File.separatorChar + DERBY_BACKUP_FOLDER);
97             if (!derbyBackupFolder.exists()) {
98                 if (!derbyBackupFolder.mkdirs()) {
99                     result = "Database not deleted: " + dbName
100                             + " Derby backup folder not created: "
101                             + derbyBackupFolder;
102                     return result;
103                 }
104             }
105
106             File JavaDoc oldDBFolder = new File JavaDoc(derbyHome + File.separatorChar + dbName);
107             if (oldDBFolder.exists()) {
108                 // Need to add a prefix because File.createTempFile's first
109
// argument must be a String at least three characters long.
110
File JavaDoc tmpFile = File.createTempFile(BAK_PREFIX + dbName,
111                         BAK_EXTENSION, derbyBackupFolder);
112                 File JavaDoc newDBFolder = new File JavaDoc(tmpFile.getAbsolutePath());
113                 /*
114                  * Delete temp file and create a temp folder using the temp
115                  * filename
116                  */

117                 if (tmpFile.delete()) {
118                     if (newDBFolder.mkdirs()) {
119                         if (!oldDBFolder.renameTo(new File JavaDoc(newDBFolder,
120                                 oldDBFolder.getName()))) {
121                             result = "Database not deleted: " + dbName
122                                     + " DB folder not renamed";
123                             return result;
124                         }
125                     }
126                 }
127             }
128         } catch (Exception JavaDoc e) {
129             e.printStackTrace();
130         }
131
132         return result;
133     }
134
135     public String JavaDoc runSQL(String JavaDoc dbName, String JavaDoc sql) {
136         String JavaDoc result = SQL_SUCCESS_MSG;
137
138         if ((sql == null) || (sql.trim().length() == 0)) {
139             result = SQL_EMPTY_MSG;
140             return result;
141         }
142
143         Connection JavaDoc conn = null;
144         Statement JavaDoc s = null;
145         try {
146
147             conn = DerbyConnectionUtil.getDerbyConnection(dbName);
148             conn.setAutoCommit(false);
149
150             s = conn.createStatement();
151             String JavaDoc[] sqlCmds = sql.split(";");
152             for (int i = 0; i < sqlCmds.length; i++) {
153                 if (sqlCmds[i].trim().length() > 0) {
154                     // debug printout (remove later)
155
log.debug("SQL" + i + ": <" + sqlCmds[i].trim() + ">");
156                     s.execute(sqlCmds[i]);
157                 }
158             }
159             conn.commit();
160         } catch (Throwable JavaDoc e) {
161             if (e instanceof SQLException JavaDoc) {
162                 result = getSQLError((SQLException JavaDoc) e);
163             } else {
164                 result = e.getMessage();
165             }
166         } finally {
167             // close DB connection
168
try {
169                 if (s != null) {
170                     s.close();
171                 }
172                 if (conn != null) {
173                     conn.close();
174                 }
175             } catch (SQLException JavaDoc e) {
176                 if (SQL_SUCCESS_MSG.equals(result)) {
177                     result = "Problem closing DB connection: " + e.getMessage();
178                 }
179             }
180         }
181
182         return result;
183     }
184
185     private boolean shutdownDB(String JavaDoc dbName) {
186         boolean ok = true;
187
188         boolean gotSQLExc = false;
189         try {
190             DerbyConnectionUtil.getDerbyConnection(dbName,
191                     DerbyConnectionUtil.SHUTDOWN_DB_PROP);
192         } catch (SQLException JavaDoc se) {
193             gotSQLExc = true;
194         }
195
196         if (!gotSQLExc) {
197             ok = false;
198         }
199
200         return ok;
201     }
202
203     private String JavaDoc getSQLError(SQLException JavaDoc e) {
204         StringBuffer JavaDoc errorMsg = new StringBuffer JavaDoc();
205         while (e != null) {
206             //errorMsg.append(e.toString());
207
errorMsg.append(e.getMessage());
208             errorMsg.append(" * ");
209             e = e.getNextException();
210         }
211
212         return errorMsg.toString();
213     }
214
215     public static void main(String JavaDoc[] args) {
216         new RunSQLHelper().runSQL("derbyDB4",
217                 "create table derbyTbl1(num int, addr varchar(40));"
218                         + "create table derbyTbl2(num int, addr varchar(40));"
219                         + "create table derbyTbl3(num int, addr varchar(40));"
220                         + "insert into derb");
221     }
222 }
223
Popular Tags