001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.AlterSequenceStatement;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.database.structure.DatabaseObject;
007: import liquibase.database.structure.Sequence;
008: import liquibase.exception.UnsupportedChangeException;
009: import org.w3c.dom.Document;
010: import org.w3c.dom.Element;
011:
012: import java.util.Arrays;
013: import java.util.HashSet;
014: import java.util.Set;
015:
016: /**
017: * Modifies properties of an existing sequence.
018: */
019: public class AlterSequenceChange extends AbstractChange {
020:
021: private String schemaName;
022: private String sequenceName;
023: private Integer incrementBy;
024: private Integer maxValue;
025: private Integer minValue;
026: private Boolean ordered;
027:
028: // StartValue is not allowed since we cannot alter the starting sequence number
029:
030: public AlterSequenceChange() {
031: super ("alterSequence", "Alter Sequence");
032: }
033:
034: public String getSchemaName() {
035: return schemaName;
036: }
037:
038: public void setSchemaName(String schemaName) {
039: this .schemaName = schemaName;
040: }
041:
042: public String getSequenceName() {
043: return sequenceName;
044: }
045:
046: public void setSequenceName(String sequenceName) {
047: this .sequenceName = sequenceName;
048: }
049:
050: public Integer getIncrementBy() {
051: return incrementBy;
052: }
053:
054: public void setIncrementBy(Integer incrementBy) {
055: this .incrementBy = incrementBy;
056: }
057:
058: public Integer getMaxValue() {
059: return maxValue;
060: }
061:
062: public void setMaxValue(Integer maxValue) {
063: this .maxValue = maxValue;
064: }
065:
066: public Integer getMinValue() {
067: return minValue;
068: }
069:
070: public void setMinValue(Integer minValue) {
071: this .minValue = minValue;
072: }
073:
074: public Boolean isOrdered() {
075: return ordered;
076: }
077:
078: public void setOrdered(Boolean ordered) {
079: this .ordered = ordered;
080: }
081:
082: public SqlStatement[] generateStatements(Database database)
083: throws UnsupportedChangeException {
084: if (!database.supportsSequences()) {
085: throw new UnsupportedChangeException(
086: "Sequences do not exist in "
087: + database.getProductName());
088: }
089:
090: return new SqlStatement[] { new AlterSequenceStatement(
091: getSchemaName() == null ? database
092: .getDefaultSchemaName() : getSchemaName(),
093: getSequenceName()).setIncrementBy(getIncrementBy())
094: .setMaxValue(getMaxValue()).setMinValue(getMinValue())
095: .setOrdered(isOrdered()) };
096: }
097:
098: public String getConfirmationMessage() {
099: return "Sequence " + getSequenceName() + " altered";
100: }
101:
102: public Element createNode(Document currentChangeLogFileDOM) {
103: Element node = currentChangeLogFileDOM
104: .createElement("alterSequence");
105: node.setAttribute("sequenceName", getSequenceName());
106: if (getSchemaName() != null) {
107: node.setAttribute("schemaName", getSchemaName());
108: }
109: if (getMinValue() != null) {
110: node.setAttribute("minValue", getMinValue().toString());
111: }
112: if (getMaxValue() != null) {
113: node.setAttribute("maxValue", getMaxValue().toString());
114: }
115: if (getIncrementBy() != null) {
116: node.setAttribute("incrementBy", getIncrementBy()
117: .toString());
118: }
119: if (isOrdered() != null) {
120: node.setAttribute("ordered", isOrdered().toString());
121: }
122:
123: return node;
124: }
125:
126: public Set<DatabaseObject> getAffectedDatabaseObjects() {
127: Sequence dbObject = new Sequence();
128: dbObject.setName(sequenceName);
129:
130: return new HashSet<DatabaseObject>(Arrays.asList(dbObject));
131: }
132: }
|