Search in sources :

Example 1 with ArrayExpression

use of org.datanucleus.query.expression.ArrayExpression in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method getInvokedSqlExpressionForInvokeExpression.

protected SQLExpression getInvokedSqlExpressionForInvokeExpression(InvokeExpression expr) {
    Expression invokedExpr = expr.getLeft();
    SQLExpression invokedSqlExpr = null;
    if (invokedExpr == null) {
    // Static method
    } else if (invokedExpr instanceof PrimaryExpression) {
        processPrimaryExpression((PrimaryExpression) invokedExpr);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof Literal) {
        processLiteral((Literal) invokedExpr);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof ParameterExpression) {
        // TODO May be needed to set the second parameter to "false" here and then if the method
        // being invoked needs the parameters as a normal SQLLiteral then allow it to convert it itself
        processParameterExpression((ParameterExpression) invokedExpr, true);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof InvokeExpression) {
        processInvokeExpression((InvokeExpression) invokedExpr);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof VariableExpression) {
        processVariableExpression((VariableExpression) invokedExpr);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof ArrayExpression) {
        ArrayExpression arrExpr = (ArrayExpression) invokedExpr;
        SQLExpression[] arrSqlExprs = new SQLExpression[arrExpr.getArraySize()];
        for (int i = 0; i < arrExpr.getArraySize(); i++) {
            Expression arrElemExpr = arrExpr.getElement(i);
            arrElemExpr.evaluate(this);
            arrSqlExprs[i] = stack.pop();
        }
        JavaTypeMapping m = exprFactory.getMappingForType(Object[].class, false);
        invokedSqlExpr = new org.datanucleus.store.rdbms.sql.expression.ArrayExpression(stmt, m, arrSqlExprs);
    } else if (invokedExpr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) invokedExpr;
        dyExpr.evaluate(this);
        invokedSqlExpr = stack.pop();
    } else {
        throw new NucleusException("Dont currently support invoke expression " + invokedExpr);
    }
    return invokedSqlExpr;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) CaseExpression(org.datanucleus.query.expression.CaseExpression) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) Expression(org.datanucleus.query.expression.Expression) TypeExpression(org.datanucleus.query.expression.TypeExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) TemporalLiteral(org.datanucleus.store.rdbms.sql.expression.TemporalLiteral) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral) Literal(org.datanucleus.query.expression.Literal) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 2 with ArrayExpression

use of org.datanucleus.query.expression.ArrayExpression in project datanucleus-core by datanucleus.

the class InMemoryExpressionEvaluator method getValueForInvokeExpression.

/**
 * Method to evaluate an InvokeExpression.
 * Will navigate along chained invocations, evaluating the first one, then the second one etc
 * until it gets the value for the passed in expression.
 * @param invokeExpr The InvokeExpression
 * @return The value
 */
