openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From awh...@apache.org
Subject svn commit: r428484 - in /incubator/openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/
Date Thu, 03 Aug 2006 19:16:48 GMT
Author: awhite
Date: Thu Aug  3 12:16:47 2006
New Revision: 428484

URL: http://svn.apache.org/viewvc?rev=428484&view=rev
Log:
More work on type promotion and casting in queries.  Also, make sure DFG is
in active fetch groups before testing it for post-load callbacks.


Modified:
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java

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?rev=428484&r1=428483&r2=428484&view=diff
==============================================================================
--- 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 Aug  3 12:16:47 2006
@@ -2293,11 +2293,13 @@
         if (requiresCastForMathFunctions && (lc != rc
             || (lhs.isConstant() && rhs.isConstant()))) {
             Class c = Filters.promote(lhs.getType(), rhs.getType());
-            castlhs = (lhs.isConstant() && rhs.isConstant())
-                || Filters.wrap(lhs.getType()) != c;
-            castrhs = (lhs.isConstant() && rhs.isConstant())
-                || Filters.wrap(rhs.getType()) != c;
             type = getJDBCType(JavaTypes.getTypeCode(c), false);
+            if (type != Types.VARBINARY && type != Types.BLOB) {
+                castlhs = (lhs.isConstant() && rhs.isConstant())
+                    || Filters.wrap(lhs.getType()) != c;
+                castrhs = (lhs.isConstant() && rhs.isConstant())
+                    || Filters.wrap(rhs.getType()) != c;
+            }
         }
 
         boolean mod = "MOD".equals(op);
@@ -2345,11 +2347,13 @@
         if (requiresCastForComparisons && (lc != rc
             || (lhs.isConstant() && rhs.isConstant()))) {
             Class c = Filters.promote(lhs.getType(), rhs.getType());
-            castlhs = (lhs.isConstant() && rhs.isConstant())
-                || Filters.wrap(lhs.getType()) != c;
-            castrhs = (lhs.isConstant() && rhs.isConstant())
-                || Filters.wrap(rhs.getType()) != c;
             type = getJDBCType(JavaTypes.getTypeCode(c), false);
+            if (type != Types.VARBINARY && type != Types.BLOB) {
+                castlhs = (lhs.isConstant() && rhs.isConstant())
+                    || Filters.wrap(lhs.getType()) != c;
+                castrhs = (lhs.isConstant() && rhs.isConstant())
+                    || Filters.wrap(rhs.getType()) != c;
+            }
         }
 
         if (castlhs)

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java?rev=428484&r1=428483&r2=428484&view=diff
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/Filters.java
Thu Aug  3 12:16:47 2006
@@ -29,6 +29,7 @@
 import org.apache.openjpa.kernel.exps.FilterListener;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.meta.JavaTypes;
 import org.apache.openjpa.util.ImplHelper;
 import org.apache.openjpa.util.InternalException;
 import org.apache.openjpa.util.UserException;
