Search in sources :

Example 1 with AstCase

use of com.developmentontheedge.sql.model.AstCase in project be5 by DevelopmentOnTheEdge.

the class MySqlTransformer method transformDecode.

@Override
protected void transformDecode(AstFunNode node) {
    AstCase astCase = new AstCase();
    for (int i = 1; i < node.jjtGetNumChildren() - 1; i++) {
        SimpleNode cond;
        if (node.child(i) instanceof AstSpecialConstant) {
            cond = new AstNullPredicate(0);
            cond.addChild(node.child(0));
        } else {
            cond = DefaultParserContext.FUNC_EQ.node(node.child(0), node.child(i));
        }
        astCase.addChild(new AstWhen(cond, node.child(++i)));
        if (i == node.jjtGetNumChildren() - 2) {
            astCase.addChild(new AstCaseElse(node.child(++i)));
        }
    }
    node.replaceWith(astCase);
}
Also used : AstCaseElse(com.developmentontheedge.sql.model.AstCaseElse) AstWhen(com.developmentontheedge.sql.model.AstWhen) AstCase(com.developmentontheedge.sql.model.AstCase) AstNullPredicate(com.developmentontheedge.sql.model.AstNullPredicate) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSpecialConstant(com.developmentontheedge.sql.model.AstSpecialConstant)

Example 2 with AstCase

use of com.developmentontheedge.sql.model.AstCase in project be5 by DevelopmentOnTheEdge.

the class PostgreSqlTransformer method transformDecode.

@Override
protected void transformDecode(AstFunNode node) {
    AstCase astCase = new AstCase();
    for (int i = 1; i < node.jjtGetNumChildren() - 1; i++) {
        SimpleNode cond;
        if (node.child(i) instanceof AstSpecialConstant) {
            cond = new AstNullPredicate(0);
            cond.addChild(node.child(0));
        } else {
            cond = DefaultParserContext.FUNC_EQ.node(node.child(0), node.child(i));
        }
        astCase.addChild(new AstWhen(cond, node.child(++i)));
        if (i == node.jjtGetNumChildren() - 2) {
            astCase.addChild(new AstCaseElse(node.child(++i)));
        }
    }
    node.replaceWith(astCase);
}
Also used : AstCaseElse(com.developmentontheedge.sql.model.AstCaseElse) AstWhen(com.developmentontheedge.sql.model.AstWhen) AstCase(com.developmentontheedge.sql.model.AstCase) AstNullPredicate(com.developmentontheedge.sql.model.AstNullPredicate) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSpecialConstant(com.developmentontheedge.sql.model.AstSpecialConstant)

Example 3 with AstCase

use of com.developmentontheedge.sql.model.AstCase in project be5 by DevelopmentOnTheEdge.

the class SqlServerTransformer method transformDecode.

@Override
protected void transformDecode(AstFunNode node) {
    AstCase astCase = new AstCase();
    for (int i = 1; i < node.jjtGetNumChildren() - 1; i++) {
        SimpleNode cond;
        if (node.child(i) instanceof AstSpecialConstant) {
            cond = new AstNullPredicate(0);
            cond.addChild(node.child(0));
        } else {
            cond = DefaultParserContext.FUNC_EQ.node(node.child(0), node.child(i));
        }
        astCase.addChild(new AstWhen(cond, node.child(++i)));
        if (i == node.jjtGetNumChildren() - 2) {
            astCase.addChild(new AstCaseElse(node.child(++i)));
        }
    }
    node.replaceWith(astCase);
}
Also used : AstCaseElse(com.developmentontheedge.sql.model.AstCaseElse) AstWhen(com.developmentontheedge.sql.model.AstWhen) AstCase(com.developmentontheedge.sql.model.AstCase) AstNullPredicate(com.developmentontheedge.sql.model.AstNullPredicate) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSpecialConstant(com.developmentontheedge.sql.model.AstSpecialConstant)

Example 4 with AstCase

use of com.developmentontheedge.sql.model.AstCase in project be5 by DevelopmentOnTheEdge.

the class SqlServerTransformer method getDateTimeDiff.

@Override
protected SimpleNode getDateTimeDiff(SimpleNode startDate, SimpleNode endDate, String format) {
    int dp;
    switch(format) {
        case "YEAR":
            format = "yy";
            dp = 5;
            break;
        case "MONTH":
            format = "mm";
            dp = 2;
            break;
        case "DAY":
            return DATEDIFF.node(new AstIdentifierConstant("dd"), startDate, endDate);
        case "HOUR":
            format = "hh";
            dp = 9;
            break;
        case "MINUTE":
            format = "mi";
            dp = 6;
            break;
        case "SECOND":
            format = "ss";
            dp = 3;
            break;
        default:
            throw new IllegalStateException("Unsupported value for datepart in TIMESTAMPDIFF: " + format);
    }
    AstFunNode dateDiff = DATEDIFF.node(new AstIdentifierConstant(format), startDate, endDate);
    AstFunNode cond = parserContext.getFunction("<=").node(getDatePart(startDate, dp, format), getDatePart(endDate, dp, format));
    return new AstCase(new AstWhen(cond, dateDiff), new AstCaseElse(DefaultParserContext.FUNC_MINUS.node(dateDiff, AstNumericConstant.of(1))));
}
Also used : AstCaseElse(com.developmentontheedge.sql.model.AstCaseElse) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstWhen(com.developmentontheedge.sql.model.AstWhen) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstCase(com.developmentontheedge.sql.model.AstCase)

Aggregations

AstCase (com.developmentontheedge.sql.model.AstCase)4 AstCaseElse (com.developmentontheedge.sql.model.AstCaseElse)4 AstWhen (com.developmentontheedge.sql.model.AstWhen)4 AstNullPredicate (com.developmentontheedge.sql.model.AstNullPredicate)3 AstSpecialConstant (com.developmentontheedge.sql.model.AstSpecialConstant)3 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)3 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)1 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)1