public Object getValueForInvokeExpression(InvokeExpression invokeExpr) {
    String method = invokeExpr.getOperation();
    if (invokeExpr.getLeft() == null) {
        // Static function
        if (method.toLowerCase().equals("count")) {
            Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
            SetExpression setexpr = new SetExpression(coll, candidateAlias);
            Expression paramExpr = invokeExpr.getArguments().get(0);
            if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
                // No dups in HashSet
                coll = new HashSet(coll);
            }
            int stackSizeOrig = stack.size();
            Object returnVal = setexpr.count(paramExpr, this);
            while (stack.size() > stackSizeOrig) {
                // Remove any expressions put on the stack while evaluating the aggregate
                stack.pop();
            }
            return returnVal;
        } else if (method.toLowerCase().equals("sum")) {
            Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
            SetExpression setexpr = new SetExpression(coll, candidateAlias);
            Expression paramExpr = invokeExpr.getArguments().get(0);
            if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
                // No dups in HashSet
                coll = new HashSet(coll);
            }
            int stackSizeOrig = stack.size();
            Object returnVal = setexpr.sum(paramExpr, this, state);
            while (stack.size() > stackSizeOrig) {
                // Remove any expressions put on the stack while evaluating the aggregate
                stack.pop();
            }
            return returnVal;
        } else if (method.toLowerCase().equals("avg")) {
            Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
            SetExpression setexpr = new SetExpression(coll, candidateAlias);
            Expression paramExpr = invokeExpr.getArguments().get(0);
            if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
                // No dups in HashSet
                coll = new HashSet(coll);
            }
            int stackSizeOrig = stack.size();
            Object returnVal = setexpr.avg(paramExpr, this, state);
            while (stack.size() > stackSizeOrig) {
                // Remove any expressions put on the stack while evaluating the aggregate
                stack.pop();
            }
            return returnVal;
        } else if (method.toLowerCase().equals("min")) {
            Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
            SetExpression setexpr = new SetExpression(coll, candidateAlias);
            Expression paramExpr = invokeExpr.getArguments().get(0);
            if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
                // No dups in HashSet
                coll = new HashSet(coll);
            }
            int stackSizeOrig = stack.size();
            Object returnVal = setexpr.min(paramExpr, this, state);
            while (stack.size() > stackSizeOrig) {
                // Remove any expressions put on the stack while evaluating the aggregate
                stack.pop();
            }
            return returnVal;
        } else if (method.toLowerCase().equals("max")) {
            Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
            SetExpression setexpr = new SetExpression(coll, candidateAlias);
            Expression paramExpr = invokeExpr.getArguments().get(0);
            if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
                // No dups in HashSet
                coll = new HashSet(coll);
            }
            int stackSizeOrig = stack.size();
            Object returnVal = setexpr.max(paramExpr, this, state);
            while (stack.size() > stackSizeOrig) {
                // Remove any expressions put on the stack while evaluating the aggregate
                stack.pop();
            }
            return returnVal;
        } else {
            // Try to find a supported static method with this name
            InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(null, method);
            if (methodEval != null) {
                return methodEval.evaluate(invokeExpr, null, this);
            }
            NucleusLogger.QUERY.warn("Query contains call to static method " + method + " yet no support is available for in-memory evaluation of this");
            return new InMemoryFailure();
        }
    } else if (invokeExpr.getLeft() instanceof ParameterExpression) {
        // {paramExpr}.method(...)
        Object invokedValue = QueryUtils.getValueForParameterExpression(parameterValues, (ParameterExpression) invokeExpr.getLeft());
        // Invoke method on this object
        Class invokedType = invokedValue != null ? invokedValue.getClass() : invokeExpr.getLeft().getSymbol().getValueType();
        InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
        if (methodEval != null) {
            return methodEval.evaluate(invokeExpr, invokedValue, this);
        }
        if (invokedValue != null) {
            NucleusLogger.QUERY.warn("Query contains call to method " + invokedValue.getClass().getName() + "." + method + " yet no support is available for this");
        } else {
            NucleusLogger.QUERY.warn("Query contains call to static method " + method + " yet no support is available for this");
        }
        return new InMemoryFailure();
    } else if (invokeExpr.getLeft() instanceof PrimaryExpression) {
        // {primaryExpr}.method(...)
        Object invokedValue = getValueForPrimaryExpression((PrimaryExpression) invokeExpr.getLeft());
        if (invokedValue instanceof InMemoryFailure) {
            return invokedValue;
        }
        // Invoke method on this object
        Class invokedType = invokedValue != null ? invokedValue.getClass() : invokeExpr.getLeft().getSymbol().getValueType();
        InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
        if (methodEval != null) {
            return methodEval.evaluate(invokeExpr, invokedValue, this);
        }
        NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
        return new InMemoryFailure();
    } else if (invokeExpr.getLeft() instanceof InvokeExpression) {
        // {invokeExpr}.method(...)
        Object invokedValue = getValueForInvokeExpression((InvokeExpression) invokeExpr.getLeft());
        // Invoke method on this object
        Class invokedType = invokedValue != null ? invokedValue.getClass() : (invokeExpr.getLeft().getSymbol() != null ? invokeExpr.getLeft().getSymbol().getValueType() : null);
        if (invokedType == null) {
            return new InMemoryFailure();
        }
        InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
        if (methodEval != null) {
            return methodEval.evaluate(invokeExpr, invokedValue, this);
        }
        NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
        return new InMemoryFailure();
    } else if (invokeExpr.getLeft() instanceof VariableExpression) {
        // {invokeExpr}.method(...)
        Object invokedValue = getValueForVariableExpression((VariableExpression) invokeExpr.getLeft());
        // Invoke method on this object
        Class invokedType = invokedValue != null ? invokedValue.getClass() : invokeExpr.getLeft().getSymbol().getValueType();
        InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
        if (methodEval != null) {
            return methodEval.evaluate(invokeExpr, invokedValue, this);
        }
        NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
        return new InMemoryFailure();
    } else if (invokeExpr.getLeft() instanceof Literal) {
        // {invokeExpr}.method(...)
        Object invokedValue = ((Literal) invokeExpr.getLeft()).getLiteral();
        // Invoke method on this object
        Class invokedType = invokedValue != null ? invokedValue.getClass() : invokeExpr.getLeft().getSymbol().getValueType();
        InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
        if (methodEval != null) {
            return methodEval.evaluate(invokeExpr, invokedValue, this);
        }
        NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
        return new InMemoryFailure();
    } else if (invokeExpr.getLeft() instanceof ArrayExpression) {
        // {invokeExpr}.method(...)
        Object invokedValue = getValueForArrayExpression((ArrayExpression) invokeExpr.getLeft());
        // Invoke method on this object
        Class invokedType = invokedValue.getClass();
        InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
        if (methodEval != null) {
            return methodEval.evaluate(invokeExpr, invokedValue, this);
        }
        NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
        return new InMemoryFailure();
    } else {
        NucleusLogger.QUERY.warn("No support is available for in-memory evaluation of methods invoked" + " on expressions of type " + invokeExpr.getLeft().getClass().getName());
        return new InMemoryFailure();
    }
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) CaseExpression(org.datanucleus.query.expression.CaseExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) Collection(java.util.Collection) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) HashSet(java.util.HashSet)

Aggregations

ArrayExpression (org.datanucleus.query.expression.ArrayExpression)2 CaseExpression (org.datanucleus.query.expression.CaseExpression)2 CreatorExpression (org.datanucleus.query.expression.CreatorExpression)2 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)2 Expression (org.datanucleus.query.expression.Expression)2 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)2 Literal (org.datanucleus.query.expression.Literal)2 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)2 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)2 VariableExpression (org.datanucleus.query.expression.VariableExpression)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 ClassExpression (org.datanucleus.query.expression.ClassExpression)1 JoinExpression (org.datanucleus.query.expression.JoinExpression)1 OrderExpression (org.datanucleus.query.expression.OrderExpression)1 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)1 TypeExpression (org.datanucleus.query.expression.TypeExpression)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)1