Search in sources :

Example 16 with ParameterLiteral

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

the class QueryToSQLMapper method processInExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processInExpression(org.datanucleus.query.expression.Expression)
     */
@Override
protected Object processInExpression(Expression expr) {
    SQLExpression right = stack.pop();
    SQLExpression left = stack.pop();
    if (right instanceof CollectionExpression || right instanceof org.datanucleus.store.rdbms.sql.expression.ArrayExpression) {
        // myElement IN myCollection
        if (right.getParameterName() != null) {
            setNotPrecompilable();
        }
        // Use Collection.contains(element)/Array.contains(element)
        List<SQLExpression> sqlExprArgs = new ArrayList();
        sqlExprArgs.add(left);
        SQLExpression sqlExpr = right.invoke("contains", sqlExprArgs);
        stack.push(sqlExpr);
        return sqlExpr;
    } else if (right.getParameterName() != null || left.getParameterName() != null) {
        // "expr IN (:param)" or ":param IN (expr)" or ":param1 IN (:param2)"
        setNotPrecompilable();
        // Replace parameter(s) with equivalent literal of correct type
        if (right instanceof ParameterLiteral) {
            right = replaceParameterLiteral((ParameterLiteral) right, left.getJavaTypeMapping());
        }
        if (left instanceof ParameterLiteral && !Collection.class.isAssignableFrom(right.getJavaTypeMapping().getJavaType())) {
            left = replaceParameterLiteral((ParameterLiteral) left, right.getJavaTypeMapping());
        }
        // Single valued parameter, so use equality
        SQLExpression inExpr = new BooleanExpression(left, Expression.OP_EQ, right);
        stack.push(inExpr);
        return inExpr;
    }
    SQLExpression inExpr = left.in(right, false);
    stack.push(inExpr);
    return inExpr;
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression)

Example 17 with ParameterLiteral

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

the class QueryToSQLMapper method processGteqExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processGteqExpression(org.datanucleus.query.expression.Expression)
     */
@Override
protected Object processGteqExpression(Expression expr) {
    SQLExpression right = stack.pop();
    SQLExpression left = stack.pop();
    if (left instanceof ParameterLiteral && !(right instanceof ParameterLiteral)) {
        left = replaceParameterLiteral((ParameterLiteral) left, right.getJavaTypeMapping());
    } else if (right instanceof ParameterLiteral && !(left instanceof ParameterLiteral)) {
        right = replaceParameterLiteral((ParameterLiteral) right, left.getJavaTypeMapping());
    }
    ExpressionUtils.checkAndCorrectExpressionMappingsForBooleanComparison(left, right);
    if (left instanceof UnboundExpression) {
        processUnboundExpression((UnboundExpression) left);
        left = stack.pop();
    }
    if (right instanceof UnboundExpression) {
        processUnboundExpression((UnboundExpression) right);
        right = stack.pop();
    }
    BooleanExpression opExpr = left.ge(right);
    stack.push(opExpr);
    return opExpr;
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression)

Example 18 with ParameterLiteral

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

the class QueryToSQLMapper method processGtExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processGtExpression(org.datanucleus.query.expression.Expression)
     */
@Override
protected Object processGtExpression(Expression expr) {
    SQLExpression right = stack.pop();
    SQLExpression left = stack.pop();
    if (left instanceof ParameterLiteral && !(right instanceof ParameterLiteral)) {
        left = replaceParameterLiteral((ParameterLiteral) left, right.getJavaTypeMapping());
    } else if (right instanceof ParameterLiteral && !(left instanceof ParameterLiteral)) {
        right = replaceParameterLiteral((ParameterLiteral) right, left.getJavaTypeMapping());
    }
    ExpressionUtils.checkAndCorrectExpressionMappingsForBooleanComparison(left, right);
    if (left instanceof UnboundExpression) {
        processUnboundExpression((UnboundExpression) left);
        left = stack.pop();
    }
    if (right instanceof UnboundExpression) {
        processUnboundExpression((UnboundExpression) right);
        right = stack.pop();
    }
    BooleanExpression opExpr = left.gt(right);
    stack.push(opExpr);
    return opExpr;
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression)

Example 19 with ParameterLiteral

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

the class QueryToSQLMapper method processEqExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processEqExpression(org.datanucleus.query.expression.Expression)
     */
