openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1535816 - in /openjpa/branches/2.3.x: ./ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/ openjpa-persistence-jdbc/src/test/resources/META-INF/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ openjpa...
Date Fri, 25 Oct 2013 18:51:08 GMT
Author: struberg
Date: Fri Oct 25 18:51:08 2013
New Revision: 1535816

URL: http://svn.apache.org/r1535816
Log:
OPENJPA-2018  correctly handle select IN with arrays

txs to rmannibucau for the patch.
Applied with minor changes


Added:
    openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/
      - copied from r1535760, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/openjpa2018/
    openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/resources/META-INF/openjpa2018.xml
      - copied unchanged from r1535760, openjpa/trunk/openjpa-persistence-jdbc/src/test/resources/META-INF/openjpa2018.xml
    openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/BindableParameter.java
      - copied unchanged from r1535760, openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/BindableParameter.java
Modified:
    openjpa/branches/2.3.x/   (props changed)
    openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AbstractQuery.java
    openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
    openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java

Propchange: openjpa/branches/2.3.x/
------------------------------------------------------------------------------
  Merged /openjpa/trunk:r1535760

Modified: openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AbstractQuery.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AbstractQuery.java?rev=1535816&r1=1535815&r2=1535816&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AbstractQuery.java
(original)
+++ openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AbstractQuery.java
Fri Oct 25 18:51:08 2013
@@ -39,6 +39,7 @@ import org.apache.openjpa.kernel.QueryLa
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.lib.util.OrderedMap;
 import org.apache.openjpa.meta.QueryMetaData;
+import org.apache.openjpa.persistence.criteria.BindableParameter;
 
 /**
  * An abstract implementation of the Query interface.
@@ -345,7 +346,10 @@ public abstract class AbstractQuery<X> i
     }
 
     public <T> OpenJPAQuery<X> setParameter(Parameter<T> p, T arg1) {
-        bindValue((Parameter<T>) p, arg1);
+        bindValue(p, arg1);
+        if (BindableParameter.class.isInstance(p)) {
+            BindableParameter.class.cast(p).setValue(arg1);
+        }
         return this;
     }
 

Modified: openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java?rev=1535816&r1=1535815&r2=1535816&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
(original)
+++ openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/Expressions.java
Fri Oct 25 18:51:08 2013
@@ -19,6 +19,8 @@
 
 package org.apache.openjpa.persistence.criteria;
 
+import java.lang.reflect.Array;
+import java.lang.reflect.ParameterizedType;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
@@ -1446,17 +1448,47 @@ class Expressions {
                 Expressions.Equal e = (Expressions.Equal)_exps.get(0);
                 ExpressionImpl<?> e2 = e.e2;
                 ExpressionImpl<?> e1 = e.e1;
-                Value val2 = Expressions.toValue(e2, factory, q);
-                if (!(val2 instanceof Literal)) {
-                    Value val1 = Expressions.toValue(e1, factory, q);
-                    Expressions.setImplicitTypes(val1, val2, e1.getJavaType(), q);
-                    inExpr = factory.contains(val2, val1);
-                    return isNegated() ? factory.not(inExpr) : inExpr;
-                } else if (((Literal)val2).getParseType() == Literal.TYPE_COLLECTION) {
-                    Collection coll = (Collection)((Literal)val2).getValue();
+
+                Class<?> e1JavaType = e1.getJavaType();
+                Class<?> e2jt = e2.getJavaType();
+
+                // array
+                if (BindableParameter.class.isInstance(e2) && BindableParameter.class.cast(e2).value()
!= null &&
+                    ((e2jt.isArray() && e2jt.getComponentType().equals(e1JavaType))
+                    || (Class.class.isInstance(e2jt) ||
+                        (ParameterizedType.class.isInstance(e2jt)
+                            && ParameterizedType.class.cast(e2jt).getActualTypeArguments().length
> 0
+                            && e1JavaType.equals(ParameterizedType.class.cast(e2jt).getActualTypeArguments()[0])))))
{
+                    final BindableParameter bp = BindableParameter.class.cast(e2);
+                    final Object value = bp.value();
+
                     _exps.clear();
-                    for (Object v : coll) {
-                        add(new Expressions.Equal(e1,v));
+                    if (value == null) {
+                        add(new Expressions.Equal(e1, null));
+                    } else if (value.getClass().isArray()) {
+                        final int len = Array.getLength(value);
+                        for (int i = 0; i < len; i++) {
+                            add(new Expressions.Equal(e1, Array.get(value, i)));
+                        }
+                    } else if (Collection.class.isInstance(value)) {
+                        for (final Object item : Collection.class.cast(value)) {
+                            add(new Expressions.Equal(e1, item));
+                        }
+                    }
+                } else {
+                    // normal case
+                    Value val2 = Expressions.toValue(e2, factory, q);
+                    if (!(val2 instanceof Literal)) {
+                        Value val1 = Expressions.toValue(e1, factory, q);
+                        Expressions.setImplicitTypes(val1, val2, e1.getJavaType(), q);
+                        inExpr = factory.contains(val2, val1);
+                        return isNegated() ? factory.not(inExpr) : inExpr;
+                    } else if (((Literal)val2).getParseType() == Literal.TYPE_COLLECTION)
{
+                        Collection coll = (Collection)((Literal)val2).getValue();
+                        _exps.clear();
+                        for (Object v : coll) {
+                            add(new Expressions.Equal(e1,v));
+                        }
                     }
                 }
             } 

Modified: openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java?rev=1535816&r1=1535815&r2=1535816&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java
(original)
+++ openjpa/branches/2.3.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/ParameterExpressionImpl.java
Fri Oct 25 18:51:08 2013
@@ -39,16 +39,17 @@ import org.apache.openjpa.util.InternalE
  * @param <T> the type of value held by this parameter.
  */
 class ParameterExpressionImpl<T> extends ExpressionImpl<T> 
-    implements ParameterExpression<T> {
+    implements ParameterExpression<T>, BindableParameter {
     private String _name;
     private int _index = 0; // index of the parameter as seen by the kernel, not position
-	
-	/**
-	 * Construct a Parameter of given expected value type and name.
-	 * 
-	 * @param cls expected value type
-	 * @param name name of the parameter which can be null.
-	 */
+    private Object value;
+
+    /**
+     * Construct a Parameter of given expected value type and name.
+     *
+     * @param cls expected value type
+     * @param name name of the parameter which can be null.
+     */
     public ParameterExpressionImpl(Class<T> cls, String name) {
         super(cls);
         if (name != null)
@@ -84,7 +85,17 @@ class ParameterExpressionImpl<T> extends
 
         return buf.toString();
     }
-    
+
+    @Override
+    public void setValue(Object value) {
+        this.value = value;
+    }
+
+    @Override
+    public Object value() {
+        return value;
+    }
+
     @Override
     public Value toValue(ExpressionFactory factory, CriteriaQueryImpl<?> q) {
         Class<?> clzz = getJavaType();



Mime
View raw message