001: package liquibase.database.sql;
002:
003: import liquibase.database.*;
004: import liquibase.exception.StatementNotSupportedOnDatabaseException;
005:
006: public class SetNullableStatement implements SqlStatement {
007: private String schemaName;
008: private String tableName;
009: private String columnName;
010: private String columnDataType;
011: private boolean nullable;
012:
013: public SetNullableStatement(String schemaName, String tableName,
014: String columnName, String columnDataType, boolean nullable) {
015: this .schemaName = schemaName;
016: this .tableName = tableName;
017: this .columnName = columnName;
018: this .columnDataType = columnDataType;
019: this .nullable = nullable;
020: }
021:
022: public String getSchemaName() {
023: return schemaName;
024: }
025:
026: public String getTableName() {
027: return tableName;
028: }
029:
030: public String getColumnName() {
031: return columnName;
032: }
033:
034: public String getColumnDataType() {
035: return columnDataType;
036: }
037:
038: public boolean isNullable() {
039: return nullable;
040: }
041:
042: public String getSqlStatement(Database database)
043: throws StatementNotSupportedOnDatabaseException {
044: String nullableString;
045: if (isNullable()) {
046: nullableString = " NULL";
047: } else {
048: nullableString = " NOT NULL";
049: }
050:
051: if (database instanceof OracleDatabase
052: || database instanceof SybaseDatabase) {
053: return "ALTER TABLE "
054: + database.escapeTableName(getSchemaName(),
055: getTableName()) + " MODIFY "
056: + database.escapeColumnName(getColumnName())
057: + nullableString;
058: } else if (database instanceof MSSQLDatabase) {
059: if (getColumnDataType() == null) {
060: throw new StatementNotSupportedOnDatabaseException(
061: "Database requires columnDataType parameter",
062: this , database);
063: }
064: return "ALTER TABLE "
065: + database.escapeTableName(getSchemaName(),
066: getTableName()) + " ALTER COLUMN "
067: + database.escapeColumnName(getColumnName()) + " "
068: + getColumnDataType() + nullableString;
069: } else if (database instanceof MySQLDatabase) {
070: if (getColumnDataType() == null) {
071: throw new StatementNotSupportedOnDatabaseException(
072: "Database requires columnDataType parameter",
073: this , database);
074: }
075: return "ALTER TABLE "
076: + database.escapeTableName(getSchemaName(),
077: getTableName()) + " MODIFY "
078: + database.escapeColumnName(getColumnName()) + " "
079: + getColumnDataType() + nullableString;
080: } else if (database instanceof DerbyDatabase
081: || database instanceof CacheDatabase) {
082: return "ALTER TABLE "
083: + database.escapeTableName(getSchemaName(),
084: getTableName()) + " ALTER COLUMN "
085: + database.escapeColumnName(getColumnName())
086: + nullableString;
087: } else if (database instanceof HsqlDatabase) {
088: // if (getColumnDataType() == null) {
089: // throw new StatementNotSupportedOnDatabaseException("Database requires columnDataType parameter", this, database);
090: // }
091: return "ALTER TABLE "
092: + database.escapeTableName(getSchemaName(),
093: getTableName()) + " ALTER COLUMN "
094: + database.escapeColumnName(getColumnName()) + " "
095: + getColumnDataType() + nullableString;
096: } else if (database instanceof FirebirdDatabase) {
097: throw new StatementNotSupportedOnDatabaseException(this ,
098: database);
099: } else if (database instanceof MaxDBDatabase) {
100: return "ALTER TABLE "
101: + database.escapeTableName(getSchemaName(),
102: getTableName()) + " COLUMN "
103: + database.escapeColumnName(getColumnName())
104: + (isNullable() ? " DEFAULT NULL" : " NOT NULL");
105: }
106: return "ALTER TABLE "
107: + database.escapeTableName(getSchemaName(),
108: getTableName()) + " ALTER COLUMN "
109: + database.escapeColumnName(getColumnName())
110: + (isNullable() ? " DROP NOT NULL" : " SET NOT NULL");
111: }
112:
113: public String getEndDelimiter(Database database) {
114: return ";";
115: }
116:
117: public boolean supportsDatabase(Database database) {
118: return !(database instanceof FirebirdDatabase);
119: }
120: }
|