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;
}
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);
}
}
}
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());
}
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());
}
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;
}
Aggregations