{
int slashPos = connectionUrl.lastIndexOf('/');
if (slashPos < 0)
{
throw new DatabaseOperationException("Cannot parse the given connection url "+connectionUrl);
}
int paramPos = connectionUrl.lastIndexOf('?');
String baseDb = connectionUrl.substring(0, slashPos + 1) + "template1";
String dbName = (paramPos > slashPos ? connectionUrl.substring(slashPos + 1, paramPos) : connectionUrl.substring(slashPos + 1));
Connection connection = null;
Statement stmt = null;
StringBuffer sql = new StringBuffer();
sql.append(createDb ? "CREATE" : "DROP");
sql.append(" DATABASE ");
sql.append(dbName);
if ((parameters != null) && !parameters.isEmpty())
{
for (Iterator it = parameters.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
sql.append(" ");
sql.append(entry.getKey().toString());
if (entry.getValue() != null)
{
sql.append(" ");
sql.append(entry.getValue().toString());
}
}
}
if (getLog().isDebugEnabled())
{
getLog().debug("About to create database via "+baseDb+" using this SQL: "+sql.toString());
}
try
{
Class.forName(jdbcDriverClassName);
connection = DriverManager.getConnection(baseDb, username, password);
stmt = connection.createStatement();
stmt.execute(sql.toString());
logWarnings(connection);
}
catch (Exception ex)
{
throw new DatabaseOperationException("Error while trying to " + (createDb ? "create" : "drop") + " a database: "+ex.getLocalizedMessage(), ex);
}
finally
{
if (stmt != null)
{