001: /*
002: * Copyright 2006 Le Duc Bao, Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.castor.ddlgen.keygenerator;
017:
018: import org.castor.ddlgen.DDLWriter;
019: import org.castor.ddlgen.GeneratorException;
020: import org.castor.ddlgen.schemaobject.KeyGenerator;
021: import org.exolab.castor.mapping.xml.KeyGeneratorDef;
022: import org.exolab.castor.mapping.xml.Param;
023:
024: /**
025: * SEQUENCE key generator can be used only with Oracle, PostgreSQL, Interbase and
026: * SAP DB. It generates keys using sequences.
027: *
028: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
029: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
030: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
031: * @since 1.1
032: */
033: public final class SequenceKeyGenerator extends KeyGenerator {
034: //--------------------------------------------------------------------------
035:
036: /** Name of key generator algorithm. */
037: public static final String ALGORITHM_NAME = "SEQUENCE";
038:
039: /** Parameter that defines the name or a pattern for the sequence. */
040: private static final String PARAM_SEQUENCE = "sequence";
041:
042: /** Paramater that defines if sequence returns generated value. */
043: private static final String PARAM_RETURNING = "returning";
044:
045: /** Parameter that defines if key is generated by a trigger. */
046: private static final String PARAM_TRIGGER = "trigger";
047:
048: /** Parameter that defines the increment of the sequence. */
049: private static final String PARAM_INCREMENT = "increment";
050:
051: //--------------------------------------------------------------------------
052:
053: /** The key generator factory that has created this key generator and will also be
054: * responsible to generate the DDL for the sequence. */
055: private final SequenceKeyGeneratorFactory _factory;
056:
057: /** Name of the sequence. Defaults to "{0}_seq". */
058: private String _sequence = "{0}_seq";
059:
060: /** Returning mode for the sequence. Used for Oracle. Defaults to "false". */
061: private boolean _isReturning = false;
062:
063: /** Use a trigger that generates keys. Used for Oracle. Defaults to "false". */
064: private boolean _isTrigger = false;
065:
066: /** Increment for the sequence. Used for interbase only. Defaults to "1". */
067: private int _increment = 1;
068:
069: //--------------------------------------------------------------------------
070:
071: /**
072: * Constructor for default SEQUENCE key generator.
073: *
074: * @param factory The key generator factory that has created this key generator and
075: * will also be responsible to generate the DDL for the sequence.
076: */
077: protected SequenceKeyGenerator(
078: final SequenceKeyGeneratorFactory factory) {
079: super (ALGORITHM_NAME, ALGORITHM_NAME);
080:
081: _factory = factory;
082: }
083:
084: /**
085: * Constructor for SEQUENCE key generator specified by given defintion.
086: *
087: * @param factory The key generator factory that has created this key generator and
088: * will also be responsible to generate the DDL for the sequence.
089: * @param definition Key generator definition.
090: * @throws GeneratorException If increment parameter can't be parsed as integer.
091: */
092: protected SequenceKeyGenerator(
093: final SequenceKeyGeneratorFactory factory,
094: final KeyGeneratorDef definition) throws GeneratorException {
095: super (ALGORITHM_NAME, definition.getAlias());
096:
097: _factory = factory;
098:
099: Param[] params = definition.getParam();
100: for (int i = 0; i < params.length; i++) {
101: String name = params[i].getName();
102: String value = params[i].getValue();
103: if (name == null) {
104: continue;
105: }
106: if (PARAM_SEQUENCE.equalsIgnoreCase(name)) {
107: _sequence = value;
108: } else if (PARAM_RETURNING.equalsIgnoreCase(name)) {
109: _isReturning = Boolean.valueOf(value).booleanValue();
110: } else if (PARAM_TRIGGER.equalsIgnoreCase(name)) {
111: _isTrigger = Boolean.valueOf(value).booleanValue();
112: } else if (PARAM_INCREMENT.equalsIgnoreCase(name)) {
113: try {
114: _increment = Integer.parseInt(value);
115: } catch (NumberFormatException nfe) {
116: throw new GeneratorException("Can't parse integer"
117: + name, nfe);
118: }
119: }
120: }
121: }
122:
123: //--------------------------------------------------------------------------
124:
125: /**
126: * Get name of the sequence.
127: *
128: * @return Name of the sequence.
129: */
130: public String getSequence() {
131: return _sequence;
132: }
133:
134: /**
135: * Get returning mode for the sequence.
136: *
137: * @return Returning mode for the sequence.
138: */
139: public boolean isReturning() {
140: return _isReturning;
141: }
142:
143: /**
144: * Shell a trigger be used to generates keys?
145: *
146: * @return <code>true</code> if keys are to be generated by a trigger,
147: * <code>false</code> otherwise.
148: */
149: public boolean isTrigger() {
150: return _isTrigger;
151: }
152:
153: /**
154: * Get increment for the sequence.
155: *
156: * @return Increment for the sequence.
157: */
158: public int getIncrement() {
159: return _increment;
160: }
161:
162: //--------------------------------------------------------------------------
163:
164: /**
165: * Delegate generation of create script to factory.
166: * <br/>
167: * {@inheritDoc}
168: */
169: public void toCreateDDL(final DDLWriter writer) {
170: _factory.toCreateDDL(this , writer);
171: }
172:
173: /**
174: * Delegate generation of drop script to factory.
175: * <br/>
176: * {@inheritDoc}
177: */
178: public void toDropDDL(final DDLWriter writer) {
179: _factory.toDropDDL(this , writer);
180: }
181:
182: //--------------------------------------------------------------------------
183: }
|