001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.PostgresDatabase;
005: import liquibase.database.sql.*;
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: * Makes an existing column into an auto-increment column.
019: * This change is only valid for databases with auto-increment/identity columns.
020: * The current version does not support MS-SQL.
021: */
022: public class AddAutoIncrementChange extends AbstractChange {
023:
024: private String schemaName;
025: private String tableName;
026: private String columnName;
027: private String columnDataType;
028:
029: public AddAutoIncrementChange() {
030: super ("addAutoIncrement", "Set Column as Auto-Increment");
031: }
032:
033: public String getSchemaName() {
034: return schemaName;
035: }
036:
037: public void setSchemaName(String schemaName) {
038: this .schemaName = schemaName;
039: }
040:
041: public String getTableName() {
042: return tableName;
043: }
044:
045: public void setTableName(String tableName) {
046: this .tableName = tableName;
047: }
048:
049: public String getColumnName() {
050: return columnName;
051: }
052:
053: public void setColumnName(String columnName) {
054: this .columnName = columnName;
055: }
056:
057: public String getColumnDataType() {
058: return columnDataType;
059: }
060:
061: public void setColumnDataType(String columnDataType) {
062: this .columnDataType = columnDataType;
063: }
064:
065: public SqlStatement[] generateStatements(Database database)
066: throws UnsupportedChangeException {
067: String schemaName = getSchemaName() == null ? database
068: .getDefaultSchemaName() : getSchemaName();
069: if (database instanceof PostgresDatabase) {
070: String sequenceName = (getTableName() + "_"
071: + getColumnName() + "_seq").toLowerCase();
072: return new SqlStatement[] {
073: new CreateSequenceStatement(schemaName,
074: sequenceName),
075: new SetNullableStatement(schemaName,
076: getTableName(), getColumnName(), null,
077: false),
078: new AddDefaultValueStatement(schemaName,
079: getTableName(), getColumnName(),
080: sequenceName), };
081: } else {
082: return new SqlStatement[] { new AddAutoIncrementStatement(
083: schemaName, getTableName(), getColumnName(),
084: getColumnDataType()) };
085: }
086: }
087:
088: public String getConfirmationMessage() {
089: return "Auto-increment added to " + getTableName() + "."
090: + getColumnName();
091: }
092:
093: public Element createNode(Document currentChangeLogFileDOM) {
094: Element node = currentChangeLogFileDOM
095: .createElement("addAutoIncrement");
096: node.setAttribute("tableName", getTableName());
097: node.setAttribute("columnName", getColumnName());
098: if (getSchemaName() != null) {
099: node.setAttribute("schemaName", getSchemaName());
100: }
101: if (getColumnDataType() != null) {
102: node.setAttribute("columnDataType", getColumnDataType());
103: }
104:
105: return node;
106: }
107:
108: public Set<DatabaseObject> getAffectedDatabaseObjects() {
109: Column column = new Column();
110:
111: Table table = new Table(getTableName());
112: column.setTable(table);
113:
114: column.setName(columnName);
115:
116: return new HashSet<DatabaseObject>(Arrays.asList(table, column));
117:
118: }
119:
120: }
|