[jOOQ/jOOQ#10456] Add static YearToMonth and DayToSecond.<intervalType>(String) constructor methods

This commit is contained in:
Lukas Eder 2020-07-29 10:45:02 +02:00
parent 99d8c5df29
commit 1e1d3a7c91
3 changed files with 295 additions and 49 deletions

View File

@ -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> 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) {

View File

@ -85,7 +85,12 @@ public final class DayToSecond extends Number implements Interval, Comparable<Da
* Generated UID
*/
private static final long serialVersionUID = -3853596481984643811L;
private static final Pattern PATTERN = Pattern.compile("^([+-])?(?:(\\d+) )?(\\d+):(\\d+):(\\d+)(?:\\.(\\d+))?$");
private static final Pattern PATTERN_DTS = Pattern.compile("^([+-])?(?:(\\d+) )?(\\d+):(\\d+):(\\d+)(?:\\.(\\d+))?$");
private static final Pattern PATTERN_DTM = Pattern.compile("^([+-])?(?:(\\d+) )?(\\d+):(\\d+)()()$");
private static final Pattern PATTERN_DTH = Pattern.compile("^([+-])?(?:(\\d+) )?(\\d+)()()()$");
private static final Pattern PATTERN_HTS = Pattern.compile("^([+-])?()(\\d+):(\\d+):(\\d+)(?:\\.(\\d+))?$");
private static final Pattern PATTERN_HTM = Pattern.compile("^([+-])?()(\\d+):(\\d+)()()$");
private static final Pattern PATTERN_MTS = Pattern.compile("^([+-])?()()(\\d+):(\\d+)(?:\\.(\\d+))?$");
private final boolean negative;
private final int days;
@ -166,7 +171,7 @@ public final class DayToSecond extends Number implements Interval, Comparable<Da
}
/**
* Parse a string representation of a <code>INTERVAL DAY TO SECOND</code>
* Parse a string representation of a <code>INTERVAL DAY TO SECOND</code>.
*
* @param string A string representation of the form
* <code>[+|-][days] [hours]:[minutes]:[seconds].[fractional seconds]</code>
@ -181,11 +186,10 @@ public final class DayToSecond extends Number implements Interval, Comparable<Da
return valueOf(Double.valueOf(string));
}
catch (NumberFormatException e) {
Matcher matcher = PATTERN.matcher(string);
DayToSecond result = dayToSecond(string);
if (matcher.find()) {
return YearToSecond.parseDS(matcher, 0);
}
if (result != null)
return result;
else {
@ -201,6 +205,188 @@ public final class DayToSecond extends Number implements Interval, Comparable<Da
return null;
}
/**
* Parse a string representation of a <code>INTERVAL DAY TO HOUR</code>.
*
* @param string A string representation of the form
* <code>[+|-][days]</code>
* @return The parsed <code>INTERVAL DAY</code> object, or <code>null</code>
* 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 <code>INTERVAL DAY TO HOUR</code>.
*
* @param string A string representation of the form
* <code>[+|-][days] [hours]</code>
* @return The parsed <code>INTERVAL DAY TO HOUR</code> object, or
* <code>null</code> 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 <code>INTERVAL DAY TO MINUTE</code>.
*
* @param string A string representation of the form
* <code>[+|-][days] [hours]:[minutes]</code>
* @return The parsed <code>INTERVAL DAY TO MINUTE</code> object, or
* <code>null</code> 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 <code>INTERVAL DAY TO SECOND</code>.
*
* @param string A string representation of the form
* <code>[+|-][days] [hours]:[minutes]:[seconds].[fractional seconds]</code>
* @return The parsed <code>INTERVAL DAY TO MINUTE</code> object, or
* <code>null</code> 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 <code>INTERVAL HOUR</code>.
*
* @param string A string representation of the form
* <code>[+|-][hours]</code>
* @return The parsed <code>INTERVAL HOUR</code> object, or
* <code>null</code> 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 <code>INTERVAL HOUR TO MINUTE</code>.
*
* @param string A string representation of the form
* <code>[+|-][hours]:[minutes]</code>
* @return The parsed <code>INTERVAL HOUR TO MINUTE</code> object, or
* <code>null</code> 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 <code>INTERVAL HOUR TO SECOND</code>.
*
* @param string A string representation of the form
* <code>[+|-][hours]:[minutes]:[seconds].[fractional seconds]</code>
* @return The parsed <code>INTERVAL HOUR TO SECOND</code> object, or
* <code>null</code> 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 <code>INTERVAL MINUTE</code>.
*
* @param string A string representation of the form
* <code>[+|-][minutes]</code>
* @return The parsed <code>INTERVAL MINUTE</code> object, or
* <code>null</code> 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 <code>INTERVAL MINUTE TO SECOND</code>.
*
* @param string A string representation of the form
* <code>[+|-][[minutes]:[seconds].[fractional seconds]</code>
* @return The parsed <code>INTERVAL MINUTE TO SECOND</code> object, or
* <code>null</code> 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 <code>INTERVAL SECOND</code>.
*
* @param string A string representation of the form
* <code>[+|-][seconds].[fractional seconds]</code>
* @return The parsed <code>INTERVAL SECOND</code> object, or
* <code>null</code> 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
* <code>INTERVAL DAY TO SECOND</code> by assuming standard 24 hour days and

View File

@ -122,7 +122,7 @@ public final class YearToMonth extends Number implements Interval, Comparable<Ye
}
/**
* Parse a string representation of a <code>INTERVAL YEAR TO MONTH</code>
* Parse a string representation of a <code>INTERVAL YEAR TO MONTH</code>.
*
* @param string A string representation of the form
* <code>[+|-][years]-[months]</code>
@ -133,9 +133,8 @@ public final class YearToMonth extends Number implements Interval, Comparable<Ye
if (string != null) {
Matcher matcher;
if ((matcher = PATTERN_SQL.matcher(string)).find()) {
if ((matcher = PATTERN_SQL.matcher(string)).find())
return YearToSecond.parseYM(matcher, 0);
}
if ((matcher = PATTERN_ISO.matcher(string)).find()) {
boolean negative = "-".equals(matcher.group(1));
@ -148,11 +147,69 @@ public final class YearToMonth extends Number implements Interval, Comparable<Ye
return new YearToMonth(years, months, negative);
}
return yearToMonth(string);
}
return null;
}
/**
* Parse a standard SQL string representation of a
* <code>INTERVAL YEAR</code>.
*
* @param string A string representation of the form
* <code>[+|-][years]</code>
* @return The parsed <code>YEAR</code> object, or <code>null</code> 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
* <code>INTERVAL YEAR TO MONTH</code>.
*
* @param string A string representation of the form
* <code>[+|-][years]-[months]</code>
* @return The parsed <code>YEAR TO MONTH</code> object, or
* <code>null</code> 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
* <code>INTERVAL MONTH</code>.
*
* @param string A string representation of the form
* <code>[+|-][months]</code>
* @return The parsed <code>MONTH</code> object, or <code>null</code> 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() {