Search in sources :

Example 1 with DropViewStatement

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

the class CreateViewChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> statements = new ArrayList<>();
    boolean replaceIfExists = false;
    if ((getReplaceIfExists() != null) && getReplaceIfExists()) {
        replaceIfExists = true;
    }
    boolean fullDefinition = false;
    if (this.fullDefinition != null) {
        fullDefinition = this.fullDefinition;
    }
    String selectQuery;
    String path = getPath();
    if (path == null) {
        selectQuery = StringUtil.trimToNull(getSelectQuery());
    } else {
        try {
            InputStream stream = openSqlStream();
            if (stream == null) {
                throw new IOException(FileUtil.getFileNotFoundMessage(path));
            }
            selectQuery = StreamUtil.readStreamAsString(stream, encoding);
            if (getChangeSet() != null) {
                ChangeLogParameters parameters = getChangeSet().getChangeLogParameters();
                if (parameters != null) {
                    selectQuery = parameters.expandExpressions(selectQuery, getChangeSet().getChangeLog());
                }
            }
        } catch (IOException e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }
    if (!supportsReplaceIfExistsOption(database) && replaceIfExists) {
        statements.add(new DropViewStatement(getCatalogName(), getSchemaName(), getViewName()));
        statements.add(createViewStatement(getCatalogName(), getSchemaName(), getViewName(), selectQuery, false).setFullDefinition(fullDefinition));
    } else {
        statements.add(createViewStatement(getCatalogName(), getSchemaName(), getViewName(), selectQuery, replaceIfExists).setFullDefinition(fullDefinition));
    }
    if ((database instanceof OracleDatabase) && (StringUtil.trimToNull(remarks) != null)) {
        SetTableRemarksStatement remarksStatement = new SetTableRemarksStatement(catalogName, schemaName, viewName, remarks);
        if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
            statements.add(remarksStatement);
        }
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) SqlStatement(liquibase.statement.SqlStatement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) SetTableRemarksStatement(liquibase.statement.core.SetTableRemarksStatement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DropViewStatement(liquibase.statement.core.DropViewStatement) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 SqlStatement (liquibase.statement.SqlStatement)1 DropViewStatement (liquibase.statement.core.DropViewStatement)1 SetTableRemarksStatement (liquibase.statement.core.SetTableRemarksStatement)1