Search in sources :

Example 1 with ObjectExpression

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

the class SQLFunctionMethod 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 args) {
    if (args == null || args.size() < 1) {
        throw new NucleusUserException("Cannot invoke SQL_function() without first argument defining the function");
    }
    SQLExpression expr = (SQLExpression) args.get(0);
    if (!(expr instanceof StringLiteral)) {
        throw new NucleusUserException("Cannot use SQL_function() without first argument defining the function");
    }
    String sql = (String) ((StringLiteral) expr).getValue();
    List<SQLExpression> funcArgs = new ArrayList<>();
    if (args.size() > 1) {
        funcArgs.addAll(args.subList(1, args.size()));
    }
    // Return as ObjectExpression with an underlying SQLFunctionMapping
    JavaTypeMapping m = new SQLFunctionMapping();
    m.initialize(stmt.getRDBMSManager(), null);
    ObjectExpression retExpr = new ObjectExpression(stmt, m, sql, funcArgs);
    return retExpr;
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ArrayList(java.util.ArrayList) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression) SQLFunctionMapping(org.datanucleus.store.rdbms.mapping.java.SQLFunctionMapping)

Example 2 with ObjectExpression

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

the class CoalesceFunction 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 (expr == null) {
        // Find expression type that this handles - all expressions need to be consistent in our implementation
        Class exprType = null;
        Class cls = null;
        int clsLevel = 0;
        for (int i = 0; i < args.size(); i++) {
            SQLExpression argExpr = args.get(i);
            if (exprType == null) {
                if (argExpr instanceof NumericExpression) {
                    exprType = NumericExpression.class;
                    cls = Integer.class;
                } else if (argExpr instanceof StringExpression) {
                    exprType = StringExpression.class;
                    cls = String.class;
                } else if (argExpr instanceof TemporalExpression) {
                    exprType = TemporalExpression.class;
                    cls = argExpr.getJavaTypeMapping().getJavaType();
                } else {
                    exprType = argExpr.getClass();
                    cls = argExpr.getJavaTypeMapping().getJavaType();
                }
            } else {
                if (!exprType.isAssignableFrom(argExpr.getClass())) {
                    throw new NucleusUserException("COALESCE invocation first argument of type " + exprType.getName() + " yet subsequent argument of type " + argExpr.getClass().getName());
                }
            }
            if (exprType == NumericExpression.class) {
                // Priority order is Double, Float, BigDecimal, BigInteger, Long, Integer
                Class argType = argExpr.getJavaTypeMapping().getJavaType();
                if (clsLevel < 5 && (argType == double.class || argType == Double.class)) {
                    cls = Double.class;
                    clsLevel = 5;
                } else if (clsLevel < 4 && (argType == float.class || argType == Float.class)) {
                    cls = Float.class;
                    clsLevel = 4;
                } else if (clsLevel < 3 && argType == BigDecimal.class) {
                    cls = BigDecimal.class;
                    clsLevel = 3;
                } else if (clsLevel < 2 && argType == BigInteger.class) {
                    cls = BigInteger.class;
                    clsLevel = 2;
                } else if (clsLevel < 1 && (argType == long.class || argType == Long.class)) {
                    cls = Long.class;
                    clsLevel = 1;
                }
            }
        }
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        if (exprType == NumericExpression.class) {
            return new NumericExpression(stmt, exprFactory.getMappingForType(cls, true), "COALESCE", args);
        } else if (exprType == StringExpression.class) {
            return new StringExpression(stmt, exprFactory.getMappingForType(cls, true), "COALESCE", args);
        } else if (exprType == TemporalExpression.class) {
            return new TemporalExpression(stmt, exprFactory.getMappingForType(cls, true), "COALESCE", args);
        } else {
            return new ObjectExpression(stmt, exprFactory.getMappingForType(cls, true), "COALESCE", args);
        }
    }
    throw new NucleusException(Localiser.msg("060002", "COALESCE", expr));
}
Also used : TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) BigDecimal(java.math.BigDecimal) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) BigInteger(java.math.BigInteger) NucleusException(org.datanucleus.exceptions.NucleusException) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression)

