001: /*
002: * $Id: CreateSequenceCommand.java,v 1.17 2005/05/02 22:24:33 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 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:
050: /**
051: * A <code>CREATE SEQUENCE</code> command.
052: *
053: * @version $Revision: 1.17 $ $Date: 2005/05/02 22:24:33 $
054: * @author Chuck Burdick
055: * @author Ahimanikya Satapathy
056: */
057: public class CreateSequenceCommand extends CreateCommand {
058: public CreateSequenceCommand() {
059: }
060:
061: public CreateSequenceCommand(String sequenceName, int startVal) {
062: setObjectName(sequenceName);
063: _startVal = BigInteger.valueOf(startVal);
064: }
065:
066: public void setStartValue(String value) {
067: _startVal = new BigInteger(value, Sequence.RADIX);
068: }
069:
070: public void setIncrementBy(String incrementBy) {
071: _incrementBy = new BigInteger(incrementBy, Sequence.RADIX);
072: }
073:
074: public void setMaxValue(String maxValue) {
075: _maxValue = new BigInteger(maxValue, Sequence.RADIX);
076: }
077:
078: public void setMinValue(String minValue) {
079: _minValue = new BigInteger(minValue, Sequence.RADIX);
080: }
081:
082: public void setCycle(boolean cycle) {
083: _isCycle = cycle;
084: }
085:
086: public void setDataType(String typeName) {
087: _typeName = typeName;
088: }
089:
090: public void setIdentityType(String type) {
091: _identityType = type;
092: }
093:
094: public String getIdentityType() {
095: return _identityType;
096: }
097:
098: public boolean execute(Database db) throws AxionException {
099: assertNotReadOnly(db);
100: if (!db.hasSequence(getObjectName())) {
101: db.createSequence(createSequence(db));
102: } else if (!isIfNotExists()) {
103: throw new AxionException("A sequence named \""
104: + getObjectName() + "\" already exists.");
105: }
106: return false;
107: }
108:
109: private void validate(DataType type) throws AxionException {
110: int precision = type.getPrecision();
111: if (_maxValue.toString(10).length() > precision) {
112: throw new AxionException("Invalid MaxValue");
113: }
114: }
115:
116: public Sequence createSequence(Database db) throws AxionException {
117: DataType type = db.getDataType(_typeName);
118: validate(type);
119:
120: // if start value is not set use default
121: if (_startVal.signum() == -1) {
122: if (_incrementBy.signum() == -1) {
123: _startVal = _maxValue; // start with maxValue
124: } else {
125: _startVal = _minValue; // start with minValue
126: }
127: }
128:
129: return new Sequence(getObjectName(), type, _startVal,
130: _incrementBy, _maxValue, _minValue, _isCycle);
131: }
132:
133: private BigInteger _startVal = BigInteger.valueOf(-1);
134: private BigInteger _incrementBy = BigInteger.valueOf(1);
135: private BigInteger _maxValue = BigInteger
136: .valueOf(Integer.MAX_VALUE);
137: private BigInteger _minValue = BigInteger.valueOf(0);
138: private boolean _isCycle = false;
139: private String _identityType;
140: private String _typeName = "INTEGER";
141: }
|