Search in sources :

Example 26 with NumericExpression

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

the class TemporalQuarterMethod3 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) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "QUARTER");
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping2 = storeMgr.getMappingManager().getMapping(String.class);
    ArrayList funcArgs = new ArrayList();
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    funcArgs.add(exprFactory.newLiteral(stmt, mapping2, "quarter"));
    funcArgs.add(invokedExpr);
    NumericExpression numExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "date_part", funcArgs);
    numExpr.encloseInParentheses();
    return numExpr;
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 27 with NumericExpression

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

the class TemporalSecondMethod6 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) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "SECOND");
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    SQLExpression sec = exprFactory.newLiteral(stmt, mapping, "%S");
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(sec);
    funcArgs.add(invokedExpr);
    return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "strftime", funcArgs);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 28 with NumericExpression

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

the class MapContainsKeyMethod 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", "containsKey", "MapExpression", 1));
    }
    MapExpression mapExpr = (MapExpression) expr;
    SQLExpression keyExpr = args.get(0);
    if (keyExpr.isParameter()) {
        // Key is a parameter so make sure its type is set
        AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
        if (mmd != null && mmd.getMap() != null) {
            Class keyCls = stmt.getQueryGenerator().getClassLoaderResolver().classForName(mmd.getMap().getKeyType());
            stmt.getQueryGenerator().bindParameter(keyExpr.getParameterName(), keyCls);
        }
    }
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    if (expr instanceof MapLiteral) {
        // Literal Map
        MapLiteral lit = (MapLiteral) expr;
        Map map = (Map) lit.getValue();
        JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
        if (map == null || map.size() == 0) {
            return exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, false));
        }
        boolean useInExpression = false;
        List<SQLExpression> mapKeyExprs = lit.getKeyLiteral().getKeyExpressions();
        if (mapKeyExprs != null && !mapKeyExprs.isEmpty()) {
            // Make sure the the map key(s) are compatible with the keyExpr
            boolean incompatible = true;
            Class elemtype = clr.classForName(keyExpr.getJavaTypeMapping().getType());
            Iterator<SQLExpression> mapKeyExprIter = mapKeyExprs.iterator();
            while (mapKeyExprIter.hasNext()) {
                SQLExpression mapKeyExpr = mapKeyExprIter.next();
                Class mapKeyType = clr.classForName(mapKeyExpr.getJavaTypeMapping().getType());
                if (keyTypeCompatible(elemtype, mapKeyType)) {
                    incompatible = false;
                    break;
                }
            }
            if (incompatible) {
                // The provided element type isn't assignable to any of the input collection elements!
                return exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, false));
            }
            // Check if we should compare using an "IN (...)" expression
            SQLExpression mapKeyExpr = mapKeyExprs.get(0);
            if (mapKeyExpr instanceof StringExpression || mapKeyExpr instanceof NumericExpression || mapKeyExpr instanceof TemporalExpression || mapKeyExpr instanceof CharacterExpression || mapKeyExpr instanceof EnumExpression) {
                useInExpression = true;
            }
        }
        if (useInExpression) {
            // Return "key IN (val1, val2, ...)"
            SQLExpression[] exprs = (mapKeyExprs != null ? mapKeyExprs.toArray(new SQLExpression[mapKeyExprs.size()]) : null);
            return new InExpression(keyExpr, exprs);
        }
        // TODO If keyExpr is a parameter and mapExpr is derived from a parameter ?
        MapKeyLiteral mapKeyLiteral = lit.getKeyLiteral();
        BooleanExpression bExpr = null;
        List<SQLExpression> elementExprs = mapKeyLiteral.getKeyExpressions();
        for (int i = 0; i < elementExprs.size(); i++) {
            if (bExpr == null) {
                bExpr = (elementExprs.get(i)).eq(keyExpr);
            } else {
                bExpr = bExpr.ior((elementExprs.get(i)).eq(keyExpr));
            }
        }
        if (bExpr != null) {
            bExpr.encloseInParentheses();
        }
        return bExpr;
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
        boolean useSubquery = getNeedsSubquery(stmt);
        JoinType joinType = JoinType.INNER_JOIN;
        if (keyExpr instanceof UnboundExpression) {
            // See if the user has defined what should be used
            String varName = ((UnboundExpression) keyExpr).getVariableName();
            String extensionName = "datanucleus.query.jdoql." + varName + ".join";
            String extensionValue = (String) stmt.getQueryGenerator().getValueForExtension(extensionName);
            if (extensionValue != null) {
                if (extensionValue.equalsIgnoreCase("SUBQUERY")) {
                    useSubquery = true;
                } else if (extensionValue.equalsIgnoreCase("INNERJOIN")) {
                    useSubquery = false;
                } else if (extensionValue.equalsIgnoreCase("LEFTOUTERJOIN")) {
                    joinType = JoinType.LEFT_OUTER_JOIN;
                }
            }
        }
        // TODO Check if *this* "containsKey" is negated, not any of them (and remove above check)
        if (useSubquery) {
            return containsAsSubquery(stmt, mapExpr, keyExpr);
        }
        return containsAsJoin(stmt, mapExpr, keyExpr, joinType);
    }
    return containsAsSubquery(stmt, mapExpr, keyExpr);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) InExpression(org.datanucleus.store.rdbms.sql.expression.InExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) JoinType(org.datanucleus.store.rdbms.sql.SQLJoin.JoinType) EnumExpression(org.datanucleus.store.rdbms.sql.expression.EnumExpression) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression) MapLiteral(org.datanucleus.store.rdbms.sql.expression.MapLiteral) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) Map(java.util.Map) MapKeyLiteral(org.datanucleus.store.rdbms.sql.expression.MapLiteral.MapKeyLiteral)

