You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tv...@apache.org on 2012/08/26 20:42:31 UTC

svn commit: r1377476 - in /db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid: AbstractIdGenerator.java AutoIncrementIdGenerator.java SequenceIdGenerator.java

Author: tv
Date: Sun Aug 26 18:42:31 2012
New Revision: 1377476

URL: http://svn.apache.org/viewvc?rev=1377476&view=rev
Log:
Less code is less bugs...

Added:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AbstractIdGenerator.java   (with props)
Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java

Added: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AbstractIdGenerator.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AbstractIdGenerator.java?rev=1377476&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AbstractIdGenerator.java (added)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AbstractIdGenerator.java Sun Aug 26 18:42:31 2012
@@ -0,0 +1,179 @@
+package org.apache.torque.oid;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.util.List;
+
+import org.apache.torque.TorqueException;
+import org.apache.torque.adapter.Adapter;
+import org.apache.torque.om.mapper.BigDecimalMapper;
+import org.apache.torque.om.mapper.IntegerMapper;
+import org.apache.torque.om.mapper.LongMapper;
+import org.apache.torque.om.mapper.RecordMapper;
+import org.apache.torque.om.mapper.StringMapper;
+import org.apache.torque.sql.SqlBuilder;
+import org.apache.torque.util.BasePeerImpl;
+
+/**
+ * This class serves as a common base class for the sequence-based and the
+ * autoincrement-based id generators
+ *
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
+ * @version $Id: AutoIncrementIdGenerator.java 1355228 2012-06-29 03:38:08Z tfischer $
+ */
+public abstract class AbstractIdGenerator implements IdGenerator
+{
+    /** The adapter that knows the correct sql syntax */
+    protected Adapter adapter;
+
+    /**
+     * The internal name of the Database that this Generator is connected to.
+     */
+    protected String databaseName = null;
+
+    /**
+     * Creates an IdGenerator which will work with the specified database.
+     *
+     * @param adapter the adapter that knows the correct sql syntax.
+     * @param databaseName The name of the databaseName to find the correct
+     *        schema.
+     */
+    public AbstractIdGenerator(final Adapter adapter, final String databaseName)
+    {
+        this.adapter = adapter;
+        this.databaseName = databaseName;
+    }
+
+    /**
+     * Returns the last ID used by this connection.
+     *
+     * @param connection The database connection to read the new id, not null.
+     * @param keyInfo the name of the table for which the id is retrieved.
+     *
+     * @return An int with the new id.
+     *
+     * @throws TorqueException if a database error occurs.
+     */
+    public int getIdAsInt(Connection connection, Object keyInfo)
+        throws TorqueException
+    {
+        return getId(connection, keyInfo, new IntegerMapper());
+    }
+
+    /**
+     * Returns the last ID used by this connection.
+     *
+     * @param connection The database connection to read the new id, not null.
+     * @param keyInfo the name of the table for which the id is retrieved.
+     *
+     * @return A long with the new id.
+     *
+     * @throws TorqueException if a database error occurs.
+     */
+    public long getIdAsLong(Connection connection, Object keyInfo)
+        throws TorqueException
+    {
+        return getId(connection, keyInfo, new LongMapper());
+    }
+
+    /**
+     * Returns the last ID used by this connection.
+     *
+     * @param connection The database connection to read the new id, not null.
+     * @param keyInfo the name of the table for which the id is retrieved.
+     *
+     * @return A BigDecimal with the new id.
+     *
+     * @throws TorqueException if a database error occurs.
+     */
+    public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo)
+        throws TorqueException
+    {
+        return getId(connection, keyInfo, new BigDecimalMapper());
+    }
+
+
+    /**
+     * Returns the last ID used by this connection.
+     *
+     * @param connection The database connection to read the new id, not null.
+     * @param keyInfo the name of the table for which the id is retrieved.
+     *
+     * @return A String with the new id.
+     *
+     * @throws TorqueException if a database error occurs.
+     */
+    public String getIdAsString(Connection connection, Object keyInfo)
+        throws TorqueException
+    {
+        return getId(connection, keyInfo, new StringMapper());
+    }
+
+    /**
+     * A flag to determine the timing of the id generation
+     *
+     * @return a <code>boolean</code> value
+     */
+    public abstract boolean isPriorToInsert();
+
+    /**
+     * A flag to determine the timing of the id generation
+     *
+     * @return a <code>boolean</code> value
+     */
+    public abstract boolean isPostInsert();
+
+    /**
+     * A flag to determine whether a Connection is required to
+     * generate an id.
+     *
+     * @return a <code>boolean</code> value
+     */
+    public abstract boolean isConnectionRequired();
+
+    /**
+     * Returns the last ID used by this connection.
+     *
+     * @param connection A Connection.
+     * @param keyInfo an Object that contains additional info.
+     * @param mapper The RecordMapper that maps from a ResultSet to the
+     *        appropriate java object.
+     *
+     * @return The generated id.
+     * @exception TorqueException if a database error occurs.
+     */
+    protected <T> T getId(
+                Connection connection,
+                Object keyInfo,
+                RecordMapper<T> mapper)
+            throws TorqueException
+    {
+        String tableName = SqlBuilder.getFullTableName(
+                String.valueOf(keyInfo),
+                databaseName);
+        String idSql = adapter.getIDMethodSQL(tableName);
+
+        BasePeerImpl<T> peer = new BasePeerImpl<T>(mapper, null, databaseName);
+        List<T> result = peer.doSelect(idSql, connection);
+        return result.get(0);
+    }
+}

