On Fri, Jul 02, 2004 at 03:46:54PM +1000, Scott Eade wrote:
> >I've made a little method which I use in an object including
> >LargeSelect, which can do this. It accesses the class which name
> >is given in parameter to the LargeSelect constructor, sends
> >a SELECT COUNT() thing and gets the result. 
> >
> >I wonder if it would be useful for others to include it in
> >LargeSelect.
> > 
> >
> Provide the method and we will see about including it.

Ok, here is a patch for LargeSelect.java in the Torque 3.1
source tree.

It provides a public int getTotalEstimatedRecords()
method, which can be called once the LargeSelect is
instantiated.

It works here, but I only tested it against a DB2, so
of course it needs to be tested with other DBMS.

Regards,

--- torque-3.1/src/java/org/apache/torque/util/LargeSelect.java 2003-09-04 
14:52:26.000000000 +0900
+++ torque-3.1.vido1/src/java/org/apache/torque/util/LargeSelect.java   2004-07-02 
19:25:50.000000000 +0900
@@ -63,6 +63,8 @@
 import java.util.Hashtable;
 import java.util.Set;
 import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
 import org.apache.commons.logging.Log;
@@ -73,6 +75,7 @@
 
 import com.workingdogs.village.QueryDataSet;
 import com.workingdogs.village.DataSetException;
+import com.workingdogs.village.Record;
 
 /**
  * This class can be used to retrieve a large result set from a database query.
@@ -169,6 +172,7 @@
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">John D. McNally</a>
  * @author <a href="mailto:[EMAIL PROTECTED]">Scott Eade</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Augustin Vidovic</a>
  * @version $Id: LargeSelect.java,v 1.13 2003/08/25 16:33:23 henning Exp $
  */
 public class LargeSelect implements Runnable, Serializable
@@ -874,6 +878,48 @@
     }
 
     /**
+     * Retrieve the total estimated number of results for this query.
+     * This method queries the DB, so if you are confident that the result
+     * will not vary during your use of this LargeSelect instance, you
+     * should call it once and keep the result, in order to spare DB
+     * connectivity usage.
+     *
+     * @return total estimated number of results
+     * @throws DataSetException
+     * @throws IllegalAccessException
+     * @throws InstantiationException
+     * @throws InvocationTargetException
+     * @throws NoSuchMethodException
+     * @throws TorqueException
+     */
+     public int getTotalEstimatedRecords() throws DataSetException, 
IllegalAccessException, InstantiationException, InvocationTargetException, 
NoSuchMethodException, TorqueException
+     {
+         Criteria c=new Criteria();
+         c.putAll(criteria);
+         for (Iterator i=c.getSelectColumns().iterator();i.hasNext();) {
+            c.remove((String)(i.next()));
+         }
+         Field[] fields = returnBuilderClass.getFields();
+         int l = fields.length;
+         for (int i=0; i<l; i++) {
+            Field f = fields[i];
+            if (f.getType()==String.class) {
+               String s=(String)(f.get(String.class));
+               if (s.indexOf(".")>0) {
+                   c.addSelectColumn("COUNT("+s+")");
+                   break;
+               }
+            }
+         }
+         Class[] argTypes = {Criteria.class};
+         Method method = 
returnBuilderClass.getMethod("doSelectVillageRecords",argTypes);
+         Object[] args = {c};
+         List result = (List)(method.invoke(returnBuilderClass.newInstance(),args));
+         Record record = (Record)(result.get(0));
+         return record.getValue(1).asInt();
+     }
+
+    /**
      * Retrieve the number of the current page.
      *
      * @return the current page number.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to