[jboss-cvs] JBossAS SVN: r63032 - trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon May 14 07:09:17 EDT 2007


Author: alex.loubyansky at jboss.com
Date: 2007-05-14 07:09:16 -0400 (Mon, 14 May 2007)
New Revision: 63032

Modified:
   trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/AbstractQueryCommand.java
   trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/DynamicQueryCommand.java
   trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/JBossQLQueryCommand.java
Log:
JBAS-4408

Modified: trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/AbstractQueryCommand.java
===================================================================
--- trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/AbstractQueryCommand.java	2007-05-14 11:08:32 UTC (rev 63031)
+++ trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/AbstractQueryCommand.java	2007-05-14 11:09:16 UTC (rev 63032)
@@ -72,6 +72,10 @@
    private CollectionFactory collectionFactory;
    private CollectionStrategy collectionStrategy;
    private ResultReader resultReader;
+   private int offsetParam;
+   private int offsetValue;
+   private int limitParam;
+   private int limitValue;
 
    // Protected
 
@@ -125,7 +129,9 @@
    public Collection fetchCollection(Schema schema, GenericEntityObjectFactory factory, Object[] args)
       throws FinderException
    {
-      return fetchCollection(entity, sql, params, collectionStrategy, schema, factory, args, log);
+      int offset = toInt(args, offsetParam, offsetValue);
+      int limit = toInt(args, limitParam, limitValue);
+      return fetchCollection(entity, sql, params, offset, limit, collectionStrategy, schema, factory, args, log);
    }
 
    public Object fetchOne(Schema schema, GenericEntityObjectFactory factory, Object[] args) throws FinderException
@@ -134,8 +140,38 @@
       return executeFetchOne(args, factory);
    }
 
+   public void setOffsetValue(int offsetValue)
+   {
+      this.offsetValue = offsetValue;
+   }
+
+   public void setLimitValue(int limitValue)
+   {
+      this.limitValue = limitValue;
+   }
+
+   public void setOffsetParam(int offsetParam)
+   {
+      this.offsetParam = offsetParam;
+   }
+
+   public void setLimitParam(int limitParam)
+   {
+      this.limitParam = limitParam;
+   }
+
    // Protected
 
+   protected static int toInt(Object[] params, int paramNumber, int defaultValue)
+   {
+      if(paramNumber == 0)
+      {
+         return defaultValue;
+      }
+      Integer arg = (Integer) params[paramNumber - 1];
+      return arg.intValue();
+   }
+
    protected Object executeFetchOne(Object[] args, GenericEntityObjectFactory factory) throws FinderException
    {
       return fetchOne(entity, sql, params, resultReader, args, factory, log);
@@ -144,6 +180,8 @@
    static Collection fetchCollection(JDBCEntityBridge2 entity,
                                      String sql,
                                      QueryParameter[] params,
+                                     int offset,
+                                     int limit,
                                      CollectionStrategy collectionStrategy,
                                      Schema schema,
                                      GenericEntityObjectFactory factory,
@@ -153,6 +191,7 @@
    {
       schema.flush();
 
+      int count = offset;
       Collection result;
 
       Connection con = null;
@@ -198,6 +237,14 @@
          }
 
          rs = ps.executeQuery();
+
+         // skip 'offset' results
+         while(count > 0 && rs.next())
+         {
+            count--;
+         }
+
+         count = limit;
       }
       catch(Exception e)
       {
@@ -211,7 +258,7 @@
          throw fe;
       }
 
-      result = collectionStrategy.readResultSet(con, ps, rs, factory);
+      result = collectionStrategy.readResultSet(con, ps, rs, limit, count, factory);
 
       return result;
    }
@@ -393,7 +440,7 @@
 
    interface CollectionStrategy
    {
-      Collection readResultSet(Connection con, PreparedStatement ps, ResultSet rs, GenericEntityObjectFactory factory)
+      Collection readResultSet(Connection con, PreparedStatement ps, ResultSet rs, int limit, int count, GenericEntityObjectFactory factory)
          throws FinderException;
    }
 
@@ -415,18 +462,19 @@
       public Collection readResultSet(Connection con,
                                       PreparedStatement ps,
                                       ResultSet rs,
+                                      int limit, int count,
                                       GenericEntityObjectFactory factory)
          throws FinderException
       {
          Collection result;
          try
          {
-            if(rs.next())
+            if((limit == 0 || count-- > 0) && rs.next())
             {
                result = collectionFactory.newCollection();
                Object instance = resultReader.readRow(rs, factory);
                result.add(instance);
-               while(rs.next())
+               while((limit == 0 || count-- > 0) && rs.next())
                {
                   instance = resultReader.readRow(rs, factory);
                   result.add(instance);

Modified: trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/DynamicQueryCommand.java
===================================================================
--- trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/DynamicQueryCommand.java	2007-05-14 11:08:32 UTC (rev 63031)
+++ trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/DynamicQueryCommand.java	2007-05-14 11:09:16 UTC (rev 63032)
@@ -99,6 +99,11 @@
 
       String sql = compiler.getSQL();
 
+      int offsetParam = compiler.getOffsetParam();
+      int offsetValue = compiler.getOffsetValue();
+      int limitParam = compiler.getLimitParam();
+      int limitValue = compiler.getLimitValue();
+
       AbstractQueryCommand.ResultReader resultReader;
       if(!compiler.isSelectEntity())
       {
@@ -118,6 +123,7 @@
 
       return AbstractQueryCommand.fetchCollection(
          entity, sql, toArray(compiler.getInputParameters()),
+         AbstractQueryCommand.toInt(args, offsetParam, offsetValue), AbstractQueryCommand.toInt(args, limitParam, limitValue),
          new AbstractQueryCommand.EagerCollectionStrategy(collectionFactory, resultReader, log),
          schema, factory, (Object[])args[1], log);
    }

Modified: trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/JBossQLQueryCommand.java
===================================================================
--- trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/JBossQLQueryCommand.java	2007-05-14 11:08:32 UTC (rev 63031)
+++ trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/JBossQLQueryCommand.java	2007-05-14 11:09:16 UTC (rev 63032)
@@ -59,6 +59,11 @@
 
       sql = compiler.getSQL();
 
+      setOffsetParam(compiler.getOffsetParam());
+      setOffsetValue(compiler.getOffsetValue());
+      setLimitParam(compiler.getLimitParam());
+      setLimitValue(compiler.getLimitValue());
+
       log = Logger.getLogger(getClass().getName() + "." + entity.getEntityName() + "#" + metadata.getMethod().getName());
       log.debug("sql: " + sql);
 




More information about the jboss-cvs-commits mailing list