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 tf...@apache.org on 2010/10/29 21:48:46 UTC

svn commit: r1028884 - in /db/torque/torque4/branches/trunk-without-village: torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java torque-test/src/test/java/org/apache/torque/DataTest.java

Author: tfischer
Date: Fri Oct 29 19:48:46 2010
New Revision: 1028884

URL: http://svn.apache.org/viewvc?rev=1028884&view=rev
Log:
reactivate BasePeer.doPSSelect and re-add test for this method

Modified:
    db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
    db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java

Modified: db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java?rev=1028884&r1=1028883&r2=1028884&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java Fri Oct 29 19:48:46 2010
@@ -1791,6 +1791,212 @@ public abstract class BasePeer
     }
 
     /**
+     * Performs a SQL <code>select</code> using a PreparedStatement.
+     *
+     * @param criteria A Criteria specifying the records to select, not null.
+     * @param mapper The mapper creating the objects from the resultSet,
+     *        not null.
+     * @param defaultTableMap The table map used for the
+     *        unqualified columns in the query, not null.
+     * @param connection the database connection for selecting records,
+     *        not null.
+     *
+     * @return The results of the query, not null.
+     * @throws TorqueException Error performing database query.
+     */
+    public static <T> List<T> doPSSelect(
+            Criteria criteria,
+            RecordMapper<T> mapper,
+            TableMap defaultTableMap,
+            Connection connection)
+        throws TorqueException
+    {
+        correctBooleans(criteria, defaultTableMap);
+
+        StringBuffer query = new StringBuffer();
+        List<Object> params = new ArrayList<Object>(criteria.size());
+
+        createPreparedStatement(criteria, query, params);
+
+        PreparedStatement statement = null;
+        ResultSet resultSet = null;
+        try
+        {
+            statement = connection.prepareStatement(query.toString());
+
+            for (int i = 0; i < params.size(); i++)
+            {
+                Object param = params.get(i);
+                if (param instanceof java.sql.Date)
+                {
+                    statement.setDate(i + 1, (java.sql.Date) param);
+                }
+                else if (param instanceof NumberKey)
+                {
+                    statement.setBigDecimal(i + 1,
+                        ((NumberKey) param).getBigDecimal());
+                }
+                else if (param instanceof Integer)
+                {
+                    statement.setInt(i + 1, ((Integer) param).intValue());
+                }
+                else
+                {
+                    statement.setString(i + 1, param.toString());
+                }
+            }
+
+            long startTime = System.currentTimeMillis();
+            log.debug("Executing query " + query + ", parameters = " + params);
+
+            resultSet = statement.executeQuery();
+            long queryEndTime = System.currentTimeMillis();
+            log.trace("query took " + (queryEndTime - startTime)
+                    + " milliseconds");
+
+            int offset;
+            Database database = Torque.getDatabase(criteria.getDbName());
+            if (database.getAdapter().supportsNativeOffset())
+            {
+                offset = 0; //database takes care of offset
+            }
+            else
+            {
+                offset = criteria.getOffset();
+            }
+
+            int limit;
+            if (database.getAdapter().supportsNativeLimit())
+            {
+                limit = -1; //database takes care of offset
+            }
+            else
+            {
+                if (database.getAdapter().supportsNativeOffset())
+                {
+                    limit = criteria.getLimit();
+                }
+                else
+                {
+                    if (criteria.getLimit() == -1 )
+                    {
+                        limit = criteria.getLimit();
+                    }
+                    else
+                    {
+                        limit = offset + criteria.getLimit();
+                    }
+                }
+            }
+
+            List<T> result = new ArrayList<T>();
+            int rowNumber = 0;
+            while (resultSet.next())
+            {
+                if (rowNumber < offset)
+                {
+                    rowNumber++;
+                    continue;
+                }
+                if (limit >= 0 && rowNumber >= limit)
+                {
+                    break;
+                }
+
+                T rowResult = mapper.processRow(resultSet, 0);
+                result.add(rowResult);
+
+                rowNumber++;
+            }
+            long mappingEndTime = System.currentTimeMillis();
+            log.trace("mapping took " + (mappingEndTime - queryEndTime)
+                    + " milliseconds");
+
+            if (criteria.isSingleRecord() && result.size() > 1)
+            {
+                throw new TooManyRowsException(
+                        "Criteria expected single Record and "
+                        + "Multiple Records were selected");
+            }
+            return result;
+        }
+        catch (SQLException e)
+        {
+            throw new TorqueException(e);
+        }
+        finally
+        {
+            if (resultSet != null)
+            {
+                try
+                {
+                    resultSet.close();
+                }
+                catch (SQLException e)
+                {
+                    log.warn("error closing resultSet", e);
+                }
+            }
+            if (statement != null)
+            {
+                try
+                {
+                    statement.close();
+                }
+                catch (SQLException e)
+                {
+                    log.warn("error closing statement", e);
+                }
+            }
+        }
+    }
+
+    /**
+     * Do a Prepared Statement select according to the given criteria.
+     *
+     * @param criteria A Criteria specifying the records to select, not null.
+     * @param mapper The mapper creating the objects from the resultSet,
+     *        not null.
+     * @param defaultTableMap The table map used for the
+     *        unqualified columns in the query, not null.
+     *
+     * @return The results of the query, not null.
+     *
+     * @throws TorqueException if a database error occurs.
+     */
+    public static <T> List<T> doPSSelect(
+                Criteria criteria,
+                RecordMapper<T> mapper,
+                TableMap defaultTableMap)
+            throws TorqueException
+    {
+        Connection connection = null;
+        try
+        {
+            connection = Transaction.beginOptional(
+                    criteria.getDbName(),
+                    criteria.isUseTransaction());
+
+            List<T> result = doPSSelect(
+                    criteria,
+                    mapper,
+                    defaultTableMap,
+                    connection);
+
+            Transaction.commit(connection);
+            connection = null;
+            return result;
+        }
+        finally
+        {
+            if (connection != null)
+            {
+                Transaction.safeRollback(connection);
+            }
+        }
+    }
+
+    /**
      * Create a new PreparedStatement.  It builds a string representation
      * of a query and a list of PreparedStatement parameters.
      *
@@ -1806,8 +2012,11 @@ public abstract class BasePeer
         List<Object> params)
         throws TorqueException
     {
-        Query query = SQLBuilder.buildQueryClause(criteria, params, new SQLBuilder.QueryCallback() {
-                public String process(Criteria.Criterion criterion, List<Object> params)
+        Query query = SQLBuilder.buildQueryClause(
+            criteria, params, new SQLBuilder.QueryCallback() {
+                public String process(
+                        Criteria.Criterion criterion,
+                        List<Object> params)
                 {
                     StringBuffer sb = new StringBuffer();
                     criterion.appendPsTo(sb, params);

Modified: db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java?rev=1028884&r1=1028883&r2=1028884&view=diff
==============================================================================
--- db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java (original)
+++ db/torque/torque4/branches/trunk-without-village/torque-test/src/test/java/org/apache/torque/DataTest.java Fri Oct 29 19:48:46 2010
@@ -1907,6 +1907,41 @@ public class DataTest extends BaseRuntim
 
 
     /**
+     * Test whether we can execute queries as prepared statements
+     * @throws Exception
+     */
+    public void testPreparedStatements() throws Exception
+    {
+        // clean LargePk table
+        Criteria criteria = new Criteria();
+        criteria.add(
+                LargePkPeer.LARGE_PK_ID,
+                (Long) null,
+                Criteria.NOT_EQUAL);
+        LargePkPeer.doDelete(criteria);
+
+        LargePk largePk = new LargePk();
+        largePk.setLargePkId(1);
+        largePk.setName("testLargePk");
+        largePk.save();
+
+        largePk = new LargePk();
+        largePk.setLargePkId(2);
+        largePk.setName("testLargePk");
+        largePk.save();
+
+        criteria = new Criteria();
+        criteria.add(LargePkPeer.LARGE_PK_ID, 2, Criteria.LESS_THAN);
+        LargePkPeer.addSelectColumns(criteria);
+        List<LargePk> result = BasePeer.doPSSelect(
+                criteria, new LargePkPeer.LargePkRecordMapper(),
+                LargePkPeer.getTableMap());
+        assertTrue("Size of largePk list should be 1 but is "
+                + result.size(),
+                result.size() == 1);
+    }
+
+    /**
      * Test whether equals() is working correctly
      * @throws Exception
      */



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