From 1d3b00bdb2e16f1c39ee32a3e29d32a0c605e97b Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 3 Jul 2021 11:04:13 +0200 Subject: [PATCH] [jOOQ/jOOQ#11960] Prevent NoClassDefFoundError The previous implementation required Redshift users to also have the pgjdbc driver on the classpath to prevent NoClassDefFoundError, but that should not be necessary. --- .../org/jooq/util/postgres/PostgresUtils.java | 64 +++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/util/postgres/PostgresUtils.java b/jOOQ/src/main/java/org/jooq/util/postgres/PostgresUtils.java index 4ddbf2b579..9cdfc8494e 100644 --- a/jOOQ/src/main/java/org/jooq/util/postgres/PostgresUtils.java +++ b/jOOQ/src/main/java/org/jooq/util/postgres/PostgresUtils.java @@ -76,15 +76,21 @@ import org.postgresql.util.PGInterval; */ public class PostgresUtils { - private static final String POSTGRESQL_HEX_STRING_PREFIX = "\\x"; + private static final String POSTGRESQL_HEX_STRING_PREFIX = "\\x"; // PGobject parsing state machine - private static final int PG_OBJECT_INIT = 0; - private static final int PG_OBJECT_BEFORE_VALUE = 1; - private static final int PG_OBJECT_QUOTED_VALUE = 2; - private static final int PG_OBJECT_UNQUOTED_VALUE = 3; - private static final int PG_OBJECT_AFTER_VALUE = 4; - private static final int PG_OBJECT_END = 5; + private static final int PG_OBJECT_INIT = 0; + private static final int PG_OBJECT_BEFORE_VALUE = 1; + private static final int PG_OBJECT_QUOTED_VALUE = 2; + private static final int PG_OBJECT_UNQUOTED_VALUE = 3; + private static final int PG_OBJECT_AFTER_VALUE = 4; + private static final int PG_OBJECT_END = 5; + + private static volatile Boolean pgIntervalAvailable; + + + + /** * Parse a Postgres-encoded bytea string @@ -279,6 +285,9 @@ public class PostgresUtils { + + + @@ -289,7 +298,7 @@ public class PostgresUtils { public static DayToSecond toDayToSecond(Object pgInterval) { boolean negative = pgInterval.toString().contains("-"); - if (pgInterval instanceof PGInterval) { + if (pgIntervalAvailable() && pgInterval instanceof PGInterval) { PGInterval i = (PGInterval) pgInterval; if (negative) i.scale(-1); @@ -330,7 +339,7 @@ public class PostgresUtils { else - throw new IllegalArgumentException("Unsupported interval type: " + pgInterval); + throw new IllegalArgumentException("Unsupported interval type. Make sure you have the pgjdbc or redshift driver on your classpath: " + pgInterval); } /** @@ -339,7 +348,7 @@ public class PostgresUtils { public static YearToMonth toYearToMonth(Object pgInterval) { boolean negative = pgInterval.toString().contains("-"); - if (pgInterval instanceof PGInterval) { + if (pgIntervalAvailable() && pgInterval instanceof PGInterval) { PGInterval i = (PGInterval) pgInterval; if (negative) i.scale(-1); @@ -366,7 +375,7 @@ public class PostgresUtils { else - throw new IllegalArgumentException("Unsupported interval type: " + pgInterval); + throw new IllegalArgumentException("Unsupported interval type. Make sure you have the pgjdbc or redshift driver on your classpath: " + pgInterval); } /** @@ -630,4 +639,37 @@ public class PostgresUtils { return sb.toString(); } + + private static final boolean pgIntervalAvailable() { + if (pgIntervalAvailable == null) { + try { + new PGInterval(); + pgIntervalAvailable = true; + } + catch (NoClassDefFoundError e) { + pgIntervalAvailable = false; + } + } + + return pgIntervalAvailable; + } + + + + + + + + + + + + + + + + + + + }