Search in sources :

Example 91 with NumericExpression

use of org.datanucleus.store.rdbms.sql.expression.NumericExpression 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)

Example 92 with NumericExpression

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

the class QueryToSQLMapper method processBitAndExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processBitAndExpression(org.datanucleus.query.expression.Expression)
     */
@Override
protected Object processBitAndExpression(Expression expr) {
    SQLExpression rightExpr = stack.pop();
    SQLExpression leftExpr = stack.pop();
    if (rightExpr instanceof BooleanExpression && leftExpr instanceof BooleanExpression) {
        // Handle as Boolean logical AND
        stack.push(leftExpr);
        stack.push(rightExpr);
        return processAndExpression(expr);
    } else if (rightExpr instanceof NumericExpression && leftExpr instanceof NumericExpression) {
        if (storeMgr.getDatastoreAdapter().supportsOption(DatastoreAdapter.OPERATOR_BITWISE_AND)) {
            SQLExpression bitAndExpr = new NumericExpression(leftExpr, Expression.OP_BIT_AND, rightExpr).encloseInParentheses();
            stack.push(bitAndExpr);
            return bitAndExpr;
        }
    }
    throw new NucleusUserException("Operation BITWISE AND is not supported for " + leftExpr + " and " + rightExpr + " for this datastore");
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression)

Aggregations

NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)92 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)85 ArrayList (java.util.ArrayList)63 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)47 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)46 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)34 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)27 NucleusException (org.datanucleus.exceptions.NucleusException)21 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)15 List (java.util.List)12 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)12 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)10 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)10 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)10 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)9 Arrays.asList (java.util.Arrays.asList)8 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)7 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)6 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)6 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)4