/** * Compiles the query and returns the mapped LDAP filter. * @return the mapped LDAP filter or null if no filter */ public String compile() { // additional filter if (filterExpr != null) { filterExpr.evaluate(this); if (!stack.isEmpty()) { Object object = stack.pop(); if (object instanceof Filter) { Filter additionalFilter = (Filter) object; return additionalFilter.toString(); } throw new NucleusException("Unexpected element on stack: object=" + object); } throw new NucleusException("Unexpected empty stack"); } return null; }
private boolean isAggregate(Expression expr) { if (expr == null) return false; if (expr instanceof InvokeExpression) { String methodName = ((InvokeExpression) expr).getOperation(); return AGGREGATE_METHODS.contains(methodName); } else { return isAggregate(expr.getRight()) || isAggregate(expr.getLeft()); } }
if (UNSUPPORTED_OPERATORS.contains(expr.getOperator())) { throw new UnsupportedDatastoreOperatorException(query.getSingleStringQuery(), expr.getOperator()); if (expr.getOperator() != null && !expr.getOperator().equals(Expression.OP_EQ) && !expr.getOperator().equals(Expression.OP_OR)) { throw new UnsupportedDatastoreFeatureException("'or' filters can only check equality"); if (expr.getOperator().equals(Expression.OP_AND)) { addExpression(expr.getLeft(), qd); addExpression(expr.getRight(), qd); } else if (expr.getOperator().equals(Expression.OP_OR)) { boolean reset = !qd.isOrExpression; qd.isOrExpression = true; addExpression(expr.getLeft(), qd); addExpression(expr.getRight(), qd); } else if (DATANUCLEUS_OP_TO_APPENGINE_OP.get(expr.getOperator()) == null) { throw new UnsupportedDatastoreOperatorException(query.getSingleStringQuery(), expr.getOperator()); } else if (expr.getLeft() instanceof PrimaryExpression) { addLeftPrimaryExpression( (PrimaryExpression) expr.getLeft(), expr.getOperator(), expr.getRight(), qd); } else { addExpression(expr.getLeft(), qd); addExpression(expr.getRight(), qd); addExpression(expr.getLeft(), qd); addExpression(expr.getRight(), qd);
private PrimaryExpression getPrimaryExpresionFromJoinAlias(QueryCompilation compilation, String alias) { for (Expression fromExpr : compilation.getExprFrom()) { for (Expression expr = fromExpr.getRight(); expr != null; expr = expr.getRight()) { if (expr instanceof JoinExpression && alias.equals(expr.getAlias())) { return ((JoinExpression) expr).getPrimaryExpression(); } } } return null; }
if (expr.getLeft() instanceof DyadicExpression && expr.getLeft().getOperator() == Expression.OP_CAST) if (expr.getLeft().getLeft() instanceof PrimaryExpression) exprCastName = "CAST_" + ((PrimaryExpression)expr.getLeft().getLeft()).getId(); else if (expr.getLeft().getLeft() instanceof VariableExpression) exprCastName = "CAST_" + ((VariableExpression)expr.getLeft().getLeft()).getId(); else if (expr.getLeft().getLeft() instanceof InvokeExpression) exprCastName = "CAST_" + expr.getLeft().getLeft(); throw new NucleusException("Don't currently support cast of " + expr.getLeft().getLeft()); expr.getLeft().getLeft().evaluate(this); sqlExpr = stack.pop(); JavaTypeMapping mapping = sqlExpr.getJavaTypeMapping(); Literal castLitExpr = (Literal)expr.getLeft().getRight(); Class castType = resolveClass((String)castLitExpr.getLiteral()); AbstractClassMetaData castCmd = ec.getMetaDataManager().getMetaDataForClass(castType, clr); expr.getLeft().evaluate(this); sqlExpr = stack.pop(); Literal castLitExpr = (Literal)expr.getLeft().getRight(); AbstractClassMetaData castCmd = ec.getMetaDataManager().getMetaDataForClass(resolveClass((String)castLitExpr.getLiteral()), clr);
for (int i=0;i<resultExprs.length;i++) String alias = resultExprs[i].getAlias(); if (alias != null && resultAliases == null) resultExprs[i].evaluate(this); SQLExpression sqlExpr = stack.pop(); int[] cols = stmt.select(sqlExpr, alias); unionStmt.setAllowUnions(false); resultExprs[i].evaluate(this); sqlExpr = stack.pop(); unionStmt.select(sqlExpr, alias);
private void processJoin(ExpressionBuilderHelper h, QueryCompilation compilation, FetchPlan fetchPlan, boolean hasWhere) { if (compilation.getExprFrom() == null) return; for (Expression fromExpr : compilation.getExprFrom()) { if (!(fromExpr instanceof ClassExpression)) { throw new NucleusDataStoreException("Unsupported from expression: " + fromExpr); } Expression expr = fromExpr.getRight(); while (expr != null) { if (expr instanceof JoinExpression) { JoinExpression je = (JoinExpression) expr; processJoinExpression(je.getPrimaryExpression(), je.getAlias(), h, compilation, true, hasWhere, false); h.sb.append("))"); // Either this was already true or will be after the first call hasWhere = true; } else { throw new NucleusDataStoreException("Unsupported expression found inside a join expression: " + fromExpr.getRight()); } expr = expr.getRight(); } } }
appendContainsExpression(h, invocation, not); } else if ("size".equals(oper)) { boolean isEmpty = invocation.getParent().getOperator() == Expression.OP_EQ;
else if (joinedExpr instanceof DyadicExpression && joinedExpr.getOperator() == Expression.OP_CAST) joinPrimExpr = (PrimaryExpression)joinedExpr.getLeft(); String castClassName = (String) ((Literal)joinedExpr.getRight()).getLiteral(); castCls = clr.classForName(castClassName); joinOnExpr.evaluate(this); BooleanExpression joinOnSqlExpr = (BooleanExpression) stack.pop(); processingOnClause = false; rightExpr = rightExpr.getRight(); joinOnExpr.evaluate(this); BooleanExpression joinOnSqlExpr = (BooleanExpression) stack.pop(); processingOnClause = false; rightExpr = rightExpr.getRight();
private PrimaryExpression getPrimaryExpresionFromJoinAlias(QueryCompilation compilation, String alias) { for (Expression fromExpr : compilation.getExprFrom()) { for (Expression expr = fromExpr.getRight(); expr != null; expr = expr.getRight()) { if (expr instanceof JoinExpression && alias.equals(expr.getAlias())) { return ((JoinExpression) expr).getPrimaryExpression(); } } } return null; }
/** * * Adds the proper ordering to the parser and returns the resulting expressions. * * @param ordering the orderBy string * @return an ordered expression array */ public Expression[] compileOrdering(String ordering) { if (ordering == null) { return null; } Node[] node = parser.parseOrder(ordering); Expression[] expr = new Expression[node.length]; for (int i = 0; i < node.length; i++) { ExpressionCompiler comp = new ExpressionCompiler(); comp.setSymbolTable(symbolTable); expr[i] = comp.compileOrderExpression(node[i]); expr[i].bind(symbolTable); } return expr; } }
private void processJoin(ExpressionBuilderHelper h, QueryCompilation compilation, FetchPlan fetchPlan, boolean hasWhere) { if (compilation.getExprFrom() == null) return; for (Expression fromExpr : compilation.getExprFrom()) { if (!(fromExpr instanceof ClassExpression)) { throw new NucleusDataStoreException("Unsupported from expression: " + fromExpr); } Expression expr = fromExpr.getRight(); while (expr != null) { if (expr instanceof JoinExpression) { JoinExpression je = (JoinExpression) expr; processJoinExpression(je.getPrimaryExpression(), je.getAlias(), h, compilation, true, hasWhere, false); h.sb.append("))"); // Either this was already true or will be after the first call hasWhere = true; } else { throw new NucleusDataStoreException("Unsupported expression found inside a join expression: " + fromExpr.getRight()); } expr = expr.getRight(); } } }
appendContainsExpression(h, invocation, not); } else if ("size".equals(oper)) { boolean isEmpty = invocation.getParent().getOperator() == Expression.OP_EQ;
private boolean isAggregate(Expression expr) { if (expr == null) return false; if (expr instanceof InvokeExpression) { String methodName = ((InvokeExpression) expr).getOperation(); return AGGREGATE_METHODS.contains(methodName); } else { return isAggregate(expr.getRight()) || isAggregate(expr.getLeft()); } }
/** * Method to compile the grouping clause of the query into the SQLStatement. * @param stmt SELECT statement */ protected void compileGrouping(SelectStatement stmt) { if (compilation.getExprGrouping() != null) { // Apply any grouping to the statement compileComponent = CompilationComponent.GROUPING; Expression[] groupExprs = compilation.getExprGrouping(); for (Expression groupExpr : groupExprs) { stmt.addGroupingExpression((SQLExpression)groupExpr.evaluate(this)); } compileComponent = null; } }