List<DatabaseInformation> dbs = null;
try {
dbs = getAvailableJDBCDatabases((DatabaseFilter)null);
} catch (WGAPIException e) {
throw new WGBackendException("Cannot create database bc. of exception when testing for existence", e);
}
if (dbs != null) {
Iterator<DatabaseInformation> it = dbs.iterator();
while (it.hasNext()) {
if (it.next().getOptions().get(Database.OPTION_PATH).equalsIgnoreCase(path)) {
throw new WGBackendException("Cannot create database bc. it already exists");
}
}
}
Properties props = new Properties();
props.setProperty("createDatabaseIfNotExist", "true");
Connection con = createJDBCConnection(info, props);
String ddlScript = null;
try {
if (ddl != null) {
Reader read = new InputStreamReader(ddl);
StringWriter write = new StringWriter();
WGUtils.inToOut(read, write, 2048);
read.close();
ddlScript = write.toString();
}
}
catch (IOException e) {
throw new WGBackendException("Cannot create database bc. of exception when reading initialisation script: " + e.getClass().getName() + " - " + e.getMessage());
}
try {
if (ddlScript != null) {
// Execute script
con.setAutoCommit(false);
Iterator statements = WGUtils.deserializeCollection(ddlScript, ";", false, new Character('\'')).iterator();
Statement st = con.createStatement();
while (statements.hasNext()) {
String code = ((String) statements.next()).trim();
if (!code.equals("")) {
st.addBatch(code);
}
}
st.executeBatch();
con.commit();
}
return info;
}
catch (SQLException e) {
throw new WGBackendException("Cannot create database bc. of exception when executing initialisation script: " + e.getClass().getName() + " - " + e.getMessage());
}
finally {
try {
if (con != null) {
con.close();
}
}
catch (SQLException e) {
throw new WGBackendException("Exception when closing connection: " + e.getClass().getName() + " - " + e.getMessage());
}
}
}