log.info("Running hbm2ddl schema export");
Connection connection = null;
FileWriter fileOutput = null;
ConnectionProvider connectionProvider = null;
Statement statement = null;
Properties props = new Properties();
props.putAll(dialect.getDefaultProperties());
props.putAll(connectionProperties);
try {
if (outputFile != null) {
log.info("writing generated schema to file: " + outputFile);
fileOutput = new FileWriter(outputFile);
}
if (export) {
log.info("exporting generated schema to database");
connectionProvider = ConnectionProviderFactory.newConnectionProvider(props);
connection = connectionProvider.getConnection();
if (!connection.getAutoCommit()) {
connection.commit();
connection.setAutoCommit(true);
}
statement = connection.createStatement();
}
for (int i = 0; i < dropSQL.length; i++) {
try {
String formatted = dropSQL[i];
formatted = customModify(formatted);
if (delimiter != null) formatted += delimiter;
if (script) System.out.println(formatted);
log.debug(formatted);
if (outputFile != null) fileOutput.write(formatted + "\n");
if (export) statement.executeUpdate(dropSQL[i]);
} catch (SQLException e) {
log.debug("Unsuccessful: " + dropSQL[i]);
log.debug(e.getMessage());
}
}
if (!justDrop) {
for (int j = 0; j < createSQL.length; j++) {
try {
String line = createSQL[j];
line = customModify(line);
String formatted = format ? format(line) : line;
if (delimiter != null) formatted += delimiter;
if (script) System.out.println(formatted);
log.debug(formatted);
if (outputFile != null) fileOutput.write(formatted + "\n");
if (export) statement.executeUpdate(createSQL[j]);
} catch (SQLException e) {
log.error("Unsuccessful: " + createSQL[j]);
log.error(e.getMessage());
}
}
}
log.info("schema export complete");
} catch (Exception e) {
log.error("schema export unsuccessful", e);
} finally {
try {
if (statement != null) statement.close();
if (connection != null) {
JDBCExceptionReporter.logWarnings(connection.getWarnings());
connection.clearWarnings();
connectionProvider.closeConnection(connection);
connectionProvider.close();
}
} catch (Exception e) {
log.error("Could not close connection", e);
}