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: public class AddUniqueConstraintStatement implements SqlStatement {
10:
11: private String schemaName;
12: private String tableName;
13: private String columnNames;
14: private String constraintName;
15: private String tablespace;
16:
17: public AddUniqueConstraintStatement(String schemaName,
18: String tableName, String columnNames, String constraintName) {
19: this .schemaName = schemaName;
20: this .tableName = tableName;
21: this .columnNames = columnNames;
22: this .constraintName = constraintName;
23: }
24:
25: public String getSchemaName() {
26: return schemaName;
27: }
28:
29: public String getTableName() {
30: return tableName;
31: }
32:
33: public String getColumnNames() {
34: return columnNames;
35: }
36:
37: public String getConstraintName() {
38: return constraintName;
39: }
40:
41: public String getTablespace() {
42: return tablespace;
43: }
44:
45: public AddUniqueConstraintStatement setTablespace(String tablespace) {
46: this .tablespace = tablespace;
47: return this ;
48: }
49:
50: public String getSqlStatement(Database database)
51: throws StatementNotSupportedOnDatabaseException {
52: String sql = "ALTER TABLE "
53: + database.escapeTableName(getSchemaName(),
54: getTableName()) + " ADD CONSTRAINT "
55: + getConstraintName() + " UNIQUE ("
56: + database.escapeColumnNameList(getColumnNames()) + ")";
57:
58: if (StringUtils.trimToNull(getTablespace()) != null
59: && database.supportsTablespaces()) {
60: if (database instanceof MSSQLDatabase) {
61: sql += " ON " + getTablespace();
62: } else if (database instanceof DB2Database) {
63: ; //not supported in DB2
64: } else {
65: sql += " USING INDEX TABLESPACE " + getTablespace();
66: }
67: }
68:
69: return sql;
70: }
71:
72: public String getEndDelimiter(Database database) {
73: return ";";
74: }
75:
76: public boolean supportsDatabase(Database database) {
77: return true;
78: }
79: }
|