1 | |
package liquibase.change.core; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import liquibase.change.AbstractChange; |
7 | |
import liquibase.change.Change; |
8 | |
import liquibase.change.ChangeMetaData; |
9 | |
import liquibase.change.ChangeWithColumns; |
10 | |
import liquibase.change.ColumnConfig; |
11 | |
import liquibase.change.ConstraintsConfig; |
12 | |
import liquibase.database.Database; |
13 | |
import liquibase.database.typeconversion.TypeConverterFactory; |
14 | |
import liquibase.exception.UnexpectedLiquibaseException; |
15 | |
import liquibase.sqlgenerator.SqlGeneratorFactory; |
16 | |
import liquibase.statement.AutoIncrementConstraint; |
17 | |
import liquibase.statement.ForeignKeyConstraint; |
18 | |
import liquibase.statement.NotNullConstraint; |
19 | |
import liquibase.statement.SqlStatement; |
20 | |
import liquibase.statement.UniqueConstraint; |
21 | |
import liquibase.statement.core.CreateTableStatement; |
22 | |
import liquibase.statement.core.SetColumnRemarksStatement; |
23 | |
import liquibase.statement.core.SetTableRemarksStatement; |
24 | |
import liquibase.util.StringUtils; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class CreateTableChange extends AbstractChange implements ChangeWithColumns { |
30 | |
|
31 | |
private List<ColumnConfig> columns; |
32 | |
private String schemaName; |
33 | |
private String tableName; |
34 | |
private String tablespace; |
35 | |
private String remarks; |
36 | |
|
37 | |
public CreateTableChange() { |
38 | 66 | super("createTable", "Create Table", ChangeMetaData.PRIORITY_DEFAULT); |
39 | 66 | columns = new ArrayList<ColumnConfig>(); |
40 | 66 | } |
41 | |
|
42 | |
@Override |
43 | |
public SqlStatement[] generateStatements(Database database) { |
44 | |
|
45 | 62 | String schemaName = getSchemaName() == null ? (database == null ? null : database.getDefaultSchemaName()) |
46 | |
: getSchemaName(); |
47 | 62 | CreateTableStatement statement = new CreateTableStatement(schemaName, getTableName()); |
48 | 62 | for (ColumnConfig column : getColumns()) { |
49 | 17 | ConstraintsConfig constraints = column.getConstraints(); |
50 | 17 | boolean isAutoIncrement = column.isAutoIncrement() != null && column.isAutoIncrement(); |
51 | |
|
52 | 17 | Object defaultValue = column.getDefaultValueObject(); |
53 | |
|
54 | 17 | if (constraints != null && constraints.isPrimaryKey() != null && constraints.isPrimaryKey()) { |
55 | |
|
56 | 1 | statement.addPrimaryKeyColumn( |
57 | |
column.getName(), |
58 | |
TypeConverterFactory.getInstance().findTypeConverter(database) |
59 | |
.getDataType(column.getType(), isAutoIncrement), defaultValue, |
60 | |
constraints.getPrimaryKeyName(), constraints.getPrimaryKeyTablespace()); |
61 | |
|
62 | |
} else { |
63 | 16 | statement.addColumn(column.getName(), TypeConverterFactory.getInstance().findTypeConverter(database) |
64 | |
.getDataType(column.getType(), column.isAutoIncrement()), defaultValue); |
65 | |
} |
66 | |
|
67 | 17 | if (constraints != null) { |
68 | 6 | if (constraints.isNullable() != null && !constraints.isNullable()) { |
69 | 2 | statement.addColumnConstraint(new NotNullConstraint(column.getName())); |
70 | |
} |
71 | |
|
72 | 6 | if (constraints.getReferences() != null) { |
73 | 4 | if (StringUtils.trimToNull(constraints.getForeignKeyName()) == null) { |
74 | 0 | throw new UnexpectedLiquibaseException("createTable with references requires foreignKeyName"); |
75 | |
} |
76 | 4 | ForeignKeyConstraint fkConstraint = new ForeignKeyConstraint(constraints.getForeignKeyName(), |
77 | |
constraints.getReferences()); |
78 | 4 | fkConstraint.setColumn(column.getName()); |
79 | 4 | fkConstraint.setDeleteCascade(constraints.isDeleteCascade() != null |
80 | |
&& constraints.isDeleteCascade()); |
81 | 4 | fkConstraint.setInitiallyDeferred(constraints.isInitiallyDeferred() != null |
82 | |
&& constraints.isInitiallyDeferred()); |
83 | 4 | fkConstraint.setDeferrable(constraints.isDeferrable() != null && constraints.isDeferrable()); |
84 | 4 | statement.addColumnConstraint(fkConstraint); |
85 | |
} |
86 | |
|
87 | 6 | if (constraints.isUnique() != null && constraints.isUnique()) { |
88 | 1 | statement.addColumnConstraint(new UniqueConstraint(constraints.getUniqueConstraintName()) |
89 | |
.addColumns(column.getName())); |
90 | |
} |
91 | |
} |
92 | |
|
93 | 17 | if (isAutoIncrement) { |
94 | 0 | statement.addColumnConstraint(new AutoIncrementConstraint(column.getName())); |
95 | |
} |
96 | 17 | } |
97 | |
|
98 | 62 | statement.setTablespace(StringUtils.trimToNull(getTablespace())); |
99 | |
|
100 | 62 | List<SqlStatement> statements = new ArrayList<SqlStatement>(); |
101 | 62 | statements.add(statement); |
102 | |
|
103 | 62 | if (StringUtils.trimToNull(remarks) != null) { |
104 | 0 | SetTableRemarksStatement remarksStatement = new SetTableRemarksStatement(schemaName, tableName, remarks); |
105 | 0 | if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) { |
106 | 0 | statements.add(remarksStatement); |
107 | |
} |
108 | |
} |
109 | |
|
110 | 62 | for (ColumnConfig column : getColumns()) { |
111 | 17 | String columnRemarks = StringUtils.trimToNull(column.getRemarks()); |
112 | 17 | if (columnRemarks != null) { |
113 | 0 | SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(schemaName, tableName, |
114 | |
column.getName(), columnRemarks); |
115 | 0 | if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) { |
116 | 0 | statements.add(remarksStatement); |
117 | |
} |
118 | |
} |
119 | 17 | } |
120 | |
|
121 | 62 | return statements.toArray(new SqlStatement[statements.size()]); |
122 | |
} |
123 | |
|
124 | |
@Override |
125 | |
protected Change[] createInverses() { |
126 | 1 | DropTableChange inverse = new DropTableChange(); |
127 | 1 | inverse.setSchemaName(getSchemaName()); |
128 | 1 | inverse.setTableName(getTableName()); |
129 | |
|
130 | 1 | return new Change[] { inverse }; |
131 | |
} |
132 | |
|
133 | |
@Override |
134 | |
public List<ColumnConfig> getColumns() { |
135 | 153 | return columns; |
136 | |
} |
137 | |
|
138 | |
public String getSchemaName() { |
139 | 63 | return schemaName; |
140 | |
} |
141 | |
|
142 | |
public void setSchemaName(String schemaName) { |
143 | 0 | this.schemaName = StringUtils.trimToNull(schemaName); |
144 | 0 | } |
145 | |
|
146 | |
public String getTableName() { |
147 | 73 | return tableName; |
148 | |
} |
149 | |
|
150 | |
public void setTableName(String tableName) { |
151 | 22 | this.tableName = tableName; |
152 | 22 | } |
153 | |
|
154 | |
public String getTablespace() { |
155 | 62 | return tablespace; |
156 | |
} |
157 | |
|
158 | |
public void setTablespace(String tablespace) { |
159 | 1 | this.tablespace = tablespace; |
160 | 1 | } |
161 | |
|
162 | |
@Override |
163 | |
public void addColumn(ColumnConfig column) { |
164 | 65 | columns.add(column); |
165 | 65 | } |
166 | |
|
167 | |
public String getRemarks() { |
168 | 0 | return remarks; |
169 | |
} |
170 | |
|
171 | |
public void setRemarks(String remarks) { |
172 | 0 | this.remarks = remarks; |
173 | 0 | } |
174 | |
|
175 | |
@Override |
176 | |
public String getConfirmationMessage() { |
177 | 1 | return "Table " + tableName + " created"; |
178 | |
} |
179 | |
} |