Skip to content

Instantly share code, notes, and snippets.

@rmannibucau
Created October 25, 2013 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmannibucau/7156344 to your computer and use it in GitHub Desktop.
Save rmannibucau/7156344 to your computer and use it in GitHub Desktop.
Index: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CollectionParam.java
===================================================================
--- openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CollectionParam.java (revision 1535760)
+++ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CollectionParam.java (working copy)
@@ -18,6 +18,7 @@
*/
package org.apache.openjpa.jdbc.kernel.exps;
+import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
@@ -33,7 +34,7 @@
import org.apache.openjpa.util.ImplHelper;
/**
- * A collection-valued input parameter in an in-expression.
+ * A collection-valued or array-valued input parameter in an in-expression.
*
* @author Catalina Wei
*/
@@ -121,8 +122,11 @@
public Object discValue[] = null;
ParamExpState(Object params) {
- if (params instanceof Collection)
+ if (params instanceof Collection) {
size = ((Collection) params).size();
+ } else if (params != null && params.getClass().isArray()) {
+ size = Array.getLength(params);
+ }
sqlValue = new Object[size];
otherLength = new int[size];
mapping = new ClassMapping[size];
@@ -144,41 +148,57 @@
ParamExpState pstate = (ParamExpState) state;
Object value = getValue(ctx.params);
- if (!(value instanceof Collection))
- throw new IllegalArgumentException(_loc.get(
- "not-collection-parm", _key).toString());
+ if (value != null && value.getClass().isArray()) {
+ final int length = Array.getLength(value);
+ if (length == 0) {
+ throw new IllegalArgumentException(_loc.get(
+ "empty-collection-parm", _key).toString());
+ }
- if (((Collection) value).isEmpty())
- throw new IllegalArgumentException(_loc.get(
- "empty-collection-parm", _key).toString());
+ for (int i = 0; i < length; i++) {
+ iterate(sel, ctx, other, otherState, pstate, i, Array.get(value, i));
+ }
+ } else {
+ if (!(value instanceof Collection))
+ throw new IllegalArgumentException(_loc.get(
+ "not-collection-parm", _key).toString());
- Iterator itr = ((Collection) value).iterator();
- for (int i = 0; i < pstate.size && itr.hasNext(); i++) {
- Object val = itr.next();
- if (other != null && !_container) {
- pstate.sqlValue[i] = other.toDataStoreValue(sel, ctx,
- otherState, val);
- pstate.otherLength[i] = other.length(sel, ctx, otherState);
- if (other instanceof Type) {
- pstate.mapping[i] = ctx.store.getConfiguration().
- getMappingRepositoryInstance().getMapping((Class) val,
- ctx.store.getContext().getClassLoader(), true);
- pstate.disc[i] = pstate.mapping[i].getDiscriminator();
- pstate.discValue[i] = pstate.disc[i] != null ?
- pstate.disc[i].getValue() : null;
- }
- } else if (ImplHelper.isManageable(val)) {
- ClassMapping mapping = ctx.store.getConfiguration().
- getMappingRepositoryInstance().getMapping(val.getClass(),
- ctx.store.getContext().getClassLoader(), true);
- pstate.sqlValue[i] = mapping.toDataStoreValue(val,
- mapping.getPrimaryKeyColumns(), ctx.store);
- pstate.otherLength[i] = mapping.getPrimaryKeyColumns().length;
- } else
- pstate.sqlValue[i] = val;
+ if (((Collection) value).isEmpty())
+ throw new IllegalArgumentException(_loc.get(
+ "empty-collection-parm", _key).toString());
+
+ Iterator itr = ((Collection) value).iterator();
+ for (int i = 0; i < pstate.size && itr.hasNext(); i++) {
+ Object val = itr.next();
+ iterate(sel, ctx, other, otherState, pstate, i, val);
+ }
}
}
+ private void iterate(Select sel, ExpContext ctx, Val other, ExpState otherState, ParamExpState pstate, int i, Object val) {
+ if (other != null && !_container) {
+ pstate.sqlValue[i] = other.toDataStoreValue(sel, ctx,
+ otherState, val);
+ pstate.otherLength[i] = other.length(sel, ctx, otherState);
+ if (other instanceof Type) {
+ pstate.mapping[i] = ctx.store.getConfiguration().
+ getMappingRepositoryInstance().getMapping((Class) val,
+ ctx.store.getContext().getClassLoader(), true);
+ pstate.disc[i] = pstate.mapping[i].getDiscriminator();
+ pstate.discValue[i] = pstate.disc[i] != null ?
+ pstate.disc[i].getValue() : null;
+ }
+ } else if (ImplHelper.isManageable(val)) {
+ ClassMapping mapping = ctx.store.getConfiguration().
+ getMappingRepositoryInstance().getMapping(val.getClass(),
+ ctx.store.getContext().getClassLoader(), true);
+ pstate.sqlValue[i] = mapping.toDataStoreValue(val,
+ mapping.getPrimaryKeyColumns(), ctx.store);
+ pstate.otherLength[i] = mapping.getPrimaryKeyColumns().length;
+ } else
+ pstate.sqlValue[i] = val;
+ }
+
public void appendTo(Select sel, ExpContext ctx, ExpState state,
SQLBuffer sql, int index) {
ParamExpState pstate = (ParamExpState) state;
Index: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java
===================================================================
--- openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java (revision 1535760)
+++ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/TestTypesafeCriteria.java (working copy)
@@ -623,6 +623,22 @@
}
}, q, jpql);
}
+
+ public void testParameters6() {
+ String jpql = "SELECT c FROM Customer c Where c.status IN :coll";
+
+ CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
+ Root<Customer> c = q.from(Customer.class);
+ ParameterExpression<Long[]> param1 = cb.parameter(Long[].class, "coll");
+ q.where(c.get(Customer_.status).in(param1));
+ q.select(c);
+
+ assertEquivalence(new QueryDecorator() {
+ public void decorate(Query q) {
+ q.setParameter("coll", new Long[] { 1L, 2L });
+ }
+ }, q, jpql);
+ }
public void testSelectList1() {
String jpql = "SELECT v.location.street, KEY(i).title, VALUE(i) FROM "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment