001: /*
002: * $Id: AlterSequenceCommand.java,v 1.2 2005/12/20 18:32:28 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.commands;
042:
043: import java.math.BigInteger;
044:
045: import org.axiondb.AxionException;
046: import org.axiondb.DataType;
047: import org.axiondb.Database;
048: import org.axiondb.Sequence;
049: import org.axiondb.util.ValuePool;
050:
051: /**
052: * A <code>ALTER SEQUENCE</code> command.
053: *
054: * NOTE: One can't change DataType as per ANSI 2003 spec.
055: *
056: * @version $Revision: 1.2 $ $Date: 2005/12/20 18:32:28 $
057: * @author Ahimanikya Satapathy
058: */
059: public class AlterSequenceCommand extends CreateCommand {
060: public AlterSequenceCommand() {
061: }
062:
063: public void setStartValue(String value) {
064: _startVal = value;
065: }
066:
067: public void setIncrementBy(String incrementBy) {
068: _incrementBy = incrementBy;
069: }
070:
071: public void setMaxValue(String maxValue) {
072: _maxValue = maxValue;
073: }
074:
075: public void setMinValue(String minValue) {
076: _minValue = minValue;
077: }
078:
079: public void setCycle(boolean cycle) {
080: _isCycle = ValuePool.getBoolean(cycle);
081: }
082:
083: public boolean execute(org.axiondb.Database db)
084: throws AxionException {
085: assertNotReadOnly(db);
086:
087: if (!db.hasSequence(getObjectName())) {
088: throw new AxionException("Sequence named \""
089: + getObjectName() + "\" does not exists.");
090: }
091: Sequence old = db.getSequence(getObjectName());
092: db.createSequence(copyAndUpdateSequenceOptions(db, old));
093:
094: return false;
095: }
096:
097: private BigInteger parseBigInt(String value) {
098: return new BigInteger(value, Sequence.RADIX);
099: }
100:
101: private Sequence copyAndUpdateSequenceOptions(Database db,
102: Sequence old) throws AxionException {
103: BigInteger startVal;
104: if (_startVal == null) {
105: startVal = (BigInteger) old.getValue();
106: } else {
107: startVal = parseBigInt(_startVal);
108: }
109:
110: BigInteger incrementBy;
111: if (_incrementBy == null) {
112: incrementBy = old.getIncrementBy();
113: } else {
114: incrementBy = parseBigInt(_incrementBy);
115: }
116:
117: BigInteger maxValue;
118: if (_maxValue == null) {
119: maxValue = old.getMaxValue();
120: } else {
121: maxValue = parseBigInt(_maxValue);
122: }
123:
124: BigInteger minValue;
125: if (_minValue == null) {
126: minValue = old.getMinValue();
127: } else {
128: minValue = parseBigInt(_minValue);
129: }
130:
131: boolean isCycle;
132: if (_isCycle == null) {
133: isCycle = old.isCycle();
134: } else {
135: isCycle = _isCycle.booleanValue();
136: }
137:
138: DataType type = old.getDataType();
139: ;
140:
141: return new Sequence(getObjectName(), type, startVal,
142: incrementBy, maxValue, minValue, isCycle);
143: }
144:
145: private String _startVal;
146: private String _incrementBy;
147: private String _maxValue;
148: private String _minValue;
149: private Boolean _isCycle;
150: }
|