From 1e1d3a7c91e9903378b16d8872a675b69babc6f5 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 29 Jul 2020 10:45:02 +0200 Subject: [PATCH] [jOOQ/jOOQ#10456] Add static YearToMonth and DayToSecond.(String) constructor methods --- .../main/java/org/jooq/impl/ParserImpl.java | 83 ++++---- .../main/java/org/jooq/types/DayToSecond.java | 198 +++++++++++++++++- .../main/java/org/jooq/types/YearToMonth.java | 63 +++++- 3 files changed, 295 insertions(+), 49 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index e1e5ee279f..1d55db7cb6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -8278,48 +8278,44 @@ final class ParserImpl implements Parser { return result; String string = parseStringLiteral(ctx); + String message = "Illegal interval literal"; - try { - if (parseKeywordIf(ctx, "YEAR")) - if (parseKeywordIf(ctx, "TO") && parseKeyword(ctx, "MONTH")) - return YearToMonth.valueOf(string); + if (parseKeywordIf(ctx, "YEAR")) + if (parseKeywordIf(ctx, "TO") && parseKeyword(ctx, "MONTH")) + return requireNotNull(ctx, YearToMonth.yearToMonth(string), message); + else + return requireNotNull(ctx, YearToMonth.year(string), message); + else if (parseKeywordIf(ctx, "MONTH")) + return requireNotNull(ctx, YearToMonth.month(string), message); + else if (parseKeywordIf(ctx, "DAY")) + if (parseKeywordIf(ctx, "TO")) + if (parseKeywordIf(ctx, "SECOND")) + return requireNotNull(ctx, DayToSecond.dayToSecond(string), message); + else if (parseKeywordIf(ctx, "MINUTE")) + return requireNotNull(ctx, DayToSecond.dayToMinute(string), message); + else if (parseKeywordIf(ctx, "HOUR")) + return requireNotNull(ctx, DayToSecond.dayToHour(string), message); else - return new YearToMonth(Integer.parseInt(string)); - else if (parseKeywordIf(ctx, "MONTH")) - return new YearToMonth(0, Integer.parseInt(string)); - else if (parseKeywordIf(ctx, "DAY")) - if (parseKeywordIf(ctx, "TO")) - if (parseKeywordIf(ctx, "SECOND")) - return DayToSecond.valueOf(string); - else if (parseKeywordIf(ctx, "MINUTE")) - return DayToSecond.valueOf(string + ":00"); - else if (parseKeywordIf(ctx, "HOUR")) - return DayToSecond.valueOf(string + ":00:00"); - else - throw ctx.expected("HOUR", "MINUTE", "SECOND"); + throw ctx.expected("HOUR", "MINUTE", "SECOND"); + else + return requireNotNull(ctx, DayToSecond.day(string), message); + else if (parseKeywordIf(ctx, "HOUR")) + if (parseKeywordIf(ctx, "TO")) + if (parseKeywordIf(ctx, "SECOND")) + return requireNotNull(ctx, DayToSecond.hourToSecond(string), message); + else if (parseKeywordIf(ctx, "MINUTE")) + return requireNotNull(ctx, DayToSecond.hourToMinute(string), message); else - return new DayToSecond(Integer.parseInt(string)); - else if (parseKeywordIf(ctx, "HOUR")) - if (parseKeywordIf(ctx, "TO")) - if (parseKeywordIf(ctx, "SECOND")) - return DayToSecond.valueOf("0 " + string); - else if (parseKeywordIf(ctx, "MINUTE")) - return DayToSecond.valueOf("0 " + string + ":00"); - else - throw ctx.expected("MINUTE", "SECOND"); - else - return new DayToSecond(0, Integer.parseInt(string)); - else if (parseKeywordIf(ctx, "MINUTE")) - if (parseKeywordIf(ctx, "TO") && parseKeyword(ctx, "SECOND")) - return DayToSecond.valueOf("0 00:" + string); - else - return new DayToSecond(0, 0, Integer.parseInt(string)); - else if (parseKeywordIf(ctx, "SECOND")) - return DayToSecond.valueOf(Double.parseDouble(string) * 1000.0); - } - catch (NumberFormatException e) { - throw ctx.expected("Unsigned number"); - } + throw ctx.expected("MINUTE", "SECOND"); + else + return requireNotNull(ctx, DayToSecond.hour(string), message); + else if (parseKeywordIf(ctx, "MINUTE")) + if (parseKeywordIf(ctx, "TO") && parseKeyword(ctx, "SECOND")) + return requireNotNull(ctx, DayToSecond.minuteToSecond(string), message); + else + return requireNotNull(ctx, DayToSecond.minute(string), message); + else if (parseKeywordIf(ctx, "SECOND")) + return requireNotNull(ctx, DayToSecond.second(string), message); DayToSecond ds = DayToSecond.valueOf(string); if (ds != null) @@ -8334,7 +8330,14 @@ final class ParserImpl implements Parser { if (ys != null) return ys; - throw ctx.exception("Illegal interval literal"); + throw ctx.exception(message); + } + + private static final T requireNotNull(ParserContext ctx, T value, String message) { + if (value != null) + return value; + else + throw ctx.exception(message); } private static final Field parseFieldDateLiteralIf(ParserContext ctx) { diff --git a/jOOQ/src/main/java/org/jooq/types/DayToSecond.java b/jOOQ/src/main/java/org/jooq/types/DayToSecond.java index 47cf0350fe..e487f2873d 100644 --- a/jOOQ/src/main/java/org/jooq/types/DayToSecond.java +++ b/jOOQ/src/main/java/org/jooq/types/DayToSecond.java @@ -85,7 +85,12 @@ public final class DayToSecond extends Number implements Interval, ComparableINTERVAL DAY TO SECOND + * Parse a string representation of a INTERVAL DAY TO SECOND. * * @param string A string representation of the form * [+|-][days] [hours]:[minutes]:[seconds].[fractional seconds] @@ -181,11 +186,10 @@ public final class DayToSecond extends Number implements Interval, ComparableINTERVAL DAY TO HOUR. + * + * @param string A string representation of the form + * [+|-][days] + * @return The parsed INTERVAL DAY object, or null + * if the string could not be parsed. + */ + public static DayToSecond day(String string) { + try { + return string == null ? null : new DayToSecond(Integer.parseInt(string)); + } + catch (NumberFormatException ignore) { + return null; + } + } + + /** + * Parse a string representation of a INTERVAL DAY TO HOUR. + * + * @param string A string representation of the form + * [+|-][days] [hours] + * @return The parsed INTERVAL DAY TO HOUR object, or + * null if the string could not be parsed. + */ + public static DayToSecond dayToHour(String string) { + if (string != null) { + Matcher matcher = PATTERN_DTH.matcher(string); + + if (matcher.find()) + return YearToSecond.parseDS(matcher, 0); + } + + return null; + } + + /** + * Parse a string representation of a INTERVAL DAY TO MINUTE. + * + * @param string A string representation of the form + * [+|-][days] [hours]:[minutes] + * @return The parsed INTERVAL DAY TO MINUTE object, or + * null if the string could not be parsed. + */ + public static DayToSecond dayToMinute(String string) { + if (string != null) { + Matcher matcher = PATTERN_DTM.matcher(string); + + if (matcher.find()) + return YearToSecond.parseDS(matcher, 0); + } + + return null; + } + + /** + * Parse a string representation of a INTERVAL DAY TO SECOND. + * + * @param string A string representation of the form + * [+|-][days] [hours]:[minutes]:[seconds].[fractional seconds] + * @return The parsed INTERVAL DAY TO MINUTE object, or + * null if the string could not be parsed. + */ + public static DayToSecond dayToSecond(String string) { + if (string != null) { + Matcher matcher = PATTERN_DTS.matcher(string); + + if (matcher.find()) + return YearToSecond.parseDS(matcher, 0); + } + + return null; + } + + /** + * Parse a string representation of a INTERVAL HOUR. + * + * @param string A string representation of the form + * [+|-][hours] + * @return The parsed INTERVAL HOUR object, or + * null if the string could not be parsed. + */ + public static DayToSecond hour(String string) { + try { + return string == null ? null : new DayToSecond(0, Integer.parseInt(string)); + } + catch (NumberFormatException ignore) { + return null; + } + } + + /** + * Parse a string representation of a INTERVAL HOUR TO MINUTE. + * + * @param string A string representation of the form + * [+|-][hours]:[minutes] + * @return The parsed INTERVAL HOUR TO MINUTE object, or + * null if the string could not be parsed. + */ + public static DayToSecond hourToMinute(String string) { + if (string != null) { + Matcher matcher = PATTERN_HTM.matcher(string); + + if (matcher.find()) + return YearToSecond.parseDS(matcher, 0); + } + + return null; + } + + /** + * Parse a string representation of a INTERVAL HOUR TO SECOND. + * + * @param string A string representation of the form + * [+|-][hours]:[minutes]:[seconds].[fractional seconds] + * @return The parsed INTERVAL HOUR TO SECOND object, or + * null if the string could not be parsed. + */ + public static DayToSecond hourToSecond(String string) { + if (string != null) { + Matcher matcher = PATTERN_HTS.matcher(string); + + if (matcher.find()) + return YearToSecond.parseDS(matcher, 0); + } + + return null; + } + + /** + * Parse a string representation of a INTERVAL MINUTE. + * + * @param string A string representation of the form + * [+|-][minutes] + * @return The parsed INTERVAL MINUTE object, or + * null if the string could not be parsed. + */ + public static DayToSecond minute(String string) { + try { + return string == null ? null : new DayToSecond(0, 0, Integer.parseInt(string)); + } + catch (NumberFormatException ignore) { + return null; + } + } + + /** + * Parse a string representation of a INTERVAL MINUTE TO SECOND. + * + * @param string A string representation of the form + * [+|-][[minutes]:[seconds].[fractional seconds] + * @return The parsed INTERVAL MINUTE TO SECOND object, or + * null if the string could not be parsed. + */ + public static DayToSecond minuteToSecond(String string) { + if (string != null) { + Matcher matcher = PATTERN_MTS.matcher(string); + + if (matcher.find()) + return YearToSecond.parseDS(matcher, 0); + } + + return null; + } + + /** + * Parse a string representation of a INTERVAL SECOND. + * + * @param string A string representation of the form + * [+|-][seconds].[fractional seconds] + * @return The parsed INTERVAL SECOND object, or + * null if the string could not be parsed. + */ + public static DayToSecond second(String string) { + try { + return string == null ? null : valueOf(Double.parseDouble(string) * 1000.0); + } + catch (NumberFormatException ignore) { + return null; + } + } + /** * Load a {@link Double} representation of a * INTERVAL DAY TO SECOND by assuming standard 24 hour days and diff --git a/jOOQ/src/main/java/org/jooq/types/YearToMonth.java b/jOOQ/src/main/java/org/jooq/types/YearToMonth.java index bda96cb31a..5ba68a62a2 100644 --- a/jOOQ/src/main/java/org/jooq/types/YearToMonth.java +++ b/jOOQ/src/main/java/org/jooq/types/YearToMonth.java @@ -122,7 +122,7 @@ public final class YearToMonth extends Number implements Interval, ComparableINTERVAL YEAR TO MONTH + * Parse a string representation of a INTERVAL YEAR TO MONTH. * * @param string A string representation of the form * [+|-][years]-[months] @@ -133,9 +133,8 @@ public final class YearToMonth extends Number implements Interval, ComparableINTERVAL YEAR. + * + * @param string A string representation of the form + * [+|-][years] + * @return The parsed YEAR object, or null if the + * string could not be parsed. + */ + public static YearToMonth year(String string) { + try { + return string == null ? null : new YearToMonth(Integer.parseInt(string)); + } + catch (NumberFormatException ignore) { + return null; + } + } + + /** + * Parse a standard SQL string representation of a + * INTERVAL YEAR TO MONTH. + * + * @param string A string representation of the form + * [+|-][years]-[months] + * @return The parsed YEAR TO MONTH object, or + * null if the string could not be parsed. + */ + public static YearToMonth yearToMonth(String string) { + if (string != null) { + Matcher matcher; + + if ((matcher = PATTERN_SQL.matcher(string)).find()) + return YearToSecond.parseYM(matcher, 0); + } + + return null; + } + + /** + * Parse a standard SQL string representation of a + * INTERVAL MONTH. + * + * @param string A string representation of the form + * [+|-][months] + * @return The parsed MONTH object, or null if the + * string could not be parsed. + */ + public static YearToMonth month(String string) { + try { + return string == null ? null : new YearToMonth(0, Integer.parseInt(string)); + } + catch (NumberFormatException ignore) { + return null; + } + } + @Override public final Duration toDuration() {