Search in sources :

Example 1 with ISqlResultsListener

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);
    }
}
Also used : IDatabaseUpgradeListener(org.jumpmind.symmetric.ext.IDatabaseUpgradeListener) IAlterDatabaseInterceptor(org.jumpmind.db.platform.IAlterDatabaseInterceptor) SqlException(org.jumpmind.db.sql.SqlException) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) ISqlResultsListener(org.jumpmind.db.sql.ISqlResultsListener) IDdlBuilder(org.jumpmind.db.platform.IDdlBuilder) Database(org.jumpmind.db.model.Database) SqlScript(org.jumpmind.db.sql.SqlScript) LogSqlResultsListener(org.jumpmind.db.sql.LogSqlResultsListener)

Aggregations

IOException (java.io.IOException)1 Database (org.jumpmind.db.model.Database)1 IAlterDatabaseInterceptor (org.jumpmind.db.platform.IAlterDatabaseInterceptor)1 IDdlBuilder (org.jumpmind.db.platform.IDdlBuilder)1 ISqlResultsListener (org.jumpmind.db.sql.ISqlResultsListener)1 LogSqlResultsListener (org.jumpmind.db.sql.LogSqlResultsListener)1 SqlException (org.jumpmind.db.sql.SqlException)1 SqlScript (org.jumpmind.db.sql.SqlScript)1 IoException (org.jumpmind.exception.IoException)1 IDatabaseUpgradeListener (org.jumpmind.symmetric.ext.IDatabaseUpgradeListener)1