protected Object processEqExpression(Expression expr) {
    SQLExpression right = stack.pop();
    SQLExpression left = stack.pop();
    if (left instanceof ParameterLiteral && !(right instanceof ParameterLiteral)) {
        left = replaceParameterLiteral((ParameterLiteral) left, right.getJavaTypeMapping());
    } else if (right instanceof ParameterLiteral && !(left instanceof ParameterLiteral)) {
        right = replaceParameterLiteral((ParameterLiteral) right, left.getJavaTypeMapping());
    }
    if (left.isParameter() && right.isParameter()) {
        if (left.isParameter() && left instanceof SQLLiteral && ((SQLLiteral) left).getValue() != null) {
            // Change this parameter to a plain literal
            useParameterExpressionAsLiteral((SQLLiteral) left);
        }
        if (right.isParameter() && right instanceof SQLLiteral && ((SQLLiteral) right).getValue() != null) {
            // Change this parameter to a plain literal
            useParameterExpressionAsLiteral((SQLLiteral) right);
        }
    }
    ExpressionUtils.checkAndCorrectExpressionMappingsForBooleanComparison(left, right);
    if (left instanceof UnboundExpression) {
        processUnboundExpression((UnboundExpression) left);
        left = stack.pop();
    }
    if (right instanceof UnboundExpression) {
        processUnboundExpression((UnboundExpression) right);
        right = stack.pop();
    }
    // Logic for when one side is cross-joined (variable) and other side not, so transfer to a left outer join
    if (!options.contains(OPTION_EXPLICIT_JOINS)) {
        boolean leftIsCrossJoin = (stmt.getJoinTypeForTable(left.getSQLTable()) == JoinType.CROSS_JOIN);
        boolean rightIsCrossJoin = (stmt.getJoinTypeForTable(right.getSQLTable()) == JoinType.CROSS_JOIN);
        if (leftIsCrossJoin && !rightIsCrossJoin && !(right instanceof SQLLiteral)) {
            // "a == b" and a is cross-joined currently (includes variable) so change to left outer join
            String varName = getAliasForSQLTable(left.getSQLTable());
            JoinType joinType = getRequiredJoinTypeForAlias(varName);
            if (joinType != null) {
                NucleusLogger.QUERY.debug("QueryToSQL.eq variable " + varName + " is mapped to table " + left.getSQLTable() + " was previously bound as CROSS JOIN but changing to " + joinType);
                String leftTblAlias = stmt.removeCrossJoin(left.getSQLTable());
                if (joinType == JoinType.LEFT_OUTER_JOIN) {
                    stmt.join(JoinType.LEFT_OUTER_JOIN, right.getSQLTable(), right.getJavaTypeMapping(), left.getSQLTable().getTable(), leftTblAlias, left.getJavaTypeMapping(), null, left.getSQLTable().getGroupName(), true);
                } else {
                    stmt.join(JoinType.INNER_JOIN, right.getSQLTable(), right.getJavaTypeMapping(), left.getSQLTable().getTable(), leftTblAlias, left.getJavaTypeMapping(), null, left.getSQLTable().getGroupName(), true);
                }
                JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
                SQLExpression opExpr = exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, true));
                stack.push(opExpr);
                return opExpr;
            }
        } else if (!leftIsCrossJoin && rightIsCrossJoin && !(left instanceof SQLLiteral)) {
            // "a == b" and b is cross-joined currently (includes variable) so change to left outer join
            String varName = getAliasForSQLTable(right.getSQLTable());
            JoinType joinType = getRequiredJoinTypeForAlias(varName);
            if (joinType != null) {
                NucleusLogger.QUERY.debug("QueryToSQL.eq variable " + varName + " is mapped to table " + right.getSQLTable() + " was previously bound as CROSS JOIN but changing to " + joinType);
                String rightTblAlias = stmt.removeCrossJoin(right.getSQLTable());
                if (joinType == JoinType.LEFT_OUTER_JOIN) {
                    stmt.join(JoinType.LEFT_OUTER_JOIN, left.getSQLTable(), left.getJavaTypeMapping(), right.getSQLTable().getTable(), rightTblAlias, right.getJavaTypeMapping(), null, right.getSQLTable().getGroupName(), true);
                } else {
                    stmt.join(JoinType.INNER_JOIN, left.getSQLTable(), left.getJavaTypeMapping(), right.getSQLTable().getTable(), rightTblAlias, right.getJavaTypeMapping(), null, right.getSQLTable().getGroupName(), true);
                }
                JavaTypeMapping m = exprFactory.getMappingForType(boolean.class, true);
                SQLExpression opExpr = exprFactory.newLiteral(stmt, m, true).eq(exprFactory.newLiteral(stmt, m, true));
                stack.push(opExpr);
                return opExpr;
            }
        }
    }
    BooleanExpression opExpr = left.eq(right);
    stack.push(opExpr);
    return opExpr;
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) JoinType(org.datanucleus.store.rdbms.sql.SQLJoin.JoinType) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression)

Example 20 with ParameterLiteral

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

the class QueryToSQLMapper method processAddExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processAddExpression(org.datanucleus.query.expression.Expression)
     */
protected Object processAddExpression(Expression expr) {
    SQLExpression right = stack.pop();
    SQLExpression left = stack.pop();
    if (left instanceof ParameterLiteral && !(right instanceof ParameterLiteral)) {
        left = replaceParameterLiteral((ParameterLiteral) left, right.getJavaTypeMapping());
    } else if (right instanceof ParameterLiteral && !(left instanceof ParameterLiteral)) {
        right = replaceParameterLiteral((ParameterLiteral) right, left.getJavaTypeMapping());
    }
    SQLExpression resultExpr = left.add(right);
    stack.push(resultExpr);
    return resultExpr;
}
Also used : ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression)

Aggregations

ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)27 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)23 NucleusException (org.datanucleus.exceptions.NucleusException)17 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)16 ArrayList (java.util.ArrayList)15 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)12 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)11 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)11 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)7 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)7 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)6 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)6 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)5 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)4 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 FetchPlanForClass (org.datanucleus.FetchPlanForClass)1