db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1305150 - in /db/torque/torque4/trunk/torque-runtime: ./ src/main/java/org/apache/torque/criteria/ src/main/java/org/apache/torque/sql/ src/main/java/org/apache/torque/util/ src/test/java/org/apache/torque/util/
Date Sun, 25 Mar 2012 21:38:04 GMT
Author: tfischer
Date: Sun Mar 25 21:38:04 2012
New Revision: 1305150

URL: http://svn.apache.org/viewvc?rev=1305150&view=rev
Log:
TORQUE-141: added a way to set the fetchSize on a query

Added:
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java
Modified:
    db/torque/torque4/trunk/torque-runtime/pom.xml
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java

Modified: db/torque/torque4/trunk/torque-runtime/pom.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/pom.xml?rev=1305150&r1=1305149&r2=1305150&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/pom.xml (original)
+++ db/torque/torque4/trunk/torque-runtime/pom.xml Sun Mar 25 21:38:04 2012
@@ -111,6 +111,12 @@
       <scope>test</scope>
     </dependency>
     
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <version>1.9.0</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java?rev=1305150&r1=1305149&r2=1305150&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java
(original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/criteria/Criteria.java
Sun Mar 25 21:38:04 2012
@@ -183,6 +183,9 @@ public class Criteria
      */
     private Map<String, String> aliases = new HashMap<String, String>();
 
+    /** The JDBC statement fetch size, if any. */
+    private Integer fetchSize;
+
     private boolean useTransaction = false;
 
     /**
@@ -625,6 +628,26 @@ public class Criteria
     }
 
     /**
+     * Returns the JDBC statement fetch size to use for queries.
+     *
+     * @return the fetch size, or null if none is set.
+     */
+    public Integer getFetchSize()
+    {
+        return fetchSize;
+    }
+
+    /**
+     * Sets the JDBC statement fetch size to use for queries.
+     *
+     * @param fetchSize the fetch size, or null for not set.
+     */
+    public void setFetchSize(Integer fetchSize)
+    {
+        this.fetchSize = fetchSize;
+    }
+
+    /**
      * Adds a select column to the Criteria.
      *
      * @param column The select column to add.
@@ -895,6 +918,7 @@ public class Criteria
         equalsBuilder.append(criteria.aliases, this.aliases);
         equalsBuilder.append(criteria.asColumns, this.asColumns);
         equalsBuilder.append(criteria.joins, this.joins);
+        equalsBuilder.append(criteria.fetchSize, this.fetchSize);
         return equalsBuilder.isEquals();
     }
 
@@ -918,6 +942,7 @@ public class Criteria
         hashCodeBuilder.append(aliases);
         hashCodeBuilder.append(asColumns);
         hashCodeBuilder.append(joins);
+        hashCodeBuilder.append(fetchSize);
         return hashCodeBuilder.toHashCode();
     }
 

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java?rev=1305150&r1=1305149&r2=1305150&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
(original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/Query.java
Sun Mar 25 21:38:04 2012
@@ -154,6 +154,9 @@ public class Query
     /** The type of the statement. */
     private Type type = Type.SELECT;
 
+    /** The JDBC statement fetch size, if any. */
+    private Integer fetchSize;
+
     /**
      * Retrieve the modifier buffer in order to add modifiers to this
      * query.  E.g. DISTINCT and ALL.
@@ -405,6 +408,26 @@ public class Query
     }
 
     /**
+     * Returns the JDBC statement fetch size to use for queries.
+     *
+     * @return the fetch size, or null if none is set.
+     */
+    public Integer getFetchSize()
+    {
+        return fetchSize;
+    }
+
+    /**
+     * Sets the JDBC statement fetch size to use for queries.
+     *
+     * @param fetchSize the fetch size, or null for not set.
+     */
+    public void setFetchSize(Integer fetchSize)
+    {
+        this.fetchSize = fetchSize;
+    }
+
+    /**
      * Outputs the query statement.
      *
      * @return A String with the query statement.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java?rev=1305150&r1=1305149&r2=1305150&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
(original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
Sun Mar 25 21:38:04 2012
@@ -136,6 +136,7 @@ public final class SqlBuilder
         processHaving(crit, sqlStatement);
         processOrderBy(db, database, crit, sqlStatement);
         processLimits(crit, sqlStatement);
+        sqlStatement.setFetchSize(crit.getFetchSize());
 
         return sqlStatement;
     }

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java?rev=1305150&r1=1305149&r2=1305150&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
(original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
Sun Mar 25 21:38:04 2012
@@ -1320,6 +1320,10 @@ public class BasePeerImpl implements Ser
         try
         {
             statement = connection.prepareStatement(query.toString());
+            if (query.getFetchSize() != null)
+            {
+                statement.setFetchSize(query.getFetchSize());
+            }
 
             List<Object> replacements = setPreparedStatementReplacements(
                     statement,

Added: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java?rev=1305150&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java
(added)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/util/BasePeerImplTest.java
Sun Mar 25 21:38:04 2012
@@ -0,0 +1,131 @@
+package org.apache.torque.util;
+
+/*
+ * 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 static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.List;
+
+import org.apache.torque.BaseTestCase;
+import org.apache.torque.criteria.Criteria;
+import org.apache.torque.om.mapper.CompositeMapper;
+import org.apache.torque.om.mapper.IntegerMapper;
+import org.apache.torque.om.mapper.StringMapper;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/**
+ * Tests the class BasePeerImpl.
+ *
+ * @version $Id: $
+ */
+public class BasePeerImplTest extends BaseTestCase
+{
+    /** System under test. */
+    private BasePeerImpl basePeerImpl;
+
+    /** Mock db connection. */
+    @Mock
+    private Connection connection;
+
+    /** Mock prepared statement. */
+    @Mock
+    private PreparedStatement preparedStatement;
+
+    /** Mock result set. */
+    @Mock
+    private ResultSet resultSet;
+
+    @Override
+    public void setUp() throws Exception
+    {
+        basePeerImpl = new BasePeerImpl();
+        super.setUp();
+        MockitoAnnotations.initMocks(this);
+        when(connection.prepareStatement((String) any()))
+                .thenReturn(preparedStatement);
+        when(preparedStatement.executeQuery()).thenReturn(resultSet);
+    }
+
+    /**
+     * Check that the fetch size gets set on the prepared statement when
+     * set in the criteria.
+     *
+     * @throws Exception if the test fails.
+     */
+    public void testDoSelectAll() throws Exception
+    {
+        Criteria criteria = new Criteria();
+        criteria.addSelectColumn(stringColumnMap);
+        criteria.addSelectColumn(integerColumnMap);
+        // single result "fd", 23 found
+        when(resultSet.next()).thenReturn(true, false);
+        when(resultSet.getString(1)).thenReturn("fd");
+        when(resultSet.getInt(2)).thenReturn(23);
+
+        CompositeMapper mapper = new CompositeMapper();
+        mapper.addMapper(new StringMapper(), 0);
+        mapper.addMapper(new IntegerMapper(), 1);
+
+        List<List<Object>> result = basePeerImpl.doSelect(
+                criteria, mapper, tableMap, connection);
+
+        // verify mock (verfication order not relevant)
+        verify(connection).prepareStatement(
+                "SELECT TABLE.COLUMN1, TABLE.COLUMN4 FROM TABLE");
+        verify(preparedStatement).executeQuery();
+        verify(preparedStatement).close();
+        verify(resultSet, times(2)).next();
+        verify(resultSet).getString(1);
+        verify(resultSet).getInt(2);
+        verify(resultSet).close();
+        verifyNoMoreInteractions(connection, preparedStatement, resultSet);
+        // verify result
+        assertEquals(1, result.size()); // one rwow
+        assertEquals(2, result.get(0).size()); // two columns
+        assertEquals("fd", result.get(0).get(0));
+        assertEquals(23, result.get(0).get(1));
+    }
+
+    /**
+     * Check that the fetch size gets set on the prepared statement when
+     * set in the criteria.
+     *
+     * @throws Exception if the test fails.
+     */
+    public void testDoSelectAllFetchSize() throws Exception
+    {
+        Criteria criteria = new Criteria();
+        criteria.setFetchSize(13);
+        // no results found
+        when(resultSet.next()).thenReturn(false);
+
+        basePeerImpl.doSelect(
+                criteria, new IntegerMapper(), tableMap, connection);
+        verify(preparedStatement).setFetchSize(13);
+    }
+}



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


Mime
View raw message