Search in sources :

Example 16 with StringExpression

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

the class StringIndexOf2Method 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(expr);
    funcArgs.add(substrExpr);
    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), "INSTR", 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 17 with StringExpression

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

the class StringIndexOf3Method 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 one = ExpressionUtils.getLiteralForOne(stmt);
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(expr);
    List funcArgs2 = new ArrayList();
    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"));
    }
    funcArgs2.add(substrExpr);
    List types = new ArrayList();
    // max 4000 according DB2 docs
    types.add("VARCHAR(4000)");
    funcArgs.add(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(String.class, true), "CAST", funcArgs2, types));
    if (args.size() == 2) {
        SQLExpression fromExpr = args.get(1);
        if (!(fromExpr instanceof NumericExpression)) {
            throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 1, "NumericExpression"));
        }
        types = new ArrayList();
        types.add("BIGINT");
        List funcArgs3 = new ArrayList();
        funcArgs3.add(new NumericExpression(fromExpr, Expression.OP_ADD, one));
        // Add 1 to the passed in value so that it is of origin 1 to be compatible with LOCATE
        // Make sure argument is typed as BIGINT
        funcArgs.add(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "CAST", funcArgs3, types));
    }
    NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "LOCATE", funcArgs);
    // Subtract 1 from the result of LOCATE to be consistent with Java strings
    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) List(java.util.List) ArrayList(java.util.ArrayList) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression)

Example 18 with StringExpression

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

the class TemporalMonthJavaMethod2 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) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "MONTH_JAVA");
    SQLExpression one = ExpressionUtils.getLiteralForOne(stmt);
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping2 = storeMgr.getMappingManager().getMapping(String.class);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    SQLExpression mm = exprFactory.newLiteral(stmt, mapping2, "MM");
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(invokedExpr);
    funcArgs.add(mm);
    ArrayList funcArgs2 = new ArrayList();
    funcArgs2.add(new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "TO_CHAR", funcArgs));
    // Delete one from the SQL "month" (origin=1) to be compatible with Java month (origin=0)
    NumericExpression numExpr = new NumericExpression(new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "TO_NUMBER", funcArgs2), Expression.OP_SUB, one);
    numExpr.encloseInParentheses();
    return numExpr;
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) 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) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 19 with StringExpression

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

the class TemporalDayMethod2 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) {
    SQLExpression invokedExpr = getInvokedExpression(expr, args, "DAY");
    RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
    JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(String.class);
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(invokedExpr);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    funcArgs.add(exprFactory.newLiteral(stmt, mapping, "DD"));
    ArrayList funcArgs2 = new ArrayList();
    funcArgs2.add(new StringExpression(stmt, mapping, "TO_CHAR", funcArgs));
    return new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class), "TO_NUMBER", funcArgs2);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) 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) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 20 with StringExpression

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

the class StringTrim2Method 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) {
        String val = (String) ((StringLiteral) expr).getValue();
        return new StringLiteral(stmt, expr.getJavaTypeMapping(), val.trim(), null);
    }
    ArrayList funcArgs = new ArrayList();
    funcArgs.add(expr);
    StringExpression strExpr = new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(String.class), "RTRIM", funcArgs);
    args.clear();
    args.add(strExpr);
    return new StringExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(String.class), "LTRIM", args);
}
Also used : StringLiteral(org.datanucleus.store.rdbms.sql.expression.StringLiteral) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList)

Aggregations

StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)43 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)33 ArrayList (java.util.ArrayList)29 NucleusException (org.datanucleus.exceptions.NucleusException)28 NumericExpression (org.datanucleus.store.rdbms.sql.expression.NumericExpression)24 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)23 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)23 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)18 ParameterLiteral (org.datanucleus.store.rdbms.sql.expression.ParameterLiteral)16 StringLiteral (org.datanucleus.store.rdbms.sql.expression.StringLiteral)12 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)9 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)8 TemporalExpression (org.datanucleus.store.rdbms.sql.expression.TemporalExpression)8 List (java.util.List)7 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)5 IntegerLiteral (org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)5 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)5 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)5 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)4 EnumExpression (org.datanucleus.store.rdbms.sql.expression.EnumExpression)4