From 7b7cd0e79f80d419ebd22d0482d553fa5b02514b Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 22 Mar 2021 17:55:13 +0100 Subject: [PATCH] [jOOQ/jOOQ#11694] Parse TO_DATE(string) and TO_TIMESTAMP(string) --- .../src/main/java/org/jooq/conf/Settings.java | 76 +++++++++++++++++++ .../main/java/org/jooq/impl/ParserImpl.java | 6 +- .../resources/xsd/jooq-runtime-3.15.0.xsd | 8 ++ 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/conf/Settings.java b/jOOQ/src/main/java/org/jooq/conf/Settings.java index 0de0de62cb..c159c5263e 100644 --- a/jOOQ/src/main/java/org/jooq/conf/Settings.java +++ b/jOOQ/src/main/java/org/jooq/conf/Settings.java @@ -261,6 +261,10 @@ public class Settings @XmlElement(type = String.class) @XmlJavaTypeAdapter(LocaleAdapter.class) protected Locale parseLocale; + @XmlElement(defaultValue = "YYYY-MM-DD") + protected String parseDateFormat = "YYYY-MM-DD"; + @XmlElement(defaultValue = "YYYY-MM-DD HH24:MI:SS.FF") + protected String parseTimestampFormat = "YYYY-MM-DD HH24:MI:SS.FF"; @XmlElement(defaultValue = "DEFAULT") @XmlSchemaType(name = "string") protected ParseNameCase parseNameCase = ParseNameCase.DEFAULT; @@ -2353,6 +2357,38 @@ public class Settings this.parseLocale = value; } + /** + * The date format to use when parsing functions whose behaviour depends on some session date format, such as NLS_DATE_FORMAT in Oracle + * + */ + public String getParseDateFormat() { + return parseDateFormat; + } + + /** + * The date format to use when parsing functions whose behaviour depends on some session date format, such as NLS_DATE_FORMAT in Oracle + * + */ + public void setParseDateFormat(String value) { + this.parseDateFormat = value; + } + + /** + * The timestamp format to use when parsing functions whose behaviour depends on some session date format, such as NLS_TIMESTAMP_FORMAT in Oracle + * + */ + public String getParseTimestampFormat() { + return parseTimestampFormat; + } + + /** + * The timestamp format to use when parsing functions whose behaviour depends on some session date format, such as NLS_TIMESTAMP_FORMAT in Oracle + * + */ + public void setParseTimestampFormat(String value) { + this.parseTimestampFormat = value; + } + /** * [#7337] The default name case for parsed identifiers. * @@ -3301,6 +3337,24 @@ public class Settings return this; } + /** + * The date format to use when parsing functions whose behaviour depends on some session date format, such as NLS_DATE_FORMAT in Oracle + * + */ + public Settings withParseDateFormat(String value) { + setParseDateFormat(value); + return this; + } + + /** + * The timestamp format to use when parsing functions whose behaviour depends on some session date format, such as NLS_TIMESTAMP_FORMAT in Oracle + * + */ + public Settings withParseTimestampFormat(String value) { + setParseTimestampFormat(value); + return this; + } + /** * [#7337] The default name case for parsed identifiers. * @@ -3534,6 +3588,8 @@ public class Settings builder.append("locale", locale); builder.append("parseDialect", parseDialect); builder.append("parseLocale", parseLocale); + builder.append("parseDateFormat", parseDateFormat); + builder.append("parseTimestampFormat", parseTimestampFormat); builder.append("parseNameCase", parseNameCase); builder.append("parseWithMetaLookups", parseWithMetaLookups); builder.append("parseSetCommands", parseSetCommands); @@ -4414,6 +4470,24 @@ public class Settings return false; } } + if (parseDateFormat == null) { + if (other.parseDateFormat!= null) { + return false; + } + } else { + if (!parseDateFormat.equals(other.parseDateFormat)) { + return false; + } + } + if (parseTimestampFormat == null) { + if (other.parseTimestampFormat!= null) { + return false; + } + } else { + if (!parseTimestampFormat.equals(other.parseTimestampFormat)) { + return false; + } + } if (parseNameCase == null) { if (other.parseNameCase!= null) { return false; @@ -4632,6 +4706,8 @@ public class Settings result = ((prime*result)+((locale == null)? 0 :locale.hashCode())); result = ((prime*result)+((parseDialect == null)? 0 :parseDialect.hashCode())); result = ((prime*result)+((parseLocale == null)? 0 :parseLocale.hashCode())); + result = ((prime*result)+((parseDateFormat == null)? 0 :parseDateFormat.hashCode())); + result = ((prime*result)+((parseTimestampFormat == null)? 0 :parseTimestampFormat.hashCode())); result = ((prime*result)+((parseNameCase == null)? 0 :parseNameCase.hashCode())); result = ((prime*result)+((parseWithMetaLookups == null)? 0 :parseWithMetaLookups.hashCode())); result = ((prime*result)+((parseSetCommands == null)? 0 :parseSetCommands.hashCode())); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index c7eeeceb91..b7b07f1cf0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -9738,8 +9738,7 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { if (parseFunctionNameIf("TO_DATE")) { parse('('); Field f1 = (Field) parseField(S); - parse(','); - Field f2 = (Field) parseField(S); + Field f2 = parseIf(',') ? (Field) parseField(S) : inline(settings().getParseDateFormat()); parse(')'); return toDate(f1, f2); @@ -9752,8 +9751,7 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { if (parseFunctionNameIf("TO_TIMESTAMP")) { parse('('); Field f1 = (Field) parseField(S); - parse(','); - Field f2 = (Field) parseField(S); + Field f2 = parseIf(',') ? (Field) parseField(S) : inline(settings().getParseTimestampFormat()); parse(')'); return toTimestamp(f1, f2); diff --git a/jOOQ/src/main/resources/xsd/jooq-runtime-3.15.0.xsd b/jOOQ/src/main/resources/xsd/jooq-runtime-3.15.0.xsd index 61f5bead80..a2084883c2 100644 --- a/jOOQ/src/main/resources/xsd/jooq-runtime-3.15.0.xsd +++ b/jOOQ/src/main/resources/xsd/jooq-runtime-3.15.0.xsd @@ -560,6 +560,14 @@ jOOQ queries, for which no specific fetchSize value was specified.]]> + + + + + + + +