protected Value getVariable(String var) { Context c = ctx(); Value v = c.getVariable(var); if (v != null) return v; if (c.getParent() != null) return c.getParent().findVariable(var); return null; }
private static Context clone(Context orig, Context parent) { Context myParent = null; if (parent == null) { Context origParent = orig.getParent(); if (origParent != null) myParent = clone(orig.getParent(), null); } else myParent = parent; Context newCtx = new Context(orig.parsed, null, myParent); newCtx.from = orig.from; newCtx.meta = orig.meta; newCtx.schemaAlias = orig.schemaAlias; newCtx.setSchemas(orig.getSchemas()); newCtx.setVariables(orig.getVariables()); newCtx.cloneFrom = orig; Object select = orig.getSelect(); if (select != null) newCtx.setSelect(((SelectImpl)select).clone(newCtx)); newCtx.subquery = orig.subquery; List<Context> subsels = orig.getSubselContexts(); if (subsels != null) { for (Context subsel : subsels) newCtx.addSubselContext(clone(subsel, newCtx)); } return newCtx; } }
public void addToContext(ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl<?> q) { String alias = q.getAlias(this); Value var = factory.newBoundVariable(alias, AbstractExpressionBuilder.TYPE_OBJECT); var.setMetaData(_entity.meta); Context currContext = q.ctx(); currContext.addSchema(alias, _entity.meta); currContext.addVariable(alias, var); if (currContext.schemaAlias == null) currContext.schemaAlias = alias; }
private Value getSubquery(JPQLNode node) { final boolean subclasses = true; // parse the subquery ParsedJPQL parsed = new ParsedJPQL(node.parser.jpql, node); Context subContext = new Context(parsed, null, ctx()); contexts.push(subContext); ClassMetaData candidate = getCandidateMetaData(node); Subquery subq = subContext.getSubquery(); if (subq == null) { subq = factory.newSubquery(candidate, subclasses, nextAlias()); subContext.setSubquery(subq); } subq.setMetaData(candidate); // evaluate from clause for resolving variables defined in subquery JPQLNode from = node.getChild(1); subContext.from = evalFromClause(from, true); try { QueryExpressions subexp = getQueryExpressions(); subq.setQueryExpressions(subexp); if (subexp.projections.length > 0) checkEmbeddable(subexp.projections[0]); return subq; } finally { // remove the subquery parse context contexts.pop(); } }
if (ctx().getParent() != null && ctx().getVariable(path.getSchemaAlias()) == null) { path.setSubqueryContext(ctx(), name);
Context ctxt = rete.getContext(); Variable var = ctxt.getVariable( "desc" ); String desc = var.stringValue( ctxt );
sel.setContext(lctx); if (parent == null && lctx.getSubselContexts() != null) { List<Context> subselCtxs = lctx.getSubselContexts(); for (Context subselCtx : subselCtxs) { Select subsel = (Select) subselCtx.getSelect(); Subquery subquery = subselCtx.getSubquery(); subsel.setParent(sel, subquery.getCandidateAlias());
/** * Populate a kernel expression tree by translating the components of this * receiver with the help of the given {@link ExpressionFactory}. */ QueryExpressions getQueryExpressions(ExpressionFactory factory) { Context context = new Context(null, null, null); _contexts.get().push(context); try { return new CriteriaExpressionBuilder().getQueryExpressions(factory, this); }finally{ _contexts.remove(); } }
/** * When a PCPath is in subselect, and it is simply a navigation * from the parent root, the joins involved in this PCPath * must happen in the main select. */ private boolean navigateFromParentRootInSubselect(Select sel) { if (sel.getParent() == null) return false; Iterator itr = (_actions == null) ? null : _actions.iterator(); boolean hasVar = false; boolean startsWithSubquery = false; while (itr != null && itr.hasNext()) { Action action = (Action) itr.next(); if (action.op == Action.VAR) hasVar = true; else if (action.op == Action.SUBQUERY) startsWithSubquery = true; } return !hasVar && !startsWithSubquery && sel.ctx().getSchema(_schemaAlias) == null; }
private Integer findAliasForQuery(Table table, PathJoins pj, Object key, boolean create) { Integer i = null; SelectImpl sel = this; String alias = _schemaAlias; if (isPathInThisContext(pj) || table.isAssociation()) alias = null; // find the context where this alias is defined Context ctx = (alias != null) ? _ctx.findContext(alias) : null; if (ctx != null) sel = (SelectImpl) ctx.getSelect(); if (!create) i = sel.findAlias(table, key); // find in parent and in myself else i = sel.getAlias(table, key); // find in myself if (i != null) return i; if (create) { // create here i = sel.createAlias(table, key); } else if (ctx != null && ctx != ctx()) { // create in other select i = ((SelectImpl)ctx.getSelect()).createAlias(table, key); } return i; }
private Expression getSubquery(String alias, Path path, Expression exp) { Value var = getVariable(alias, true); // this bind is for validateMapPath to resolve alias Expression bindVar = factory.bindVariable(var, path); FieldMetaData fmd = path.last(); ClassMetaData candidate = getFieldType(fmd); if (candidate == null && fmd.isElementCollection()) candidate = fmd.getDefiningMetaData(); setCandidate(candidate, alias); Context subContext = ctx(); Subquery subquery = ctx().getSubquery(); if (subquery == null){ subquery = factory.newSubquery(candidate, true, alias); subContext.setSubquery(subquery); } else { subquery.setSubqAlias(alias); } Path subpath = factory.newPath(subquery); subpath.setSchemaAlias(path.getCorrelationVar()); subpath.setMetaData(candidate); subquery.setMetaData(candidate); if (fmd.isElementCollection()) exp = and(exp, bindVar); else exp = and(exp, factory.equal(path, subpath)); return exp; }
public void setSubquery(Subquery subquery) { this.subquery = subquery; this.select = subquery.getSelect(); parent.addSubselContext(this); }
/** * Adds a join condition to the given expression. * * @param node the node to check * @param inner whether or not the join should be an inner join * @param exp an existing expression to AND, or null if none * @return the Expression with the join condition added */ private Expression addJoin(JPQLNode node, boolean inner, Expression exp) { // the type will be the declared type for the field JPQLNode firstChild = firstChild(node); Path path = null; if (firstChild.id == JJTQUALIFIEDPATH) path = (Path) getQualifiedPath(firstChild); else path = getPath(firstChild, false, inner); JPQLNode alias = node.getChildCount() >= 2 ? right(node) : null; // OPENJPA-15 support subquery's from clause do not start with // identification_variable_declaration() if (inner && ctx().getParent() != null && ctx().schemaAlias == null) { return getSubquery(alias.text, path, exp); } return addJoin(path, alias, exp); }
protected boolean isSeenVariable(String var) { Context c = ctx().findContext(var); if (c != null) return true; return false; }
protected void addVariableToContext(String id, Value var) { ctx().addVariable(id, var); }
protected void addSchemaToContext(String id, ClassMetaData meta) { ctx().addSchema(id.toLowerCase(), meta); }
private Value getSubquery(JPQLNode node) { final boolean subclasses = true; // parse the subquery ParsedJPQL parsed = new ParsedJPQL(node.parser.jpql, node); Context subContext = new Context(parsed, null, ctx()); contexts.push(subContext); ClassMetaData candidate = getCandidateMetaData(node); Subquery subq = subContext.getSubquery(); if (subq == null) { subq = factory.newSubquery(candidate, subclasses, nextAlias()); subContext.setSubquery(subq); } subq.setMetaData(candidate); // evaluate from clause for resolving variables defined in subquery JPQLNode from = node.getChild(1); subContext.from = evalFromClause(from, true); try { QueryExpressions subexp = getQueryExpressions(); subq.setQueryExpressions(subexp); if (subexp.projections.length > 0) checkEmbeddable(subexp.projections[0]); return subq; } finally { // remove the subquery parse context contexts.pop(); } }
if (ctx().getParent() != null && ctx().getVariable(path.getSchemaAlias()) == null) { path.setSubqueryContext(ctx(), name);
/** * Given an alias find the variable in JPQL contexts. */ public Value findVariable(String alias) { Value var = getVariable(alias); if (var != null) return var; for (Context p = parent; p != null; ) { var = p.getVariable(alias); if (var != null) return var; p = p.parent; } return null; } }
sel.setContext(lctx); if (parent == null && lctx.getSubselContexts() != null) { List<Context> subselCtxs = lctx.getSubselContexts(); for (Context subselCtx : subselCtxs) { Select subsel = (Select) subselCtx.getSelect(); Subquery subquery = subselCtx.getSubquery(); subsel.setParent(sel, subquery.getCandidateAlias());