dropDB = connection.createStatement();
String dropSql = "drop database if exists " + dbName;
try {
dropDB.execute(dropSql);
} catch (SQLException e) {
throw new ExtendedSQLException(e, dropSql);
}
createDB = connection.createStatement();
String createSql = "create database " + dbName + " with owner " + user;
try {
createDB.execute(createSql);
} catch (SQLException e) {
throw new ExtendedSQLException(e, createSql);
}
log.info("Dropped and created postgres database " + dbName + ".");
} finally {
if (dropDB != null) {
dropDB.close();
}
if (createDB != null) {
createDB.close();
}
if (connection != null) {
connection.close();
}
}
} else if (dbTypeMapping.equals("Oracle10g")) {
Connection connection = null;
PreparedStatement cleanUserStatement = null;
try {
connection = DbUtil.getConnection(dbUrl, adminUser, adminPassword);
connection.setAutoCommit(false);
String plsql = "declare cursor all_objects_to_drop is\n"
+ "select * from user_objects where object_type in ('TABLE', 'VIEW', 'FUNCTION', 'SEQUENCE');\n"
+ "begin\n"
+ " for obj in all_objects_to_drop loop\n"
+ " begin\n"
+ " if obj.object_type = 'TABLE' then\n"
+ " execute immediate('DROP '||obj.object_type||' '||obj.object_name||' CASCADE CONSTRAINTS PURGE');\n"
+ " else\n"
+ " execute immediate('DROP '||obj.object_type||' '||obj.object_name);\n"
+ " end if;\n"
+ " exception when others then null;\n"
+ " end;\n"
+ " end loop;\n"
+ " end;\n";
try {
cleanUserStatement = connection.prepareStatement(plsql);
} catch (SQLException e) {
throw new ExtendedSQLException(e, plsql);
}
cleanUserStatement.execute();
connection.commit();
log.info("Cleaned Oracle database " + dbName + ".");