| 1 | |
package liquibase.sql; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.Arrays; |
| 5 | |
import java.util.HashSet; |
| 6 | |
import java.util.List; |
| 7 | |
import java.util.Set; |
| 8 | |
|
| 9 | |
import liquibase.database.structure.DatabaseObject; |
| 10 | |
|
| 11 | 0 | public class UnparsedSql implements Sql { |
| 12 | |
|
| 13 | |
private String sql; |
| 14 | |
private String endDelimiter; |
| 15 | 21 | private Set<DatabaseObject> affectedDatabaseObjects = new HashSet<DatabaseObject>(); |
| 16 | |
|
| 17 | |
public UnparsedSql(String sql, DatabaseObject... affectedDatabaseObjects) { |
| 18 | 21 | this(sql, ";", affectedDatabaseObjects); |
| 19 | 21 | } |
| 20 | |
|
| 21 | 21 | public UnparsedSql(String sql, String endDelimiter, DatabaseObject... affectedDatabaseObjects) { |
| 22 | 21 | this.sql = sql.trim(); |
| 23 | 21 | this.endDelimiter = endDelimiter; |
| 24 | |
|
| 25 | 21 | this.affectedDatabaseObjects.addAll(Arrays.asList(affectedDatabaseObjects)); |
| 26 | 21 | List<DatabaseObject> moreAffectedDatabaseObjects = new ArrayList<DatabaseObject>(); |
| 27 | |
|
| 28 | 21 | boolean foundMore = true; |
| 29 | 42 | while (foundMore) { |
| 30 | 21 | for (DatabaseObject object : this.affectedDatabaseObjects) { |
| 31 | 0 | DatabaseObject[] containingObjects = object.getContainingObjects(); |
| 32 | 0 | if (containingObjects != null) { |
| 33 | 0 | for (DatabaseObject containingObject : containingObjects) { |
| 34 | 0 | if (containingObject != null && !this.affectedDatabaseObjects.contains(containingObject) |
| 35 | |
&& !moreAffectedDatabaseObjects.contains(containingObject)) { |
| 36 | 0 | moreAffectedDatabaseObjects.add(containingObject); |
| 37 | |
} |
| 38 | |
} |
| 39 | |
} |
| 40 | 0 | } |
| 41 | 21 | foundMore = moreAffectedDatabaseObjects.size() > 0; |
| 42 | 21 | this.affectedDatabaseObjects.addAll(moreAffectedDatabaseObjects); |
| 43 | 21 | moreAffectedDatabaseObjects.clear(); |
| 44 | |
} |
| 45 | |
|
| 46 | 21 | this.affectedDatabaseObjects.addAll(moreAffectedDatabaseObjects); |
| 47 | 21 | } |
| 48 | |
|
| 49 | |
@Override |
| 50 | |
public String toSql() { |
| 51 | 21 | return sql; |
| 52 | |
} |
| 53 | |
|
| 54 | |
@Override |
| 55 | |
public String getEndDelimiter() { |
| 56 | 0 | return endDelimiter; |
| 57 | |
} |
| 58 | |
|
| 59 | |
@Override |
| 60 | |
public Set<? extends DatabaseObject> getAffectedDatabaseObjects() { |
| 61 | 0 | return affectedDatabaseObjects; |
| 62 | |
} |
| 63 | |
} |