Search in sources :

Example 6 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class ChangeSet method generateCheckSum.

public CheckSum generateCheckSum() {
    if (checkSum == null) {
        StringBuffer stringToMD5 = new StringBuffer();
        for (Change change : getChanges()) {
            stringToMD5.append(change.generateCheckSum()).append(":");
        }
        for (SqlVisitor visitor : this.getSqlVisitors()) {
            stringToMD5.append(visitor.generateCheckSum()).append(";");
        }
        checkSum = CheckSum.compute(stringToMD5.toString());
    }
    return checkSum;
}
Also used : Change(liquibase.change.Change) EmptyChange(liquibase.change.core.EmptyChange) RawSQLChange(liquibase.change.core.RawSQLChange) DbmsTargetedChange(liquibase.change.DbmsTargetedChange) SqlVisitor(liquibase.sql.visitor.SqlVisitor)

Example 7 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class ChangeSet method handleChildNode.

protected void handleChildNode(ParsedNode child, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    if (child.getName().equals("rollback")) {
        handleRollbackNode(child, resourceAccessor);
    } else if (child.getName().equals("validCheckSum") || child.getName().equals("validCheckSums")) {
        if (child.getValue() == null) {
            return;
        }
        if (child.getValue() instanceof Collection) {
            for (Object checksum : (Collection) child.getValue()) {
                addValidCheckSum((String) checksum);
            }
        } else {
            addValidCheckSum(child.getValue(String.class));
        }
    } else if (child.getName().equals("modifySql")) {
        String dbmsString = StringUtils.trimToNull(child.getChildValue(null, "dbms", String.class));
        String contextString = StringUtils.trimToNull(child.getChildValue(null, "context", String.class));
        String labelsString = StringUtils.trimToNull(child.getChildValue(null, "labels", String.class));
        boolean applyToRollback = child.getChildValue(null, "applyToRollback", false);
        Set<String> dbms = new HashSet<String>();
        if (dbmsString != null) {
            dbms.addAll(StringUtils.splitAndTrim(dbmsString, ","));
        }
        ContextExpression context = null;
        if (contextString != null) {
            context = new ContextExpression(contextString);
        }
        Labels labels = null;
        if (labelsString != null) {
            labels = new Labels(labelsString);
        }
        List<ParsedNode> potentialVisitors = child.getChildren();
        for (ParsedNode node : potentialVisitors) {
            SqlVisitor sqlVisitor = SqlVisitorFactory.getInstance().create(node.getName());
            if (sqlVisitor != null) {
                sqlVisitor.setApplyToRollback(applyToRollback);
                if (dbms.size() > 0) {
                    sqlVisitor.setApplicableDbms(dbms);
                }
                sqlVisitor.setContexts(context);
                sqlVisitor.setLabels(labels);
                sqlVisitor.load(node, resourceAccessor);
                addSqlVisitor(sqlVisitor);
            }
        }
    } else if (child.getName().equals("preConditions")) {
        this.preconditions = new PreconditionContainer();
        try {
            this.preconditions.load(child, resourceAccessor);
        } catch (ParsedNodeException e) {
            e.printStackTrace();
        }
    } else if (child.getName().equals("changes")) {
        for (ParsedNode changeNode : child.getChildren()) {
            handleChildNode(changeNode, resourceAccessor);
        }
    } else {
        Change change = toChange(child, resourceAccessor);
        if (change == null && child.getValue() instanceof String) {
            this.setAttribute(child.getName(), child.getValue());
        } else {
            addChange(change);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) PreconditionContainer(liquibase.precondition.core.PreconditionContainer) ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels) Change(liquibase.change.Change) EmptyChange(liquibase.change.core.EmptyChange) RawSQLChange(liquibase.change.core.RawSQLChange) DbmsTargetedChange(liquibase.change.DbmsTargetedChange) SqlVisitor(liquibase.sql.visitor.SqlVisitor) ParsedNodeException(liquibase.parser.core.ParsedNodeException)

Example 8 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class AbstractJdbcDatabaseTest method executeRollbackStatements_WithChangeOverload_ShouldIncludeAppendTextFromApplyToRollbackTrueVisitor.

@Test
public void executeRollbackStatements_WithChangeOverload_ShouldIncludeAppendTextFromApplyToRollbackTrueVisitor() throws Exception {
    Database database = getDatabase();
    final MockExecutor mockExecutor = new MockExecutor();
    ExecutorService.getInstance().setExecutor(database, mockExecutor);
    final List<SqlVisitor> sqlVisitors = new ArrayList<SqlVisitor>();
    final CreateTableChange change = new CreateTableChange();
    change.setTableName("test_table");
    final AppendSqlVisitor appendSqlVisitor = new AppendSqlVisitor();
    appendSqlVisitor.setApplyToRollback(true);
    appendSqlVisitor.setValue(" SHOULD BE APPENDED");
    sqlVisitors.add(appendSqlVisitor);
    database.executeRollbackStatements(change, sqlVisitors);
    assertEquals("DROP TABLE test_table SHOULD BE APPENDED;", mockExecutor.getRanSql().trim());
}
Also used : AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) MockExecutor(liquibase.sdk.executor.MockExecutor) ArrayList(java.util.ArrayList) CreateTableChange(liquibase.change.core.CreateTableChange) SqlVisitor(liquibase.sql.visitor.SqlVisitor) AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) Test(org.junit.Test)