Propchange: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AbstractIdGenerator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java?rev=1377476&r1=1377475&r2=1377476&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/AutoIncrementIdGenerator.java Sun Aug 26 18:42:31 2012
@@ -19,19 +19,7 @@ package org.apache.torque.oid;
  * under the License.
  */
 
-import java.math.BigDecimal;
-import java.sql.Connection;
-import java.util.List;
-
-import org.apache.torque.TorqueException;
 import org.apache.torque.adapter.Adapter;
-import org.apache.torque.om.mapper.BigDecimalMapper;
-import org.apache.torque.om.mapper.IntegerMapper;
-import org.apache.torque.om.mapper.LongMapper;
-import org.apache.torque.om.mapper.RecordMapper;
-import org.apache.torque.om.mapper.StringMapper;
-import org.apache.torque.sql.SqlBuilder;
-import org.apache.torque.util.BasePeerImpl;
 
 /**
  * This generator works with databases that have an sql syntax that
@@ -41,16 +29,8 @@ import org.apache.torque.util.BasePeerIm
  * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
  * @version $Id$
  */
-public class AutoIncrementIdGenerator implements IdGenerator
+public class AutoIncrementIdGenerator extends AbstractIdGenerator
 {
-    /** The adapter that knows the correct sql syntax */
-    private final Adapter adapter;
-
-    /**
-     * The internal name of the Database that this Generator is connected to.
-     */
-    private String databaseName = null;
-
     /**
      * Creates an IdGenerator which will work with the specified database.
      *
@@ -60,77 +40,7 @@ public class AutoIncrementIdGenerator im
      */
     public AutoIncrementIdGenerator(final Adapter adapter, final String databaseName)
     {
-        this.adapter = adapter;
-        this.databaseName = databaseName;
-    }
-
-    /**
-     * Returns the last ID used by this connection.
-     *
-     * @param connection The database connection to read the sequence, not null.
-     * @param keyInfo the name of the table on which the last insert occured.
-     *
-     * @return An int with the last value auto-incremented as a
-     *         result of an insert.
-     *
-     * @throws TorqueException if a database error occurs.
-     */
-    public int getIdAsInt(Connection connection, Object keyInfo)
-        throws TorqueException
-    {
-        return getId(connection, keyInfo, new IntegerMapper());
-    }
-
-    /**
-     * Returns the last ID used by this connection.
-     *
-     * @param connection The database connection to read the sequence, not null.
-     * @param keyInfo the name of the table on which the last insert occured.
-     *
-     * @return A long with the last value auto-incremented as a
-     *         result of an insert.
-     *
-     * @throws TorqueException if a database error occurs.
-     */
-    public long getIdAsLong(Connection connection, Object keyInfo)
-        throws TorqueException
-    {
-        return getId(connection, keyInfo, new LongMapper());
-    }
-
-    /**
-     * Returns the last ID used by this connection.
-     *
-     * @param connection The database connection to read the sequence, not null.
-     * @param keyInfo the name of the table on which the last insert occured.
-     *
-     * @return A BigDecimal with the last value auto-incremented as a
-     *         result of an insert.
-     *
-     * @throws TorqueException if a database error occurs.
-     */
-    public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo)
-        throws TorqueException
-    {
-        return getId(connection, keyInfo, new BigDecimalMapper());
-    }
-
-
-    /**
-     * Returns the last ID used by this connection.
-     *
-     * @param connection The database connection to read the sequence, not null.
-     * @param keyInfo the name of the table on which the last insert occured.
-     *
-     * @return A String with the last value auto-incremented as a
-     *         result of an insert.
-     *
-     * @throws TorqueException if a database error occurs.
-     */
-    public String getIdAsString(Connection connection, Object keyInfo)
-        throws TorqueException
-    {
-        return getId(connection, keyInfo, new StringMapper());
+        super(adapter, databaseName);
     }
 
     /**
@@ -163,33 +73,4 @@ public class AutoIncrementIdGenerator im
     {
         return true;
     }
-
-    /**
-     * Returns the last ID used by this connection.
-     *
-     * @param connection A Connection.
-     * @param keyInfo an Object that contains additional info.
-     * @param mapper The RecordMapper that maps from a ResultSet to the
-     *        appropriate java object.
-     *
-     * @return The generated id.
-     * @exception TorqueException if a database error occurs.
-     */
-    private <T> T getId(
-                Connection connection,
-                Object keyInfo,
-                RecordMapper<T> mapper)
-            throws TorqueException
-    {
-        String tableName = SqlBuilder.getFullTableName(
-                String.valueOf(keyInfo),
-                databaseName);
-        String idSql = adapter.getIDMethodSQL(tableName);
-
-        List<T> result = new BasePeerImpl<Object>().doSelect(
-                idSql,
-                mapper,
-                connection);
-        return result.get(0);
-    }
 }

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java?rev=1377476&r1=1377475&r2=1377476&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/SequenceIdGenerator.java Sun Aug 26 18:42:31 2012
@@ -19,19 +19,7 @@ package org.apache.torque.oid;
  * under the License.
  */
 
