Index: cpa/src/main/java/org/castor/cpa/persistence/sql/driver/GenericFactory.java =================================================================== --- cpa/src/main/java/org/castor/cpa/persistence/sql/driver/GenericFactory.java (revision 8341) +++ cpa/src/main/java/org/castor/cpa/persistence/sql/driver/GenericFactory.java (working copy) @@ -142,6 +142,13 @@ return false; } + /** + * {@inheritDoc} + */ + public String getSequenceNextValString(final String seqName) { + return null; + } + //----------------------------------------------------------------------------------- } Index: cpa/src/main/java/org/castor/cpa/persistence/sql/driver/OracleFactory.java =================================================================== --- cpa/src/main/java/org/castor/cpa/persistence/sql/driver/OracleFactory.java (revision 8341) +++ cpa/src/main/java/org/castor/cpa/persistence/sql/driver/OracleFactory.java (working copy) @@ -140,6 +140,11 @@ return false; } + @Override + public String getSequenceNextValString(final String seqName) { + return seqName + ".nextval"; + } + //----------------------------------------------------------------------------------- } Index: cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractAfterKeyGenerator.java =================================================================== --- cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractAfterKeyGenerator.java (revision 8353) +++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractAfterKeyGenerator.java (working copy) @@ -143,7 +143,7 @@ SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities(); if (_seqName != null && !_triggerPresent) { - insert.addSequence(_seqName, ids[0].getName()); + insert.addInsert(ids[0].getName(), _seqName); } insert.toString(_ctx); Index: cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/SequenceDuringKeyGenerator.java =================================================================== --- cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/SequenceDuringKeyGenerator.java (revision 8353) +++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/SequenceDuringKeyGenerator.java (working copy) @@ -238,7 +238,7 @@ SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities(); //_statement = this.patchSQL(_statement, ids[0].getName()); if (!_triggerPresent) { - insert.addSequence(_seqName, ids[0].getName()); + insert.addInsert(ids[0].getName(), _seqName); } insert.toString(_ctx); Index: cpa/src/main/java/org/castor/cpa/persistence/sql/query/Insert.java =================================================================== --- cpa/src/main/java/org/castor/cpa/persistence/sql/query/Insert.java (revision 8348) +++ cpa/src/main/java/org/castor/cpa/persistence/sql/query/Insert.java (working copy) @@ -15,13 +15,13 @@ */ package org.castor.cpa.persistence.sql.query; -import java.text.MessageFormat; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import org.castor.cpa.persistence.sql.query.expression.Column; import org.castor.cpa.persistence.sql.query.expression.Parameter; import org.castor.cpa.persistence.sql.query.expression.Expression; +import org.castor.cpa.persistence.sql.query.expression.NextVal; /** * Class to generate SQL Insert query statements. @@ -42,12 +42,6 @@ /** Parameter values needs to be inserted. */ private List _values; - /** Sequence expression of the form SEQUENCENAME.nextval. */ - private String _seqExpression; - - /** ID of the table. */ - private String _primKeyName; - //----------------------------------------------------------------------------------- /** @@ -87,34 +81,13 @@ /** * Appends a field representing a column to be inserted into the table. * - * @param qualifier Qualifier to be appended. * @param name Name of the column to be inserted. - */ - public void addInsert(final String qualifier, final String name) { - addInsert(new Column(new Table(qualifier), name), new Parameter(name)); - } - - /** - * Appends sequence to the insert statement. - * * @param seqName Name of the sequence. - * @param primKeyName ID of the Table. */ - public void addSequence(final String seqName, final String primKeyName) { - _primKeyName = primKeyName; - _seqExpression = MessageFormat.format(seqName, - new Object[] {this._qualifier.name(), _primKeyName}); - _seqExpression += ".nextval"; + public void addInsert(final String name, final String seqName) { + addInsert(new Column(name), new NextVal(seqName)); } - /** - * - * @return {code}true{code} If sequence has been added to this instance - * of insert hierarchy. - */ - public boolean hasSequence() { - return !(_seqExpression == null); - } //----------------------------------------------------------------------------------- @Override @@ -126,19 +99,8 @@ _qualifier.toString(ctx); - if (hasSequence()) { - ctx.append(QueryConstants.LPAREN); - ctx.append(ctx.quoteName(_primKeyName)); - if (_fields != null) { - ctx.append(QueryConstants.SEPERATOR); - ctx.append(QueryConstants.SPACE); - } - } - if (_fields != null) { - if (!hasSequence()) { - ctx.append(QueryConstants.LPAREN); - } + ctx.append(QueryConstants.LPAREN); for (Iterator iter = _fields.iterator(); iter.hasNext(); ) { iter.next().toString(ctx); @@ -147,25 +109,16 @@ ctx.append(QueryConstants.SPACE); } } - } - - if (hasSequence() || _fields != null) { + ctx.append(QueryConstants.RPAREN); } ctx.append(QueryConstants.SPACE); ctx.append(QueryConstants.VALUES); - ctx.append(QueryConstants.LPAREN); - - if (hasSequence()) { - ctx.append(ctx.quoteName(_seqExpression)); - if (_values != null) { - ctx.append(QueryConstants.SEPERATOR); - ctx.append(QueryConstants.SPACE); - } - } if (_values != null) { + ctx.append(QueryConstants.LPAREN); + for (Iterator iter = _values.iterator(); iter.hasNext(); ) { iter.next().toString(ctx); if (iter.hasNext()) { @@ -173,8 +126,9 @@ ctx.append(QueryConstants.SPACE); } } + + ctx.append(QueryConstants.RPAREN); } - ctx.append(QueryConstants.RPAREN); } //----------------------------------------------------------------------------------- Index: cpa/src/main/java/org/castor/cpa/persistence/sql/query/QueryContext.java =================================================================== --- cpa/src/main/java/org/castor/cpa/persistence/sql/query/QueryContext.java (revision 8348) +++ cpa/src/main/java/org/castor/cpa/persistence/sql/query/QueryContext.java (working copy) @@ -85,6 +85,17 @@ return _factory.quoteName(name); } + /** + * Returns the database engine specific string to fetch sequence next value. + * + * @param seqName Name of the sequence. + * @return String to fetch sequence next value. + */ + public String getSequenceNextValString(final String seqName) { + if (_factory == null) { return seqName + ".nextval"; } + return _factory.getSequenceNextValString(seqName); + } + //----------------------------------------------------------------------------------- /** Index: cpa/src/main/java/org/castor/cpa/persistence/sql/query/expression/Function.java =================================================================== --- cpa/src/main/java/org/castor/cpa/persistence/sql/query/expression/Function.java (revision 0) +++ cpa/src/main/java/org/castor/cpa/persistence/sql/query/expression/Function.java (revision 0) @@ -0,0 +1,25 @@ +/* + * Copyright 2009 Ralf Joachim, Ahmad Hassan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.castor.cpa.persistence.sql.query.expression; + +/** + * Base class for all any sql functions. + * + * @author Ahmad Hassan + * @author Ralf Joachim + * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $ + */ +public abstract class Function extends Expression { } Index: cpa/src/main/java/org/castor/cpa/persistence/sql/query/expression/NextVal.java =================================================================== --- cpa/src/main/java/org/castor/cpa/persistence/sql/query/expression/NextVal.java (revision 0) +++ cpa/src/main/java/org/castor/cpa/persistence/sql/query/expression/NextVal.java (revision 0) @@ -0,0 +1,52 @@ +/* + * Copyright 2009 Ralf Joachim, Ahmad Hassan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.castor.cpa.persistence.sql.query.expression; + +import org.castor.cpa.persistence.sql.query.QueryContext; + +/** + * Class for NEXTVAL sql function. + * + * @author Ahmad Hassan + * @author Ralf Joachim + * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $ + */ +public class NextVal extends Function { + //----------------------------------------------------------------------------------- + + /** Name of the sequence to get next value of. */ + private final String _seqName; + + //----------------------------------------------------------------------------------- + + /** + * Constructor. + * + * @param seqName Name of the sequence. + */ + public NextVal(final String seqName) { + _seqName = seqName; + } + + //----------------------------------------------------------------------------------- + + @Override + public void toString(final QueryContext ctx) { + ctx.append(ctx.getSequenceNextValString(_seqName)); + } + + //----------------------------------------------------------------------------------- +} Index: cpa/src/main/java/org/exolab/castor/persist/spi/PersistenceFactory.java =================================================================== --- cpa/src/main/java/org/exolab/castor/persist/spi/PersistenceFactory.java (revision 8320) +++ cpa/src/main/java/org/exolab/castor/persist/spi/PersistenceFactory.java (working copy) @@ -190,6 +190,14 @@ */ boolean isKeyGeneratorSequenceTypeSupported(int type); + /** + * Returns the database engine specific string to fetch sequence next value. + * + * @param seqName Name of the sequence. + * @return String to fetch sequence next value. + */ + String getSequenceNextValString(String seqName); + //----------------------------------------------------------------------------------- } Index: src/doc/release-notes.xml =================================================================== --- src/doc/release-notes.xml (revision 8354) +++ src/doc/release-notes.xml (working copy) @@ -91,6 +91,26 @@ ]]> + + + Improve SQL Insert class hierarchy to treat Sequence NextVal as an Expression. + + + Ahmad Hassan + ahmad.hassan@gmail.com + + + Ralf Joachim + ralf.joachim@syscon.eu + + + Ahmad Hassan + ahmad.hassan@gmail.com + + Enh. + JDO + 20090810 + Move SQLStatementCreate to org.castor.cpa.persistence.sql.engine package.