openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dez...@apache.org
Subject svn commit: r802200 - in /openjpa/branches/1.1.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/
Date Fri, 07 Aug 2009 21:21:45 GMT
Author: dezzio
Date: Fri Aug  7 21:21:45 2009
New Revision: 802200

URL: http://svn.apache.org/viewvc?rev=802200&view=rev
Log:
OpenJPA-51: merged fix from trunk revs 676467,676787: "bad sql pushdown, sub select is missing
from clause"

Added:
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Customer.java
      - copied unchanged from r676467, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Customer.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Order.java
      - copied unchanged from r676467, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/Order.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderItem.java
      - copied unchanged from r676467, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/OrderItem.java
    openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
      - copied, changed from r676467, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
Modified:
    openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
  (contents, props changed)

Modified: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java?rev=802200&r1=802199&r2=802200&view=diff
==============================================================================
--- openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
(original)
+++ openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
Fri Aug  7 21:21:45 2009
@@ -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;
 
@@ -526,6 +529,71 @@
             else
                 _joinSyntax = _parent._joinSyntax;
         }
+        
+        if (_parent.getAliases() == null || _subPath == null)
+            return;
+        
+        // resolve aliases for subselect from parent
+        Set<Map.Entry> entries = _parent.getAliases().entrySet();
+        for (Map.Entry entry : entries) {
+            Object key = entry.getKey();
+            Integer alias = (Integer) entry.getValue();
+            if (key.toString().indexOf(_subPath) != -1 ||
+                _parent.findTableAlias(alias) == false) {
+                if (_aliases == null)
+                    _aliases = new HashMap();
+                _aliases.put(key, alias);
+
+                Object tableString = _parent.getTables().get(alias);
+                if (_tables == null)
+                    _tables = new TreeMap();
+                _tables.put(alias, tableString);
+                
+                _removedAliasFromParent.set(alias.intValue());
+            }
+        }
+        
+        if (_aliases != null) {
+            // aliases moved into subselect should be removed from parent
+            entries = _aliases.entrySet();
+            for (Map.Entry entry : entries) {
+                Object key = entry.getKey();
+                Integer alias = (Integer) entry.getValue();
+                if (key.toString().indexOf(_subPath) != -1 ||
+                    _parent.findTableAlias(alias) == false) {
+                    _parent.removeAlias(key);
+
+                    Object tableString = _parent.getTables().get(alias);
+                    _parent.removeTable(alias);
+                }
+            }
+        }
+    }
+    
+    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;
+    }
+    
+    public void removeAlias(Object key) {
+        _aliases.remove(key);
+    }
+    
+    public Map getTables() {
+        return _tables;
+    }
+    
+    public void removeTable(Object key) {
+        _tables.remove(key);
     }
 
     public Select getFromSelect() {
@@ -656,13 +724,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() {
@@ -1552,8 +1637,13 @@
             return;
         if (_parent._joins != null && !_parent._joins.isEmpty()) {
             boolean removed = false;
-            if (!_removedAliasFromParent.isEmpty())
-                removed = _parent._joins.joins().removeAll(pj.joins());
+            if (!_removedAliasFromParent.isEmpty()) {
+                for (Iterator itr = pj.joins().iterator(); itr.hasNext();) {
+                   Join jn = (Join) itr.next();
+                   if (_aliases.containsValue(jn.getIndex1()))
+                       removed = _parent._joins.joins().remove(jn);
+                }
+            }
             if (!removed)
                 pj.joins().removeAll(_parent._joins.joins());
         }

Propchange: openjpa/branches/1.1.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Fri Aug  7 21:21:45 2009
@@ -0,0 +1 @@
+/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java:676467,676787,774580

Copied: openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
(from r676467, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java)
URL: http://svn.apache.org/viewvc/openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java?p2=openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java&p1=openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java&r1=676467&r2=802200&rev=802200&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
(original)
+++ openjpa/branches/1.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestSubquery.java
Fri Aug  7 21:21:45 2009
@@ -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