liquibase.sql.UnparsedSql Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
package liquibase.sql;
import liquibase.structure.DatabaseObject;
import liquibase.util.StringUtil;
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 = StringUtil.trimToEmpty(sql);
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;
}
}