001: package liquibase.database.sql;
002:
003: import liquibase.database.*;
004:
005: public class AddDefaultValueStatement implements SqlStatement {
006: private String schemaName;
007: private String tableName;
008: private String columnName;
009: private Object defaultValue;
010:
011: public AddDefaultValueStatement(String schemaName,
012: String tableName, String columnName, Object defaultValue) {
013: this .schemaName = schemaName;
014: this .tableName = tableName;
015: this .columnName = columnName;
016: this .defaultValue = defaultValue;
017: }
018:
019: public boolean supportsDatabase(Database database) {
020: return true;
021: }
022:
023: public String getSqlStatement(Database database) {
024: if (database instanceof SybaseDatabase) {
025: return "ALTER TABLE "
026: + database.escapeTableName(getSchemaName(),
027: getTableName())
028: + " REPLACE "
029: + database.escapeColumnName(getColumnName())
030: + " DEFAULT "
031: + database
032: .convertJavaObjectToString(getDefaultValue());
033: } else if (database instanceof MSSQLDatabase) {
034: return "ALTER TABLE "
035: + database.escapeTableName(getSchemaName(),
036: getTableName())
037: + " ADD CONSTRAINT "
038: + ((MSSQLDatabase) database)
039: .generateDefaultConstraintName(
040: getTableName(), getColumnName())
041: + " DEFAULT "
042: + database
043: .convertJavaObjectToString(getDefaultValue())
044: + " FOR " + getColumnName();
045: } else if (database instanceof MySQLDatabase) {
046: return "ALTER TABLE "
047: + database.escapeTableName(getSchemaName(),
048: getTableName())
049: + " ALTER "
050: + database.escapeColumnName(getColumnName())
051: + " SET DEFAULT "
052: + database
053: .convertJavaObjectToString(getDefaultValue());
054: } else if (database instanceof OracleDatabase) {
055: return "ALTER TABLE "
056: + database.escapeTableName(getSchemaName(),
057: getTableName())
058: + " MODIFY "
059: + database.escapeColumnName(getColumnName())
060: + " DEFAULT "
061: + database
062: .convertJavaObjectToString(getDefaultValue());
063: } else if (database instanceof DerbyDatabase) {
064: return "ALTER TABLE "
065: + database.escapeTableName(getSchemaName(),
066: getTableName())
067: + " ALTER COLUMN "
068: + database.escapeColumnName(getColumnName())
069: + " WITH DEFAULT "
070: + database
071: .convertJavaObjectToString(getDefaultValue());
072: } else if (database instanceof MaxDBDatabase) {
073: return "ALTER TABLE "
074: + database.escapeTableName(getSchemaName(),
075: getTableName())
076: + " COLUMN "
077: + database.escapeColumnName(getColumnName())
078: + " ADD DEFAULT "
079: + database
080: .convertJavaObjectToString(getDefaultValue());
081: }
082:
083: return "ALTER TABLE "
084: + database.escapeTableName(getSchemaName(),
085: getTableName()) + " ALTER COLUMN "
086: + database.escapeColumnName(getColumnName())
087: + " SET DEFAULT "
088: + database.convertJavaObjectToString(getDefaultValue());
089: }
090:
091: public String getColumnName() {
092: return columnName;
093: }
094:
095: public String getSchemaName() {
096: return schemaName;
097: }
098:
099: public String getTableName() {
100: return tableName;
101: }
102:
103: public String getEndDelimiter(Database database) {
104: return ";";
105: }
106:
107: public Object getDefaultValue() {
108: return defaultValue;
109: }
110: }
|