Search in sources :

Example 56 with NumericExpression

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

the class TemporalDayOfWeekMethod6 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, "WEEKDAY");
    expr.toSQLText().prepend("WEEKDAY FROM ");
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(invokedExpr);
    // Add one to the SQL (origin=0) to be compatible with Java Calendar day of week (origin=1)
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    NumericExpression numExpr = new NumericExpression(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "EXTRACT", funcArgs), Expression.OP_ADD, one);
    numExpr.encloseInParentheses();
    return numExpr;
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression)

Example 57 with NumericExpression

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

the class TemporalHourMethod6 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, "HOUR");
    invokedExpr.toSQLText().prepend("HOUR FROM ");
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(invokedExpr);
    return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "EXTRACT", funcArgs);
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression)

Example 58 with NumericExpression

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

the class TemporalSecondMethod7 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");
    invokedExpr.toSQLText().prepend("SECOND FROM ");
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(invokedExpr);
    return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "EXTRACT", funcArgs);
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression)

Example 59 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)

Example 60 with NumericExpression

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

the class MapContainsValueMethod 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", "containsValue", "MapExpression", 1));
    }
    MapExpression mapExpr = (MapExpression) expr;
    SQLExpression valExpr = args.get(0);
    if (valExpr.isParameter()) {
        // Value is a parameter so make sure its type is set
        AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
        if (mmd != null && mmd.getMap() != null) {
            Class valCls = stmt.getQueryGenerator().getClassLoaderResolver().classForName(mmd.getMap().getValueType());
            stmt.getQueryGenerator().bindParameter(valExpr.getParameterName(), valCls);
        }
    }
    ClassLoaderResolver clr = stmt.getQueryGenerator().getClassLoaderResolver();
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (mapExpr instanceof MapLiteral) {
        MapLiteral lit = (MapLiteral) mapExpr;
        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> mapValExprs = lit.getValueLiteral().getValueExpressions();
        if (mapValExprs != null && !mapValExprs.isEmpty()) {
            // Make sure the the map key(s) are compatible with the keyExpr
            boolean incompatible = true;
            Class elemtype = clr.classForName(valExpr.getJavaTypeMapping().getType());
            Iterator<SQLExpression> mapKeyExprIter = mapValExprs.iterator();
            while (mapKeyExprIter.hasNext()) {
                SQLExpression mapKeyExpr = mapKeyExprIter.next();
                Class mapKeyType = clr.classForName(mapKeyExpr.getJavaTypeMapping().getType());
                if (valueTypeCompatible(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 = mapValExprs.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 = (mapValExprs != null ? mapValExprs.toArray(new SQLExpression[mapValExprs.size()]) : null);
            return new InExpression(valExpr, exprs);
        }
        // TODO If valExpr is a parameter and mapExpr is derived from a parameter ?
        MapValueLiteral mapValueLiteral = lit.getValueLiteral();
        BooleanExpression bExpr = null;
        List<SQLExpression> elementExprs = mapValueLiteral.getValueExpressions();
        for (int i = 0; i < elementExprs.size(); i++) {
            if (bExpr == null) {
                bExpr = (elementExprs.get(i)).eq(valExpr);
            } else {
                bExpr = bExpr.ior((elementExprs.get(i)).eq(valExpr));
            }
        }
        if (bExpr != null) {
            bExpr.encloseInParentheses();
        }
        return bExpr;
    }
    if (stmt.getQueryGenerator().getCompilationComponent() == CompilationComponent.FILTER) {
        boolean useSubquery = getNeedsSubquery(stmt);
        JoinType joinType = JoinType.INNER_JOIN;
        if (valExpr instanceof UnboundExpression) {
            // See if the user has defined what should be used
            String varName = ((UnboundExpression) valExpr).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* "containsValue" is negated, not any of them (and remove above check)
        if (useSubquery) {
            return containsAsSubquery(stmt, mapExpr, valExpr);
        }
        return containsAsJoin(stmt, mapExpr, valExpr, joinType);
    }
    return containsAsSubquery(stmt, mapExpr, valExpr);
}
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) MapValueLiteral(org.datanucleus.store.rdbms.sql.expression.MapLiteral.MapValueLiteral) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) Map(java.util.Map)

Aggregations

NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)74 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)67 ArrayList (java.util.ArrayList)60 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)40 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)40 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)28 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)25 NucleusException (org.datanucleus.exceptions.NucleusException)20 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)13 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 List (java.util.List)9 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)8 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)8 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)6 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)6 Arrays.asList (java.util.Arrays.asList)5 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)5 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)4