use of com.developmentontheedge.sql.model.Function 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);
}
}
use of com.developmentontheedge.sql.model.Function in project be5 by DevelopmentOnTheEdge.
the class PostgreSqlTransformer method transformLastDay.
@Override
protected void transformLastDay(AstFunNode node) {
Function opPlus = DefaultParserContext.FUNC_PLUS;
Function opMinus = DefaultParserContext.FUNC_MINUS;
SimpleNode dateTrunc = parserContext.getFunction("date_trunc").node(new AstStringConstant("MONTH"), node.child(0));
AstInterval monthInterval = new AstInterval(new AstStringConstant("1 MONTH"));
AstInterval dayInterval = new AstInterval(new AstStringConstant("1 DAY"));
SimpleNode expr = opPlus.node(dateTrunc, opMinus.node(monthInterval, dayInterval));
node.replaceWith(new AstCast(expr, "DATE"));
}
use of com.developmentontheedge.sql.model.Function in project be5 by DevelopmentOnTheEdge.
the class PostgreSqlTransformer method transformDateAdd.
@Override
protected void transformDateAdd(AstFunNode node) {
Function opPlus = DefaultParserContext.FUNC_PLUS;
Function opTimes = DefaultParserContext.FUNC_TIMES;
SimpleNode date = node.child(0);
SimpleNode number = new AstParenthesis(node.child(1));
String name = node.getFunction().getName();
String type = name.equalsIgnoreCase("add_months") ? "1 MONTH" : name.equalsIgnoreCase("add_days") ? "1 DAY" : "1 MILLISECOND";
AstInterval interval = new AstInterval(new AstStringConstant(type));
node.replaceWith(new AstParenthesis(opPlus.node(date, opTimes.node(interval, number))));
}
use of com.developmentontheedge.sql.model.Function in project be5 by DevelopmentOnTheEdge.
the class DB2Transformer method transformDateAdd.
@Override
protected void transformDateAdd(AstFunNode node) {
Function opPlus = DefaultParserContext.FUNC_PLUS;
Function opTimes = DefaultParserContext.FUNC_TIMES;
SimpleNode date = node.child(0);
SimpleNode number = node.child(1);
String name = node.getFunction().getName();
String type = name.equalsIgnoreCase("add_months") ? "MONTHS" : name.equalsIgnoreCase("add_days") ? "DAYS" : "MICROSECONDS";
if (type.equals("MICROSECONDS"))
number = new AstParenthesis(opTimes.node(number, AstNumericConstant.of(1000)));
node.replaceWith(new AstParenthesis(opPlus.node(date, new AstFirstDayOf(number, type))));
}
Aggregations