openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fa...@apache.org
Subject svn commit: r676787 - in /openjpa: branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/ trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jd...
Date Tue, 15 Jul 2008 02:06:56 GMT
Author: fancy
Date: Mon Jul 14 19:06:55 2008
New Revision: 676787

URL: http://svn.apache.org/viewvc?rev=676787&view=rev
Log:
OPENJPA-51 A  non-correlated subquery resulting SQL syntax error

Modified:
    openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
    openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java

Modified: openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java?rev=676787&r1=676786&r2=676787&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
(original)
+++ openjpa/branches/1.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
Mon Jul 14 19:06:55 2008
@@ -118,6 +118,9 @@
     // 'parent.address.street' for the purposes of comparisons
     private Map _aliases = null;
 
+    // to cache table alias using Table as the key
+    private Map _tableAliases = null;
+
     // map of indexes to table aliases like 'TABLENAME t0'
     private SortedMap _tables = null;
 
@@ -481,7 +484,8 @@
         	Map.Entry entry = (Map.Entry) it.next();
             Object key = entry.getKey();
             Integer alias = (Integer) entry.getValue();
-            if (key.toString().indexOf(_subPath) != -1) {
+            if (key.toString().indexOf(_subPath) != -1 ||
+                _parent.findTableAlias(alias) == false) {
                 if (_aliases == null)
                     _aliases = new HashMap();
                 _aliases.put(key, alias);
@@ -503,7 +507,8 @@
             	Map.Entry entry = (Map.Entry) it.next();
                 Object key = entry.getKey();
                 Integer alias = (Integer) entry.getValue();
-                if (key.toString().indexOf(_subPath) != -1) {
+                if (key.toString().indexOf(_subPath) != -1 ||
+                    _parent.findTableAlias(alias) == false) {
                     _parent.removeAlias(key);
 
                     Object tableString = _parent.getTables().get(alias);
@@ -513,6 +518,16 @@
         }
     }
     
+    private boolean findTableAlias(Integer alias) {
+        // if alias is defined and referenced, return true.
+        String value = "t" + alias.toString() + ".";
+        if (_tableAliases != null)
+            return _tableAliases.containsValue(value) &&
+               _tables.containsKey(alias);
+        else
+            return true;
+    }
+    
     public Map getAliases() {
         return _aliases;
     }
@@ -653,13 +668,30 @@
      * Return the alias for the given column.
      */
     private String getColumnAlias(String col, Table table, PathJoins pj) {
+        String tableAlias = null;
+        if (pj == null || pj.path() == null) {
+            if (_tableAliases == null)
+                _tableAliases = new HashMap();
+            tableAlias = (String) _tableAliases.get(table);
+            if (tableAlias == null) {
+                tableAlias = getTableAlias(table, pj).toString();
+                _tableAliases.put(table, tableAlias);
+            }
+            return new StringBuilder(tableAlias).append(col).toString();
+        }
+        return getTableAlias(table, pj).append(col).toString();
+    }
+    
+    private StringBuilder getTableAlias(Table table, PathJoins pj) {
+        StringBuilder buf = new StringBuilder();
         if (_from != null) {
             String alias = toAlias(_from.getTableIndex(table, pj, true));
             if (_dict.requiresAliasForSubselect)
-                return FROM_SELECT_ALIAS + "." + alias + "_" + col;
-            return alias + "_" + col;
+                return buf.append(FROM_SELECT_ALIAS).append(".").append(alias).
+                    append("_");
+            return buf.append(alias).append("_");
         }
-        return toAlias(getTableIndex(table, pj, true)) + "." + col;
+        return buf.append(toAlias(getTableIndex(table, pj, true))).append(".");
     }
 
     public boolean isAggregate() {

Modified: openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java?rev=676787&r1=676786&r2=676787&view=diff
==============================================================================
--- openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
(original)
+++ openjpa/branches/1.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
Mon Jul 14 19:06:55 2008
@@ -36,6 +36,9 @@
     }
 
     static String[]  querys = new String[] {
+        "select o1.oid from Order o1 where o1.oid in " +
+            " (select distinct o.oid from OrderItem i, Order o" +
+            " where i.quantity > 10 and o.amount > 1000 and i.lid = o.oid)" ,
         "select o.oid from Order o where o.customer.name =" +
             " (select max(o2.customer.name) from Order o2" +
             " where o.customer.cid.id = o2.customer.cid.id)",

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java?rev=676787&r1=676786&r2=676787&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java Mon
Jul 14 19:06:55 2008
@@ -582,7 +582,8 @@
         for (Map.Entry entry : entries) {
             Object key = entry.getKey();
             Integer alias = (Integer) entry.getValue();
-            if (key.toString().indexOf(_subPath) != -1) {
+            if (key.toString().indexOf(_subPath) != -1 ||
+                _parent.findTableAlias(alias) == false) {
                 if (_aliases == null)
                     _aliases = new HashMap();
                 _aliases.put(key, alias);
@@ -602,7 +603,8 @@
             for (Map.Entry entry : entries) {
                 Object key = entry.getKey();
                 Integer alias = (Integer) entry.getValue();
-                if (key.toString().indexOf(_subPath) != -1) {
+                if (key.toString().indexOf(_subPath) != -1 ||
+                    _parent.findTableAlias(alias) == false) {
                     _parent.removeAlias(key);
 
                     Object tableString = _parent.getTables().get(alias);
@@ -612,6 +614,16 @@
         }
     }
     
+    private boolean findTableAlias(Integer alias) {
+        // if alias is defined and referenced, return true.
+        String value = "t" + alias.toString() + ".";
+        if (_tableAliases != null)
+            return _tableAliases.containsValue(value) &&
+               _tables.containsKey(alias);
+        else
+            return true;
+    }
+    
     public Map getAliases() {
         return _aliases;
     }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java?rev=676787&r1=676786&r2=676787&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
Mon Jul 14 19:06:55 2008
@@ -36,6 +36,9 @@
     }
 
     static String[]  querys = new String[] {
+        "select o1.oid from Order o1 where o1.oid in " +
+            " (select distinct o.oid from OrderItem i, Order o" +
+            " where i.quantity > 10 and o.amount > 1000 and i.lid = o.oid)" ,
         "select o.oid from Order o where o.customer.name =" +
             " (select max(o2.customer.name) from Order o2" +
             " where o.customer.cid.id = o2.customer.cid.id)",



Mime
View raw message