From cc6ed7f8610394dde5c01c5e0f2668f83546e441 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Mon, 19 Mar 2018 11:19:33 +0100 Subject: [PATCH] [#7325] Error when fetching SQLite timestamps in ISO format (including the letter T) --- .../resources/org/jooq/web/manual-3.11.xml | 12 +++++++++++ jOOQ/src/main/java/org/jooq/ParserCLI.java | 21 +++++++++++++++++-- .../java/org/jooq/impl/DefaultBinding.java | 8 +++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/jOOQ-manual/src/main/resources/org/jooq/web/manual-3.11.xml b/jOOQ-manual/src/main/resources/org/jooq/web/manual-3.11.xml index f2261757e5..83d3596f2a 100644 --- a/jOOQ-manual/src/main/resources/org/jooq/web/manual-3.11.xml +++ b/jOOQ-manual/src/main/resources/org/jooq/web/manual-3.11.xml @@ -9722,6 +9722,18 @@ from ( The Parser API can be used as a translator between source and target dialects programmatically, as we've seen in the . This functionality can also usefully be accessed on the command line as shown below:

+ Specify the output keyword style (org.jooq.conf.RenderKeywordStyle) + -i / --identifier Specify the output identifier style (org.jooq.conf.RenderNameStyle) + -t / --to-dialect Specify the output dialect (org.jooq.SQLDialect) + -s / --sql Specify the input SQL string + +$ java -jar jooq-{jooq-version}.jar org.jooq.ParserCLI -t ORACLE -s "SELECT substring('abcde', 2, 3)" +select substr('abcde', 2, 3) from dual;]]> +

Another way to use this API is the https://www.jooq.org/translate website.

diff --git a/jOOQ/src/main/java/org/jooq/ParserCLI.java b/jOOQ/src/main/java/org/jooq/ParserCLI.java index 26e2a2b7bf..f35f84a3e5 100644 --- a/jOOQ/src/main/java/org/jooq/ParserCLI.java +++ b/jOOQ/src/main/java/org/jooq/ParserCLI.java @@ -41,6 +41,7 @@ import org.jooq.conf.RenderKeywordStyle; import org.jooq.conf.RenderNameStyle; import org.jooq.conf.Settings; import org.jooq.impl.DSL; +import org.jooq.impl.ParserException; /** * A command line interface to the Parser API, which works in a similar way as @@ -76,7 +77,23 @@ public final class ParserCLI { settings.setRenderNameStyle(a.name); DSLContext ctx = DSL.using(a.toDialect, settings); - System.out.println(ctx.render(ctx.parser().parse(a.sql))); + try { + System.out.println(ctx.render(ctx.parser().parse(a.sql))); + } + catch (ParserException e1) { + ParserException e = e1; + + if (!a.sql.trim().matches("^(?is:(ALTER|BEGIN|COMMENT|CREATE|DECLARE|DELETE|DESCRIBE|DROP|GRANT|INSERT|MERGE|RENAME|REVOKE|SELECT|SET|SHOW|TRUNCATE|UPDATE|USE).*)$")) { + try { + System.out.println(ctx.render(ctx.parser().parseField(a.sql))); + } + catch (ParserException e2) { + e = e1.position() >= e2.position() ? e1 : e2; + } + } + + System.err.println(e.getMessage()); + } } private static final Args parse(String[] args) { @@ -170,7 +187,7 @@ public final class ParserCLI { System.out.println(" -s / --sql Specify the input SQL string"); } - static final class Args { + public static final class Args { String sql; RenderKeywordStyle keywords; RenderNameStyle name; diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java b/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java index 48141d0d8c..c346e93aa6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java @@ -417,14 +417,18 @@ public class DefaultBinding implements Binding { // If that fails, try reading a formatted date catch (NumberFormatException e) { + // [#7325] In SQLite dates could be stored in both ISO standard formats: + // With T (default standard), or without T (optional standard, JDBC standard) + date = StringUtils.replace(date, "T", " "); + if (type == Timestamp.class) return Timestamp.valueOf(date).getTime(); // Dates may come with " 00:00:00". This is safely trimming time information - if (type == Date.class) + else if (type == Date.class) return Date.valueOf(date.split(" ")[0]).getTime(); - if (type == Time.class) + else if (type == Time.class) return Time.valueOf(date).getTime(); throw new SQLException("Could not parse date " + date, e);