Search in sources :

Example 1 with CharacterLiteral

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

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

Aggregations

BaseDatastoreAdapter (org.datanucleus.store.rdbms.adapter.BaseDatastoreAdapter)2 SQLText (org.datanucleus.store.rdbms.sql.SQLText)2 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)2 CharacterLiteral (org.datanucleus.store.rdbms.sql.expression.CharacterLiteral)2 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)2