001: package liquibase.change;
002:
003: import liquibase.database.DB2Database;
004: import liquibase.database.Database;
005: import liquibase.database.sql.ReorganizeTableStatement;
006: import liquibase.database.sql.SetNullableStatement;
007: import liquibase.database.sql.SqlStatement;
008: import liquibase.database.sql.UpdateStatement;
009: import liquibase.database.structure.Column;
010: import liquibase.database.structure.DatabaseObject;
011: import liquibase.database.structure.Table;
012: import liquibase.exception.UnsupportedChangeException;
013: import org.w3c.dom.Document;
014: import org.w3c.dom.Element;
015:
016: import java.util.*;
017:
018: /**
019: * Adds a not-null constraint to an existing column.
020: */
021: public class AddNotNullConstraintChange extends AbstractChange {
022: private String schemaName;
023: private String tableName;
024: private String columnName;
025: private String defaultNullValue;
026: private String columnDataType;
027:
028: public AddNotNullConstraintChange() {
029: super ("addNotNullConstraint", "Add Not-Null Constraint");
030: }
031:
032: public String getSchemaName() {
033: return schemaName;
034: }
035:
036: public void setSchemaName(String schemaName) {
037: this .schemaName = schemaName;
038: }
039:
040: public String getTableName() {
041: return tableName;
042: }
043:
044: public void setTableName(String tableName) {
045: this .tableName = tableName;
046: }
047:
048: public String getColumnName() {
049: return columnName;
050: }
051:
052: public void setColumnName(String columnName) {
053: this .columnName = columnName;
054: }
055:
056: public String getDefaultNullValue() {
057: return defaultNullValue;
058: }
059:
060: public void setDefaultNullValue(String defaultNullValue) {
061: this .defaultNullValue = defaultNullValue;
062: }
063:
064: public String getColumnDataType() {
065: return columnDataType;
066: }
067:
068: public void setColumnDataType(String columnDataType) {
069: this .columnDataType = columnDataType;
070: }
071:
072: public SqlStatement[] generateStatements(Database database)
073: throws UnsupportedChangeException {
074: List<SqlStatement> statements = new ArrayList<SqlStatement>();
075:
076: String schemaName = getSchemaName() == null ? database
077: .getDefaultSchemaName() : getSchemaName();
078: if (defaultNullValue != null) {
079: statements.add(new UpdateStatement(schemaName,
080: getTableName()).addNewColumnValue(getColumnName(),
081: getDefaultNullValue()).setWhereClause(
082: getColumnName() + " IS NULL"));
083: }
084:
085: statements.add(new SetNullableStatement(schemaName,
086: getTableName(), getColumnName(), getColumnDataType(),
087: false));
088:
089: if (database instanceof DB2Database) {
090: statements.add(new ReorganizeTableStatement(schemaName,
091: getTableName()));
092: }
093:
094: return statements.toArray(new SqlStatement[statements.size()]);
095:
096: }
097:
098: protected Change[] createInverses() {
099: DropNotNullConstraintChange inverse = new DropNotNullConstraintChange();
100: inverse.setColumnName(getColumnName());
101: inverse.setSchemaName(getSchemaName());
102: inverse.setTableName(getTableName());
103: inverse.setColumnDataType(getColumnDataType());
104:
105: return new Change[] { inverse };
106: }
107:
108: public String getConfirmationMessage() {
109: return "Null constraint has been added to " + getTableName()
110: + "." + getColumnName();
111: }
112:
113: public Element createNode(Document currentChangeLogFileDOM) {
114: Element element = currentChangeLogFileDOM
115: .createElement("addNotNullConstraint");
116: if (getSchemaName() != null) {
117: element.setAttribute("schemaName", getSchemaName());
118: }
119:
120: element.setAttribute("tableName", getTableName());
121: element.setAttribute("columnName", getColumnName());
122: element.setAttribute("defaultNullValue", getDefaultNullValue());
123: return element;
124: }
125:
126: public Set<DatabaseObject> getAffectedDatabaseObjects() {
127:
128: Table table = new Table(getTableName());
129:
130: Column column = new Column();
131: column.setTable(table);
132: column.setName(getColumnName());
133:
134: return new HashSet<DatabaseObject>(Arrays.asList(table, column));
135:
136: }
137: }
|