1 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 ; 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 28 public class RunSQLHelper { 29 30 private final static Log log = LogFactory.getLog(RunSQLHelper.class); 31 32 public static final String SQL_SUCCESS_MSG = "SQL command/s successful"; 33 34 public static final String SQL_EMPTY_MSG = "SQL Command/s can't be empty"; 35 36 private static final String DB_CREATED_MSG = "Database created"; 37 38 private static final String DB_DELETED_MSG = "Database deleted"; 39 40 private static final String DERBY_BACKUP_FOLDER = "derby.backup"; 41 42 private static final String PARENT_FOLDER = ".."; 43 44 private static final String BAK_EXTENSION = ".bak"; 45 46 private static final String BAK_PREFIX = "BAK_"; 47 48 public String createDB(String dbName) { 49 String result = DB_CREATED_MSG + ": " + dbName; 50 51 Connection conn = null; 52 try { 53 conn = DerbyConnectionUtil.getDerbyConnection(dbName, 54 DerbyConnectionUtil.CREATE_DB_PROP); 55 } catch (Throwable e) { 56 if (e instanceof SQLException ) { 57 result = getSQLError((SQLException ) e); 58 } else { 59 result = e.getMessage(); 60 } 61 } finally { 62 try { 64 if (conn != null) { 65 conn.close(); 66 } 67 } catch (SQLException e) { 68 result = "Problem closing DB connection"; 69 } 70 } 71 72 return result; 73 } 74 75 public String backupDB(String derbyHome, String dbName) { 76 return ""; 77 } 78 79 public String restoreDB(String derbyHome, String dbName) { 80 return ""; 81 } 82 83 public String deleteDB(String derbyHome, String dbName) { 84 String result = DB_DELETED_MSG + ": " + dbName; 85 86 if (!shutdownDB(dbName)) { 88 result = "Database not deleted: " + dbName 89 + " Couldn't shutdown db: " + dbName; 90 return result; 91 } 92 93 try { 94 File derbyBackupFolder = new File (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 oldDBFolder = new File (derbyHome + File.separatorChar + dbName); 107 if (oldDBFolder.exists()) { 108 File tmpFile = File.createTempFile(BAK_PREFIX + dbName, 111 BAK_EXTENSION, derbyBackupFolder); 112 File newDBFolder = new File (tmpFile.getAbsolutePath()); 113 117 if (tmpFile.delete()) { 118 if (newDBFolder.mkdirs()) { 119 if (!oldDBFolder.renameTo(new File (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 e) { 129 e.printStackTrace(); 130 } 131 132 return result; 133 } 134 135 public String runSQL(String dbName, String sql) { 136 String 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 conn = null; 144 Statement s = null; 145 try { 146 147 conn = DerbyConnectionUtil.getDerbyConnection(dbName); 148 conn.setAutoCommit(false); 149 150 s = conn.createStatement(); 151 String [] sqlCmds = sql.split(";"); 152 for (int i = 0; i < sqlCmds.length; i++) { 153 if (sqlCmds[i].trim().length() > 0) { 154 log.debug("SQL" + i + ": <" + sqlCmds[i].trim() + ">"); 156 s.execute(sqlCmds[i]); 157 } 158 } 159 conn.commit(); 160 } catch (Throwable e) { 161 if (e instanceof SQLException ) { 162 result = getSQLError((SQLException ) e); 163 } else { 164 result = e.getMessage(); 165 } 166 } finally { 167 try { 169 if (s != null) { 170 s.close(); 171 } 172 if (conn != null) { 173 conn.close(); 174 } 175 } catch (SQLException 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 dbName) { 186 boolean ok = true; 187 188 boolean gotSQLExc = false; 189 try { 190 DerbyConnectionUtil.getDerbyConnection(dbName, 191 DerbyConnectionUtil.SHUTDOWN_DB_PROP); 192 } catch (SQLException se) { 193 gotSQLExc = true; 194 } 195 196 if (!gotSQLExc) { 197 ok = false; 198 } 199 200 return ok; 201 } 202 203 private String getSQLError(SQLException e) { 204 StringBuffer errorMsg = new StringBuffer (); 205 while (e != null) { 206 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 [] 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 |