liquibase.sql.UnparsedSql Maven / Gradle / Ivy
package liquibase.sql;
import liquibase.structure.DatabaseObject;
import liquibase.util.StringUtils;
import java.util.*;
public class UnparsedSql implements Sql {
private String sql;
private String endDelimiter;
private Set affectedDatabaseObjects = new HashSet<>();
public UnparsedSql(String sql, DatabaseObject... affectedDatabaseObjects) {
this(sql, ";", affectedDatabaseObjects);
}
public UnparsedSql(String sql, String endDelimiter, DatabaseObject... affectedDatabaseObjects) {
this.sql = StringUtils.trimToEmpty(sql.trim());
this.endDelimiter = endDelimiter;
this.affectedDatabaseObjects.addAll(Arrays.asList(affectedDatabaseObjects));
List moreAffectedDatabaseObjects = new ArrayList<>();
boolean foundMore = true;
while (foundMore) {
for (DatabaseObject object : this.affectedDatabaseObjects) {
DatabaseObject[] containingObjects = object.getContainingObjects();
if (containingObjects != null) {
for (DatabaseObject containingObject : containingObjects) {
if ((containingObject != null) && !this.affectedDatabaseObjects.contains(containingObject) &&
!moreAffectedDatabaseObjects.contains(containingObject)) {
moreAffectedDatabaseObjects.add(containingObject);
}
}
}
}
foundMore = !moreAffectedDatabaseObjects.isEmpty();
this.affectedDatabaseObjects.addAll(moreAffectedDatabaseObjects);
moreAffectedDatabaseObjects.clear();
}
this.affectedDatabaseObjects.addAll(moreAffectedDatabaseObjects);
}
@Override
public String toSql() {
return sql;
}
@Override
public String toString() {
return toSql()+getEndDelimiter();
}
@Override
public String getEndDelimiter() {
return endDelimiter;
}
@Override
public Set extends DatabaseObject> getAffectedDatabaseObjects() {
return affectedDatabaseObjects;
}
}