1 | |
package liquibase.change.core; |
2 | |
|
3 | |
import liquibase.change.AbstractChange; |
4 | |
import liquibase.change.Change; |
5 | |
import liquibase.change.ChangeMetaData; |
6 | |
import liquibase.database.Database; |
7 | |
import liquibase.statement.SqlStatement; |
8 | |
import liquibase.statement.DatabaseFunction; |
9 | |
import liquibase.statement.core.AddDefaultValueStatement; |
10 | |
import liquibase.util.ISODateFormat; |
11 | |
import liquibase.util.StringUtils; |
12 | |
|
13 | |
import java.text.NumberFormat; |
14 | |
import java.text.ParseException; |
15 | |
import java.util.Locale; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class AddDefaultValueChange extends AbstractChange { |
21 | |
|
22 | |
private String schemaName; |
23 | |
private String tableName; |
24 | |
private String columnName; |
25 | |
private String columnDataType; |
26 | |
private String defaultValue; |
27 | |
private String defaultValueNumeric; |
28 | |
private String defaultValueDate; |
29 | |
private Boolean defaultValueBoolean; |
30 | |
private DatabaseFunction defaultValueComputed; |
31 | |
|
32 | |
public AddDefaultValueChange() { |
33 | 25 | super("addDefaultValue", "Add Default Value", ChangeMetaData.PRIORITY_DEFAULT); |
34 | 25 | } |
35 | |
|
36 | |
public String getSchemaName() { |
37 | 52 | return schemaName; |
38 | |
} |
39 | |
|
40 | |
public void setSchemaName(String schemaName) { |
41 | 7 | this.schemaName = StringUtils.trimToNull(schemaName); |
42 | 7 | } |
43 | |
|
44 | |
public String getTableName() { |
45 | 53 | return tableName; |
46 | |
} |
47 | |
|
48 | |
public void setTableName(String tableName) { |
49 | 11 | this.tableName = tableName; |
50 | 11 | } |
51 | |
|
52 | |
public String getColumnName() { |
53 | 53 | return columnName; |
54 | |
} |
55 | |
|
56 | |
public void setColumnName(String columnName) { |
57 | 11 | this.columnName = columnName; |
58 | 11 | } |
59 | |
|
60 | |
public String getColumnDataType() { |
61 | 52 | return columnDataType; |
62 | |
} |
63 | |
|
64 | |
public void setColumnDataType(String columnDataType) { |
65 | 1 | this.columnDataType = columnDataType; |
66 | 1 | } |
67 | |
|
68 | |
public String getDefaultValue() { |
69 | 53 | return defaultValue; |
70 | |
} |
71 | |
|
72 | |
public void setDefaultValue(String defaultValue) { |
73 | 4 | this.defaultValue = defaultValue; |
74 | 4 | } |
75 | |
|
76 | |
public String getDefaultValueNumeric() { |
77 | 53 | return defaultValueNumeric; |
78 | |
} |
79 | |
|
80 | |
public void setDefaultValueNumeric(String defaultValueNumeric) { |
81 | 6 | this.defaultValueNumeric = defaultValueNumeric; |
82 | 6 | } |
83 | |
|
84 | |
public String getDefaultValueDate() { |
85 | 48 | return defaultValueDate; |
86 | |
} |
87 | |
|
88 | |
public void setDefaultValueDate(String defaultValueDate) { |
89 | 4 | this.defaultValueDate = defaultValueDate; |
90 | 4 | } |
91 | |
|
92 | |
public Boolean getDefaultValueBoolean() { |
93 | 53 | return defaultValueBoolean; |
94 | |
} |
95 | |
|
96 | |
public void setDefaultValueBoolean(Boolean defaultValueBoolean) { |
97 | 5 | this.defaultValueBoolean = defaultValueBoolean; |
98 | 5 | } |
99 | |
|
100 | |
public DatabaseFunction getDefaultValueComputed() { |
101 | 45 | return defaultValueComputed; |
102 | |
} |
103 | |
|
104 | |
public void setDefaultValueComputed(DatabaseFunction defaultValueComputed) { |
105 | 0 | this.defaultValueComputed = defaultValueComputed; |
106 | 0 | } |
107 | |
|
108 | |
public SqlStatement[] generateStatements(Database database) { |
109 | 52 | Object defaultValue = null; |
110 | |
|
111 | 52 | if (getDefaultValue() != null) { |
112 | 1 | defaultValue = getDefaultValue(); |
113 | 51 | } else if (getDefaultValueBoolean() != null) { |
114 | 2 | defaultValue = Boolean.valueOf(getDefaultValueBoolean()); |
115 | 49 | } else if (getDefaultValueNumeric() != null) { |
116 | |
try { |
117 | 3 | defaultValue = NumberFormat.getInstance(Locale.US).parse(getDefaultValueNumeric()); |
118 | 1 | } catch (ParseException e) { |
119 | 1 | defaultValue = new DatabaseFunction(getDefaultValueNumeric()); |
120 | 3 | } |
121 | 46 | } else if (getDefaultValueDate() != null) { |
122 | |
try { |
123 | 1 | defaultValue = new ISODateFormat().parse(getDefaultValueDate()); |
124 | 1 | } catch (ParseException e) { |
125 | 1 | defaultValue = new DatabaseFunction(getDefaultValueDate()); |
126 | 1 | } |
127 | 45 | } else if (getDefaultValueComputed() != null) { |
128 | 0 | defaultValue = getDefaultValueComputed(); |
129 | |
} |
130 | |
|
131 | 52 | return new SqlStatement[] { new AddDefaultValueStatement(getSchemaName(), getTableName(), getColumnName(), |
132 | |
getColumnDataType(), defaultValue) }; |
133 | |
} |
134 | |
|
135 | |
@Override |
136 | |
protected Change[] createInverses() { |
137 | 0 | DropDefaultValueChange inverse = new DropDefaultValueChange(); |
138 | 0 | inverse.setSchemaName(getSchemaName()); |
139 | 0 | inverse.setTableName(getTableName()); |
140 | 0 | inverse.setColumnName(getColumnName()); |
141 | 0 | inverse.setColumnDataType(getColumnDataType()); |
142 | |
|
143 | 0 | return new Change[] { inverse }; |
144 | |
} |
145 | |
|
146 | |
public String getConfirmationMessage() { |
147 | 1 | return "Default value added to " + getTableName() + "." + getColumnName(); |
148 | |
} |
149 | |
} |