use of org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral in project datanucleus-rdbms by datanucleus.
the class RoundMethod method getExpression.
/* (non-Javadoc)
* @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
*/
public SQLExpression getExpression(SQLStatement stmt, SQLExpression ignore, List<SQLExpression> args) {
if (args == null || args.size() == 0) {
throw new NucleusUserException("Cannot invoke ROUND without an argument");
} else if (args.size() > 2) {
throw new NucleusUserException("Cannot invoke ROUND with more than 2 arguments");
}
SQLExpression expr = args.get(0);
if (expr == null) {
return new NullLiteral(stmt, null, null, null);
} else if (expr instanceof SQLLiteral) {
if (expr instanceof ByteLiteral) {
int originalValue = ((BigInteger) ((ByteLiteral) expr).getValue()).intValue();
BigInteger absValue = new BigInteger(String.valueOf(Math.floor(originalValue)));
return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
} else if (expr instanceof IntegerLiteral) {
int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
Double absValue = Double.valueOf(Math.floor(originalValue));
return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
} else if (expr instanceof FloatingPointLiteral) {
double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
Double absValue = Double.valueOf(Math.floor(originalValue));
return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
}
throw new IllegalExpressionOperationException("ROUND()", expr);
} else {
// Relay to the SQL "ROUND(expr)" function
Class numericType = int.class;
if (args.size() == 2) {
numericType = double.class;
}
return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(numericType, true), "ROUND", args);
}
}
Aggregations