1 package liquibase.change;
2
3 import liquibase.database.structure.Column;
4 import liquibase.statement.DatabaseFunction;
5 import liquibase.util.ISODateFormat;
6
7 import java.text.NumberFormat;
8 import java.text.ParseException;
9 import java.util.Date;
10 import java.util.Locale;
11
12
13
14
15
16 public class ColumnConfig {
17 private String name;
18 private String type;
19 private String value;
20 private Number valueNumeric;
21 private Date valueDate;
22 private Boolean valueBoolean;
23 private DatabaseFunction valueComputed;
24
25 private String defaultValue;
26 private Number defaultValueNumeric;
27 private Date defaultValueDate;
28 private Boolean defaultValueBoolean;
29 private DatabaseFunction defaultValueComputed;
30
31 private ConstraintsConfig constraints;
32 private Boolean autoIncrement;
33 private String remarks;
34
35 public ColumnConfig(Column columnStructure) {
36 setName(columnStructure.getName());
37 setType(columnStructure.getTypeName());
38 if (columnStructure.getDefaultValue() != null) {
39 setDefaultValue(columnStructure.getDefaultValue().toString());
40 }
41 setAutoIncrement(columnStructure.isAutoIncrement());
42 ConstraintsConfig constraints = new ConstraintsConfig();
43 constraints.setNullable(columnStructure.isNullable());
44 constraints.setPrimaryKey(columnStructure.isPrimaryKey());
45 constraints.setUnique(columnStructure.isUnique());
46 setConstraints(constraints);
47 }
48
49 public ColumnConfig(ColumnConfig column) {
50 setName(column.getName());
51 setType(column.getType());
52 setDefaultValue(column.getDefaultValue());
53 setAutoIncrement(column.isAutoIncrement());
54 if (column.getConstraints() != null) {
55 ConstraintsConfig constraints = new ConstraintsConfig();
56 constraints.setNullable(column.getConstraints().isNullable());
57 constraints.setPrimaryKey(column.getConstraints().isPrimaryKey());
58 constraints.setPrimaryKeyTablespace(column.getConstraints().getPrimaryKeyTablespace());
59 constraints.setUnique(column.getConstraints().isUnique());
60 }
61 setConstraints(constraints);
62 }
63
64 public ColumnConfig() {
65
66 }
67
68 public String getName() {
69 return name;
70 }
71
72 public ColumnConfig setName(String name) {
73 this.name = name;
74 return this;
75 }
76
77 public String getType() {
78 return type;
79 }
80
81 public ColumnConfig setType(String type) {
82 this.type = type;
83 return this;
84 }
85
86 public String getValue() {
87 return value;
88 }
89
90 public void setValue(String value) {
91
92
93
94
95
96
97
98
99
100 this.value = value;
101 }
102
103 public Number getValueNumeric() {
104 return valueNumeric;
105 }
106
107 public ColumnConfig setValueNumeric(String valueNumeric) {
108 if (valueNumeric == null || valueNumeric.equalsIgnoreCase("null")) {
109 this.valueNumeric = null;
110 } else {
111 valueNumeric = valueNumeric.replaceFirst("^\\(", "");
112 valueNumeric = valueNumeric.replaceFirst("\\)$", "");
113
114 if (valueNumeric.matches("\\d+\\.?\\d*")) {
115 try {
116 this.valueNumeric = NumberFormat.getInstance(Locale.US).parse(valueNumeric);
117 } catch (ParseException e) {
118 throw new RuntimeException(e);
119 }
120 } else {
121 this.valueComputed = new DatabaseFunction(valueNumeric);
122 }
123 }
124
125 return this;
126 }
127
128 public ColumnConfig setValueNumeric(Number valueNumeric) {
129 this.valueNumeric = valueNumeric;
130
131 return this;
132 }
133
134 public Boolean getValueBoolean() {
135 return valueBoolean;
136 }
137
138 public ColumnConfig setValueBoolean(Boolean valueBoolean) {
139 this.valueBoolean = valueBoolean;
140
141 return this;
142 }
143
144 public DatabaseFunction getValueComputed() {
145 return valueComputed;
146 }
147
148 public ColumnConfig setValueComputed(DatabaseFunction valueComputed) {
149 this.valueComputed = valueComputed;
150
151 return this;
152 }
153
154 public Date getValueDate() {
155 return valueDate;
156 }
157
158 public ColumnConfig setValueDate(Date valueDate) {
159 this.valueDate = valueDate;
160
161 return this;
162 }
163
164 public ColumnConfig setValueDate(String valueDate) {
165 if (valueDate == null || valueDate.equalsIgnoreCase("null")) {
166 this.valueDate = null;
167 } else {
168 try {
169 this.valueDate = new ISODateFormat().parse(valueDate);
170 } catch (ParseException e) {
171
172 this.valueComputed = new DatabaseFunction(valueDate);
173 }
174 }
175
176 return this;
177 }
178
179 public Object getValueObject() {
180 if (getValue() != null) {
181 return getValue();
182 } else if (getValueBoolean() != null) {
183 return getValueBoolean();
184 } else if (getValueNumeric() != null) {
185 return getValueNumeric();
186 } else if (getValueDate() != null) {
187 return getValueDate();
188 } else if (getValueComputed() != null) {
189 return getValueComputed();
190 }
191 return null;
192 }
193
194 public String getDefaultValue() {
195 return defaultValue;
196 }
197
198 public ColumnConfig setDefaultValue(String defaultValue) {
199 this.defaultValue = defaultValue;
200
201 return this;
202 }
203
204 public Number getDefaultValueNumeric() {
205 return defaultValueNumeric;
206 }
207
208 public ColumnConfig setDefaultValueNumeric(Number defaultValueNumeric) {
209 this.defaultValueNumeric = defaultValueNumeric;
210
211 return this;
212 }
213
214 public ColumnConfig setDefaultValueNumeric(String defaultValueNumeric) throws ParseException {
215 if (defaultValueNumeric == null || defaultValueNumeric.equalsIgnoreCase("null")) {
216 this.defaultValueNumeric = null;
217 } else {
218 if ("GENERATED_BY_DEFAULT".equals(defaultValueNumeric)) {
219 setAutoIncrement(true);
220 } else {
221 defaultValueNumeric = defaultValueNumeric.replaceFirst("^\\(", "");
222 defaultValueNumeric = defaultValueNumeric.replaceFirst("\\)$", "");
223 try {
224 this.defaultValueNumeric = NumberFormat.getInstance(Locale.US).parse(defaultValueNumeric);
225 } catch (ParseException e) {
226 this.defaultValueComputed = new DatabaseFunction(defaultValueNumeric);
227 }
228 }
229 }
230
231 return this;
232 }
233
234 public Date getDefaultValueDate() {
235 return defaultValueDate;
236 }
237
238 public ColumnConfig setDefaultValueDate(String defaultValueDate) {
239 if (defaultValueDate == null || defaultValueDate.equalsIgnoreCase("null")) {
240 this.defaultValueDate = null;
241 } else {
242 try {
243 this.defaultValueDate = new ISODateFormat().parse(defaultValueDate);
244 } catch (ParseException e) {
245
246 this.defaultValueComputed = new DatabaseFunction(defaultValueDate);
247 }
248 }
249
250 return this;
251 }
252
253 public ColumnConfig setDefaultValueDate(Date defaultValueDate) {
254 this.defaultValueDate = defaultValueDate;
255
256 return this;
257 }
258
259 public Boolean getDefaultValueBoolean() {
260 return defaultValueBoolean;
261 }
262
263 public ColumnConfig setDefaultValueBoolean(Boolean defaultValueBoolean) {
264 this.defaultValueBoolean = defaultValueBoolean;
265
266 return this;
267 }
268
269 public DatabaseFunction getDefaultValueComputed() {
270 return defaultValueComputed;
271 }
272
273 public ColumnConfig setDefaultValueComputed(DatabaseFunction defaultValueComputed) {
274 this.defaultValueComputed = defaultValueComputed;
275
276 return this;
277 }
278
279 public Object getDefaultValueObject() {
280 if (getDefaultValue() != null) {
281 return getDefaultValue();
282 } else if (getDefaultValueBoolean() != null) {
283 return getDefaultValueBoolean();
284 } else if (getDefaultValueNumeric() != null) {
285 return getDefaultValueNumeric();
286 } else if (getDefaultValueDate() != null) {
287 return getDefaultValueDate();
288 } else if (getDefaultValueComputed() != null) {
289 return getDefaultValueComputed();
290 }
291 return null;
292 }
293
294 public ConstraintsConfig getConstraints() {
295 return constraints;
296 }
297
298 public ColumnConfig setConstraints(ConstraintsConfig constraints) {
299 this.constraints = constraints;
300
301 return this;
302 }
303
304 public Boolean isAutoIncrement() {
305 return autoIncrement;
306 }
307
308 public ColumnConfig setAutoIncrement(Boolean autoIncrement) {
309 this.autoIncrement = autoIncrement;
310
311 return this;
312 }
313
314 public boolean isPrimaryKey() {
315 return getConstraints() != null && getConstraints().isPrimaryKey() != null && getConstraints().isPrimaryKey();
316 }
317
318 public boolean isNullable() {
319 return getConstraints() == null || getConstraints().isNullable();
320 }
321
322 public boolean hasDefaultValue() {
323 return this.getDefaultValue() != null || this.getDefaultValueBoolean() != null
324 || this.getDefaultValueDate() != null || this.getDefaultValueNumeric() != null
325 || this.getDefaultValueComputed() != null;
326 }
327
328 public String getRemarks() {
329 return remarks;
330 }
331
332 public ColumnConfig setRemarks(String remarks) {
333 this.remarks = remarks;
334 return this;
335 }
336 }