[#3824] PostgreSQL dateAdd() erroneously returns a TIMESTAMP value

This commit is contained in:
lukaseder 2014-12-02 15:06:31 +01:00
parent b364042695
commit 9cea374742
2 changed files with 10 additions and 3 deletions

View File

@ -44,6 +44,8 @@ import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.keyword;
import java.sql.Date;
import org.jooq.Configuration;
import org.jooq.DatePart;
import org.jooq.Field;
@ -148,7 +150,12 @@ class DateAdd<T extends java.util.Date> extends AbstractFunction<T> {
default: throwUnsupported();
}
return date.add(field("({0} || {1})::interval", interval, inline(keyword)));
// [#3824] Ensure that the output for DATE arithmetic will also
// be of type DATE, not TIMESTAMP
if (getDataType().getType() == Date.class)
return field("({0} + ({1} || {2})::interval)::date", getDataType(), date, interval, inline(keyword));
else
return field("({0} + ({1} || {2})::interval)", getDataType(), date, interval, inline(keyword));
}
case SQLITE: {

View File

@ -570,10 +570,10 @@ class Expression<T> extends AbstractFunction<T> {
// with incompatible data types and timezones
// ? + CAST (? || ' days' as interval)
if (operator == ADD) {
return lhs.add(rhsAsNumber().concat(" day").cast(DayToSecond.class));
return new DateAdd(lhs, rhsAsNumber(), DatePart.DAY);
}
else {
return lhs.sub(rhsAsNumber().concat(" day").cast(DayToSecond.class));
return new DateAdd(lhs, rhsAsNumber().neg(), DatePart.DAY);
}
}