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");
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations