openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From awh...@apache.org
Subject svn commit: r505017 - in /incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc: kernel/exps/ meta/ meta/strats/ sql/
Date Thu, 08 Feb 2007 19:36:34 GMT
Author: awhite
Date: Thu Feb  8 11:36:33 2007
New Revision: 505017

URL: http://svn.apache.org/viewvc?view=rev&rev=505017
Log:
Add a DiscriminatorStrategy.hasClassConditions() method rather than having the
DiscriminatorStrategy.getClassConditions() method return null for no conditions
so that we can detect whether the joins to the base class owning the 
discriminator are necessary before making them.  Otherwise, we run the risk of
creating table aliases for base class tables without joining them, which can
result in bad SQL. 


Modified:
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InstanceofExpression.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorStrategy.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/AbstractDiscriminatorStrategy.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SubclassJoinDiscriminatorStrategy.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SuperclassDiscriminatorStrategy.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InstanceofExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InstanceofExpression.java?view=diff&rev=505017&r1=505016&r2=505017
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InstanceofExpression.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InstanceofExpression.java
Thu Feb  8 11:36:33 2007
@@ -87,7 +87,7 @@
                 getClassLoader(), false);
 
             // if not looking for a PC, don't bother with indicator
-            if (mapping == null)
+            if (mapping == null || !discrim.hasClassConditions(mapping, true))
                 discrim = null;
             else {
                 ClassMapping owner = discrim.getClassMapping();
@@ -144,10 +144,7 @@
             ctx.store.loadSubclasses(istate.discrim.getClassMapping());
             SQLBuffer buf = istate.discrim.getClassConditions(sel,
                 istate.joins, istate.mapping, true);
-            if (buf == null)
-                sql.append("1 = 1");
-            else
-                sql.append(buf);
+            sql.append(buf);
         }
         sel.append(sql, istate.joins);
     }

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java?view=diff&rev=505017&r1=505016&r2=505017
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java
Thu Feb  8 11:36:33 2007
@@ -297,6 +297,8 @@
         if (_mapping.getJoinablePCSuperclassMapping() == null
             && _mapping.getJoinablePCSubclassMappings().length == 0)
             return false;
+        if (!hasClassConditions(_mapping, subs))
+            return false;
 
         // join down to base class where conditions will be added
         ClassMapping from = _mapping;
@@ -310,12 +312,8 @@
             }
         }
 
-        SQLBuffer buf = getClassConditions(sel, joins, _mapping, subs);
-        if (buf != null) {
-            sel.where(buf, joins);
-            return true;
-        }
-        return false;
+        sel.where(getClassConditions(sel, joins, _mapping, subs), joins);
+        return true;
     }
 
     ////////////////////////////////////////
@@ -392,6 +390,10 @@
     public Class getClass(JDBCStore store, ClassMapping base, Result result)
         throws SQLException, ClassNotFoundException {
         return assertStrategy().getClass(store, base, result);
+    }
+
+    public boolean hasClassConditions(ClassMapping base, boolean subs) {
+        return assertStrategy().hasClassConditions(base, subs);
     }
 
     public SQLBuffer getClassConditions(Select sel, Joins joins, 

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorStrategy.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorStrategy.java?view=diff&rev=505017&r1=505016&r2=505017
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorStrategy.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorStrategy.java
Thu Feb  8 11:36:33 2007
@@ -59,10 +59,16 @@
         throws SQLException, ClassNotFoundException;
 
     /**
+     * Whether any class conditions are necessary.
+     *
+     * @see #getClassConditions
+     */
+    public boolean hasClassConditions(ClassMapping base, boolean subs);
+
+    /**
      * Return SQL to limit the classes selected as much as possible to the
-     * given base class, and optionally its subclasses. Return null if
-     * no conditions needed. The select and joins instances are supplied
-     * in order to get column aliases.
+     * given base class, and optionally its subclasses. The select and joins 
+     * instances are supplied in order to get column aliases.
      */
     public SQLBuffer getClassConditions(Select sel, Joins joins, 
         ClassMapping base, boolean subs);

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/AbstractDiscriminatorStrategy.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/AbstractDiscriminatorStrategy.java?view=diff&rev=505017&r1=505016&r2=505017
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/AbstractDiscriminatorStrategy.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/AbstractDiscriminatorStrategy.java
Thu Feb  8 11:36:33 2007
@@ -82,6 +82,10 @@
         return base.getDescribedType();
     }
 
+    public boolean hasClassConditions(ClassMapping base, boolean subs) {
+        return false;
+    }
+
     public SQLBuffer getClassConditions(Select sel, Joins joins, 
         ClassMapping base, boolean subs) {
         return null;

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java?view=diff&rev=505017&r1=505016&r2=505017
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/InValueDiscriminatorStrategy.java
Thu Feb  8 11:36:33 2007
@@ -113,19 +113,23 @@
         return getClass(cls, store);
     }
 
-    public SQLBuffer getClassConditions(Select sel, Joins joins, 
-        ClassMapping base, boolean subclasses) {
+    public boolean hasClassConditions(ClassMapping base, boolean subclasses) {
         // if selecting the first mapped class and all subclasses, no need
         // to limit the query
         if (isFinal || (base.getJoinablePCSuperclassMapping() == null
             && subclasses))
-            return null;
+            return false;
 
         // if no subclasses or superclass, no need for conditions
         ClassMapping[] subs = base.getJoinablePCSubclassMappings();
         if (subs.length == 0 && base.getJoinablePCSuperclassMapping() == null)
-            return null;
+            return false;
+
+        return true;
+    }
 
+    public SQLBuffer getClassConditions(Select sel, Joins joins, 
+        ClassMapping base, boolean subclasses) {
         Column col = disc.getColumns()[0];
         SQLBuffer sql = new SQLBuffer(sel.getConfiguration().
             getDBDictionaryInstance());
@@ -136,6 +140,7 @@
         sql.append(alias);
 
         // if not selecting subclasses, limit to just the given class
+        ClassMapping[] subs = base.getJoinablePCSubclassMappings();
         if (!outer && (!subclasses || subs.length == 0))
             return sql.append(" = ").appendValue(getDiscriminatorValue(base),
                 col);

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SubclassJoinDiscriminatorStrategy.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SubclassJoinDiscriminatorStrategy.java?view=diff&rev=505017&r1=505016&r2=505017
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SubclassJoinDiscriminatorStrategy.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SubclassJoinDiscriminatorStrategy.java
Thu Feb  8 11:36:33 2007
@@ -130,16 +130,20 @@
         return derived;
     }
 
-    public SQLBuffer getClassConditions(Select sel, Joins joins, 
-        ClassMapping base, boolean subclasses) {
+    public boolean hasClassConditions(ClassMapping base, boolean subclasses) {
         if (isFinal || subclasses)
-            return null;
+            return false;
         ClassMapping[] subs = base.getJoinablePCSubclassMappings();
         if (subs.length == 0)
-            return null;
+            return false;
+        return true;
+    }
 
+    public SQLBuffer getClassConditions(Select sel, Joins joins, 
+        ClassMapping base, boolean subclasses) {
         // add conditions making sure no subclass tables have records for
         // this instance
+        ClassMapping[] subs = base.getJoinablePCSubclassMappings();
         SQLBuffer buf = null;
         Column[] pks;
         for (int i = 0; i < subs.length; i++) {

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SuperclassDiscriminatorStrategy.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SuperclassDiscriminatorStrategy.java?view=diff&rev=505017&r1=505016&r2=505017
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SuperclassDiscriminatorStrategy.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/SuperclassDiscriminatorStrategy.java
Thu Feb  8 11:36:33 2007
@@ -61,6 +61,11 @@
             getDiscriminator().getClass(store, base, res);
     }
 
+    public boolean hasClassConditions(ClassMapping base, boolean subclasses) {
+        return disc.getClassMapping().getPCSuperclassMapping().
+            getDiscriminator().hasClassConditions(base, subclasses);
+    }
+
     public SQLBuffer getClassConditions(Select sel, Joins joins, 
         ClassMapping base, boolean subclasses) {
         return disc.getClassMapping().getPCSuperclassMapping().

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?view=diff&rev=505017&r1=505016&r2=505017
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
Thu Feb  8 11:36:33 2007
@@ -1743,7 +1743,8 @@
      * cases where a subselect is required and the database doesn't support
      * subselects), this method should return null.
      */
-    public SQLBuffer toDelete(ClassMapping mapping, Select sel, Object[] params) {
+    public SQLBuffer toDelete(ClassMapping mapping, Select sel, 
+        Object[] params) {
         return toBulkOperation(mapping, sel, null, params, null);
     }
 



Mime
View raw message