Search in sources :

Example 1 with CaseNumericExpression

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

the class StringIndexOf5Method 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"));
    }
    ArrayList funcArgs = new ArrayList();
    if (args.size() == 1) {
        // strExpr.indexOf(str1)
        funcArgs.add(expr);
        funcArgs.add(substrExpr);
        SQLExpression oneExpr = ExpressionUtils.getLiteralForOne(stmt);
        NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "STRPOS", funcArgs);
        return new NumericExpression(locateExpr, Expression.OP_SUB, oneExpr);
    }
    // strExpr.indexOf(str1, pos)
    SQLExpression fromExpr = args.get(1);
    if (!(fromExpr instanceof NumericExpression)) {
        throw new NucleusException(Localiser.msg("060003", "indexOf", "StringExpression", 1, "NumericExpression"));
    }
    // Find the substring starting at this position
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    ArrayList substrArgs = new ArrayList(1);
    substrArgs.add(fromExpr);
    SQLExpression strExpr = exprFactory.invokeMethod(stmt, "java.lang.String", "substring", expr, substrArgs);
    funcArgs.add(strExpr);
    funcArgs.add(substrExpr);
    NumericExpression locateExpr = new NumericExpression(stmt, stmt.getSQLExpressionFactory().getMappingForType(int.class, true), "STRPOS", funcArgs);
    SQLExpression[] whenExprs = new SQLExpression[1];
    NumericExpression zeroExpr = new IntegerLiteral(stmt, exprFactory.getMappingForType(Integer.class, false), Integer.valueOf(0), null);
    whenExprs[0] = locateExpr.gt(zeroExpr);
    SQLExpression[] actionExprs = new SQLExpression[1];
    SQLExpression oneExpr = ExpressionUtils.getLiteralForOne(stmt);
    NumericExpression posExpr1 = new NumericExpression(locateExpr, Expression.OP_SUB, oneExpr);
    actionExprs[0] = new NumericExpression(posExpr1, Expression.OP_ADD, fromExpr);
    SQLExpression elseExpr = new IntegerLiteral(stmt, exprFactory.getMappingForType(Integer.class, false), Integer.valueOf(-1), null);
    return new CaseNumericExpression(whenExprs, actionExprs, elseExpr);
}
Also used : ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) ArrayList(java.util.ArrayList) CaseNumericExpression(org.datanucleus.store.rdbms.sql.expression.CaseNumericExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CaseNumericExpression(org.datanucleus.store.rdbms.sql.expression.CaseNumericExpression) NucleusException(org.datanucleus.exceptions.NucleusException) CharacterExpression(org.datanucleus.store.rdbms.sql.expression.CharacterExpression) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral)

Example 2 with CaseNumericExpression

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

the class OptionalOrElseMethod 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() != 1) {
        throw new NucleusException("Optional.orElse should be passed 1 argument");
    }
    SQLExpression elseExpr = args.get(0);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    OptionalMapping opMapping = (OptionalMapping) ((OptionalExpression) expr).getJavaTypeMapping();
    JavaTypeMapping javaMapping = opMapping.getWrappedMapping();
    SQLExpression getExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), javaMapping);
    SQLExpression isNotNullExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), javaMapping).ne(new NullLiteral(stmt, javaMapping, null, null));
    if (javaMapping instanceof StringMapping) {
        return new CaseStringExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    } else if (javaMapping instanceof IntegerMapping || javaMapping instanceof LongMapping || javaMapping instanceof ShortMapping || javaMapping instanceof FloatMapping || javaMapping instanceof DoubleMapping || javaMapping instanceof BigIntegerMapping || javaMapping instanceof BigDecimalMapping) // TODO Maybe use javaMapping.getJavaType compared to Number to avoid the check above
    {
        return new CaseNumericExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    } else if (javaMapping instanceof BooleanMapping) {
        return new CaseBooleanExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    }
    return new CaseExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) BigDecimalMapping(org.datanucleus.store.rdbms.mapping.java.BigDecimalMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) CaseNumericExpression(org.datanucleus.store.rdbms.sql.expression.CaseNumericExpression) StringMapping(org.datanucleus.store.rdbms.mapping.java.StringMapping) BigIntegerMapping(org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping) IntegerMapping(org.datanucleus.store.rdbms.mapping.java.IntegerMapping) BooleanMapping(org.datanucleus.store.rdbms.mapping.java.BooleanMapping) DoubleMapping(org.datanucleus.store.rdbms.mapping.java.DoubleMapping) OptionalMapping(org.datanucleus.store.rdbms.mapping.java.OptionalMapping) ShortMapping(org.datanucleus.store.rdbms.mapping.java.ShortMapping) CaseExpression(org.datanucleus.store.rdbms.sql.expression.CaseExpression) CaseStringExpression(org.datanucleus.store.rdbms.sql.expression.CaseStringExpression) LongMapping(org.datanucleus.store.rdbms.mapping.java.LongMapping) BigIntegerMapping(org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping) FloatMapping(org.datanucleus.store.rdbms.mapping.java.FloatMapping) CaseBooleanExpression(org.datanucleus.store.rdbms.sql.expression.CaseBooleanExpression) NucleusException(org.datanucleus.exceptions.NucleusException) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral)

Aggregations

NucleusException (org.datanucleus.exceptions.NucleusException)2 CaseNumericExpression (org.datanucleus.store.rdbms.sql.expression.CaseNumericExpression)2 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)2 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)2 ArrayList (java.util.ArrayList)1 BigDecimalMapping (org.datanucleus.store.rdbms.mapping.java.BigDecimalMapping)1 BigIntegerMapping (org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping)1 BooleanMapping (org.datanucleus.store.rdbms.mapping.java.BooleanMapping)1 DoubleMapping (org.datanucleus.store.rdbms.mapping.java.DoubleMapping)1 FloatMapping (org.datanucleus.store.rdbms.mapping.java.FloatMapping)1 IntegerMapping (org.datanucleus.store.rdbms.mapping.java.IntegerMapping)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 LongMapping (org.datanucleus.store.rdbms.mapping.java.LongMapping)1 OptionalMapping (org.datanucleus.store.rdbms.mapping.java.OptionalMapping)1 ShortMapping (org.datanucleus.store.rdbms.mapping.java.ShortMapping)1 StringMapping (org.datanucleus.store.rdbms.mapping.java.StringMapping)1 CaseBooleanExpression (org.datanucleus.store.rdbms.sql.expression.CaseBooleanExpression)1 CaseExpression (org.datanucleus.store.rdbms.sql.expression.CaseExpression)1 CaseStringExpression (org.datanucleus.store.rdbms.sql.expression.CaseStringExpression)1 CharacterExpression (org.datanucleus.store.rdbms.sql.expression.CharacterExpression)1