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);
}
}
Aggregations