Search in sources :

Example 21 with IntegerLiteral

use of org.datanucleus.store.rdbms.sql.expression.IntegerLiteral in project datanucleus-rdbms by datanucleus.

the class MathSqrtMethod 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 Math.sqrt without an argument");
    }
    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.sqrt(originalValue)));
            return new ByteLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof IntegerLiteral) {
            int originalValue = ((Number) ((IntegerLiteral) expr).getValue()).intValue();
            Double absValue = new Double(Math.sqrt(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        } else if (expr instanceof FloatingPointLiteral) {
            double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr).getValue()).doubleValue();
            Double absValue = new Double(Math.sqrt(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.sqrt()", expr);
    } else {
        // Relay to the equivalent "sqrt(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "sqrt", null, args);
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) 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

IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)21 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)20 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)17 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)16 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)16 BigInteger (java.math.BigInteger)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)15 ByteLiteral (org.datanucleus.store.rdbms.sql.expression.ByteLiteral)15 FloatingPointLiteral (org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral)15 IllegalExpressionOperationException (org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException)15 NucleusException (org.datanucleus.exceptions.NucleusException)6 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)6 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)6 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)6 ArrayList (java.util.ArrayList)5 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)5 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)4 VersionMetaData (org.datanucleus.metadata.VersionMetaData)1 ArrayExpression (org.datanucleus.query.expression.ArrayExpression)1 CaseExpression (org.datanucleus.query.expression.CaseExpression)1