001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.CreateSequenceStatement;
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: * Creates a new sequence.
018: */
019: public class CreateSequenceChange extends AbstractChange {
020:
021: private String schemaName;
022: private String sequenceName;
023: private Integer startValue;
024: private Integer incrementBy;
025: private Integer maxValue;
026: private Integer minValue;
027: private Boolean ordered;
028:
029: public CreateSequenceChange() {
030: super ("createSequence", "Create Sequence");
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 getSequenceName() {
042: return sequenceName;
043: }
044:
045: public void setSequenceName(String sequenceName) {
046: this .sequenceName = sequenceName;
047: }
048:
049: public Integer getStartValue() {
050: return startValue;
051: }
052:
053: public void setStartValue(Integer startValue) {
054: this .startValue = startValue;
055: }
056:
057: public Integer getIncrementBy() {
058: return incrementBy;
059: }
060:
061: public void setIncrementBy(Integer incrementBy) {
062: this .incrementBy = incrementBy;
063: }
064:
065: public Integer getMaxValue() {
066: return maxValue;
067: }
068:
069: public void setMaxValue(Integer maxValue) {
070: this .maxValue = maxValue;
071: }
072:
073: public Integer getMinValue() {
074: return minValue;
075: }
076:
077: public void setMinValue(Integer minValue) {
078: this .minValue = minValue;
079: }
080:
081: public Boolean isOrdered() {
082: return ordered;
083: }
084:
085: public void setOrdered(Boolean ordered) {
086: this .ordered = ordered;
087: }
088:
089: public SqlStatement[] generateStatements(Database database)
090: throws UnsupportedChangeException {
091: return new SqlStatement[] { new CreateSequenceStatement(
092: getSchemaName() == null ? database
093: .getDefaultSchemaName() : getSchemaName(),
094: getSequenceName()).setIncrementBy(getIncrementBy())
095: .setMaxValue(getMaxValue()).setMinValue(getMinValue())
096: .setOrdered(isOrdered()).setStartValue(getStartValue()) };
097: }
098:
099: protected Change[] createInverses() {
100: DropSequenceChange inverse = new DropSequenceChange();
101: inverse.setSequenceName(getSequenceName());
102:
103: return new Change[] { inverse };
104: }
105:
106: public String getConfirmationMessage() {
107: return "Sequence " + getSequenceName() + " created";
108: }
109:
110: public Element createNode(Document currentChangeLogFileDOM) {
111: Element node = currentChangeLogFileDOM
112: .createElement("createSequence");
113: node.setAttribute("sequenceName", getSequenceName());
114: if (getMinValue() != null) {
115: node.setAttribute("minValue", getMinValue().toString());
116: }
117: if (getMaxValue() != null) {
118: node.setAttribute("maxValue", getMaxValue().toString());
119: }
120: if (getIncrementBy() != null) {
121: node.setAttribute("incrementBy", getIncrementBy()
122: .toString());
123: }
124: if (isOrdered() != null) {
125: node.setAttribute("ordered", isOrdered().toString());
126: }
127:
128: if (getStartValue() != null) {
129: node.setAttribute("startValue", getStartValue().toString());
130: }
131:
132: return node;
133: }
134:
135: public Set<DatabaseObject> getAffectedDatabaseObjects() {
136: Sequence dbObject = new Sequence();
137: dbObject.setName(sequenceName);
138:
139: return new HashSet<DatabaseObject>(Arrays.asList(dbObject));
140: }
141: }
|