Search in sources :

Example 6 with SQLLiteral

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

the class MathAsinMethod 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.asin 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.asin(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.asin(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.asin(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.asin()", expr);
    } else {
        // Relay to the equivalent "asin(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "asin", 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)

Example 7 with SQLLiteral

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

the class ListGetMethod 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 expr, List<SQLExpression> args) {
    if (args == null || args.size() == 0 || args.size() > 1) {
        throw new NucleusException(Localiser.msg("060016", "get", "CollectionExpression", 1));
    }
    CollectionExpression listExpr = (CollectionExpression) expr;
    AbstractMemberMetaData mmd = listExpr.getJavaTypeMapping().getMemberMetaData();
    if (!List.class.isAssignableFrom(mmd.getType())) {
        throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the field is not a List!");
    } else if (mmd.getOrderMetaData() != null && !mmd.getOrderMetaData().isIndexedList()) {
        throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the field is not an 'indexed' List!");
    }
    SQLExpression idxExpr = args.get(0);
    if (idxExpr instanceof SQLLiteral) {
        if (!(((SQLLiteral) idxExpr).getValue() instanceof Number)) {
            throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the index is not a numeric literal so not yet supported");
        }
    } else {
        throw new UnsupportedOperationException("Query contains " + expr + ".get(int) yet the index is not a numeric literal so not yet supported");
    }
    if (listExpr instanceof CollectionLiteral && idxExpr instanceof SQLLiteral) {
        CollectionLiteral lit = (CollectionLiteral) expr;
        if (lit.getValue() == null) {
            return new NullLiteral(stmt, null, null, null);
        }
        return lit.invoke("get", args);
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
        return getAsInnerJoin(stmt, listExpr, idxExpr);
    } else if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.ORDERING || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.RESULT || stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.HAVING) {
        return getAsSubquery(stmt, listExpr, idxExpr);
    }
    throw new NucleusException("List.get() is not supported for " + listExpr + " with argument " + idxExpr + " for query component " + stmt.getQueryGenerator().getCompilationComponent());
}
Also used : CollectionLiteral(org.datanucleus.store.rdbms.sql.expression.CollectionLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) List(java.util.List) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral)

Example 8 with SQLLiteral

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

the class MathCosMethod 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.cos 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.cos(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.cos(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.cos(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.cos()", expr);
    } else {
        // Relay to the equivalent "cos(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "cos", 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)

Example 9 with SQLLiteral

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

the class MathLogMethod 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.log 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.log(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.log(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.log(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.log()", expr);
    } else {
        // Relay to the equivalent "log(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "log", 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)

Example 10 with SQLLiteral

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

the class MathPowerMethod 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.power 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.cos(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.cos(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.cos(originalValue));
            return new FloatingPointLiteral(stmt, expr.getJavaTypeMapping(), absValue, null);
        }
        throw new IllegalExpressionOperationException("Math.power()", expr);
    } else {
        // Relay to the equivalent "cos(expr)" function
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        return exprFactory.invokeMethod(stmt, null, "POWER", 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

SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)30 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)27 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)22 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)19 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)18 IllegalExpressionOperationException (org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException)17 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)17 BigInteger (java.math.BigInteger)15 ByteLiteral (org.datanucleus.store.rdbms.sql.expression.ByteLiteral)15 FloatingPointLiteral (org.datanucleus.store.rdbms.sql.expression.FloatingPointLiteral)15 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)9 NucleusException (org.datanucleus.exceptions.NucleusException)7 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)7 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)7 List (java.util.List)6 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)6 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)6 ArrayList (java.util.ArrayList)5 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)5 MapExpression (org.datanucleus.store.rdbms.sql.expression.MapExpression)4