Search in sources :

Example 1 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 2 with BooleanExpression

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

the class StringMatchesMethod method getBooleanLikeExpression.

protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
    if (stmt.getDatastoreAdapter().supportsOption(DatastoreAdapter.RAW_PREFIX_LIKE_STATEMENTS)) {
        // for spanner escape of Like statements should be done by double backslash '\\' or we should use raw strings r''
        // we choose raw strings approach otherwise we have to change the escape logic in datanucleus-core
        // see https://cloud.google.com/spanner/docs/operators#comparison_operators
        regExpr.toSQLText().prepend("r");
    }
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    BooleanExpression likeExpr = new BooleanExpression(stmt, exprFactory.getMappingForType(boolean.class, false));
    SQLText sql = likeExpr.toSQLText();
    sql.clearStatement();
    if (Expression.OP_LIKE.isHigherThanLeftSide(expr.getLowestOperator())) {
        sql.append("(").append(expr).append(")");
    } else {
        sql.append(expr);
    }
    sql.append(" LIKE ");
    if (Expression.OP_LIKE.isHigherThanRightSide(regExpr.getLowestOperator())) {
        sql.append("(").append(regExpr).append(")");
    } else {
        sql.append(regExpr);
    }
    BaseDatastoreAdapter dba = (BaseDatastoreAdapter) stmt.getRDBMSManager().getDatastoreAdapter();
    if (escapeExpr != null) {
        if (escapeExpr instanceof CharacterLiteral) {
            String chr = "" + ((CharacterLiteral) escapeExpr).getValue();
            if (chr.equals(dba.getEscapeCharacter())) {
                // If the escape character specified matches the Java character then apply the known working ESCAPE
                // This is because some datastore JDBC drivers require additional "\" characters to allow
                // for Java usage
                sql.append(dba.getEscapePatternExpression());
            } else {
                sql.append(" ESCAPE " + escapeExpr);
            }
        } else {
            sql.append(" ESCAPE " + escapeExpr);
        }
    } else {
        sql.append(" " + dba.getEscapePatternExpression());
    }
    return likeExpr;
}
Also used : BaseDatastoreAdapter(org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLText(org.datanucleus.store.rdbms.sql.SQLText) CharacterLiteral(org.datanucleus.store.rdbms.sql.expression.CharacterLiteral)

Example 3 with BooleanExpression

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

the class StringSimilarPostgresqlMethod method getBooleanLikeExpression.

protected BooleanExpression getBooleanLikeExpression(SQLStatement stmt, SQLExpression expr, SQLExpression regExpr, SQLExpression escapeExpr) {
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    BooleanExpression similarToExpr = new BooleanExpression(stmt, exprFactory.getMappingForType(boolean.class, false));
    SQLText sql = similarToExpr.toSQLText();
    sql.clearStatement();
    if (OP_SIMILAR_TO.isHigherThanLeftSide(expr.getLowestOperator())) {
        sql.append("(").append(expr).append(")");
    } else {
        sql.append(expr);
    }
    sql.append(" SIMILAR TO ");
    if (OP_SIMILAR_TO.isHigherThanRightSide(regExpr.getLowestOperator())) {
        sql.append("(").append(regExpr).append(")");
    } else {
        sql.append(regExpr);
    }
    BaseDatastoreAdapter dba = (BaseDatastoreAdapter) stmt.getRDBMSManager().getDatastoreAdapter();
    if (escapeExpr != null) {
        if (escapeExpr instanceof CharacterLiteral) {
            String chr = "" + ((CharacterLiteral) escapeExpr).getValue();
            if (chr.equals(dba.getEscapeCharacter())) {
                // If the escape character specified matches the Java character then apply the known working ESCAPE
                // This is because some datastore JDBC drivers require additional "\" characters to allow
                // for Java usage
                sql.append(dba.getEscapePatternExpression());
            } else {
                sql.append(" ESCAPE " + escapeExpr);
            }
        } else {
            sql.append(" ESCAPE " + escapeExpr);
        }
    } else {
        sql.append(" " + dba.getEscapePatternExpression());
    }
    return similarToExpr;
}
Also used : BaseDatastoreAdapter(org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLText(org.datanucleus.store.rdbms.sql.SQLText) CharacterLiteral(org.datanucleus.store.rdbms.sql.expression.CharacterLiteral)

Example 4 with BooleanExpression

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

the class StringStartsWith3Method 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"));
    }
    // {stringExpr}.indexOf(strExpr1 [,numExpr2])
    ArrayList funcArgs = new ArrayList();
    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"));
    }
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    if (args.size() > 1) {
        SQLExpression numExpr = args.get(1);
        funcArgs.add(substrExpr);
        funcArgs.add(expr);
        return new BooleanExpression(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "LOCATE", funcArgs), Expression.OP_EQ, one.add(numExpr));
    }
    funcArgs.add(substrExpr);
    funcArgs.add(expr);
    return new BooleanExpression(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "LOCATE", funcArgs), Expression.OP_EQ, one);
}
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) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

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

Aggregations

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