[jOOQ/jOOQ#11960] Add support for Redshift YEAR TO MONTH interval literals

This commit is contained in:
Lukas Eder 2021-07-02 14:57:45 +02:00
parent 661195af53
commit b3f4ff8dc0
5 changed files with 143 additions and 21 deletions

View File

@ -124,5 +124,11 @@
</dependencies>
</project>

View File

@ -44,6 +44,7 @@ module org.jooq {
requires static org.postgresql.jdbc;
exports org.jooq;

View File

@ -2262,6 +2262,12 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
// [#566] Interval data types are best bound as Strings
if (REQUIRE_PG_INTERVAL.contains(ctx.dialect()))
ctx.render().visit(inline(toPGInterval(value).toString()));
else
super.sqlInline0(ctx, value);
@ -4646,6 +4652,12 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
// [#566] Interval data types are best bound as Strings
if (REQUIRE_PG_INTERVAL.contains(ctx.dialect()))
ctx.render().visit(inline(toPGInterval(value).toString()));
else
super.sqlInline0(ctx, value);
@ -4721,6 +4733,12 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
// [#566] Interval data types are best bound as Strings
if (REQUIRE_PG_INTERVAL.contains(ctx.dialect()))
ctx.render().visit(inline(toPGInterval(value).toString()));
else
super.sqlInline0(ctx, value);

View File

@ -51,15 +51,19 @@ import java.util.List;
import org.jooq.Converter;
import org.jooq.EnumType;
// ...
import org.jooq.Record;
import org.jooq.exception.DataTypeException;
import org.jooq.tools.StringUtils;
import org.jooq.types.DayToSecond;
import org.jooq.types.YearToMonth;
import org.jooq.types.YearToSecond;
// ...
import org.postgresql.util.PGInterval;
// ...
/**
* A collection of utilities to cover the Postgres JDBC driver's missing
* implementations.
@ -236,29 +240,97 @@ public class PostgresUtils {
);
}
/**
* Convert a Postgres interval to a jOOQ <code>DAY TO SECOND</code> interval
*/
public static DayToSecond toDayToSecond(Object pgInterval) {
boolean negative = pgInterval.toString().contains("-");
PGInterval i = (PGInterval) pgInterval;
if (negative)
i.scale(-1);
if (pgInterval instanceof PGInterval) {
PGInterval i = (PGInterval) pgInterval;
if (negative)
i.scale(-1);
Double seconds = i.getSeconds();
DayToSecond result = new DayToSecond(
i.getDays(),
i.getHours(),
i.getMinutes(),
seconds.intValue(),
(int) (1000000000 * (seconds - seconds.intValue()))
);
Double seconds = i.getSeconds();
DayToSecond result = new DayToSecond(
i.getDays(),
i.getHours(),
i.getMinutes(),
seconds.intValue(),
(int) (1000000000 * (seconds - seconds.intValue()))
);
if (negative)
result = result.neg();
if (negative)
result = result.neg();
return result;
return result;
}
else
throw new IllegalArgumentException("Unsupported interval type: " + pgInterval);
}
/**
@ -267,16 +339,34 @@ public class PostgresUtils {
public static YearToMonth toYearToMonth(Object pgInterval) {
boolean negative = pgInterval.toString().contains("-");
PGInterval i = (PGInterval) pgInterval;
if (negative)
i.scale(-1);
if (pgInterval instanceof PGInterval) {
PGInterval i = (PGInterval) pgInterval;
if (negative)
i.scale(-1);
YearToMonth result = new YearToMonth(i.getYears(), i.getMonths());
YearToMonth result = new YearToMonth(i.getYears(), i.getMonths());
if (negative)
result = result.neg();
if (negative)
result = result.neg();
return result;
return result;
}
else
throw new IllegalArgumentException("Unsupported interval type: " + pgInterval);
}
/**

View File

@ -36,6 +36,7 @@
<postgres.version>42.2.22</postgres.version>
<sqlserver.version>9.2.1.jre11</sqlserver.version>
<oracle.version>21.1.0.0</oracle.version>
<redshift.version>2.0.0.6</redshift.version>
<!-- R2DBC SPI version and some matching driver versions -->
<io.r2dbc.version>0.9.0.M1</io.r2dbc.version>
@ -364,6 +365,12 @@
<!-- jooq-meta-extensions and integration tests have this dependency -->
<dependency>
<groupId>org.hibernate</groupId>