001: package liquibase.change;
002:
003: import liquibase.database.DB2Database;
004: import liquibase.database.Database;
005: import liquibase.database.sql.AddPrimaryKeyStatement;
006: import liquibase.database.sql.ReorganizeTableStatement;
007: import liquibase.database.sql.SqlStatement;
008: import liquibase.database.structure.Column;
009: import liquibase.database.structure.DatabaseObject;
010: import liquibase.database.structure.Table;
011: import liquibase.exception.UnsupportedChangeException;
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014:
015: import java.util.HashSet;
016: import java.util.Set;
017:
018: /**
019: * Creates a primary key out of an existing column or set of columns.
020: */
021: public class AddPrimaryKeyChange extends AbstractChange {
022:
023: private String schemaName;
024: private String tableName;
025: private String tablespace;
026: private String columnNames;
027: private String constraintName;
028:
029: public AddPrimaryKeyChange() {
030: super ("addPrimaryKey", "Add Primary Key");
031: }
032:
033: public String getTableName() {
034: return tableName;
035: }
036:
037: public void setTableName(String tableName) {
038: this .tableName = tableName;
039: }
040:
041: public String getSchemaName() {
042: return schemaName;
043: }
044:
045: public void setSchemaName(String schemaName) {
046: this .schemaName = schemaName;
047: }
048:
049: public String getColumnNames() {
050: return columnNames;
051: }
052:
053: public void setColumnNames(String columnNames) {
054: this .columnNames = columnNames;
055: }
056:
057: public String getConstraintName() {
058: return constraintName;
059: }
060:
061: public void setConstraintName(String constraintName) {
062: this .constraintName = constraintName;
063: }
064:
065: public String getTablespace() {
066: return tablespace;
067: }
068:
069: public void setTablespace(String tablespace) {
070: this .tablespace = tablespace;
071: }
072:
073: public SqlStatement[] generateStatements(Database database)
074: throws UnsupportedChangeException {
075: String schemaName = getSchemaName() == null ? database
076: .getDefaultSchemaName() : getSchemaName();
077:
078: AddPrimaryKeyStatement statement = new AddPrimaryKeyStatement(
079: schemaName, getTableName(), getColumnNames(),
080: getConstraintName());
081: statement.setTablespace(getTablespace());
082:
083: if (database instanceof DB2Database) {
084: return new SqlStatement[] {
085: statement,
086: new ReorganizeTableStatement(schemaName,
087: getTableName()) };
088: }
089:
090: return new SqlStatement[] { statement };
091: }
092:
093: protected Change[] createInverses() {
094: DropPrimaryKeyChange inverse = new DropPrimaryKeyChange();
095: inverse.setSchemaName(getSchemaName());
096: inverse.setTableName(getTableName());
097: inverse.setConstraintName(getConstraintName());
098:
099: return new Change[] { inverse, };
100: }
101:
102: public String getConfirmationMessage() {
103: return "Primary key added to " + getTableName() + " ("
104: + getColumnNames() + ")";
105: }
106:
107: public Element createNode(Document currentChangeLogFileDOM) {
108: Element node = currentChangeLogFileDOM
109: .createElement(getTagName());
110: if (getSchemaName() != null) {
111: node.setAttribute("schemaName", getSchemaName());
112: }
113: node.setAttribute("tableName", getTableName());
114: node.setAttribute("columnNames", getColumnNames());
115: if (getConstraintName() != null) {
116: node.setAttribute("constraintName", getConstraintName());
117: }
118: if (getTablespace() != null) {
119: node.setAttribute("tablespace", getTablespace());
120: }
121:
122: return node;
123: }
124:
125: public Set<DatabaseObject> getAffectedDatabaseObjects() {
126:
127: Set<DatabaseObject> dbObjects = new HashSet<DatabaseObject>();
128:
129: Table table = new Table(getTableName());
130: dbObjects.add(table);
131:
132: for (String columnName : columnNames.split(",")) {
133: Column column = new Column();
134: column.setTable(table);
135: column.setName(columnName.trim());
136:
137: dbObjects.add(column);
138: }
139:
140: return dbObjects;
141:
142: }
143:
144: }
|