Example 9 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class AbstractJdbcDatabaseTest method executeRollbackStatements_WithStatementsOverload_ShouldNotIncludeAppendTextFromApplyToRollbackFalseVisitor.

@Test
public void executeRollbackStatements_WithStatementsOverload_ShouldNotIncludeAppendTextFromApplyToRollbackFalseVisitor() throws Exception {
    Database database = getDatabase();
    final MockExecutor mockExecutor = new MockExecutor();
    ExecutorService.getInstance().setExecutor(database, mockExecutor);
    final List<SqlVisitor> sqlVisitors = new ArrayList<SqlVisitor>();
    final SqlStatement dropTableStatement = new DropTableStatement(null, null, "test_table", false);
    final AppendSqlVisitor appendSqlVisitor = new AppendSqlVisitor();
    appendSqlVisitor.setApplyToRollback(false);
    appendSqlVisitor.setValue(" SHOULD NOT BE APPENDED");
    sqlVisitors.add(appendSqlVisitor);
    database.executeRollbackStatements(new SqlStatement[] { dropTableStatement }, sqlVisitors);
    assertEquals("DROP TABLE test_table;", mockExecutor.getRanSql().trim());
}
Also used : AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) SqlStatement(liquibase.statement.SqlStatement) MockExecutor(liquibase.sdk.executor.MockExecutor) ArrayList(java.util.ArrayList) DropTableStatement(liquibase.statement.core.DropTableStatement) SqlVisitor(liquibase.sql.visitor.SqlVisitor) AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) Test(org.junit.Test)

Example 10 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class AbstractExecutor method applyVisitors.

protected String[] applyVisitors(SqlStatement statement, List<SqlVisitor> sqlVisitors) throws DatabaseException {
    Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, database);
    if (sql == null) {
        return new String[0];
    }
    String[] returnSql = new String[sql.length];
    for (int i = 0; i < sql.length; i++) {
        if (sql[i] == null) {
            continue;
        }
        returnSql[i] = sql[i].toSql();
        if (sqlVisitors != null) {
            for (SqlVisitor visitor : sqlVisitors) {
                returnSql[i] = visitor.modifySql(returnSql[i], database);
            }
        }
    }
    return returnSql;
}
Also used : SqlVisitor(liquibase.sql.visitor.SqlVisitor) Sql(liquibase.sql.Sql)

Aggregations

SqlVisitor (liquibase.sql.visitor.SqlVisitor)10 ArrayList (java.util.ArrayList)6 MockExecutor (liquibase.sdk.executor.MockExecutor)4 AppendSqlVisitor (liquibase.sql.visitor.AppendSqlVisitor)4 Test (org.junit.Test)4 ContextExpression (liquibase.ContextExpression)2 Change (liquibase.change.Change)2 DbmsTargetedChange (liquibase.change.DbmsTargetedChange)2 CreateTableChange (liquibase.change.core.CreateTableChange)2 EmptyChange (liquibase.change.core.EmptyChange)2 RawSQLChange (liquibase.change.core.RawSQLChange)2 SqlStatement (liquibase.statement.SqlStatement)2 DropTableStatement (liquibase.statement.core.DropTableStatement)2 Labels (liquibase.Labels)1 ParsedNode (liquibase.parser.core.ParsedNode)1 ParsedNodeException (liquibase.parser.core.ParsedNodeException)1 PreconditionContainer (liquibase.precondition.core.PreconditionContainer)1 Sql (liquibase.sql.Sql)1