Search in sources :

Example 51 with Literal

use of org.datanucleus.query.expression.Literal in project datanucleus-api-jdo by datanucleus.

the class AbstractJDOQLTypedQuery method getJDOQLForExpression.

public String getJDOQLForExpression(Expression expr) {
    if (expr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) expr;
        Expression left = dyExpr.getLeft();
        Expression right = dyExpr.getRight();
        StringBuilder str = new StringBuilder("(");
        if (dyExpr.getOperator() == Expression.OP_DISTINCT) {
            // Distinct goes in front of the left expression
            str.append("DISTINCT ");
        }
        if (left != null) {
            str.append(getJDOQLForExpression(left));
        }
        // Special cases
        if (dyExpr.getOperator() == Expression.OP_AND) {
            str.append(" && ");
        } else if (dyExpr.getOperator() == Expression.OP_OR) {
            str.append(" || ");
        } else if (dyExpr.getOperator() == Expression.OP_ADD) {
            str.append(" + ");
        } else if (dyExpr.getOperator() == Expression.OP_SUB) {
            str.append(" - ");
        } else if (dyExpr.getOperator() == Expression.OP_MUL) {
            str.append(" * ");
        } else if (dyExpr.getOperator() == Expression.OP_DIV) {
            str.append(" / ");
        } else if (dyExpr.getOperator() == Expression.OP_EQ) {
            str.append(" == ");
        } else if (dyExpr.getOperator() == Expression.OP_GT) {
            str.append(" > ");
        } else if (dyExpr.getOperator() == Expression.OP_LT) {
            str.append(" < ");
        } else if (dyExpr.getOperator() == Expression.OP_GTEQ) {
            str.append(" >= ");
        } else if (dyExpr.getOperator() == Expression.OP_LTEQ) {
            str.append(" <= ");
        } else if (dyExpr.getOperator() == Expression.OP_NOTEQ) {
            str.append(" != ");
        } else if (dyExpr.getOperator() == Expression.OP_DISTINCT) {
        // Processed above
        } else {
            // TODO Support other operators
            throw new UnsupportedOperationException("Dont currently support operator " + dyExpr.getOperator() + " in JDOQL conversion");
        }
        if (right != null) {
            str.append(getJDOQLForExpression(right));
        }
        str.append(")");
        return str.toString();
    } else if (expr instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) expr;
        if (primExpr.getLeft() != null) {
            return getJDOQLForExpression(primExpr.getLeft()) + "." + primExpr.getId();
        }
        return primExpr.getId();
    } else if (expr instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) expr;
        if (paramExpr.getId() != null) {
            return ":" + paramExpr.getId();
        }
        return "?" + paramExpr.getPosition();
    } else if (expr instanceof VariableExpression) {
        VariableExpression varExpr = (VariableExpression) expr;
        return varExpr.getId();
    } else if (expr instanceof InvokeExpression) {
        InvokeExpression invExpr = (InvokeExpression) expr;
        StringBuilder str = new StringBuilder();
        if (invExpr.getLeft() != null) {
            str.append(getJDOQLForExpression(invExpr.getLeft())).append(".");
        }
        str.append(invExpr.getOperation());
        str.append("(");
        List<Expression> args = invExpr.getArguments();
        if (args != null) {
            Iterator<Expression> iter = args.iterator();
            while (iter.hasNext()) {
                str.append(getJDOQLForExpression(iter.next()));
                if (iter.hasNext()) {
                    str.append(",");
                }
            }
        }
        str.append(")");
        return str.toString();
    } else if (expr instanceof Literal) {
        Literal litExpr = (Literal) expr;
        Object value = litExpr.getLiteral();
        if (value instanceof String || value instanceof Character) {
            return "'" + value.toString() + "'";
        } else if (value instanceof Boolean) {
            return ((Boolean) value ? "TRUE" : "FALSE");
        } else {
            if (litExpr.getLiteral() == null) {
                return "null";
            }
            return litExpr.getLiteral().toString();
        }
    } else {
        throw new UnsupportedOperationException("Dont currently support " + expr.getClass().getName() + " in JDOQLHelper");
    }
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) 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)

Example 52 with Literal

use of org.datanucleus.query.expression.Literal in project datanucleus-api-jdo by datanucleus.

the class StringExpressionImpl method substring.

/* (non-Javadoc)
     * @see org.datanucleus.query.typesafe.StringExpression#substring(int)
     */
