Search in sources :

Example 16 with FloatingPointLiteral

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);
    }
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) FloatingPointLiteral(org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral) ByteLiteral(org.datanucleus.store.rdbms.sql.expression.ByteLiteral) BigInteger(java.math.BigInteger) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Aggregations

BigInteger (java.math.BigInteger)16 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)16 ByteLiteral (org.datanucleus.store.rdbms.sql.expression.ByteLiteral)16 FloatingPointLiteral (org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral)16 IllegalExpressionOperationException (org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException)16 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)16 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)16 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)16 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)16 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)15 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)1