Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DatabaseSnapshot |
|
| 2.3846153846153846;2.385 |
1 | package liquibase.snapshot; | |
2 | ||
3 | import java.util.Collection; | |
4 | import java.util.HashSet; | |
5 | import java.util.Set; | |
6 | ||
7 | import liquibase.database.Database; | |
8 | import liquibase.database.structure.Column; | |
9 | import liquibase.database.structure.ForeignKey; | |
10 | import liquibase.database.structure.Index; | |
11 | import liquibase.database.structure.PrimaryKey; | |
12 | import liquibase.database.structure.Sequence; | |
13 | import liquibase.database.structure.Table; | |
14 | import liquibase.database.structure.UniqueConstraint; | |
15 | import liquibase.database.structure.View; | |
16 | import liquibase.util.StringUtils; | |
17 | ||
18 | public class DatabaseSnapshot { | |
19 | ||
20 | private Database database; | |
21 | 0 | private Set<Table> tables = new HashSet<Table>(); |
22 | 0 | private Set<View> views = new HashSet<View>(); |
23 | 0 | private Set<ForeignKey> foreignKeys = new HashSet<ForeignKey>(); |
24 | 0 | private Set<UniqueConstraint> uniqueConstraints = new HashSet<UniqueConstraint>(); |
25 | 0 | private Set<Index> indexes = new HashSet<Index>(); |
26 | 0 | private Set<PrimaryKey> primaryKeys = new HashSet<PrimaryKey>(); |
27 | 0 | private Set<Sequence> sequences = new HashSet<Sequence>(); |
28 | ||
29 | private String schema; | |
30 | ||
31 | private Table databaseChangeLogTable; | |
32 | private Table databaseChangeLogLockTable; | |
33 | ||
34 | 0 | public DatabaseSnapshot(Database database, String requestedSchema) { |
35 | 0 | this.database = database; |
36 | 0 | this.schema = requestedSchema; |
37 | 0 | } |
38 | ||
39 | public Database getDatabase() { | |
40 | 0 | return database; |
41 | } | |
42 | ||
43 | public Set<Table> getTables() { | |
44 | 0 | return tables; |
45 | } | |
46 | ||
47 | public Set<View> getViews() { | |
48 | 0 | return views; |
49 | } | |
50 | ||
51 | public Set<ForeignKey> getForeignKeys() { | |
52 | 0 | return foreignKeys; |
53 | } | |
54 | ||
55 | public Set<Index> getIndexes() { | |
56 | 0 | return indexes; |
57 | } | |
58 | ||
59 | public Set<PrimaryKey> getPrimaryKeys() { | |
60 | 0 | return primaryKeys; |
61 | } | |
62 | ||
63 | public Set<Sequence> getSequences() { | |
64 | 0 | return sequences; |
65 | } | |
66 | ||
67 | public Set<UniqueConstraint> getUniqueConstraints() { | |
68 | 0 | return this.uniqueConstraints; |
69 | } | |
70 | ||
71 | public Table getTable(String tableName) { | |
72 | 0 | for (Table table : getTables()) { |
73 | 0 | if (table.getName().equalsIgnoreCase(tableName)) { |
74 | 0 | return table; |
75 | } | |
76 | } | |
77 | 0 | return null; |
78 | } | |
79 | ||
80 | public ForeignKey getForeignKey(String foreignKeyName) { | |
81 | 0 | for (ForeignKey fk : getForeignKeys()) { |
82 | 0 | if (fk.getName().equalsIgnoreCase(foreignKeyName)) { |
83 | 0 | return fk; |
84 | } | |
85 | } | |
86 | 0 | return null; |
87 | } | |
88 | ||
89 | public Sequence getSequence(String sequenceName) { | |
90 | 0 | for (Sequence sequence : getSequences()) { |
91 | 0 | if (sequence.getName().equalsIgnoreCase(sequenceName)) { |
92 | 0 | return sequence; |
93 | } | |
94 | } | |
95 | 0 | return null; |
96 | } | |
97 | ||
98 | public Index getIndex(String indexName) { | |
99 | 0 | for (Index index : getIndexes()) { |
100 | 0 | if (StringUtils.trimToEmpty(index.getName()).equalsIgnoreCase(indexName)) { |
101 | 0 | return index; |
102 | } | |
103 | } | |
104 | 0 | return null; |
105 | } | |
106 | ||
107 | public View getView(String viewName) { | |
108 | 0 | for (View view : getViews()) { |
109 | 0 | if (view.getName().equalsIgnoreCase(viewName)) { |
110 | 0 | return view; |
111 | } | |
112 | } | |
113 | 0 | return null; |
114 | } | |
115 | ||
116 | public PrimaryKey getPrimaryKey(String pkName) { | |
117 | 0 | for (PrimaryKey pk : getPrimaryKeys()) { |
118 | 0 | if (pk.getName().equalsIgnoreCase(pkName)) { |
119 | 0 | return pk; |
120 | } | |
121 | } | |
122 | 0 | return null; |
123 | } | |
124 | ||
125 | public PrimaryKey getPrimaryKeyForTable(String tableName) { | |
126 | 0 | for (PrimaryKey pk : getPrimaryKeys()) { |
127 | 0 | if (pk.getTable().getName().equalsIgnoreCase(tableName)) { |
128 | 0 | return pk; |
129 | } | |
130 | } | |
131 | 0 | return null; |
132 | } | |
133 | ||
134 | public UniqueConstraint getUniqueConstraint(String ucName) { | |
135 | 0 | for (UniqueConstraint uc : getUniqueConstraints()) { |
136 | 0 | if (uc.getName().equalsIgnoreCase(ucName)) { |
137 | 0 | return uc; |
138 | } | |
139 | } | |
140 | 0 | return null; |
141 | } | |
142 | ||
143 | public String getSchema() { | |
144 | 0 | return schema; |
145 | } | |
146 | ||
147 | public boolean isPrimaryKey(Column columnInfo) { | |
148 | 0 | for (PrimaryKey pk : getPrimaryKeys()) { |
149 | 0 | if (columnInfo.getTable() == null) { |
150 | 0 | continue; |
151 | } | |
152 | 0 | if (pk.getTable().getName().equalsIgnoreCase(columnInfo.getTable().getName())) { |
153 | 0 | if (pk.getColumnNamesAsList().contains(columnInfo.getName())) { |
154 | 0 | return true; |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | 0 | return false; |
160 | } | |
161 | ||
162 | public Collection<Column> getColumns() { | |
163 | 0 | Set<Column> returnSet = new HashSet<Column>(); |
164 | ||
165 | 0 | for (Table table : getTables()) { |
166 | 0 | for (Column column : table.getColumns()) { |
167 | 0 | returnSet.add(column); |
168 | } | |
169 | } | |
170 | ||
171 | 0 | return returnSet; |
172 | } | |
173 | ||
174 | public Column getColumn(String tableName, String columnName) { | |
175 | 0 | for (Table table : getTables()) { |
176 | 0 | for (Column column : table.getColumns()) { |
177 | 0 | if (table.getName().equalsIgnoreCase(tableName) && column.getName().equalsIgnoreCase(columnName)) { |
178 | 0 | return column; |
179 | } | |
180 | } | |
181 | } | |
182 | 0 | return null; |
183 | } | |
184 | ||
185 | public boolean hasDatabaseChangeLogTable() { | |
186 | 0 | return databaseChangeLogTable != null; |
187 | } | |
188 | ||
189 | public Table getDatabaseChangeLogTable() { | |
190 | 0 | return databaseChangeLogTable; |
191 | } | |
192 | ||
193 | public void setDatabaseChangeLogTable(Table table) { | |
194 | 0 | this.databaseChangeLogTable = table; |
195 | 0 | } |
196 | ||
197 | public Table getDatabaseChangeLogLockTable() { | |
198 | 0 | return databaseChangeLogLockTable; |
199 | } | |
200 | ||
201 | public void setDatabaseChangeLogLockTable(Table table) { | |
202 | 0 | this.databaseChangeLogLockTable = table; |
203 | 0 | } |
204 | } |