[jOOQ/jOOQ#15239] Add Settings.parseMetaViewSource to allow for opting out of parsing view source in MetaImpl

This commit is contained in:
Lukas Eder 2023-06-16 13:50:56 +02:00
parent 694c266de9
commit 312f82c30c
3 changed files with 70 additions and 17 deletions

View File

@ -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()));

View File

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

View File

@ -1581,6 +1581,10 @@ inside of queries (including procedural statements) are still not supported.]]><
<element name="parseMetaDefaultExpressions" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[[#8469] Whether to parse default expressions retrieved from {@link java.sql.DatabaseMetaData}.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="parseMetaViewSources" type="boolean" minOccurs="0" maxOccurs="1" default="true">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[[#8469] Whether to parse view sources retrieved from {@link java.sql.DatabaseMetaData}.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="readonlyTableRecordInsert" type="jooq-runtime:WriteIfReadonly" minOccurs="0" maxOccurs="1" default="IGNORE">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[[#9864] The behaviour when trying to insert into readonly columns using {@link org.jooq.TableRecord#insert()}.]]></jxb:javadoc></jxb:property></appinfo></annotation>