Search in sources :

Example 1 with View

use of liquibase.structure.core.View in project liquibase by liquibase.

the class RenameViewChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    try {
        ChangeStatus changeStatus = new ChangeStatus();
        View newView = SnapshotGeneratorFactory.getInstance().createSnapshot(new View(getCatalogName(), getSchemaName(), getNewViewName()), database);
        View oldView = SnapshotGeneratorFactory.getInstance().createSnapshot(new View(getCatalogName(), getSchemaName(), getOldViewName()), database);
        if (newView == null && oldView == null) {
            return changeStatus.unknown("Neither view exists");
        }
        if (newView != null && oldView != null) {
            return changeStatus.unknown("Both views exist");
        }
        changeStatus.assertComplete(newView != null, "New view does not exist");
        return changeStatus;
    } catch (Exception e) {
        return new ChangeStatus().unknown(e);
    }
}
Also used : View(liquibase.structure.core.View)

Example 2 with View

use of liquibase.structure.core.View in project liquibase by liquibase.

the class MissingViewChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) missingObject;
    CreateViewChange change = createViewChange();
    change.setViewName(view.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(view.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(view.getSchema().getName());
    }
    String selectQuery = view.getDefinition();
    boolean fullDefinitionOverridden = false;
    if (selectQuery == null) {
        selectQuery = "COULD NOT DETERMINE VIEW QUERY";
    } else if (comparisonDatabase instanceof OracleDatabase && view.getColumns() != null && view.getColumns().size() > 0) {
        String viewName;
        if (change.getCatalogName() == null && change.getSchemaName() == null) {
            viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
        } else {
            viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
        }
        selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtils.join(view.getColumns(), ", ", new StringUtils.StringUtilsFormatter() {

            @Override
            public String toString(Object obj) {
                if (((Column) obj).getComputed() != null && ((Column) obj).getComputed()) {
                    return ((Column) obj).getName();
                } else {
                    return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
                }
            }
        }) + ") AS " + selectQuery;
        change.setFullDefinition(true);
        fullDefinitionOverridden = true;
    }
    change.setSelectQuery(selectQuery);
    if (!fullDefinitionOverridden) {
        change.setFullDefinition(view.getContainsFullDefinition());
    }
    return new Change[] { change };
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) CreateViewChange(liquibase.change.core.CreateViewChange) DatabaseObject(liquibase.structure.DatabaseObject) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) View(liquibase.structure.core.View)

Example 3 with View

use of liquibase.structure.core.View in project liquibase by liquibase.

the class UnexpectedViewChangeGenerator method fixUnexpected.

@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) unexpectedObject;
    DropViewChange change = new DropViewChange();
    change.setViewName(view.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(view.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(view.getSchema().getName());
    }
    for (Column column : view.getColumns()) {
        control.setAlreadyHandledUnexpected(column);
    }
    ;
    return new Change[] { change };
}
Also used : Column(liquibase.structure.core.Column) DropViewChange(liquibase.change.core.DropViewChange) DropViewChange(liquibase.change.core.DropViewChange) Change(liquibase.change.Change) View(liquibase.structure.core.View)

Example 4 with View

use of liquibase.structure.core.View in project liquibase by liquibase.

the class ChangedViewChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) changedObject;
    CreateViewChange change = createViewChange();
    change.setViewName(view.getName());
    change.setReplaceIfExists(true);
    if (control.getIncludeCatalog()) {
        change.setCatalogName(view.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(view.getSchema().getName());
    }
    String selectQuery = view.getDefinition();
    boolean fullDefinitionOverridden = false;
    if (selectQuery == null) {
        selectQuery = "COULD NOT DETERMINE VIEW QUERY";
    } else if (comparisonDatabase instanceof OracleDatabase && view.getColumns() != null && view.getColumns().size() > 0) {
        String viewName;
        if (change.getCatalogName() == null && change.getSchemaName() == null) {
            viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
        } else {
            viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
        }
        selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtils.join(view.getColumns(), ", ", new StringUtils.StringUtilsFormatter() {

            @Override
            public String toString(Object obj) {
                if (((Column) obj).getComputed() != null && ((Column) obj).getComputed()) {
                    return ((Column) obj).getName();
                } else {
                    return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
                }
            }
        }) + ") AS " + selectQuery;
        change.setFullDefinition(true);
        fullDefinitionOverridden = true;
    }
    change.setSelectQuery(selectQuery);
    if (!fullDefinitionOverridden) {
        change.setFullDefinition(view.getContainsFullDefinition());
    }
    return new Change[] { change };
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) CreateViewChange(liquibase.change.core.CreateViewChange) DatabaseObject(liquibase.structure.DatabaseObject) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) View(liquibase.structure.core.View)

Example 5 with View

use of liquibase.structure.core.View in project liquibase by liquibase.

the class CreateViewGeneratorInformix method generateSql.

@Override
public Sql[] generateSql(CreateViewStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String viewName = database.escapeViewName(statement.getCatalogName(), statement.getSchemaName(), statement.getViewName());
    String createClause = "CREATE VIEW  " + viewName + " AS SELECT * FROM (" + statement.getSelectQuery() + ") AS v";
    if (statement.isReplaceIfExists()) {
        return new Sql[] { new UnparsedSql("DROP VIEW IF EXISTS " + viewName), new UnparsedSql(createClause, new View().setName(viewName).setSchema(statement.getCatalogName(), statement.getViewName())) };
    }
    return new Sql[] { new UnparsedSql(createClause) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) View(liquibase.structure.core.View) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Aggregations

View (liquibase.structure.core.View)9 OracleDatabase (liquibase.database.core.OracleDatabase)4 Change (liquibase.change.Change)3 Column (liquibase.structure.core.Column)3 Schema (liquibase.structure.core.Schema)3 SQLException (java.sql.SQLException)2 CatalogAndSchema (liquibase.CatalogAndSchema)2 CreateViewChange (liquibase.change.core.CreateViewChange)2 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)2 Database (liquibase.database.Database)2 InformixDatabase (liquibase.database.core.InformixDatabase)2 DatabaseException (liquibase.exception.DatabaseException)2 CachedRow (liquibase.snapshot.CachedRow)2 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)2 DatabaseObject (liquibase.structure.DatabaseObject)2 DropViewChange (liquibase.change.core.DropViewChange)1 ParsedNodeException (liquibase.parser.core.ParsedNodeException)1 Sql (liquibase.sql.Sql)1 UnparsedSql (liquibase.sql.UnparsedSql)1 GetViewDefinitionStatement (liquibase.statement.core.GetViewDefinitionStatement)1