Search in sources :

Example 26 with SQLLiteral

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

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

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

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

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

Example 29 with SQLLiteral

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

the class JDOHelperGetObjectIdMethod 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 JDOHelper.getObjectId without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        return new NullLiteral(stmt, null, null, null);
    }
    if (expr instanceof SQLLiteral) {
        RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
        ApiAdapter api = storeMgr.getApiAdapter();
        Object id = api.getIdForObject(((SQLLiteral) expr).getValue());
        if (id == null) {
            return new NullLiteral(stmt, null, null, null);
        }
        JavaTypeMapping m = stmt.getSQLExpressionFactory().getMappingForType(id.getClass(), true);
        return new ObjectLiteral(stmt, m, id, null);
    } else if (ObjectExpression.class.isAssignableFrom(expr.getClass())) {
        // When the expression represents a PC object need to extract out as the identity
        if (expr.getJavaTypeMapping() instanceof PersistableMapping) {
            JavaTypeMapping mapping = new PersistableIdMapping((PersistableMapping) expr.getJavaTypeMapping());
            return new ObjectExpression(stmt, expr.getSQLTable(), mapping);
        } else if (expr.getJavaTypeMapping() instanceof ReferenceMapping) {
            JavaTypeMapping mapping = new ReferenceIdMapping((ReferenceMapping) expr.getJavaTypeMapping());
            return new ObjectExpression(stmt, expr.getSQLTable(), mapping);
        }
        return expr;
    }
    throw new IllegalExpressionOperationException("JDOHelper.getObjectId", expr);
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) ObjectLiteral(org.datanucleus.store.rdbms.sql.expression.ObjectLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) ReferenceIdMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceIdMapping) PersistableIdMapping(org.datanucleus.store.rdbms.mapping.java.PersistableIdMapping) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression)

Example 30 with SQLLiteral

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

the class JDOHelperGetVersionMethod 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 JDOHelper.getVersion without an argument");
    }
    SQLExpression expr = args.get(0);
    if (expr == null) {
        throw new NucleusUserException("Cannot invoke JDOHelper.getVersion on null expression");
    }
    if (expr instanceof SQLLiteral) {
        RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
        ApiAdapter api = storeMgr.getApiAdapter();
        Object obj = ((SQLLiteral) expr).getValue();
        if (obj == null || !api.isPersistable(obj)) {
            return new NullLiteral(stmt, null, null, null);
        }
        Object ver = stmt.getRDBMSManager().getApiAdapter().getVersionForObject(obj);
        JavaTypeMapping m = stmt.getSQLExpressionFactory().getMappingForType(ver.getClass(), true);
        return new ObjectLiteral(stmt, m, ver, null);
    } else if (ObjectExpression.class.isAssignableFrom(expr.getClass())) {
        if (((ObjectExpression) expr).getJavaTypeMapping() instanceof PersistableMapping) {
            JavaTypeMapping mapping = ((ObjectExpression) expr).getJavaTypeMapping();
            DatastoreClass table = (DatastoreClass) expr.getSQLTable().getTable();
            if (// Version of candidate
            table.getIdMapping() == mapping) {
                mapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true);
                if (mapping == null) {
                    throw new NucleusUserException("Cannot use JDOHelper.getVersion on object that has no version information");
                }
                if (table.getVersionMetaData().getVersionStrategy() == VersionStrategy.VERSION_NUMBER) {
                    return new NumericExpression(stmt, expr.getSQLTable(), mapping);
                }
                return new TemporalExpression(stmt, expr.getSQLTable(), mapping);
            }
            throw new NucleusUserException("Dont currently support JDOHelper.getVersion(ObjectExpression) for expr=" + expr + " on table=" + expr.getSQLTable());
        // TODO Implement this
        }
        return expr;
    }
    throw new IllegalExpressionOperationException("JDOHelper.getVersion", expr);
}
Also used : TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ApiAdapter(org.datanucleus.api.ApiAdapter) ObjectLiteral(org.datanucleus.store.rdbms.sql.expression.ObjectLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) IllegalExpressionOperationException(org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression)

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