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