01: package liquibase.database.sql;
02:
03: import liquibase.database.*;
04: import liquibase.exception.StatementNotSupportedOnDatabaseException;
05:
06: public class AddAutoIncrementStatement implements SqlStatement {
07:
08: private String schemaName;
09: private String tableName;
10: private String columnName;
11: private String columnDataType;
12:
13: public AddAutoIncrementStatement(String schemaName,
14: String tableName, String columnName, String columnDataType) {
15: this .schemaName = schemaName;
16: this .tableName = tableName;
17: this .columnName = columnName;
18: this .columnDataType = columnDataType;
19: }
20:
21: public String getSchemaName() {
22: return schemaName;
23: }
24:
25: public String getTableName() {
26: return tableName;
27: }
28:
29: public String getColumnName() {
30: return columnName;
31: }
32:
33: public String getColumnDataType() {
34: return columnDataType;
35: }
36:
37: public boolean supportsDatabase(Database database) {
38: return !(database instanceof OracleDatabase
39: || database instanceof MSSQLDatabase
40: || database instanceof DerbyDatabase
41: || database instanceof CacheDatabase
42: || database instanceof FirebirdDatabase
43: || database instanceof H2Database || database instanceof PostgresDatabase);
44: }
45:
46: public String getSqlStatement(Database database)
47: throws StatementNotSupportedOnDatabaseException {
48: if (!supportsDatabase(database)) {
49: throw new StatementNotSupportedOnDatabaseException(this ,
50: database);
51: }
52:
53: if (database instanceof HsqlDatabase) {
54: return "ALTER TABLE "
55: + database.escapeTableName(getSchemaName(),
56: getTableName()) + " ALTER COLUMN "
57: + database.escapeColumnName(getColumnName()) + " "
58: + getColumnDataType()
59: + " GENERATED BY DEFAULT AS IDENTITY IDENTITY";
60: } else if (database instanceof DB2Database) {
61: return "ALTER TABLE "
62: + database.escapeTableName(getSchemaName(),
63: getTableName()) + " ALTER COLUMN "
64: + database.escapeColumnName(getColumnName())
65: + " SET GENERATED ALWAYS AS IDENTITY";
66: }
67:
68: return "ALTER TABLE "
69: + database.escapeTableName(getSchemaName(),
70: getTableName()) + " MODIFY "
71: + database.escapeColumnName(getColumnName()) + " "
72: + getColumnDataType() + " AUTO_INCREMENT";
73: }
74:
75: public String getEndDelimiter(Database database) {
76: return ";";
77: }
78: }
|