You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by fa...@apache.org on 2007/09/21 05:29:39 UTC

svn commit: r577972 - in /openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql: DB2Dictionary.java DBDictionary.java

Author: fancy
Date: Thu Sep 20 20:29:38 2007
New Revision: 577972

URL: http://svn.apache.org/viewvc?rev=577972&view=rev
Log:
OPENJPA-378 DB2 "FETCH FIRST <n> ROWS ONLY" clause should not be generated in subselect for SELECT COUNT(*) from (subselect ...) s

Modified:
    openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
    openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java

Modified: openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java?rev=577972&r1=577971&r2=577972&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java (original)
+++ openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java Thu Sep 20 20:29:38 2007
@@ -148,11 +148,14 @@
             && super.supportsRandomAccessResultSet(sel, forUpdate);
     }
 
-    protected void appendSelectRange(SQLBuffer buf, long start, long end) {
+    protected void appendSelectRange(SQLBuffer buf, long start, long end,
+        boolean subselect) {
         // appends the literal range string, since DB2 is unable to handle
         // a bound parameter for it
-        buf.append(" FETCH FIRST ").append(Long.toString(end)).
-            append(" ROWS ONLY");
+        // do not generate FETCH FIRST clause for subselect
+        if (!subselect)
+            buf.append(" FETCH FIRST ").append(Long.toString(end)).
+                append(" ROWS ONLY");
     }
 
     public String[] getCreateSequenceSQL(Sequence seq) {

Modified: openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=577972&r1=577971&r2=577972&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java (original)
+++ openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java Thu Sep 20 20:29:38 2007
@@ -1736,7 +1736,7 @@
         from.append("(");
         from.append(toSelect(subSelect, null, subFrom, where,
             sel.getGrouping(), sel.getHaving(), null, sel.isDistinct(),
-            false, sel.getStartIndex(), sel.getEndIndex()));
+            false, sel.getStartIndex(), sel.getEndIndex(), true));
         from.append(")");
         if (requiresAliasForSubselect)
             from.append(" ").append(Select.FROM_SELECT_ALIAS);
@@ -2186,6 +2186,19 @@
     /**
      * Combine the given components into a SELECT statement.
      */
+    private SQLBuffer toSelect(SQLBuffer selects, JDBCFetchConfiguration fetch,
+        SQLBuffer from, SQLBuffer where, SQLBuffer group,
+        SQLBuffer having, SQLBuffer order,
+        boolean distinct, boolean forUpdate, long start, long end,
+        boolean subselect) {
+        return toOperation(getSelectOperation(fetch), selects, from, where,
+            group, having, order, distinct, start, end,
+            getForUpdateClause(fetch, forUpdate, null), subselect);
+    }
+
+    /**
+     * Combine the given components into a SELECT statement.
+     */
     public SQLBuffer toSelect(SQLBuffer selects, JDBCFetchConfiguration fetch,
         SQLBuffer from, SQLBuffer where, SQLBuffer group,
         SQLBuffer having, SQLBuffer order,
@@ -2227,16 +2240,27 @@
         SQLBuffer from, SQLBuffer where, SQLBuffer group, SQLBuffer having,
         SQLBuffer order, boolean distinct, long start, long end,
         String forUpdateClause) {
+        return toOperation(op, selects, from, where, group, having, order,
+            distinct, start, end, forUpdateClause, false);
+    }
+
+    /**
+     * Return the SQL for the given selecting operation.
+     */
+    private SQLBuffer toOperation(String op, SQLBuffer selects,
+        SQLBuffer from, SQLBuffer where, SQLBuffer group, SQLBuffer having,
+        SQLBuffer order, boolean distinct, long start, long end,
+        String forUpdateClause, boolean subselect) {
         SQLBuffer buf = new SQLBuffer(this);
         buf.append(op);
 
         boolean range = start != 0 || end != Long.MAX_VALUE;
         if (range && rangePosition == RANGE_PRE_DISTINCT)
-            appendSelectRange(buf, start, end);
+            appendSelectRange(buf, start, end, subselect);
         if (distinct)
             buf.append(" DISTINCT");
         if (range && rangePosition == RANGE_POST_DISTINCT)
-            appendSelectRange(buf, start, end);
+            appendSelectRange(buf, start, end, subselect);
 
         buf.append(" ").append(selects).append(" FROM ").append(from);
 
@@ -2251,11 +2275,11 @@
         if (order != null && !order.isEmpty())
             buf.append(" ORDER BY ").append(order);
         if (range && rangePosition == RANGE_POST_SELECT)
-            appendSelectRange(buf, start, end);
+            appendSelectRange(buf, start, end, subselect);
         if (forUpdateClause != null)
             buf.append(" ").append(forUpdateClause);
         if (range && rangePosition == RANGE_POST_LOCK)
-            appendSelectRange(buf, start, end);
+            appendSelectRange(buf, start, end, subselect);
         return buf;
     }
 
@@ -2263,7 +2287,8 @@
      * If this dictionary can select ranges,
      * use this method to append the range SQL.
      */
-    protected void appendSelectRange(SQLBuffer buf, long start, long end) {
+    protected void appendSelectRange(SQLBuffer buf, long start, long end
+        , boolean subselect) {
     }
 
     /**