Example 29 with NumericExpression

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

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

the class ListIndexOfMethod 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) {
    SQLExpression collSqlExpr = null;
    SQLExpression elemSqlExpr = null;
    if (expr != null) {
        if (args == null || args.size() == 0 || args.size() > 1) {
            throw new NucleusException(Localiser.msg("060016", "indexOf", "CollectionExpression", 1));
        }
        elemSqlExpr = args.get(0);
        collSqlExpr = expr;
    } else {
        if (args == null || args.size() != 2) {
            throw new NucleusException(Localiser.msg("060016", "indexOf", "CollectionExpression", 2));
        }
        elemSqlExpr = args.get(0);
        collSqlExpr = args.get(1);
    }
    AbstractMemberMetaData mmd = collSqlExpr.getJavaTypeMapping().getMemberMetaData();
    if (mmd == null) {
        // TODO Cater for the List being an input parameter
        throw new NucleusException(Localiser.msg("060020", "indexOf", collSqlExpr.getClass().getName()));
    }
    if (!mmd.hasCollection()) {
        throw new NucleusException("List.indexOf expression for field " + mmd.getFullFieldName() + " does not represent a collection!");
    } else if (!mmd.getOrderMetaData().isIndexedList()) {
        throw new NucleusException("List.indexOf expression for field " + mmd.getFullFieldName() + " does not represent an indexed list!");
    }
    JavaTypeMapping orderMapping = null;
    SQLTable orderTable = null;
    Table joinTbl = stmt.getRDBMSManager().getTable(mmd);
    if (joinTbl != null) {
        // 1-N via join table
        CollectionTable collTable = (CollectionTable) joinTbl;
        orderTable = stmt.getTableForDatastoreContainer(collTable);
        if (orderTable == null) {
            // TODO Allow control over the join type? the alias?
            orderTable = stmt.join(JoinType.LEFT_OUTER_JOIN, collSqlExpr.getSQLTable(), collSqlExpr.getSQLTable().getTable().getIdMapping(), collTable, null, collTable.getOwnerMapping(), null, null);
        }
        orderMapping = collTable.getOrderMapping();
    } else {
        // 1-N via FK
        orderTable = elemSqlExpr.getSQLTable();
        orderMapping = ((ClassTable) elemSqlExpr.getSQLTable().getTable()).getExternalMapping(mmd, MappingType.EXTERNAL_INDEX);
    }
    return new NumericExpression(stmt, orderTable, orderMapping);
}
Also used : CollectionTable(org.datanucleus.store.rdbms.table.CollectionTable) Table(org.datanucleus.store.rdbms.table.Table) ClassTable(org.datanucleus.store.rdbms.table.ClassTable) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) CollectionTable(org.datanucleus.store.rdbms.table.CollectionTable) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

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