public StringExpression substring(int pos) {
    List<org.datanucleus.query.expression.Expression> args = new ArrayList();
    args.add(new Literal(pos));
    org.datanucleus.query.expression.Expression invokeExpr = new InvokeExpression(queryExpr, "substring", args);
    return new StringExpressionImpl(invokeExpr);
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) NumericExpression(javax.jdo.query.NumericExpression) CharacterExpression(javax.jdo.query.CharacterExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) StringExpression(javax.jdo.query.StringExpression) PersistableExpression(javax.jdo.query.PersistableExpression) BooleanExpression(javax.jdo.query.BooleanExpression) Expression(javax.jdo.query.Expression) Literal(org.datanucleus.query.expression.Literal) ArrayList(java.util.ArrayList)

Example 53 with Literal

use of org.datanucleus.query.expression.Literal in project datanucleus-api-jdo by datanucleus.

the class StringExpressionImpl method indexOf.

/* (non-Javadoc)
     * @see org.datanucleus.query.typesafe.StringExpression#indexOf(java.lang.String, int)
     */
public NumericExpression indexOf(String str, int pos) {
    List<org.datanucleus.query.expression.Expression> args = new ArrayList();
    args.add(new Literal(str));
    args.add(new Literal(pos));
    org.datanucleus.query.expression.Expression invokeExpr = new InvokeExpression(queryExpr, "indexOf", args);
    return new NumericExpressionImpl<Integer>(invokeExpr);
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) NumericExpression(javax.jdo.query.NumericExpression) CharacterExpression(javax.jdo.query.CharacterExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) StringExpression(javax.jdo.query.StringExpression) PersistableExpression(javax.jdo.query.PersistableExpression) BooleanExpression(javax.jdo.query.BooleanExpression) Expression(javax.jdo.query.Expression) Literal(org.datanucleus.query.expression.Literal) ArrayList(java.util.ArrayList)

Example 54 with Literal

use of org.datanucleus.query.expression.Literal in project datanucleus-api-jdo by datanucleus.

the class StringExpressionImpl method startsWith.

/* (non-Javadoc)
     * @see org.datanucleus.query.typesafe.StringExpression#startsWith(java.lang.String)
     */
public BooleanExpression startsWith(String str) {
    List<org.datanucleus.query.expression.Expression> args = new ArrayList();
    args.add(new Literal(str));
    org.datanucleus.query.expression.Expression invokeExpr = new InvokeExpression(queryExpr, "startsWith", args);
    return new BooleanExpressionImpl(invokeExpr);
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) NumericExpression(javax.jdo.query.NumericExpression) CharacterExpression(javax.jdo.query.CharacterExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) StringExpression(javax.jdo.query.StringExpression) PersistableExpression(javax.jdo.query.PersistableExpression) BooleanExpression(javax.jdo.query.BooleanExpression) Expression(javax.jdo.query.Expression) Literal(org.datanucleus.query.expression.Literal) ArrayList(java.util.ArrayList)

Example 55 with Literal

use of org.datanucleus.query.expression.Literal in project datanucleus-api-jdo by datanucleus.

the class StringExpressionImpl method equalsIgnoreCase.

/* (non-Javadoc)
     * @see org.datanucleus.query.typesafe.StringExpression#equalsIgnoreCase(java.lang.String)
     */
public BooleanExpression equalsIgnoreCase(String str) {
    List<org.datanucleus.query.expression.Expression> args = new ArrayList();
    args.add(new Literal(str));
    org.datanucleus.query.expression.Expression invokeExpr = new InvokeExpression(queryExpr, "equalsIgnoreCase", args);
    return new BooleanExpressionImpl(invokeExpr);
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) NumericExpression(javax.jdo.query.NumericExpression) CharacterExpression(javax.jdo.query.CharacterExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) StringExpression(javax.jdo.query.StringExpression) PersistableExpression(javax.jdo.query.PersistableExpression) BooleanExpression(javax.jdo.query.BooleanExpression) Expression(javax.jdo.query.Expression) Literal(org.datanucleus.query.expression.Literal) ArrayList(java.util.ArrayList)

Aggregations

Literal (org.datanucleus.query.expression.Literal)83 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)62 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)61 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)60 NucleusException (org.datanucleus.exceptions.NucleusException)54 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)52 VariableExpression (org.datanucleus.query.expression.VariableExpression)38 Expression (org.datanucleus.query.expression.Expression)37 ArrayList (java.util.ArrayList)18 BooleanExpression (javax.jdo.query.BooleanExpression)18 NumericExpression (javax.jdo.query.NumericExpression)18 PersistableExpression (javax.jdo.query.PersistableExpression)18 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)18 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)17 OrderExpression (org.datanucleus.query.expression.OrderExpression)17 HashMap (java.util.HashMap)14 Expression (javax.jdo.query.Expression)14 ClassExpression (org.datanucleus.query.expression.ClassExpression)12 JoinExpression (org.datanucleus.query.expression.JoinExpression)12 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)12