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.ChangeProperty; |
10 | |
import liquibase.change.ChangeWithColumns; |
11 | |
import liquibase.change.ColumnConfig; |
12 | |
import liquibase.database.Database; |
13 | |
import liquibase.statement.SqlStatement; |
14 | |
import liquibase.statement.core.CreateIndexStatement; |
15 | |
import liquibase.util.StringUtils; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class CreateIndexChange extends AbstractChange implements ChangeWithColumns { |
21 | |
|
22 | |
private String schemaName; |
23 | |
private String tableName; |
24 | |
private String indexName; |
25 | |
private Boolean unique; |
26 | |
private String tablespace; |
27 | |
private List<ColumnConfig> columns; |
28 | |
|
29 | |
|
30 | |
@ChangeProperty(includeInSerialization = false) |
31 | |
private String associatedWith; |
32 | |
|
33 | |
public CreateIndexChange() { |
34 | 16 | super("createIndex", "Create Index", ChangeMetaData.PRIORITY_DEFAULT); |
35 | 16 | columns = new ArrayList<ColumnConfig>(); |
36 | 16 | } |
37 | |
|
38 | |
public String getIndexName() { |
39 | 46 | return indexName; |
40 | |
} |
41 | |
|
42 | |
public void setIndexName(String indexName) { |
43 | 2 | this.indexName = indexName; |
44 | 2 | } |
45 | |
|
46 | |
public String getSchemaName() { |
47 | 45 | return schemaName; |
48 | |
} |
49 | |
|
50 | |
public void setSchemaName(String schemaName) { |
51 | 0 | this.schemaName = StringUtils.trimToNull(schemaName); |
52 | 0 | } |
53 | |
|
54 | |
public String getTableName() { |
55 | 45 | return tableName; |
56 | |
} |
57 | |
|
58 | |
public void setTableName(String tableName) { |
59 | 1 | this.tableName = tableName; |
60 | 1 | } |
61 | |
|
62 | |
@Override |
63 | |
public List<ColumnConfig> getColumns() { |
64 | 90 | return columns; |
65 | |
} |
66 | |
|
67 | |
public void setColumns(List<ColumnConfig> columns) { |
68 | 0 | this.columns = columns; |
69 | 0 | } |
70 | |
|
71 | |
@Override |
72 | |
public void addColumn(ColumnConfig column) { |
73 | 2 | columns.add(column); |
74 | 2 | } |
75 | |
|
76 | |
public String getTablespace() { |
77 | 45 | return tablespace; |
78 | |
} |
79 | |
|
80 | |
public void setTablespace(String tablespace) { |
81 | 0 | this.tablespace = tablespace; |
82 | 0 | } |
83 | |
|
84 | |
@Override |
85 | |
public SqlStatement[] generateStatements(Database database) { |
86 | 45 | List<String> columns = new ArrayList<String>(); |
87 | 45 | for (ColumnConfig column : getColumns()) { |
88 | 0 | columns.add(column.getName()); |
89 | |
} |
90 | |
|
91 | 45 | return new SqlStatement[] { new CreateIndexStatement(getIndexName(), |
92 | |
getSchemaName() == null ? database.getDefaultSchemaName() : getSchemaName(), getTableName(), |
93 | |
this.isUnique(), getAssociatedWith(), columns.toArray(new String[getColumns().size()])) |
94 | |
.setTablespace(getTablespace()) }; |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
protected Change[] createInverses() { |
99 | 0 | DropIndexChange inverse = new DropIndexChange(); |
100 | 0 | inverse.setSchemaName(getSchemaName()); |
101 | 0 | inverse.setTableName(getTableName()); |
102 | 0 | inverse.setIndexName(getIndexName()); |
103 | |
|
104 | 0 | return new Change[] { inverse }; |
105 | |
} |
106 | |
|
107 | |
@Override |
108 | |
public String getConfirmationMessage() { |
109 | 1 | return "Index " + getIndexName() + " created"; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public void setUnique(Boolean isUnique) { |
117 | 0 | this.unique = isUnique; |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public Boolean isUnique() { |
124 | 45 | return this.unique; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public String getAssociatedWith() { |
132 | 45 | return associatedWith; |
133 | |
} |
134 | |
|
135 | |
public void setAssociatedWith(String associatedWith) { |
136 | 0 | this.associatedWith = associatedWith; |
137 | 0 | } |
138 | |
} |