01: package liquibase.database.sql;
02:
03: import liquibase.database.DB2Database;
04: import liquibase.database.Database;
05: import liquibase.database.MSSQLDatabase;
06: import liquibase.database.MySQLDatabase;
07: import liquibase.exception.StatementNotSupportedOnDatabaseException;
08: import liquibase.util.StringUtils;
09:
10: public class AddPrimaryKeyStatement implements SqlStatement {
11:
12: private String schemaName;
13: private String tableName;
14: private String tablespace;
15: private String columnNames;
16: private String constraintName;
17:
18: public AddPrimaryKeyStatement(String schemaName, String tableName,
19: String columnNames, String constraintName) {
20: this .schemaName = schemaName;
21: this .tableName = tableName;
22: this .columnNames = columnNames;
23: this .constraintName = constraintName;
24: }
25:
26: public String getSchemaName() {
27: return schemaName;
28: }
29:
30: public String getTableName() {
31: return tableName;
32: }
33:
34: public String getTablespace() {
35: return tablespace;
36: }
37:
38: public AddPrimaryKeyStatement setTablespace(String tablespace) {
39: this .tablespace = tablespace;
40: return this ;
41: }
42:
43: public String getColumnNames() {
44: return columnNames;
45: }
46:
47: public String getConstraintName() {
48: return constraintName;
49: }
50:
51: public String getSqlStatement(Database database)
52: throws StatementNotSupportedOnDatabaseException {
53: String sql;
54: if (getConstraintName() == null
55: || database instanceof MySQLDatabase) {
56: sql = "ALTER TABLE "
57: + database.escapeTableName(getSchemaName(),
58: getTableName()) + " ADD PRIMARY KEY ("
59: + database.escapeColumnNameList(getColumnNames())
60: + ")";
61: } else {
62: sql = "ALTER TABLE "
63: + database.escapeTableName(getSchemaName(),
64: getTableName()) + " ADD CONSTRAINT "
65: + getConstraintName() + " PRIMARY KEY ("
66: + database.escapeColumnNameList(getColumnNames())
67: + ")";
68: }
69:
70: if (StringUtils.trimToNull(getTablespace()) != null
71: && database.supportsTablespaces()) {
72: if (database instanceof MSSQLDatabase) {
73: sql += " ON " + getTablespace();
74: } else if (database instanceof DB2Database) {
75: ; //not supported in DB2
76: } else {
77: sql += " USING INDEX TABLESPACE " + getTablespace();
78: }
79: }
80:
81: return sql;
82: }
83:
84: public String getEndDelimiter(Database database) {
85: return ";";
86: }
87:
88: public boolean supportsDatabase(Database database) {
89: return true;
90: }
91: }
|