use of org.jumpmind.db.sql.ISqlResultsListener in project symmetric-ds by JumpMind.
the class AbstractSymmetricDialect method createOrAlterTablesIfNecessary.
/*
* @return true if SQL was executed.
*/
public boolean createOrAlterTablesIfNecessary(String... tableNames) {
try {
log.info("Checking if SymmetricDS tables need created or altered");
Database modelFromXml = readSymmetricSchemaFromXml();
Database modelFromDatabase = readSymmetricSchemaFromDatabase();
if (tableNames != null && tableNames.length > 0) {
tableNames = platform.alterCaseToMatchDatabaseDefaultCase(tableNames);
modelFromXml.removeAllTablesExcept(tableNames);
modelFromDatabase.removeAllTablesExcept(tableNames);
}
IDdlBuilder builder = platform.getDdlBuilder();
List<IAlterDatabaseInterceptor> alterDatabaseInterceptors = extensionService.getExtensionPointList(IAlterDatabaseInterceptor.class);
IAlterDatabaseInterceptor[] interceptors = alterDatabaseInterceptors.toArray(new IAlterDatabaseInterceptor[alterDatabaseInterceptors.size()]);
if (builder.isAlterDatabase(modelFromDatabase, modelFromXml, interceptors)) {
String delimiter = platform.getDatabaseInfo().getSqlCommandDelimiter();
ISqlResultsListener resultsListener = new LogSqlResultsListener(log);
List<IDatabaseUpgradeListener> databaseUpgradeListeners = extensionService.getExtensionPointList(IDatabaseUpgradeListener.class);
for (IDatabaseUpgradeListener listener : databaseUpgradeListeners) {
String sql = listener.beforeUpgrade(this, this.parameterService.getTablePrefix(), modelFromDatabase, modelFromXml);
SqlScript script = new SqlScript(sql, getPlatform().getSqlTemplate(), true, false, false, delimiter, null);
script.setListener(resultsListener);
script.execute(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
}
String alterSql = builder.alterDatabase(modelFromDatabase, modelFromXml, interceptors);
if (isNotBlank(alterSql)) {
log.info("There are SymmetricDS tables that needed altered");
log.debug("Alter SQL generated: {}", alterSql);
SqlScript script = new SqlScript(alterSql, getPlatform().getSqlTemplate(), true, false, false, delimiter, null);
script.setListener(resultsListener);
script.execute(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
for (IDatabaseUpgradeListener listener : databaseUpgradeListeners) {
String sql = listener.afterUpgrade(this, this.parameterService.getTablePrefix(), modelFromXml);
script = new SqlScript(sql, getPlatform().getSqlTemplate(), true, false, false, delimiter, null);
script.setListener(resultsListener);
script.execute(platform.getDatabaseInfo().isRequiresAutoCommitForDdl());
}
log.info("Done with auto update of SymmetricDS tables");
return true;
} else {
return false;
}
} else {
return false;
}
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
Aggregations