001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.SetNullableStatement;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.database.structure.Column;
007: import liquibase.database.structure.DatabaseObject;
008: import liquibase.database.structure.Table;
009: import liquibase.exception.UnsupportedChangeException;
010: import org.w3c.dom.Document;
011: import org.w3c.dom.Element;
012:
013: import java.util.Arrays;
014: import java.util.HashSet;
015: import java.util.Set;
016:
017: /**
018: * Drops a not-null constraint from an existing column.
019: */
020: public class DropNotNullConstraintChange extends AbstractChange {
021:
022: private String schemaName;
023: private String tableName;
024: private String columnName;
025: private String columnDataType;
026:
027: public DropNotNullConstraintChange() {
028: super ("dropNotNullConstraint", "Drop Not-Null Constraint");
029: }
030:
031: public String getSchemaName() {
032: return schemaName;
033: }
034:
035: public void setSchemaName(String schemaName) {
036: this .schemaName = schemaName;
037: }
038:
039: public String getTableName() {
040: return tableName;
041: }
042:
043: public void setTableName(String tableName) {
044: this .tableName = tableName;
045: }
046:
047: public String getColumnName() {
048: return columnName;
049: }
050:
051: public void setColumnName(String columnName) {
052: this .columnName = columnName;
053: }
054:
055: public String getColumnDataType() {
056: return columnDataType;
057: }
058:
059: public void setColumnDataType(String columnDataType) {
060: this .columnDataType = columnDataType;
061: }
062:
063: public SqlStatement[] generateStatements(Database database)
064: throws UnsupportedChangeException {
065: return new SqlStatement[] { new SetNullableStatement(
066: getSchemaName() == null ? database
067: .getDefaultSchemaName() : getSchemaName(),
068: getTableName(), getColumnName(), getColumnDataType(),
069: true) };
070: }
071:
072: protected Change[] createInverses() {
073: AddNotNullConstraintChange inverse = new AddNotNullConstraintChange();
074: inverse.setColumnName(getColumnName());
075: inverse.setSchemaName(getSchemaName());
076: inverse.setTableName(getTableName());
077: inverse.setColumnDataType(getColumnDataType());
078:
079: return new Change[] { inverse };
080: }
081:
082: public String getConfirmationMessage() {
083: return "Null constraint dropped from " + getTableName() + "."
084: + getColumnName();
085: }
086:
087: public Element createNode(Document currentChangeLogFileDOM) {
088: Element element = currentChangeLogFileDOM
089: .createElement("dropNotNullConstraint");
090: if (getSchemaName() != null) {
091: element.setAttribute("schemaName", getSchemaName());
092: }
093:
094: element.setAttribute("tableName", getTableName());
095: element.setAttribute("columnName", getColumnName());
096: return element;
097: }
098:
099: public Set<DatabaseObject> getAffectedDatabaseObjects() {
100:
101: Table table = new Table(getTableName());
102:
103: Column column = new Column();
104: column.setTable(table);
105: column.setName(columnName);
106:
107: return new HashSet<DatabaseObject>(Arrays.asList(table, column));
108: }
109: }
|