@@ -106,76 +107,100 @@
     }
 
     /**
-     * Given two numeric types, return type they should both be converted
-     * to before performing any mathematical operations between them.
+     * Given two types, return type they should both be converted
+     * to before performing any operations between them.
      */
     public static Class promote(Class c1, Class c2) {
         if (c1 == c2)
-            return c1;
-        if (c1 == Object.class)
-            return c2;
-        if (c2 == Object.class)
-            return c1;
-
+            return unwrap(c1);
         Class w1 = wrap(c1);
         Class w2 = wrap(c2);
         if (w1 == w2)
-            return c1;
-        c1 = w1;
-        c2 = w2;
+            return unwrap(c1);
 
         // not numbers?
-        boolean c1Number = Number.class.isAssignableFrom(c1);
-        boolean c2Number = Number.class.isAssignableFrom(c2);
-        if (!c1Number || !c2Number) {
+        boolean w1Number = Number.class.isAssignableFrom(w1);
+        boolean w2Number = Number.class.isAssignableFrom(w2);
+        if (!w1Number || !w2Number) {
             // the only non-numeric promotion we do is string to char,
             // or from char/string to number
-            if (!c1Number) {
-                if (c2Number && (c1 == Character.class || c1 == String.class))
-                    return (c2 == Byte.class || c2 == Short.class)
-                        ? Integer.class : c2;
-                if (!c2Number && c1 == Character.class && c2 == String.class)
+            if (!w1Number) {
+                if (w2Number && (w1 == Character.class || w1 == String.class))
+                    return (w2 == Byte.class || w2 == Short.class)
+                        ? Integer.class : unwrap(c2);
+                if (!w2Number && w1 == Character.class && w2 == String.class)
                     return String.class;
+                if (w2Number)
+                    return unwrap(c2);
             }
-            if (!c2Number) {
-                if (c1Number && (c2 == Character.class || c2 == String.class))
-                    return (c1 == Byte.class || c1 == Short.class)
-                        ? Integer.class : c1;
-                if (!c1Number && c2 == Character.class && c1 == String.class)
+            if (!w2Number) {
+                if (w1Number && (w2 == Character.class || w2 == String.class))
+                    return (w1 == Byte.class || w1 == Short.class)
+                        ? Integer.class : unwrap(c1);
+                if (!w1Number && w2 == Character.class && w1 == String.class)
                     return String.class;
+                if (w1Number)
+                    return unwrap(c1);
             }
 
-            // if neither are numbers and one is a superclass of the
-            // other, return the least-derived of the two types
-            if (!c1Number && !c2Number) {
-                if (c1.isAssignableFrom(c2))
-                    return c1;
-                if (c2.isAssignableFrom(c1))
-                    return c2;
+            // if neither are numbers, use least-derived of the two.  if neither
+            // is assignable from the other but one is a standard type, assume
+            // the other can be converted to that standard type
+            if (!w1Number && !w2Number) {
+                if (w1 == Object.class)
+                    return unwrap(c2);
+                if (w2 == Object.class)
+                    return unwrap(c1);
+                if (w1.isAssignableFrom(w2))
+                    return unwrap(c1);
+                if (w2.isAssignableFrom(w1))
+                    return unwrap(c2);
+                if (isNonstandardType(w1))
+                    return (isNonstandardType(w2)) ? Object.class : unwrap(c2);
+                if (isNonstandardType(w2))
+                    return (isNonstandardType(w1)) ? Object.class : unwrap(c1);
             }
-
-            return c1;
+            return Object.class;
         }
 
-        if (c1 == BigDecimal.class || c2 == BigDecimal.class)
+        if (w1 == BigDecimal.class || w2 == BigDecimal.class)
             return BigDecimal.class;
-        if (c1 == BigInteger.class) {
-            if (c2 == Float.class || c2 == Double.class)
+        if (w1 == BigInteger.class) {
+            if (w2 == Float.class || w2 == Double.class)
                 return BigDecimal.class;
             return BigInteger.class;
         }
-        if (c2 == BigInteger.class) {
-            if (c1 == Float.class || c1 == Double.class)
+        if (w2 == BigInteger.class) {
+            if (w1 == Float.class || w1 == Double.class)
                 return BigDecimal.class;
             return BigInteger.class;
         }
-        if (c1 == Double.class || c2 == Double.class)
+        if (w1 == Double.class || w2 == Double.class)
             return double.class;
-        if (c1 == Float.class || c2 == Float.class)
+        if (w1 == Float.class || w2 == Float.class)
             return float.class;
-        if (c1 == Long.class || c2 == Long.class)
+        if (w1 == Long.class || w2 == Long.class)
             return long.class;
         return int.class;
+    }
+
+    /**
+     * Return whether the given type is not a standard persistent type.
+     */
+    private static boolean isNonstandardType(Class c) {
+        switch (JavaTypes.getTypeCode(c))
+        {
+        case JavaTypes.ARRAY:
+        case JavaTypes.COLLECTION:
+        case JavaTypes.MAP:
+        case JavaTypes.PC:
+        case JavaTypes.PC_UNTYPED:
+        case JavaTypes.OID:
+        case JavaTypes.OBJECT:
+            return true;
+        default:
+            return false;
+        }
     }
 
     /**

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java?rev=428484&r1=428483&r2=428484&view=diff
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java
Thu Aug  3 12:16:47 2006
@@ -2872,25 +2872,24 @@
         if (mgr == null || !mgr.hasLoadListeners(getManagedInstance(), _meta))
             return;
 
+        if (fetch == null)
+            fetch = _broker.getFetchConfiguration();
         // is this field a post-load field?
         if (field != -1) {
-            FieldMetaData fmd = _meta.getField (field);
+            FieldMetaData fmd = _meta.getField(field);
             if (fmd.isInDefaultFetchGroup() 
-               && postLoad(_meta.getFetchGroup(FetchGroup.NAME_DEFAULT), fetch))
-               return;
+                && fetch.hasFetchGroup(FetchGroup.NAME_DEFAULT)
+                && postLoad(FetchGroup.NAME_DEFAULT, fetch))
+                return;
             String[] fgs = fmd.getCustomFetchGroups();
             for (int i = 0; i < fgs.length; i++)
-                if (postLoad(_meta.getFetchGroup(fgs[i]), fetch))
+                if (fetch.hasFetchGroup(fgs[i]) && postLoad(fgs[i], fetch))
                     return;
         } else {
-            if (postLoad(_meta.getFetchGroup(FetchGroup.NAME_DEFAULT), fetch))
-                return;
-            
-            Iterator fgs = fetch.getFetchGroups().iterator();
-            for (;fgs.hasNext();) {
-            	FetchGroup fg = _meta.getFetchGroup(fgs.next().toString());
-                if (fg != null && postLoad(fg, fetch))
-                    return; 
+            for (Iterator itr = fetch.getFetchGroups().iterator(); 
+                itr.hasNext();) {
+                if (postLoad((String) itr.next(), fetch))
+                    return;
             }
         }
     }
@@ -2899,14 +2898,15 @@
      * Perform post-load actions if the given fetch group is a post-load group
      * and is fully loaded.
      */
-    private boolean postLoad(FetchGroup fg, FetchConfiguration fetch) {
-        if (!fg.isPostLoad())
+    private boolean postLoad(String fgName, FetchConfiguration fetch) {
+        FetchGroup fg = _meta.getFetchGroup(fgName);
+        if (fg == null || !fg.isPostLoad())
             return false;
+
         FieldMetaData[] fmds = _meta.getFields();
         for (int i = 0; i < fmds.length; i++)
-        	if (fmds[i].isInFetchGroup(fg.getName()))
-        		if (!_loaded.get(i))
-        			return false;
+            if (!_loaded.get(i) && fmds[i].isInFetchGroup(fgName))
+                return false;
 
         _flags |= FLAG_LOADED;
         _broker.fireLifecycleEvent(getManagedInstance(), fetch, _meta, 



Mime
View raw message