Search in sources :

Example 6 with ParameterLiteral

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

the class StringIndexOfMethod 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", "indexOf", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    // {stringExpr}.indexOf(strExpr1 [,numExpr2])
    SQLExpression substrExpr = args.get(0);
    if (!(substrExpr instanceof StringExpression) && !(substrExpr instanceof CharacterExpression) && !(substrExpr instanceof ParameterLiteral)) {
        throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 0, "StringExpression/CharacterExpression/ParameterLiteral"));
    }
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(substrExpr);
    funcArgs.add(expr);
    if (args.size() == 2) {
        SQLExpression fromExpr = args.get(1);
        if (!(fromExpr instanceof NumericExpression)) {
            throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 1, "NumericExpression"));
        }
        funcArgs.add(fromExpr.add(one));
    }
    NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "LOCATE", funcArgs);
    return new NumericExpression(locateExpr, Expression.OP_SUB, one).encloseInParentheses();
}
Also used : 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) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 7 with ParameterLiteral

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

the class StringLength2Method 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) {
    DatastoreAdapter dba = stmt.getDatastoreAdapter();
    if (!(dba instanceof FirebirdAdapter)) {
        throw new NucleusException("StringLength2Method being used for evaluation of String.length yet this is for Firebird ONLY. Please report this");
    }
    FirebirdAdapter fba = (FirebirdAdapter) dba;
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    if (fba.supportsCharLengthFunction()) {
        // Firebird v2+ support CHAR_LENGTH
        if (!expr.isParameter() && expr instanceof StringLiteral) {
            JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
            String val = (String) ((StringLiteral) expr).getValue();
            return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
        } else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
            ArrayList funcArgs = new ArrayList();
            funcArgs.add(expr);
            return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "CHAR_LENGTH", funcArgs);
        } else {
            throw new NucleusException(Localiser.msg("060001", "length", expr));
        }
    }
    if (expr instanceof StringLiteral) {
        // Firebird v1 requires STRLEN
        JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
        String val = (String) ((StringLiteral) expr).getValue();
        return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
    } else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
        ArrayList funcArgs = new ArrayList();
        funcArgs.add(expr);
        return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "STRLEN", funcArgs);
    } else {
        throw new NucleusException(Localiser.msg("060001", "length", expr));
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) FirebirdAdapter(org.datanucleus.store.rdbms.adapter.FirebirdAdapter) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) NucleusException(org.datanucleus.exceptions.NucleusException) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 8 with ParameterLiteral

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

the class StringLength3Method 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 (expr instanceof StringLiteral) {
        SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
        JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
        String val = (String) ((StringLiteral) expr).getValue();
        return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
    } else if (expr instanceof StringExpression || expr instanceof ParameterLiteral) {
        ArrayList funcArgs = new ArrayList();
        funcArgs.add(expr);
        return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "LENGTH", funcArgs);
    } else {
        throw new NucleusException(Localiser.msg("060001", "length", expr));
    }
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 9 with ParameterLiteral

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

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

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