use of org.jooq.DatePart in project jOOQ by jOOQ.
the class DateDiff method accept.
@Override
public final void accept(Context<?> ctx) {
DatePart p = part == null ? DAY : part;
switch(ctx.family()) {
case MARIADB:
case MYSQL:
switch(p) {
case MILLENNIUM:
case CENTURY:
case DECADE:
case YEAR:
ctx.visit(partDiff(p));
return;
case QUARTER:
case MONTH:
ctx.visit(monthDiff(p));
return;
case DAY:
ctx.visit(N_DATEDIFF).sql('(').visit(endDate).sql(", ").visit(startDate).sql(')');
return;
case MILLISECOND:
ctx.visit(new DateDiff<>(MICROSECOND, startDate, endDate).div(inline(1000)));
return;
case NANOSECOND:
ctx.visit(new DateDiff<>(MICROSECOND, startDate, endDate).times(inline(1000)));
return;
}
ctx.visit(N_TIMESTAMPDIFF).sql('(').visit(p.toName()).sql(", ").visit(startDate).sql(", ").visit(endDate).sql(')');
return;
case DERBY:
{
Name name = N_SQL_TSI_DAY;
switch(p) {
case MILLENNIUM:
case CENTURY:
case DECADE:
case YEAR:
ctx.visit(partDiff(p));
return;
case QUARTER:
case MONTH:
ctx.visit(monthDiff(p));
return;
case DAY:
name = N_SQL_TSI_DAY;
break;
case HOUR:
name = N_SQL_TSI_HOUR;
break;
case MINUTE:
name = N_SQL_TSI_MINUTE;
break;
case SECOND:
name = N_SQL_TSI_SECOND;
break;
case NANOSECOND:
name = N_SQL_TSI_FRAC_SECOND;
break;
case MILLISECOND:
ctx.visit(new DateDiff<>(NANOSECOND, startDate, endDate).div(inline(1000000L)));
return;
case MICROSECOND:
ctx.visit(new DateDiff<>(NANOSECOND, startDate, endDate).div(inline(1000L)));
return;
}
ctx.sql("{fn ").visit(N_TIMESTAMPDIFF).sql('(').visit(name).sql(", ").visit(startDate).sql(", ").visit(endDate).sql(") }");
return;
}
case FIREBIRD:
case H2:
case HSQLDB:
switch(p) {
case MILLENNIUM:
case CENTURY:
case DECADE:
ctx.visit(partDiff(p));
return;
case QUARTER:
if (ctx.family() == FIREBIRD) {
ctx.visit(monthDiff(QUARTER));
return;
}
break;
case HOUR:
case MINUTE:
case SECOND:
case MILLISECOND:
case MICROSECOND:
case NANOSECOND:
if (ctx.family() == HSQLDB) {
ctx.visit(N_DATEDIFF).sql('(').visit(p.toKeyword()).sql(", ").visit(startDate.cast(TIMESTAMP)).sql(", ").visit(endDate.cast(TIMESTAMP)).sql(')');
return;
}
break;
}
ctx.visit(N_DATEDIFF).sql('(').visit(p.toKeyword()).sql(", ").visit(startDate).sql(", ").visit(endDate).sql(')');
return;
case SQLITE:
ctx.sql('(').visit(N_STRFTIME).sql("('%s', ").visit(endDate).sql(") - ").visit(N_STRFTIME).sql("('%s', ").visit(startDate).sql(")) / 86400");
return;
case CUBRID:
case POSTGRES:
case YUGABYTEDB:
switch(p) {
case MILLENNIUM:
case CENTURY:
case DECADE:
case YEAR:
ctx.visit(partDiff(p));
return;
case QUARTER:
case MONTH:
ctx.visit(monthDiff(p));
return;
case DAY:
switch(ctx.family()) {
case POSTGRES:
case YUGABYTEDB:
if (endDate.getDataType().isDate() && startDate.getDataType().isDate())
ctx.sql('(').visit(endDate).sql(" - ").visit(startDate).sql(')');
else
ctx.visit(N_EXTRACT).sql('(').visit(K_DAY).sql(' ').visit(K_FROM).sql(' ').visit(endDate).sql(" - ").visit(startDate).sql(')');
return;
default:
// [#4481] Parentheses are important in case this expression is
// placed in the context of other arithmetic
ctx.sql('(').visit(endDate).sql(" - ").visit(startDate).sql(')');
return;
}
case HOUR:
case MINUTE:
ctx.visit(partDiff(EPOCH).div(p == HOUR ? inline(3600) : inline(60)));
return;
case SECOND:
ctx.visit(partDiff(EPOCH));
return;
case MILLISECOND:
case MICROSECOND:
case NANOSECOND:
ctx.visit(partDiff(EPOCH).times(p == MILLISECOND ? inline(1000) : p == MICROSECOND ? inline(1000000) : inline(1000000000)));
return;
}
break;
}
ctx.visit(castIfNeeded(endDate.minus(startDate), Integer.class));
}
Aggregations