Search in sources :

Example 1 with PredefinedFunction

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

the class OracleTransformer method getDateTimeDiff.

@Override
protected SimpleNode getDateTimeDiff(SimpleNode startDate, SimpleNode endDate, String format) {
    Function trunc = parserContext.getFunction("trunc");
    AstParenthesis dateDifference = new AstParenthesis(DefaultParserContext.FUNC_MINUS.node(new AstCast(endDate, "DATE"), new AstCast(startDate, "DATE")));
    PredefinedFunction opTimes = DefaultParserContext.FUNC_TIMES;
    switch(format) {
        case "SECOND":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24 * 60 * 60)));
        case "MINUTE":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24 * 60)));
        case "HOUR":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24)));
        case "DAY":
            return trunc.node(dateDifference);
        case "MONTH":
            return MONTHS_BETWEEN.node(trunc.node(endDate, new AstStringConstant(format)), trunc.node(startDate, new AstStringConstant(format)));
        case "YEAR":
            PredefinedFunction opDivide = DefaultParserContext.FUNC_DIVIDE;
            return parserContext.getFunction("floor").node(opDivide.node(MONTHS_BETWEEN.node(endDate, startDate), AstNumericConstant.of(12)));
        default:
            throw new IllegalStateException("Unsupported value for datepart in TIMESTAMPDIFF: " + format);
    }
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstCast(com.developmentontheedge.sql.model.AstCast) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction)

Aggregations

AstCast (com.developmentontheedge.sql.model.AstCast)1 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)1 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)1 Function (com.developmentontheedge.sql.model.Function)1 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)1