From 312f82c30cb5c18cf87ec091656fdd7024c10d24 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 16 Jun 2023 13:50:56 +0200 Subject: [PATCH] [jOOQ/jOOQ#15239] Add Settings.parseMetaViewSource to allow for opting out of parsing view source in MetaImpl --- .../src/main/java/org/jooq/conf/Settings.java | 42 +++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DDL.java | 41 ++++++++++-------- .../org/jooq/xsd/jooq-runtime-3.19.0.xsd | 4 ++ 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/conf/Settings.java b/jOOQ/src/main/java/org/jooq/conf/Settings.java index a8435a8bdd..9c634d9b3d 100644 --- a/jOOQ/src/main/java/org/jooq/conf/Settings.java +++ b/jOOQ/src/main/java/org/jooq/conf/Settings.java @@ -511,6 +511,8 @@ public class Settings protected Boolean parseRetainCommentsBetweenQueries = false; @XmlElement(defaultValue = "true") protected Boolean parseMetaDefaultExpressions = true; + @XmlElement(defaultValue = "true") + protected Boolean parseMetaViewSources = true; @XmlElement(defaultValue = "IGNORE") @XmlSchemaType(name = "string") protected WriteIfReadonly readonlyTableRecordInsert = WriteIfReadonly.IGNORE; @@ -5838,6 +5840,30 @@ public class Settings this.parseMetaDefaultExpressions = value; } + /** + * [#8469] Whether to parse view sources retrieved from {@link java.sql.DatabaseMetaData}. + * + * @return + * possible object is + * {@link Boolean } + * + */ + public Boolean isParseMetaViewSources() { + return parseMetaViewSources; + } + + /** + * Sets the value of the parseMetaViewSources property. + * + * @param value + * allowed object is + * {@link Boolean } + * + */ + public void setParseMetaViewSources(Boolean value) { + this.parseMetaViewSources = value; + } + /** * [#9864] The behaviour when trying to insert into readonly columns using {@link org.jooq.TableRecord#insert()}. * @@ -7447,6 +7473,11 @@ public class Settings return this; } + public Settings withParseMetaViewSources(Boolean value) { + setParseMetaViewSources(value); + return this; + } + /** * [#9864] The behaviour when trying to insert into readonly columns using {@link org.jooq.TableRecord#insert()}. * @@ -7765,6 +7796,7 @@ public class Settings builder.append("parseIgnoreCommentStop", parseIgnoreCommentStop); builder.append("parseRetainCommentsBetweenQueries", parseRetainCommentsBetweenQueries); builder.append("parseMetaDefaultExpressions", parseMetaDefaultExpressions); + builder.append("parseMetaViewSources", parseMetaViewSources); builder.append("readonlyTableRecordInsert", readonlyTableRecordInsert); builder.append("readonlyUpdatableRecordUpdate", readonlyUpdatableRecordUpdate); builder.append("readonlyInsert", readonlyInsert); @@ -9702,6 +9734,15 @@ public class Settings return false; } } + if (parseMetaViewSources == null) { + if (other.parseMetaViewSources!= null) { + return false; + } + } else { + if (!parseMetaViewSources.equals(other.parseMetaViewSources)) { + return false; + } + } if (readonlyTableRecordInsert == null) { if (other.readonlyTableRecordInsert!= null) { return false; @@ -9993,6 +10034,7 @@ public class Settings result = ((prime*result)+((parseIgnoreCommentStop == null)? 0 :parseIgnoreCommentStop.hashCode())); result = ((prime*result)+((parseRetainCommentsBetweenQueries == null)? 0 :parseRetainCommentsBetweenQueries.hashCode())); result = ((prime*result)+((parseMetaDefaultExpressions == null)? 0 :parseMetaDefaultExpressions.hashCode())); + result = ((prime*result)+((parseMetaViewSources == null)? 0 :parseMetaViewSources.hashCode())); result = ((prime*result)+((readonlyTableRecordInsert == null)? 0 :readonlyTableRecordInsert.hashCode())); result = ((prime*result)+((readonlyUpdatableRecordUpdate == null)? 0 :readonlyUpdatableRecordUpdate.hashCode())); result = ((prime*result)+((readonlyInsert == null)? 0 :readonlyInsert.hashCode())); diff --git a/jOOQ/src/main/java/org/jooq/impl/DDL.java b/jOOQ/src/main/java/org/jooq/impl/DDL.java index c623fd29d2..f753530cb3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDL.java @@ -37,6 +37,7 @@ */ package org.jooq.impl; +import static java.lang.Boolean.FALSE; import static java.util.Arrays.asList; import static org.jooq.DDLFlag.CHECK; import static org.jooq.DDLFlag.COMMENT; @@ -58,7 +59,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; -import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jooq.Check; @@ -94,7 +94,6 @@ import org.jooq.TableOptions.TableType; import org.jooq.UniqueKey; import org.jooq.tools.JooqLogger; import org.jooq.tools.StringUtils; -import org.jooq.tools.json.ParseException; /** * @author Lukas Eder @@ -172,29 +171,37 @@ final class DDL { return q.as(""); try { - Query[] queries = ctx.parser().parse(options.source()).queries(); + if (!FALSE.equals(ctx.settings().isParseMetaViewSources())) { + Query[] queries = ctx.parser().parse(options.source()).queries(); - if (queries.length > 0) { - if (queries[0] instanceof CreateViewImpl cv) - return q.as(cv.$select()); - else if (queries[0] instanceof Select s) - return q.as(s); + if (queries.length > 0) { + if (queries[0] instanceof CreateViewImpl cv) + return q.as(cv.$select()); + else if (queries[0] instanceof Select s) + return q.as(s); + else + return q.as(""); + } else return q.as(""); } - else - return q.as(""); } catch (ParserException e) { log.info("Cannot parse view source: " + options.source(), e); - - // [#15238] If the command prefix is supplied, use a heursitic where the view - // body is expected to start after the first "AS" keyword - if (options.source().toLowerCase().startsWith("create")) - return q.as(P_CREATE_VIEW.matcher(options.source()).replaceFirst("$1")); - else - return q.as(options.source()); } + + return applyAsPlainSQL(q, options); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private final Query applyAsPlainSQL(CreateViewAsStep q, TableOptions options) { + + // [#15238] If the command prefix is supplied, use a heursitic where the view + // body is expected to start after the first "AS" keyword + if (options.source().toLowerCase().startsWith("create")) + return q.as(P_CREATE_VIEW.matcher(options.source()).replaceFirst("$1")); + else + return q.as(options.source()); } final Query createSequence(Sequence sequence) { diff --git a/jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.19.0.xsd b/jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.19.0.xsd index e0e6c36944..e9df5b7fe6 100644 --- a/jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.19.0.xsd +++ b/jOOQ/src/main/resources/org/jooq/xsd/jooq-runtime-3.19.0.xsd @@ -1581,6 +1581,10 @@ inside of queries (including procedural statements) are still not supported.]]>< + + + +