1 | |
package liquibase.database.structure; |
2 | |
|
3 | |
import liquibase.util.SqlUtil; |
4 | |
|
5 | 0 | public class Column implements DatabaseObject, Comparable<Column> { |
6 | |
private Table table; |
7 | |
private View view; |
8 | |
private String name; |
9 | |
private int dataType; |
10 | |
private int columnSize; |
11 | |
private int decimalDigits; |
12 | |
private LengthSemantics lengthSemantics; |
13 | |
private Boolean nullable; |
14 | |
private String typeName; |
15 | |
private Object defaultValue; |
16 | 0 | private boolean autoIncrement = false; |
17 | 0 | private boolean primaryKey = false; |
18 | 0 | private boolean unique = false; |
19 | |
|
20 | |
|
21 | 0 | private boolean initPrecision = true; |
22 | |
|
23 | 0 | private boolean certainDataType = true; |
24 | |
private String remarks; |
25 | |
|
26 | |
|
27 | |
private String tablespace; |
28 | |
|
29 | |
public Table getTable() { |
30 | 0 | return table; |
31 | |
} |
32 | |
|
33 | |
@Override |
34 | |
public DatabaseObject[] getContainingObjects() { |
35 | 0 | return new DatabaseObject[] { getTable() }; |
36 | |
} |
37 | |
|
38 | |
public Column setTable(Table table) { |
39 | 0 | this.table = table; |
40 | |
|
41 | 0 | return this; |
42 | |
} |
43 | |
|
44 | |
public View getView() { |
45 | 0 | return view; |
46 | |
} |
47 | |
|
48 | |
public Column setView(View view) { |
49 | 0 | this.view = view; |
50 | |
|
51 | 0 | return this; |
52 | |
} |
53 | |
|
54 | |
public String getTablespace() { |
55 | 0 | return tablespace; |
56 | |
} |
57 | |
|
58 | |
public Column setTablespace(String tablespace) { |
59 | 0 | this.tablespace = tablespace; |
60 | 0 | return this; |
61 | |
} |
62 | |
|
63 | |
public String getName() { |
64 | 0 | return name; |
65 | |
} |
66 | |
|
67 | |
public Column setName(String name) { |
68 | 0 | this.name = name; |
69 | |
|
70 | 0 | return this; |
71 | |
} |
72 | |
|
73 | |
public int getDataType() { |
74 | 0 | return dataType; |
75 | |
} |
76 | |
|
77 | |
public Column setDataType(int dataType) { |
78 | 0 | this.dataType = dataType; |
79 | |
|
80 | 0 | return this; |
81 | |
} |
82 | |
|
83 | |
public int getColumnSize() { |
84 | 0 | return columnSize; |
85 | |
} |
86 | |
|
87 | |
public Column setColumnSize(int columnSize) { |
88 | 0 | this.columnSize = columnSize; |
89 | |
|
90 | 0 | return this; |
91 | |
} |
92 | |
|
93 | |
public int getDecimalDigits() { |
94 | 0 | return decimalDigits; |
95 | |
} |
96 | |
|
97 | |
public Column setDecimalDigits(int decimalDigits) { |
98 | 0 | this.decimalDigits = decimalDigits; |
99 | |
|
100 | 0 | return this; |
101 | |
} |
102 | |
|
103 | |
public Boolean isNullable() { |
104 | 0 | return nullable; |
105 | |
} |
106 | |
|
107 | |
public Column setNullable(Boolean nullable) { |
108 | 0 | this.nullable = nullable; |
109 | |
|
110 | 0 | return this; |
111 | |
} |
112 | |
|
113 | |
public String getTypeName() { |
114 | 0 | return typeName; |
115 | |
} |
116 | |
|
117 | |
public Column setTypeName(String typeName) { |
118 | 0 | this.typeName = typeName; |
119 | |
|
120 | 0 | return this; |
121 | |
} |
122 | |
|
123 | |
public Object getDefaultValue() { |
124 | 0 | return defaultValue; |
125 | |
} |
126 | |
|
127 | |
public Column setDefaultValue(Object defaultValue) { |
128 | 0 | this.defaultValue = defaultValue; |
129 | |
|
130 | 0 | return this; |
131 | |
} |
132 | |
|
133 | |
@Override |
134 | |
public String toString() { |
135 | |
String tableOrViewName; |
136 | 0 | if (table == null) { |
137 | 0 | tableOrViewName = view.getName(); |
138 | |
} else { |
139 | 0 | tableOrViewName = table.getName(); |
140 | |
} |
141 | 0 | return tableOrViewName + "." + getName(); |
142 | |
} |
143 | |
|
144 | |
@Override |
145 | |
public int compareTo(Column o) { |
146 | |
try { |
147 | |
|
148 | 0 | int returnValue = 0; |
149 | 0 | if (this.getTable() != null && o.getTable() == null) { |
150 | 0 | return 1; |
151 | 0 | } else if (this.getTable() == null && o.getTable() != null) { |
152 | 0 | return -1; |
153 | 0 | } else if (this.getTable() == null && o.getTable() == null) { |
154 | 0 | returnValue = this.getView().compareTo(o.getView()); |
155 | |
} else { |
156 | 0 | returnValue = this.getTable().compareTo(o.getTable()); |
157 | |
} |
158 | |
|
159 | 0 | if (returnValue == 0) { |
160 | 0 | returnValue = this.getName().compareTo(o.getName()); |
161 | |
} |
162 | |
|
163 | 0 | return returnValue; |
164 | 0 | } catch (Exception e) { |
165 | 0 | throw new RuntimeException(e); |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public boolean equals(Object o) { |
171 | |
try { |
172 | 0 | if (this == o) |
173 | 0 | return true; |
174 | 0 | if (o == null || getClass() != o.getClass()) |
175 | 0 | return false; |
176 | |
|
177 | 0 | Column column = (Column) o; |
178 | |
|
179 | 0 | return name.equalsIgnoreCase(column.name) |
180 | |
&& !(table != null ? !table.equals(column.table) : column.table != null) |
181 | |
&& !(view != null ? !view.equals(column.view) : column.view != null); |
182 | 0 | } catch (Exception e) { |
183 | 0 | throw new RuntimeException(e); |
184 | |
} |
185 | |
|
186 | |
} |
187 | |
|
188 | |
@Override |
189 | |
public int hashCode() { |
190 | |
try { |
191 | |
int result; |
192 | 0 | result = (table != null ? table.hashCode() : 0); |
193 | 0 | result = 31 * result + (view != null ? view.hashCode() : 0); |
194 | 0 | result = 31 * result + name.toUpperCase().hashCode(); |
195 | 0 | return result; |
196 | 0 | } catch (Exception e) { |
197 | 0 | throw new RuntimeException(e); |
198 | |
} |
199 | |
} |
200 | |
|
201 | |
public boolean isNumeric() { |
202 | 0 | return SqlUtil.isNumeric(getDataType()); |
203 | |
} |
204 | |
|
205 | |
public boolean isUnique() { |
206 | 0 | return unique; |
207 | |
} |
208 | |
|
209 | |
public Column setUnique(boolean unique) { |
210 | 0 | this.unique = unique; |
211 | |
|
212 | 0 | return this; |
213 | |
} |
214 | |
|
215 | |
public boolean isAutoIncrement() { |
216 | 0 | return autoIncrement; |
217 | |
} |
218 | |
|
219 | |
public Column setAutoIncrement(boolean autoIncrement) { |
220 | 0 | this.autoIncrement = autoIncrement; |
221 | |
|
222 | 0 | return this; |
223 | |
} |
224 | |
|
225 | |
public boolean isDataTypeDifferent(Column otherColumn) { |
226 | 0 | if (!this.isCertainDataType() || !otherColumn.isCertainDataType()) { |
227 | 0 | return false; |
228 | |
} else { |
229 | 0 | return this.getDataType() != otherColumn.getDataType() |
230 | |
|| this.getColumnSize() != otherColumn.getColumnSize() |
231 | |
|| this.getDecimalDigits() != otherColumn.getDecimalDigits() |
232 | |
|| this.getLengthSemantics() != otherColumn.getLengthSemantics(); |
233 | |
} |
234 | |
} |
235 | |
|
236 | |
@SuppressWarnings({ "SimplifiableIfStatement" }) |
237 | |
public boolean isNullabilityDifferent(Column otherColumn) { |
238 | 0 | if (this.isNullable() == null && otherColumn.isNullable() == null) { |
239 | 0 | return false; |
240 | |
} |
241 | 0 | if (this.isNullable() == null && otherColumn.isNullable() != null) { |
242 | 0 | return true; |
243 | |
} |
244 | 0 | if (this.isNullable() != null && otherColumn.isNullable() == null) { |
245 | 0 | return true; |
246 | |
} |
247 | 0 | return !this.isNullable().equals(otherColumn.isNullable()); |
248 | |
} |
249 | |
|
250 | |
public boolean isDifferent(Column otherColumn) { |
251 | 0 | return isDataTypeDifferent(otherColumn) || isNullabilityDifferent(otherColumn); |
252 | |
} |
253 | |
|
254 | |
public boolean isPrimaryKey() { |
255 | 0 | return primaryKey; |
256 | |
} |
257 | |
|
258 | |
public Column setPrimaryKey(boolean primaryKey) { |
259 | 0 | this.primaryKey = primaryKey; |
260 | |
|
261 | 0 | return this; |
262 | |
} |
263 | |
|
264 | |
public boolean isCertainDataType() { |
265 | 0 | return certainDataType; |
266 | |
} |
267 | |
|
268 | |
public Column setCertainDataType(boolean certainDataType) { |
269 | 0 | this.certainDataType = certainDataType; |
270 | |
|
271 | 0 | return this; |
272 | |
} |
273 | |
|
274 | |
public String getRemarks() { |
275 | 0 | return remarks; |
276 | |
} |
277 | |
|
278 | |
public Column setRemarks(String remarks) { |
279 | 0 | this.remarks = remarks; |
280 | |
|
281 | 0 | return this; |
282 | |
} |
283 | |
|
284 | |
public boolean isInitPrecision() { |
285 | 0 | return initPrecision; |
286 | |
} |
287 | |
|
288 | |
public void setInitPrecision(boolean initPrecision) { |
289 | 0 | this.initPrecision = initPrecision; |
290 | 0 | } |
291 | |
|
292 | |
public LengthSemantics getLengthSemantics() { |
293 | 0 | return lengthSemantics; |
294 | |
} |
295 | |
|
296 | |
public Column setLengthSemantics(LengthSemantics lengthSemantics) { |
297 | 0 | this.lengthSemantics = lengthSemantics; |
298 | |
|
299 | 0 | return this; |
300 | |
} |
301 | |
|
302 | 0 | public static enum LengthSemantics { |
303 | 0 | CHAR, BYTE |
304 | |
} |
305 | |
} |