Example 3 with ObjectExpression

use of org.datanucleus.store.rdbms.sql.expression.ObjectExpression 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().getStrategy() == 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)

Example 4 with ObjectExpression

use of org.datanucleus.store.rdbms.sql.expression.ObjectExpression 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 5 with ObjectExpression

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

the class NullIfFunction 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 (expr == null) {
        // Find expression type that this handles - all expressions need to be consistent in our implementation
        Class exprType = null;
        Class cls = Integer.class;
        int clsLevel = 0;
        // Priority order is Double, Float, BigDecimal, BigInteger, Long, Integer
        for (int i = 0; i < args.size(); i++) {
            SQLExpression argExpr = args.get(i);
            if (exprType == null) {
                if (argExpr instanceof NumericExpression) {
                    exprType = NumericExpression.class;
                    cls = Integer.class;
                } else if (argExpr instanceof StringExpression) {
                    exprType = StringExpression.class;
                    cls = String.class;
                } else if (argExpr instanceof TemporalExpression) {
                    exprType = TemporalExpression.class;
                    cls = argExpr.getJavaTypeMapping().getJavaType();
                } else {
                    exprType = argExpr.getClass();
                    cls = argExpr.getJavaTypeMapping().getJavaType();
                }
            } else {
                if (!exprType.isAssignableFrom(argExpr.getClass())) {
                    throw new NucleusUserException("NULLIF invocation first argument of type " + exprType.getName() + " yet subsequent argument of type " + argExpr.getClass().getName());
                }
            }
            if (exprType == NumericExpression.class) {
                // Priority order is Double, Float, BigDecimal, BigInteger, Long, Integer
                Class argType = argExpr.getJavaTypeMapping().getJavaType();
                if (clsLevel < 5 && (argType == double.class || argType == Double.class)) {
                    cls = Double.class;
                    clsLevel = 5;
                } else if (clsLevel < 4 && (argType == float.class || argType == Float.class)) {
                    cls = Float.class;
                    clsLevel = 4;
                } else if (clsLevel < 3 && argType == BigDecimal.class) {
                    cls = BigDecimal.class;
                    clsLevel = 3;
                } else if (clsLevel < 2 && argType == BigInteger.class) {
                    cls = BigInteger.class;
                    clsLevel = 2;
                } else if (clsLevel < 1 && (argType == long.class || argType == Long.class)) {
                    cls = Long.class;
                    clsLevel = 1;
                }
            }
        }
        if (exprType == NumericExpression.class) {
            return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(cls, true), "NULLIF", args);
        } else if (exprType == StringExpression.class) {
            return new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(cls, true), "NULLIF", args);
        } else if (exprType == TemporalExpression.class) {
            return new TemporalExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(cls, true), "NULLIF", args);
        } else {
            return new ObjectExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(cls, true), "NULLIF", args);
        }
    }
    throw new NucleusException(Localiser.msg("060002", "NULLIF", expr));
}
Also used : TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) BigInteger(java.math.BigInteger) NucleusException(org.datanucleus.exceptions.NucleusException) ObjectExpression(org.datanucleus.store.rdbms.sql.expression.ObjectExpression)

Aggregations

NucleusUserException (org.datanucleus.exceptions.NucleusUserException)5 ObjectExpression (org.datanucleus.store.rdbms.sql.expression.ObjectExpression)5 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)5 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)3 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 ApiAdapter (org.datanucleus.api.ApiAdapter)2 NucleusException (org.datanucleus.exceptions.NucleusException)2 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)2 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)2 IllegalExpressionOperationException (org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException)2 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)2 ObjectLiteral (org.datanucleus.store.rdbms.sql.expression.ObjectLiteral)2 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)2 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)2 ArrayList (java.util.ArrayList)1 PersistableIdMapping (org.datanucleus.store.rdbms.mapping.java.PersistableIdMapping)1 ReferenceIdMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceIdMapping)1