Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

reset SelectImpl for subsequent query execution

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/subquery@794969 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit aad7fb3866a5fddc9234d24f432b74c6c12855c1 1 parent 83367a9
Fay Wang authored
View
30 openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java
@@ -52,6 +52,7 @@
import org.apache.openjpa.jdbc.sql.SQLBuffer;
import org.apache.openjpa.jdbc.sql.SQLExceptions;
import org.apache.openjpa.jdbc.sql.Select;
+import org.apache.openjpa.jdbc.sql.SelectImpl;
import org.apache.openjpa.jdbc.sql.Union;
import org.apache.openjpa.kernel.ExpressionStoreQuery;
import org.apache.openjpa.kernel.Filters;
@@ -60,6 +61,7 @@
import org.apache.openjpa.kernel.QueryContext;
import org.apache.openjpa.kernel.QueryHints;
import org.apache.openjpa.kernel.exps.Constant;
+import org.apache.openjpa.kernel.exps.Context;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.kernel.exps.ExpressionParser;
import org.apache.openjpa.kernel.exps.FilterListener;
@@ -143,13 +145,28 @@ public boolean supportsDataStoreExecution() {
protected ExpressionFactory getExpressionFactory(ClassMetaData meta) {
return new JDBCExpressionFactory((ClassMapping) meta);
}
+
+ private void resetSelect(Context ctx) {
+ List<Context> subselCtxs = ctx.getSubselContexts();
+ if (subselCtxs != null) {
+ for (Context subselCtx : subselCtxs) {
+ SelectImpl sel = (SelectImpl)subselCtx.getSelect();
+ sel.reset();
+ resetSelect(subselCtx);
+ }
+ }
+ }
protected ResultObjectProvider executeQuery(Executor ex,
ClassMetaData base, ClassMetaData[] metas, boolean subclasses,
ExpressionFactory[] facts, QueryExpressions[] exps, Object[] params,
Range range) {
- if (exps[0].ctx() != null)
- exps[0].ctx().resetAliasCount();
+ Context expCtx = exps[0].ctx();
+ if (expCtx != null) {
+ expCtx.resetAliasCount();
+ expCtx.setSelect(null);
+ resetSelect(expCtx);
+ }
if (metas.length > 1 && exps[0].isAggregate())
throw new UserException(Localizer.forPackage(JDBCStoreQuery.class).
get("mult-mapping-aggregate", Arrays.asList(metas)));
@@ -449,8 +466,13 @@ protected Number executeUpdate(Executor ex, ClassMetaData base,
private Number executeBulkOperation(ClassMetaData[] metas,
boolean subclasses, ExpressionFactory[] facts, QueryExpressions[] exps,
Object[] params, Map updates) {
- if (exps[0].ctx() != null)
- exps[0].ctx().resetAliasCount();
+ Context expCtx = exps[0].ctx();
+ if (ctx != null) {
+ expCtx.resetAliasCount();
+ expCtx.setSelect(null);
+ resetSelect(expCtx);
+ }
+
// we cannot execute a bulk delete statement when have mappings in
// multiple tables, so indicate we want to use in-memory with null
ClassMapping[] mappings = (ClassMapping[]) metas;
View
13 openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java
@@ -20,6 +20,7 @@
import java.io.Serializable;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.apache.openjpa.jdbc.meta.ClassMapping;
@@ -29,6 +30,7 @@
import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.kernel.exps.AbstractExpressionVisitor;
import org.apache.openjpa.kernel.exps.Constant;
+import org.apache.openjpa.kernel.exps.Context;
import org.apache.openjpa.kernel.exps.Expression;
import org.apache.openjpa.kernel.exps.QueryExpressions;
import org.apache.openjpa.kernel.exps.Subquery;
@@ -129,11 +131,14 @@ private Select newSelect(ExpContext ctx, Select parent,
if (sel.ctx() == null)
sel.setContext(exps.ctx());
- if (parent == null && exps.ctx().getSubselContext() != null) {
+ if (parent == null && exps.ctx().getSubselContexts() != null) {
// this is the case subselect was created before parent got created
- Select subsel = (Select) exps.ctx().getSubselContext().getSelect();
- Subquery subquery = exps.ctx().getSubselContext().getSubquery();
- subsel.setParent(sel, subquery.getCandidateAlias());
+ List<Context> subselCtxs = exps.ctx().getSubselContexts();
+ for (Context subselCtx : subselCtxs) {
+ Select subsel = (Select) subselCtx.getSelect();
+ Subquery subquery = subselCtx.getSubquery();
+ subsel.setParent(sel, subquery.getCandidateAlias());
+ }
}
initialize(sel, ctx, exps, state);
View
33 openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
@@ -2213,6 +2213,37 @@ public Joins setSubselect(String alias) {
*/
private static class Placeholder {
}
+
+ public void reset() {
+ _aliases = null;
+ _eager = null;
+ _eagerKeys = null;
+ _expectedResultCount = 0;
+ _flags = 0;
+ _from = null;
+ _grouped = null;
+ _grouping = null;
+ _having = null;
+ _joins = null;
+ _joinSyntax = 0;
+ _nullIds = 0;
+ _ordered = null;
+ _ordering = null;
+ _orders = 0;
+ _outer = null;
+ _parent = null;
+ _placeholders = 0;
+ _preJoins = null;
+ _schemaAlias = null;
+ _selects._aliases = null;
+ _selects._ids = null;
+ _subPath = null;
+ _subsels = null;
+ _tables = null;
+ _where = null;
+
+ }
+
/**
* Key type used for aliases.
@@ -2921,6 +2952,8 @@ public void moveJoinsToParent() {
}
private void addJoinsToParent(SelectImpl parent, Join join) {
+ if (parent._aliases == null)
+ return;
Object aliases[] = parent._aliases.values().toArray();
boolean found1 = false;
boolean found2 = false;
View
30 openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/Context.java
@@ -19,7 +19,9 @@
package org.apache.openjpa.kernel.exps;
import java.io.Serializable;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.ParsedJPQL;
@@ -38,7 +40,7 @@
public Subquery subquery;
public Expression from = null;
private Context parent = null;
- private Context subsel = null;
+ private List<Context> subsels = null;
private Object select = null;
private int aliasCount = -1;
private Map<String,Value> variables = new HashMap<String,Value>();
@@ -51,14 +53,14 @@ public Context(ParsedJPQL parsed, Subquery subquery, Context parent) {
this.parent = parent;
if (subquery != null) {
this.select = subquery.getSelect();
- parent.setSubselContext(this);
+ parent.addSubselContext(this);
}
}
public void setSubquery(Subquery subquery) {
this.subquery = subquery;
this.select = subquery.getSelect();
- parent.setSubselContext(this);
+ parent.addSubselContext(this);
}
public ClassMetaData meta() {
@@ -118,16 +120,18 @@ public Object getSelect() {
* Register the subquery context in this context.
* @param sub
*/
- private void setSubselContext(Context sub) {
- subsel = sub;
+ private void addSubselContext(Context sub) {
+ if (subsels == null)
+ subsels = new ArrayList<Context>();
+ subsels.add(sub);
}
/**
* Returns the subquery context.
* @return
*/
- public Context getSubselContext() {
- return subsel;
+ public List<Context> getSubselContexts() {
+ return subsels;
}
/**
@@ -187,10 +191,14 @@ public Context findContext(String alias) {
return p;
p = p.parent;
}
- if (subsel != null) {
- var = subsel.getVariable(alias);
- if (var != null)
- return subsel;
+ if (subsels != null) {
+ for (Context subsel : subsels) {
+ if (subsel != null) {
+ var = subsel.getVariable(alias);
+ if (var != null)
+ return subsel;
+ }
+ }
}
return null;
}
Please sign in to comment.
Something went wrong with that request. Please try again.