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 8348)
+++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractAfterKeyGenerator.java (working copy)
@@ -41,19 +41,13 @@
import org.exolab.castor.jdo.engine.SQLColumnInfo;
import org.exolab.castor.jdo.engine.SQLFieldInfo;
import org.exolab.castor.jdo.engine.nature.ClassDescriptorJDONature;
-import org.exolab.castor.jdo.engine.nature.FieldDescriptorJDONature;
import org.castor.core.util.AbstractProperties;
import org.castor.core.util.Messages;
import org.castor.cpa.CPAProperties;
import org.castor.cpa.persistence.sql.engine.SQLStatementInsertCheck;
import org.castor.cpa.persistence.sql.query.Insert;
import org.castor.cpa.persistence.sql.query.QueryContext;
-import org.castor.jdo.engine.DatabaseContext;
-import org.castor.jdo.engine.DatabaseRegistry;
-import org.castor.jdo.engine.SQLTypeInfos;
import org.exolab.castor.mapping.ClassDescriptor;
-import org.exolab.castor.mapping.FieldDescriptor;
-import org.exolab.castor.mapping.MappingException;
/**
* Abstract class that implements the KeyGenerator interface for AFTER_INSERT style. The key
@@ -64,7 +58,7 @@
* @author Ralf Joachim
* @version $Revision$ $Date: 2009-07-13 17:22:43 (Tue, 28 Jul 2009) $
*/
-public abstract class AbstractAfterKeyGenerator implements KeyGenerator {
+public abstract class AbstractAfterKeyGenerator extends AbstractKeyGenerator {
//-----------------------------------------------------------------------------------
/** The Jakarta
@@ -99,11 +93,14 @@
/** Name of the Sequence. */
private String _seqName;
+ //-----------------------------------------------------------------------------------
+
/**
* Constructor.
*
* @param factory Persistence factory for the database engine the entity is persisted in.
* Used to format the SQL statement
+ * @param params Parameters for key generator.
*/
public AbstractAfterKeyGenerator(final PersistenceFactory factory, final Properties params) {
_factory = factory;
@@ -116,14 +113,9 @@
_seqName = params.getProperty("sequence", "{0}_seq");
}
}
-
- /**
- * {@inheritDoc}
- */
- public final byte getStyle() {
- return AFTER_INSERT;
- }
+ //-----------------------------------------------------------------------------------
+
/**
* {@inheritDoc}
*/
@@ -190,9 +182,7 @@
Class[] types = new Class[] {String.class, int.class};
Object[] args = new Object[] {_ctx.toString(), rgk};
Method method = Connection.class.getMethod("prepareStatement", types);
- stmt = (PreparedStatement) method.invoke(conn, args);
-
- // stmt = conn.prepareStatement(_statement, Statement.RETURN_GENERATED_KEYS);
+ stmt = (PreparedStatement) method.invoke(conn, args);
} else {
stmt = conn.prepareStatement(_ctx.toString());
}
@@ -217,7 +207,6 @@
Class cls = PreparedStatement.class;
Method method = cls.getMethod("getGeneratedKeys", (Class[]) null);
ResultSet keySet = (ResultSet) method.invoke(stmt, (Object[]) null);
- // ResultSet keySet = stmt.getGeneratedKeys();
int i = 1;
int sqlType;
@@ -374,46 +363,7 @@
}
}
}
-
- /**
- * Operning new JDBC Connection.
- *
- * @param database The database on which it opens the JDBC connection.
- * @return A JDBC Connection
- * @throws PersistenceException If fails to open connection.
- */
- private Connection getSeparateConnection(final Database database)
- throws PersistenceException {
- DatabaseContext context = null;
- try {
- context = DatabaseRegistry.getDatabaseContext(database.getDatabaseName());
- } catch (MappingException e) {
- throw new PersistenceException(Messages.message("persist.cannotCreateSeparateConn"), e);
- }
-
- try {
- Connection conn = context.getConnectionFactory().createConnection();
- conn.setAutoCommit(false);
- return conn;
- } catch (SQLException e) {
- throw new PersistenceException(Messages.message("persist.cannotCreateSeparateConn"), e);
- }
- }
-
- /**
- * Close the JDBC Connection.
- *
- * @param conn A JDBC Connection.
- */
- private void closeSeparateConnection(final Connection conn) {
- try {
- if (!conn.isClosed()) {
- conn.close();
- }
- } catch (SQLException e) {
- LOG.error(e.getMessage(), e);
- }
- }
+ //-----------------------------------------------------------------------------------
}
Index: cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractBeforeKeyGenerator.java
===================================================================
--- cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractBeforeKeyGenerator.java (revision 8348)
+++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractBeforeKeyGenerator.java (working copy)
@@ -26,8 +26,6 @@
import org.castor.cpa.persistence.sql.engine.SQLStatementInsertCheck;
import org.castor.cpa.persistence.sql.query.Insert;
import org.castor.cpa.persistence.sql.query.QueryContext;
-import org.castor.jdo.engine.DatabaseContext;
-import org.castor.jdo.engine.DatabaseRegistry;
import org.castor.persist.ProposedEntity;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.DuplicateIdentityException;
@@ -37,7 +35,6 @@
import org.exolab.castor.jdo.engine.SQLFieldInfo;
import org.exolab.castor.jdo.engine.nature.ClassDescriptorJDONature;
import org.exolab.castor.mapping.ClassDescriptor;
-import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.persist.spi.Identity;
import org.exolab.castor.persist.spi.PersistenceFactory;
@@ -50,7 +47,7 @@
* @author Ralf Joachim
* @version $Revision$ $Date: 2009-07-13 17:22:43 (Tue, 28 Jul 2009) $
*/
-public abstract class AbstractBeforeKeyGenerator implements KeyGenerator {
+public abstract class AbstractBeforeKeyGenerator extends AbstractKeyGenerator {
//-----------------------------------------------------------------------------------
/** The Jakarta
@@ -75,6 +72,8 @@
* and parameters binding. */
private final QueryContext _ctx;
+ //-----------------------------------------------------------------------------------
+
/**
* Constructor.
*
@@ -86,12 +85,7 @@
_ctx = new QueryContext(_factory);
}
- /**
- * {@inheritDoc}
- */
- public final byte getStyle() {
- return BEFORE_INSERT;
- }
+ //-----------------------------------------------------------------------------------
/**
* {@inheritDoc}
@@ -308,46 +302,6 @@
}
}
}
-
- /**
- * Operning new JDBC Connection.
- *
- * @param database The database on which it opens the JDBC connection.
- * @return A JDBC Connection
- * @throws PersistenceException If fails to open connection.
- */
- private Connection getSeparateConnection(final Database database)
- throws PersistenceException {
- DatabaseContext context = null;
- try {
- context = DatabaseRegistry.getDatabaseContext(database.getDatabaseName());
- } catch (MappingException e) {
- throw new PersistenceException(Messages.message("persist.cannotCreateSeparateConn"), e);
- }
-
- try {
- Connection conn = context.getConnectionFactory().createConnection();
- conn.setAutoCommit(false);
- return conn;
- } catch (SQLException e) {
- throw new PersistenceException(Messages.message("persist.cannotCreateSeparateConn"), e);
- }
- }
- /**
- * Close the JDBC Connection.
- *
- * @param conn A JDBC Connection.
- */
-
- private void closeSeparateConnection(final Connection conn) {
- try {
- if (!conn.isClosed()) {
- conn.close();
- }
- } catch (SQLException e) {
- LOG.error(e.getMessage(), e);
- }
- }
-
+ //-----------------------------------------------------------------------------------
}
Index: cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractDuringKeyGenerator.java
===================================================================
--- cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractDuringKeyGenerator.java (revision 8341)
+++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractDuringKeyGenerator.java (working copy)
@@ -1,34 +0,0 @@
-/*
- * Copyright 2009 Ahmad Hassan, Ralf Joachim
- *
- * 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.keygen;
-
-/**
- * Abstract Class that implements the KeyGenerator Interface for DURING_INSERT style. The key
- * generator is used for producing identities for objects before they are created in the
- * database.
- *
- * @author Ahmad Hassan
- * @author Ralf Joachim
- * @version $Revision$ $Date: 2009-07-13 17:22:43 (Tue, 28 Jul 2009) $
- */
-public abstract class AbstractDuringKeyGenerator implements KeyGenerator {
- /**
- * {@inheritDoc}
- */
- public final byte getStyle() {
- return DURING_INSERT;
- }
-}
Index: cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractKeyGenerator.java
===================================================================
--- cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractKeyGenerator.java (revision 0)
+++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractKeyGenerator.java (revision 0)
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2009 Ahmad Hassan, Ralf Joachim
+ *
+ * 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.keygen;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.castor.core.util.Messages;
+import org.castor.jdo.engine.DatabaseContext;
+import org.castor.jdo.engine.DatabaseRegistry;
+import org.exolab.castor.jdo.Database;
+import org.exolab.castor.jdo.PersistenceException;
+import org.exolab.castor.mapping.MappingException;
+
+/**
+ * Abstract Class that implements the KeyGenerator Interface and provide
+ * implementation for methods that are common in more than one subclass of this
+ * AbstractKeyGenerator.
+ *
+ * @author Ahmad Hassan
+ * @author Ralf Joachim
+ * @version $Revision$ $Date: 2009-07-13 17:22:43 (Tue, 28 Jul 2009) $
+ */
+public abstract class AbstractKeyGenerator implements KeyGenerator {
+ //-----------------------------------------------------------------------------------
+
+ /** The Jakarta
+ * Commons Logging instance used for all logging. */
+ private static final Log LOG = LogFactory.getLog(AbstractKeyGenerator.class);
+
+ //-----------------------------------------------------------------------------------
+
+ /**
+ * Operning new JDBC Connection.
+ *
+ * @param database The database on which it opens the JDBC connection.
+ * @return A JDBC Connection
+ * @throws PersistenceException If fails to open connection.
+ */
+ public final Connection getSeparateConnection(final Database database)
+ throws PersistenceException {
+ DatabaseContext context = null;
+ try {
+ context = DatabaseRegistry.getDatabaseContext(database.getDatabaseName());
+ } catch (MappingException e) {
+ throw new PersistenceException(Messages.message("persist.cannotCreateSeparateConn"), e);
+ }
+
+ try {
+ Connection conn = context.getConnectionFactory().createConnection();
+ conn.setAutoCommit(false);
+ return conn;
+ } catch (SQLException e) {
+ throw new PersistenceException(Messages.message("persist.cannotCreateSeparateConn"), e);
+ }
+ }
+
+ /**
+ * Close the JDBC Connection.
+ *
+ * @param conn A JDBC Connection.
+ */
+
+ public final void closeSeparateConnection(final Connection conn) {
+ try {
+ if (!conn.isClosed()) {
+ conn.close();
+ }
+ } catch (SQLException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+
+ //-----------------------------------------------------------------------------------
+}
Index: cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractNoKeyGenerator.java
===================================================================
--- cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractNoKeyGenerator.java (revision 8341)
+++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/AbstractNoKeyGenerator.java (working copy)
@@ -1,32 +0,0 @@
-/*
- * Copyright 2009 Ahmad Hassan, Ralf Joachim
- *
- * 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.keygen;
-
-/**
- * Abstract Class that provides functionality for NOKEYGEN_INSERT style.
- *
- * @author Ahmad Hassan
- * @author Ralf Joachim
- * @version $Revision$ $Date: 2009-07-13 17:22:43 (Tue, 28 Jul 2009) $
- */
-public abstract class AbstractNoKeyGenerator implements KeyGenerator {
- /**
- * {@inheritDoc}
- */
- public byte getStyle() {
- return NOGEN_INSERT;
- }
-}
Index: cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/KeyGenerator.java
===================================================================
--- cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/KeyGenerator.java (revision 8348)
+++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/KeyGenerator.java (working copy)
@@ -22,7 +22,6 @@
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.PersistenceException;
import org.exolab.castor.jdo.engine.SQLEngine;
-import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.persist.spi.Identity;
/**
@@ -42,21 +41,6 @@
* @version $Revision$ $Date: 2005-04-25 15:33:21 -0600 (Mon, 25 Apr 2005) $
*/
public interface KeyGenerator {
- /** For the key generators of BEFORE_INSERT style {@link #generateKey}
- * is called before INSERT. {@link #patchSQL} may be used but usually doesn't. */
- byte BEFORE_INSERT = -1;
-
- /** For the key generators of DURING_INSERT style {@link #generateKey}
- * is never called, all work is done by {@link #patchSQL}. */
- byte DURING_INSERT = 0;
-
- /** For the key generators of AFTER_INSERT style {@link #generateKey}
- * is called after INSERT. {@link #patchSQL} may be used but usually doesn't. */
- byte AFTER_INSERT = 1;
-
- /** For key generators that does not generate key before running insert query. */
- byte NOGEN_INSERT = 2;
-
/**
* Generate a new key for the specified table. This method is
* called when a new object is about to be created. In some
@@ -76,11 +60,6 @@
throws PersistenceException;
/**
- * Style of the key generator: BEFORE_INSERT, DURING_INSERT or AFTER_INSERT.
- */
- byte getStyle();
-
- /**
* Is key generated in the same connection as INSERT?
* For DURING_INSERT style this method is never called.
*/
Index: cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/NoKeyGenerator.java
===================================================================
--- cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/NoKeyGenerator.java (revision 8348)
+++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/NoKeyGenerator.java (working copy)
@@ -35,11 +35,9 @@
import org.exolab.castor.jdo.engine.SQLFieldInfo;
import org.exolab.castor.jdo.engine.nature.ClassDescriptorJDONature;
import org.exolab.castor.mapping.ClassDescriptor;
-import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.persist.spi.Identity;
import org.exolab.castor.persist.spi.PersistenceFactory;
-
/**
* Key generator implementation that does not generate key.
*
@@ -47,8 +45,9 @@
* @author Ralf Joachim
* @version $Revision: 8241 $ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
*/
-public final class NoKeyGenerator extends AbstractNoKeyGenerator {
+public final class NoKeyGenerator extends AbstractKeyGenerator {
//-----------------------------------------------------------------------------------
+
/** The Jakarta
* Commons Logging instance used for all logging. */
private static final Log LOG = LogFactory.getLog(NoKeyGenerator.class);
@@ -71,6 +70,8 @@
* and parameters binding. */
private final QueryContext _ctx;
+ //-----------------------------------------------------------------------------------
+
/**
* Constructor.
*
@@ -82,6 +83,8 @@
_ctx = new QueryContext(_factory);
}
+ //-----------------------------------------------------------------------------------
+
/**
* {@inheritDoc}
*/
@@ -98,14 +101,6 @@
final String primKeyName, final Properties props) throws PersistenceException {
return null;
}
-
- /**
- * {@inheritDoc}
- */
- public String patchSQL(final String insert, final String primKeyName)
- throws MappingException {
- return null;
- }
/**
* {@inheritDoc}
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 8348)
+++ cpa/src/main/java/org/castor/cpa/persistence/sql/keygen/SequenceDuringKeyGenerator.java (working copy)
@@ -45,12 +45,13 @@
/**
* SEQUENCE key generator.
+ *
* @author Oleg Nitz
* @author Bruce Snyder
* @version $Revision: 8241 $ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
* @see SequenceKeyGeneratorFactory
*/
-public final class SequenceDuringKeyGenerator extends AbstractDuringKeyGenerator {
+public final class SequenceDuringKeyGenerator extends AbstractKeyGenerator {
//-----------------------------------------------------------------------------------
private abstract class SequenceKeyGenValueHandler {
Index: src/doc/release-notes.xml
===================================================================
--- src/doc/release-notes.xml (revision 8348)
+++ src/doc/release-notes.xml (working copy)
@@ -91,6 +91,27 @@
]]>
+
+
+ Move common method to AbstractKeyGenerator and remove getStyle method from
+ KeyGenerator Interface.
+
+
+ Ahmad Hassan
+ ahmad.hassan@gmail.com
+
+
+ Ralf Joachim
+ ralf.joachim@syscon.eu
+
+
+ Ahmad Hassan
+ ahmad.hassan@gmail.com
+
+ Enh.
+ JDO
+ 20090810
+
Build statements with Insert class hierarchy where no special syntax is