001: package liquibase.database.sql;
002:
003: import liquibase.database.*;
004: import liquibase.exception.StatementNotSupportedOnDatabaseException;
005:
006: public class AlterSequenceStatement implements SqlStatement {
007:
008: private String schemaName;
009: private String sequenceName;
010: private Integer incrementBy;
011: private Integer maxValue;
012: private Integer minValue;
013: private Boolean ordered;
014:
015: public AlterSequenceStatement(String schemaName, String sequenceName) {
016: this .schemaName = schemaName;
017: this .sequenceName = sequenceName;
018: }
019:
020: public String getSchemaName() {
021: return schemaName;
022: }
023:
024: public String getSequenceName() {
025: return sequenceName;
026: }
027:
028: public Integer getIncrementBy() {
029: return incrementBy;
030: }
031:
032: public AlterSequenceStatement setIncrementBy(Integer incrementBy) {
033: this .incrementBy = incrementBy;
034: return this ;
035: }
036:
037: public Integer getMaxValue() {
038: return maxValue;
039: }
040:
041: public AlterSequenceStatement setMaxValue(Integer maxValue) {
042: this .maxValue = maxValue;
043: return this ;
044: }
045:
046: public Integer getMinValue() {
047: return minValue;
048: }
049:
050: public AlterSequenceStatement setMinValue(Integer minValue) {
051: this .minValue = minValue;
052: return this ;
053: }
054:
055: public Boolean getOrdered() {
056: return ordered;
057: }
058:
059: public AlterSequenceStatement setOrdered(Boolean ordered) {
060: this .ordered = ordered;
061: return this ;
062: }
063:
064: public String getSqlStatement(Database database)
065: throws StatementNotSupportedOnDatabaseException {
066: if (!supportsDatabase(database)) {
067: throw new StatementNotSupportedOnDatabaseException(this ,
068: database);
069: }
070:
071: StringBuffer buffer = new StringBuffer();
072: buffer.append("ALTER SEQUENCE ");
073: buffer.append(database.escapeSequenceName(getSchemaName(),
074: getSequenceName()));
075:
076: if (getIncrementBy() != null) {
077: if (database instanceof FirebirdDatabase
078: || database instanceof HsqlDatabase) {
079: throw new StatementNotSupportedOnDatabaseException(
080: "Database does not support altering sequences with increment",
081: this , database);
082: } else {
083: buffer.append(" INCREMENT BY ")
084: .append(getIncrementBy());
085: }
086: }
087: if (getMinValue() != null) {
088: if (database instanceof FirebirdDatabase
089: || database instanceof HsqlDatabase) {
090: buffer.append(" RESTART WITH ").append(getMinValue());
091: } else {
092: buffer.append(" MINVALUE ").append(getMinValue());
093: }
094: }
095:
096: if (getMaxValue() != null) {
097: if (database instanceof FirebirdDatabase
098: || database instanceof HsqlDatabase) {
099: throw new StatementNotSupportedOnDatabaseException(
100: "Database does not support altering sequences with maxValue",
101: this , database);
102: } else {
103: buffer.append(" MAXVALUE ").append(getMaxValue());
104: }
105: }
106:
107: if (getOrdered() != null) {
108: if (database instanceof OracleDatabase
109: || database instanceof DB2Database
110: || database instanceof MaxDBDatabase) {
111: if (getOrdered()) {
112: buffer.append(" ORDER");
113: }
114: } else {
115: throw new StatementNotSupportedOnDatabaseException(
116: "Database does not support creating sequences with 'order'",
117: this , database);
118: }
119: }
120:
121: return buffer.toString();
122: }
123:
124: public String getEndDelimiter(Database database) {
125: return ";";
126: }
127:
128: public boolean supportsDatabase(Database database) {
129: return database.supportsSequences();
130: }
131: }
|