-import java.math.BigDecimal;
-import java.sql.Connection;
-import java.util.List;
-
-import org.apache.torque.TorqueException;
 import org.apache.torque.adapter.Adapter;
-import org.apache.torque.om.mapper.BigDecimalMapper;
-import org.apache.torque.om.mapper.IntegerMapper;
-import org.apache.torque.om.mapper.LongMapper;
-import org.apache.torque.om.mapper.RecordMapper;
-import org.apache.torque.om.mapper.StringMapper;
-import org.apache.torque.sql.SqlBuilder;
-import org.apache.torque.util.BasePeerImpl;
 
 /**
  * This generator works with databases that have an sql syntax for
@@ -40,14 +28,8 @@ import org.apache.torque.util.BasePeerIm
  * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
  * @version $Id$
  */
-public class SequenceIdGenerator implements IdGenerator
+public class SequenceIdGenerator extends AbstractIdGenerator
 {
-    /** the adapter that knows the correct sql syntax */
-    private final Adapter adapter;
-
-    /** The internal name of the Database that this Generator is connected to */
-    private String databaseName = null;
-
     /**
      * Creates an IdGenerator which will work with the specified database.
      *
@@ -57,72 +39,7 @@ public class SequenceIdGenerator impleme
      */
     public SequenceIdGenerator(final Adapter adapter, final String databaseName)
     {
-        this.adapter = adapter;
-        this.databaseName = databaseName;
-    }
-
-    /**
-     * Retrieves an id as an int.
-     *
-     * @param connection The database connection to read the sequence, not null.
-     * @param keyInfo the name of the sequence, not null.
-     *
-     * @return An int with the value for the id.
-     *
-     * @throws TorqueException if a database error occurs.
-     */
-    public int getIdAsInt(Connection connection, Object keyInfo)
-        throws TorqueException
-    {
-        return getId(connection, keyInfo, new IntegerMapper());
-    }
-
-    /**
-     * Retrieves an id as an long.
-     *
-     * @param connection The database connection to read the sequence, not null.
-     * @param keyInfo the name of the sequence, not null.
-     *
-     * @return A long with the value for the id.
-     *
-     * @throws TorqueException if a database error occurs.
-     */
-    public long getIdAsLong(Connection connection, Object keyInfo)
-        throws TorqueException
-    {
-        return getId(connection, keyInfo, new LongMapper());
-    }
-
-    /**
-     * Retrieves an id as a BigDecimal.
-     *
-     * @param connection The database connection to read the sequence, not null.
-     * @param keyInfo the name of the sequence, not null.
-     *
-     * @return A BigDecimal id.
-     *
-     * @exception TorqueException if a database error occurs.
-     */
-    public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo)
-        throws TorqueException
-    {
-        return getId(connection, keyInfo, new BigDecimalMapper());
-    }
-
-    /**
-     * Retrieves an id as an String.
-     *
-     * @param connection The database connection to read the sequence, not null.
-     * @param keyInfo the name of the sequence, not null.
-     *
-     * @return A String id.
-     *
-     * @throws TorqueException if a database error occurs.
-     */
-    public String getIdAsString(Connection connection, Object keyInfo)
-        throws TorqueException
-    {
-        return getId(connection, keyInfo, new StringMapper());
+        super(adapter, databaseName);
     }
 
     /**
@@ -155,34 +72,4 @@ public class SequenceIdGenerator impleme
     {
         return true;
     }
-
-    /**
-     * Retrieves an id from a sequence.
-     *
-     * @param connection A Connection.
-     * @param keyInfo an Object that contains additional info.
-     * @param mapper The RecordMapper that maps from a ResultSet to the
-     *        appropriate java object.
-     *
-     * @return The generated id.
-     * @throws TorqueException if a database error occurs.
-     */
-    private <T> T getId(
-                Connection connection,
-                Object keyInfo,
-                RecordMapper<T> mapper)
-            throws TorqueException
-    {
-        String sequenceName
-                = SqlBuilder.getFullTableName(
-                        String.valueOf(keyInfo),
-                        databaseName);
-        String idSql = adapter.getIDMethodSQL(sequenceName);
-
-        List<T> result = new BasePeerImpl<Object>().doSelect(
-                idSql,
-                mapper,
-                connection);
-        return result.get(0);
-    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org