01: package liquibase.database.sql;
02:
03: import liquibase.database.DB2Database;
04: import liquibase.database.Database;
05: import liquibase.database.MSSQLDatabase;
06: import liquibase.exception.StatementNotSupportedOnDatabaseException;
07: import liquibase.util.StringUtils;
08:
09: import java.util.Arrays;
10: import java.util.Iterator;
11:
12: public class CreateIndexStatement implements SqlStatement {
13:
14: private String tableSchemaName;
15: private String indexName;
16: private String tableName;
17: private String[] columns;
18: private String tablespace;
19:
20: public CreateIndexStatement(String indexName,
21: String tableSchemaName, String tableName, String... columns) {
22: this .indexName = indexName;
23: this .tableSchemaName = tableSchemaName;
24: this .tableName = tableName;
25: this .columns = columns;
26: }
27:
28: public String getTableSchemaName() {
29: return tableSchemaName;
30: }
31:
32: public String getIndexName() {
33: return indexName;
34: }
35:
36: public String getTableName() {
37: return tableName;
38: }
39:
40: public String[] getColumns() {
41: return columns;
42: }
43:
44: public String getTablespace() {
45: return tablespace;
46: }
47:
48: public CreateIndexStatement setTablespace(String tablespace) {
49: this .tablespace = tablespace;
50:
51: return this ;
52: }
53:
54: public String getSqlStatement(Database database)
55: throws StatementNotSupportedOnDatabaseException {
56: StringBuffer buffer = new StringBuffer();
57: buffer.append("CREATE INDEX ");
58: buffer.append(getIndexName()).append(" ON ");
59: buffer.append(
60: database.escapeTableName(getTableSchemaName(),
61: getTableName())).append("(");
62: Iterator<String> iterator = Arrays.asList(getColumns())
63: .iterator();
64: while (iterator.hasNext()) {
65: String column = iterator.next();
66: buffer.append(database.escapeColumnName(column));
67: if (iterator.hasNext()) {
68: buffer.append(", ");
69: }
70: }
71: buffer.append(")");
72:
73: if (StringUtils.trimToNull(getTablespace()) != null
74: && database.supportsTablespaces()) {
75: if (database instanceof MSSQLDatabase) {
76: buffer.append(" ON ").append(getTablespace());
77: } else if (database instanceof DB2Database) {
78: // cannot add indexes to tablespace in DB2
79: } else {
80: buffer.append(" TABLESPACE ").append(getTablespace());
81: }
82: }
83:
84: return buffer.toString();
85: }
86:
87: public String getEndDelimiter(Database database) {
88: return ";";
89: }
90:
91: public boolean supportsDatabase(Database database) {
92: return true;
93: }
94: }
|