Search in sources :

Example 1 with SelectFromDatabaseChangeLogStatement

use of liquibase.statement.core.SelectFromDatabaseChangeLogStatement in project liquibase by liquibase.

the class SelectFromDatabaseChangeLogGenerator method generateSql.

@Override
public Sql[] generateSql(SelectFromDatabaseChangeLogStatement statement, final Database database, SqlGeneratorChain sqlGeneratorChain) {
    List<ColumnConfig> columnsToSelect = Arrays.asList(statement.getColumnsToSelect());
    String sql = "SELECT " + (database instanceof MSSQLDatabase && statement.getLimit() != null ? "TOP " + statement.getLimit() + " " : "") + StringUtils.join(columnsToSelect, ",", new StringUtils.StringUtilsFormatter<ColumnConfig>() {

        @Override
        public String toString(ColumnConfig column) {
            if (column.getComputed() != null && column.getComputed()) {
                return column.getName();
            } else {
                return database.escapeColumnName(null, null, null, column.getName());
            }
        }
    }).toUpperCase() + " FROM " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName());
    SelectFromDatabaseChangeLogStatement.WhereClause whereClause = statement.getWhereClause();
    if (whereClause != null) {
        if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByTag) {
            sql += " WHERE " + database.escapeColumnName(null, null, null, "TAG") + "='" + ((SelectFromDatabaseChangeLogStatement.ByTag) whereClause).getTagName() + "'";
        } else if (whereClause instanceof SelectFromDatabaseChangeLogStatement.ByNotNullCheckSum) {
            sql += " WHERE " + database.escapeColumnName(null, null, null, "MD5SUM") + " IS NOT NULL";
        } else {
            throw new UnexpectedLiquibaseException("Unknown where clause type: " + whereClause.getClass().getName());
        }
    }
    if (statement.getOrderByColumns() != null && statement.getOrderByColumns().length > 0) {
        sql += " ORDER BY ";
        Iterator<String> orderBy = Arrays.asList(statement.getOrderByColumns()).iterator();
        while (orderBy.hasNext()) {
            String orderColumn = orderBy.next();
            String[] orderColumnData = orderColumn.split(" ");
            sql += database.escapeColumnName(null, null, null, orderColumnData[0]);
            if (orderColumnData.length == 2) {
                sql += " ";
                sql += orderColumnData[1].toUpperCase();
            }
            if (orderBy.hasNext()) {
                sql += ", ";
            }
        }
    }
    if (statement.getLimit() != null) {
        if (database instanceof OracleDatabase) {
            if (whereClause == null) {
                sql += " WHERE ROWNUM=" + statement.getLimit();
            } else {
                sql += " AND ROWNUM=" + statement.getLimit();
            }
        } else if (database instanceof MySQLDatabase || database instanceof PostgresDatabase) {
            sql += " LIMIT " + statement.getLimit();
        } else if (database instanceof DB2Database) {
            sql += " FETCH FIRST " + statement.getLimit() + " ROWS ONLY";
        }
    }
    return new Sql[] { new UnparsedSql(sql) };
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) UnparsedSql(liquibase.sql.UnparsedSql) SelectFromDatabaseChangeLogStatement(liquibase.statement.core.SelectFromDatabaseChangeLogStatement) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql) StringUtils(liquibase.util.StringUtils) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

ColumnConfig (liquibase.change.ColumnConfig)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 Sql (liquibase.sql.Sql)1 UnparsedSql (liquibase.sql.UnparsedSql)1 SelectFromDatabaseChangeLogStatement (liquibase.statement.core.SelectFromDatabaseChangeLogStatement)1 StringUtils (liquibase.util.StringUtils)1