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.KeyGeneratorFactory;
021: import org.castor.ddlgen.schemaobject.Field;
022: import org.castor.ddlgen.schemaobject.KeyGenerator;
023: import org.castor.ddlgen.schemaobject.Table;
024: import org.exolab.castor.mapping.xml.KeyGeneratorDef;
025:
026: /**
027: * Abstract base class for SEQUENCE key generator factories.
028: *
029: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
030: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
031: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
032: * @since 1.1
033: */
034: public abstract class SequenceKeyGeneratorFactory implements
035: KeyGeneratorFactory {
036: //--------------------------------------------------------------------------
037:
038: /**
039: * {@inheritDoc}
040: */
041: public final String getAlgorithmName() {
042: return SequenceKeyGenerator.ALGORITHM_NAME;
043: }
044:
045: /**
046: * {@inheritDoc}
047: */
048: public final boolean hasMandatoryParameters() {
049: return false;
050: }
051:
052: /**
053: * {@inheritDoc}
054: */
055: public final KeyGenerator createKeyGenerator()
056: throws GeneratorException {
057: return new SequenceKeyGenerator(this );
058: }
059:
060: /**
061: * {@inheritDoc}
062: */
063: public final KeyGenerator createKeyGenerator(
064: final KeyGeneratorDef definition) throws GeneratorException {
065: return new SequenceKeyGenerator(this , definition);
066: }
067:
068: //--------------------------------------------------------------------------
069:
070: /**
071: * Generate create script for given SEQUENCE key generator.
072: *
073: * @param key SEQUENCE key generator to generate DDL for.
074: * @param writer DDLWriter to write schema objects to.
075: */
076: public abstract void toCreateDDL(final KeyGenerator key,
077: final DDLWriter writer);
078:
079: /**
080: * Generate drop script for given SEQUENCE key generator.
081: *
082: * @param key SEQUENCE key generator to generate DDL for.
083: * @param writer DDLWriter to write schema objects to.
084: */
085: public abstract void toDropDDL(final KeyGenerator key,
086: final DDLWriter writer);
087:
088: //--------------------------------------------------------------------------
089:
090: /**
091: * Build a string containing names of all primary key columns separated by '_'.
092: *
093: * @param table Table build primary key column names string for.
094: * @return String containing names of all primary key columns separated by '_'.
095: */
096: protected final String toPrimaryKeyList(final Table table) {
097: boolean hasPrimaryKey = false;
098: StringBuffer sb = new StringBuffer();
099:
100: boolean isFirstField = true;
101: for (int i = 0; i < table.getFieldCount(); i++) {
102: Field field = table.getField(i);
103: if (field.isIdentity()) {
104: hasPrimaryKey = true;
105: if (!isFirstField) {
106: sb.append("_");
107: }
108: isFirstField = false;
109: sb.append(field.getName());
110: }
111: }
112:
113: //have no primary key
114: if (!hasPrimaryKey) {
115: return "";
116: }
117: return sb.toString();
118: }
119:
120: /**
121: * Build a string containing types of all primary key columns separated by '_'.
122: *
123: * @param table Table build primary key column types string for.
124: * @return String containing types of all primary key columns separated by '_'.
125: */
126: protected final String toPrimaryKeyTypeList(final Table table) {
127: boolean hasPrimaryKey = false;
128: StringBuffer sb = new StringBuffer();
129:
130: boolean isFirstField = true;
131: for (int i = 0; i < table.getFieldCount(); i++) {
132: Field field = table.getField(i);
133: if (field.isIdentity()) {
134: hasPrimaryKey = true;
135: if (!isFirstField) {
136: sb.append("_");
137: }
138: isFirstField = false;
139: sb.append(field.getType().getSqlType());
140: }
141: }
142:
143: if (!hasPrimaryKey) {
144: return "";
145: }
146: return sb.toString();
147: }
148:
149: //--------------------------------------------------------------------------
150: }
|