001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.AddUniqueConstraintStatement;
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.HashSet;
014: import java.util.Set;
015:
016: /**
017: * Adds a unique constraint to an existing column.
018: */
019: public class AddUniqueConstraintChange extends AbstractChange {
020:
021: private String schemaName;
022: private String tableName;
023: private String columnNames;
024: private String constraintName;
025: private String tablespace;
026:
027: public AddUniqueConstraintChange() {
028: super ("addUniqueConstraint", "Add Unique 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 getColumnNames() {
048: return columnNames;
049: }
050:
051: public void setColumnNames(String columnNames) {
052: this .columnNames = columnNames;
053: }
054:
055: public String getConstraintName() {
056: return constraintName;
057: }
058:
059: public void setConstraintName(String constraintName) {
060: this .constraintName = constraintName;
061: }
062:
063: public String getTablespace() {
064: return tablespace;
065: }
066:
067: public void setTablespace(String tablespace) {
068: this .tablespace = tablespace;
069: }
070:
071: public SqlStatement[] generateStatements(Database database)
072: throws UnsupportedChangeException {
073: AddUniqueConstraintStatement statement = new AddUniqueConstraintStatement(
074: getSchemaName() == null ? database
075: .getDefaultSchemaName() : getSchemaName(),
076: getTableName(), getColumnNames(), getConstraintName());
077: statement.setTablespace(getTablespace());
078:
079: return new SqlStatement[] { statement };
080: }
081:
082: public String getConfirmationMessage() {
083: return "Unique constraint added to " + getTableName() + "("
084: + getColumnNames() + ")";
085: }
086:
087: protected Change[] createInverses() {
088: DropUniqueConstraintChange inverse = new DropUniqueConstraintChange();
089: inverse.setSchemaName(getSchemaName());
090: inverse.setTableName(getTableName());
091: inverse.setConstraintName(getConstraintName());
092:
093: return new Change[] { inverse, };
094: }
095:
096: public Element createNode(Document currentChangeLogFileDOM) {
097: Element element = currentChangeLogFileDOM
098: .createElement(getTagName());
099: if (getSchemaName() != null) {
100: element.setAttribute("schemaName", getSchemaName());
101: }
102: if (getTablespace() != null) {
103: element.setAttribute("tablespace", getTablespace());
104: }
105:
106: element.setAttribute("tableName", getTableName());
107: element.setAttribute("columnNames", getColumnNames());
108: element.setAttribute("constraintName", getConstraintName());
109:
110: return element;
111: }
112:
113: public Set<DatabaseObject> getAffectedDatabaseObjects() {
114:
115: Set<DatabaseObject> returnSet = new HashSet<DatabaseObject>();
116:
117: Table table = new Table(getTableName());
118: returnSet.add(table);
119:
120: for (String columnName : getColumnNames().split(",")) {
121: Column column = new Column();
122: column.setTable(table);
123: column.setName(columnName.trim());
124:
125: returnSet.add(column);
126: }
127:
128: return returnSet;
129:
130: }
131:
132: }
|