Search in sources :

Example 21 with BooleanExpression

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

the class StringStartsWithMethod 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() > 2) {
        throw new NucleusException(Localiser.msg("060003", "startsWith", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
    }
    SQLExpression substrExpr = args.get(0);
    if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
        throw new NucleusException(Localiser.msg("060003", "startsWith", "StringExpression", 0, "StringExpression/CharacterExpression/Parameter"));
    }
    if (args.size() > 1) {
        // TODO Use numExpr in a SUBSTRING
        if (substrExpr.isParameter()) {
            // Any pattern expression cannot be a parameter here
            SQLLiteral substrLit = (SQLLiteral) substrExpr;
            stmt.getQueryGenerator().useParameterExpressionAsLiteral(substrLit);
            if (substrLit.getValue() == null) {
                return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr));
            }
        }
        SQLExpression likeSubstrExpr = new StringLiteral(stmt, expr.getJavaTypeMapping(), '%', null);
        return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr).add(likeSubstrExpr));
    }
    // Create a new StringExpression and manually update its SQL
    if (substrExpr.isParameter()) {
        // Any pattern expression cannot be a parameter here
        SQLLiteral substrLit = (SQLLiteral) substrExpr;
        stmt.getQueryGenerator().useParameterExpressionAsLiteral(substrLit);
        if (substrLit.getValue() == null) {
            return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr));
        }
    }
    SQLExpression likeSubstrExpr = new StringLiteral(stmt, expr.getJavaTypeMapping(), '%', null);
    return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr).add(likeSubstrExpr));
}
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) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 22 with BooleanExpression

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

the class StringEndsWithMethod 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() > 2) {
        throw new NucleusException(Localiser.msg("060003", "endsWith", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    SQLExpression substrExpr = args.get(0);
    if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
        throw new NucleusException(Localiser.msg("060003", "endsWith", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    if (args.size() > 1) {
        // TODO Use numExpr in a SUBSTRING
        if (substrExpr.isParameter()) {
            // Any pattern expression cannot be a parameter here
            SQLLiteral substrLit = (SQLLiteral) substrExpr;
            stmt.getQueryGenerator().useParameterExpressionAsLiteral(substrLit);
            if (substrLit.getValue() == null) {
                return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr));
            }
        }
        SQLExpression likeSubstrExpr = new StringLiteral(stmt, expr.getJavaTypeMapping(), '%', null);
        return new BooleanExpression(expr, Expression.OP_LIKE, likeSubstrExpr.add(ExpressionUtils.getEscapedPatternExpression(substrExpr)));
    }
    // Create a new StringExpression and manually update its SQL
    if (substrExpr.isParameter()) {
        // Any pattern expression cannot be a parameter here
        SQLLiteral substrLit = (SQLLiteral) substrExpr;
        stmt.getQueryGenerator().useParameterExpressionAsLiteral(substrLit);
        if (substrLit.getValue() == null) {
            return new BooleanExpression(expr, Expression.OP_LIKE, ExpressionUtils.getEscapedPatternExpression(substrExpr));
        }
    }
    SQLExpression likeSubstrExpr = new StringLiteral(stmt, expr.getJavaTypeMapping(), '%', null);
    return new BooleanExpression(expr, Expression.OP_LIKE, likeSubstrExpr.add(ExpressionUtils.getEscapedPatternExpression(substrExpr)));
}
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) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 23 with BooleanExpression

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

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

the class QueryToSQLMapper method processAndExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processAndExpression(org.datanucleus.query.expression.Expression)
     */
protected Object processAndExpression(Expression expr) {
    SQLExpression rightExpr = stack.pop();
    SQLExpression leftExpr = stack.pop();
    if (!(rightExpr instanceof BooleanExpression)) {
        throw new NucleusUserException("Query has clause " + rightExpr + " used with AND. This is illegal, and should be a boolean expression");
    }
    if (!(leftExpr instanceof BooleanExpression)) {
        throw new NucleusUserException("Query has clause " + leftExpr + " used with AND. This is illegal, and should be a boolean expression");
    }
    BooleanExpression right = (BooleanExpression) rightExpr;
    BooleanExpression left = (BooleanExpression) leftExpr;
    if (left.getSQLStatement() != null && right.getSQLStatement() != null && left.getSQLStatement() != right.getSQLStatement()) {
        if (left.getSQLStatement() == stmt && right.getSQLStatement().isChildStatementOf(stmt)) {
            // Apply right to its sub-statement now and return left
            right.getSQLStatement().whereAnd(right, true);
            stack.push(left);
            return left;
        } else if (right.getSQLStatement() == stmt && left.getSQLStatement().isChildStatementOf(stmt)) {
            // Apply left to its sub-statement now and return right
            left.getSQLStatement().whereAnd(left, true);
            stack.push(right);
            return right;
        }
    // TODO Cater for more situations
    }
    if (compileComponent == CompilationComponent.FILTER) {
        // Make sure any simple boolean field clauses are suitable
        left = getBooleanExpressionForUseInFilter(left);
        right = getBooleanExpressionForUseInFilter(right);
    }
    BooleanExpression opExpr = left.and(right);
    stack.push(opExpr);
    return opExpr;
}
Also used : BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 25 with BooleanExpression

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

Aggregations

BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)39 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)33 NucleusException (org.datanucleus.exceptions.NucleusException)14 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)14 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)14 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)13 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)12 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)11 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)11 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)10 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)8 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)8 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)7 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)7 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)6 BooleanSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression)6 MapExpression (org.datanucleus.store.rdbms.sql.expression.MapExpression)6 ArrayList (java.util.ArrayList)5 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)5 CollectionExpression (org.datanucleus.store.rdbms.sql.expression.CollectionExpression)5