Author: tfischer
Date: Thu Aug 16 13:34:35 2012
New Revision: 1373827

URL: http://svn.apache.org/viewvc?rev=1373827&view=rev
Log:
TORQUE-218: Enhance Derby adapter to use limit/offset functionality
Thanks to T. Schmidt for the patch

Added:
    
db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/other/database-howtos/derby-howto.xml
Modified:
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java
    
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DerbyAdapter.java
    db/torque/torque4/trunk/torque-site/src/site/site.xml
    
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java?rev=1373827&r1=1373826&r2=1373827&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/AbstractAdapter.java
 Thu Aug 16 13:34:35 2012
@@ -154,8 +154,8 @@ public abstract class AbstractAdapter im
     }
 
     /**
-     * This method is used to check whether the database natively
-     * supports limiting the size of the resultset.
+     * Returns whether the database can natively limit the size of the 
ResultSet
+     * of a query.
      *
      * @return true if the database natively supports limiting the
      *         size of the resultset.
@@ -166,9 +166,8 @@ public abstract class AbstractAdapter im
     }
 
     /**
-     * This method is used to check whether the database natively
-     * supports returning results starting at an offset position other
-     * than 0.
+     * Returns whether the database natively supports returning results
+     * starting at an offset position other than 0.
      *
      * @return true if the database natively supports returning
      *         results starting at an offset position other than 0.

Modified: 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DerbyAdapter.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DerbyAdapter.java?rev=1373827&r1=1373826&r2=1373827&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DerbyAdapter.java
 (original)
+++ 
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DerbyAdapter.java
 Thu Aug 16 13:34:35 2012
@@ -25,12 +25,13 @@ import java.sql.Statement;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.torque.sql.Query;
 
 /**
  * This is used to connect to an embedded Apache Derby Database using
  * the supplied JDBC driver.
  *
- * @author <a href="mailto:h...@intermeta.de";>Henning P. Schmiedehausen</a>
+ * @author <a href="mailto:h...@intermeta.de";>Henning P. Schmiedehausen</a>, 
tins
  * @version $Id$
  */
 public class DerbyAdapter extends AbstractAdapter
@@ -183,4 +184,62 @@ public class DerbyAdapter extends Abstra
     {
         return true;
     }
+
+    /**
+     * Returns whether the database can natively limit the size of the 
ResultSet
+     * of a query.
+     * Limit is supported since Derby 10.5.1.1.
+     *
+     * @return true.
+     */
+    @Override
+    public boolean supportsNativeLimit()
+    {
+        return true;
+    }
+
+    /**
+     * Returns whether the database natively supports returning results
+     * starting at an offset position other than 0.
+     * Offset is supported since Derby 10.5.1.1.
+     *
+     * @return true.
+     */
+    @Override
+    public boolean supportsNativeOffset()
+    {
+        return true;
+    }
+
+    /**
+     * Build Derby-style query with limit or offset.
+     * The resulting query may look like this:
+     * <pre>
+     *  select * from TABLENAME fetch next 3 rows only;
+     *  select * from TABLENAME offset 3 rows fetch next 3 rows only;
+     * </pre>
+     *
+     * @param query The query to modify.
+     * @param offset the offset value.
+     * @param limit the limit value.
+     */
+    @Override
+    public void generateLimits(Query query, long offset, int limit)
+    {
+        StringBuilder postLimit = new StringBuilder();
+
+        if (offset > 0)
+        {
+            postLimit.append(" OFFSET ").append(offset).append(" ROWS");
+        }
+
+        if (limit >= 0)
+        {
+            postLimit.append(" FETCH NEXT ").append(limit).append(" ROWS 
ONLY");
+        }
+
+        query.setPostLimit(postLimit.toString());
+        query.setLimit(null);
+        query.setOffset(null);
+    }
 }

Modified: db/torque/torque4/trunk/torque-site/src/site/site.xml
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/site.xml?rev=1373827&r1=1373826&r2=1373827&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-site/src/site/site.xml (original)
+++ db/torque/torque4/trunk/torque-site/src/site/site.xml Thu Aug 16 13:34:35 
2012
@@ -195,6 +195,7 @@
        <menu name="Other Documentation">
       <item name="Supported Databases" 
href="/documentation/other/supported-databases.html"/>
       <item name="Database Howtos"     
href="/documentation/other/database-howtos/index.html" collapse="true">
+        <item name="Derby"             
href="/documentation/other/database-howtos/derby-howto.html"/>
         <item name="HSQLDB"            
href="/documentation/other/database-howtos/hsqldb-howto.html"/>
         <item name="MS SQL Server"     
href="/documentation/other/database-howtos/mssql-howto.html"/>
         <item name="MySQL"             
href="/documentation/other/database-howtos/mysql-howto.html"/>

Added: 
db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/other/database-howtos/derby-howto.xml
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/other/database-howtos/derby-howto.xml?rev=1373827&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/other/database-howtos/derby-howto.xml
 (added)
+++ 
db/torque/torque4/trunk/torque-site/src/site/xdoc/documentation/other/database-howtos/derby-howto.xml
 Thu Aug 16 13:34:35 2012
@@ -0,0 +1,43 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<document>
+
+ <properties>
+  <title>Derby Howto</title>
+ </properties>
+
+ <body>
+
+  <section name="Introduction">
+    This document contains hints for using Torque with HSQLDB.
+  </section>
+
+  <section name="Supported versions">
+
+    <p>
+      Derby 10.5.1.1 introduced support for limit and offset.
+      This is used in within Torque, so limit and offset does not
+      work for Derby &lt; 10.5.1.1
+    </p>
+
+  </section>
+
+ </body>
+</document>

Modified: 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java?rev=1373827&r1=1373826&r2=1373827&view=diff
==============================================================================
--- 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
 (original)
+++ 
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
 Thu Aug 16 13:34:35 2012
@@ -31,6 +31,7 @@ import java.util.Set;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.torque.adapter.Adapter;
+import org.apache.torque.adapter.DerbyAdapter;
 import org.apache.torque.adapter.MssqlAdapter;
 import org.apache.torque.adapter.MysqlAdapter;
 import org.apache.torque.criteria.Criteria;
@@ -184,13 +185,24 @@ public class DataTest extends BaseDataba
         }
         catch (TorqueException e)
         {
-            if (Torque.getDB(Torque.getDefaultDB()).supportsNativeLimit())
+            if (defaultAdapter.supportsNativeLimit())
             {
-                throw e;
+                if (!(defaultAdapter instanceof DerbyAdapter))
+                {
+                    throw e;
+                }
+                else
+                {
+                    log.error("testLimitOffset(): "
+                            + "A limit of 0 is not supported for Derby");
+                }
+            }
+            else
+            {
+                log.error("testLimitOffset(): "
+                        + "A limit of 0 is not supported for Databases "
+                        + "without native limit support");
             }
-            log.error("testLimitOffset(): "
-                    + "A limit of 0 is not supported for Databases "
-                    + "without native limit support");
         }
 
         // check that Offset also works without limit



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

Reply via email to