[jOOQ/jOOQ#8105] Disable foreign key checks and parse SET commands

- [jOOQ/jOOQ#9780] Add Settings.parseSetCommands
- [jOOQ/jOOQ#8105] Interpreter should have an option to disable foreign key checks
This commit is contained in:
Lukas Eder 2020-01-30 17:44:46 +01:00
parent c0d825c03e
commit 311ff6a50b
5 changed files with 156 additions and 2 deletions

View File

@ -229,6 +229,8 @@ public class Settings
@XmlElement(defaultValue = "OFF")
@XmlSchemaType(name = "string")
protected ParseWithMetaLookups parseWithMetaLookups = ParseWithMetaLookups.OFF;
@XmlElement(defaultValue = "false")
protected Boolean parseSetCommands = false;
@XmlElement(defaultValue = "IGNORE")
@XmlSchemaType(name = "string")
protected ParseUnsupportedSyntax parseUnsupportedSyntax = ParseUnsupportedSyntax.IGNORE;
@ -1951,6 +1953,30 @@ public class Settings
this.parseWithMetaLookups = value;
}
/**
* [#9780] Whether commands of the type <code>SET key = value</code> should be parsed rather than ignored.
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean isParseSetCommands() {
return parseSetCommands;
}
/**
* Sets the value of the parseSetCommands property.
*
* @param value
* allowed object is
* {@link Boolean }
*
*/
public void setParseSetCommands(Boolean value) {
this.parseSetCommands = value;
}
/**
* [#5917] Whether the parser should accept unsupported (but known) syntax.
*
@ -2688,6 +2714,11 @@ public class Settings
return this;
}
public Settings withParseSetCommands(Boolean value) {
setParseSetCommands(value);
return this;
}
/**
* [#5917] Whether the parser should accept unsupported (but known) syntax.
*
@ -2852,6 +2883,7 @@ public class Settings
builder.append("parseLocale", parseLocale);
builder.append("parseNameCase", parseNameCase);
builder.append("parseWithMetaLookups", parseWithMetaLookups);
builder.append("parseSetCommands", parseSetCommands);
builder.append("parseUnsupportedSyntax", parseUnsupportedSyntax);
builder.append("parseUnknownFunctions", parseUnknownFunctions);
builder.append("parseIgnoreComments", parseIgnoreComments);
@ -3591,6 +3623,15 @@ public class Settings
return false;
}
}
if (parseSetCommands == null) {
if (other.parseSetCommands!= null) {
return false;
}
} else {
if (!parseSetCommands.equals(other.parseSetCommands)) {
return false;
}
}
if (parseUnsupportedSyntax == null) {
if (other.parseUnsupportedSyntax!= null) {
return false;
@ -3740,6 +3781,7 @@ public class Settings
result = ((prime*result)+((parseLocale == null)? 0 :parseLocale.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()));
result = ((prime*result)+((parseUnsupportedSyntax == null)? 0 :parseUnsupportedSyntax.hashCode()));
result = ((prime*result)+((parseUnknownFunctions == null)? 0 :parseUnknownFunctions.hashCode()));
result = ((prime*result)+((parseIgnoreComments == null)? 0 :parseIgnoreComments.hashCode()));

View File

@ -102,6 +102,7 @@ import org.jooq.conf.InterpreterSearchSchema;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.DataDefinitionException;
import org.jooq.impl.ConstraintImpl.Action;
import org.jooq.tools.Convert;
import org.jooq.tools.JooqLogger;
@SuppressWarnings("serial")
@ -225,6 +226,9 @@ final class Interpreter {
else if (query instanceof Merge)
;
else if (query instanceof SetCommand)
accept0((SetCommand) query);
else
throw unsupportedQuery(query);
}
@ -1003,6 +1007,13 @@ final class Interpreter {
currentSchema = schema;
}
private final void accept0(SetCommand query) {
if ("foreign_key_checks".equals(query.$name().last().toLowerCase(locale)))
delayForeignKeyDeclarations = !Convert.convert(query.$value().getValue(), boolean.class);
else
throw unsupportedQuery(query);
}
// -------------------------------------------------------------------------
// Exceptions
// -------------------------------------------------------------------------

View File

@ -2017,10 +2017,27 @@ final class ParserImpl implements Parser {
return parseSetSchema(ctx);
else if (parseKeywordIf(ctx, "SEARCH_PATH"))
return parseSetSearchPath(ctx);
else
return parseSetCommand(ctx);
}
private static final Query parseSetCommand(ParserContext ctx) {
if (TRUE.equals(ctx.settings().isParseSetCommands())) {
Name name = parseIdentifier(ctx);
// TODO: [#9780] Are there any possible syntaxes and data types?
parseIf(ctx, '=');
Object value = parseSignedIntegerIf(ctx);
// TODO: [#9781] Create public DSL API for this
return new SetCommand(ctx.dsl.configuration(), name, value != null ? inline(value) : inline(parseStringLiteral(ctx)));
}
// There are many SET commands in programs like sqlplus, which we'll simply ignore
parseUntilEOL(ctx);
return IGNORE_NO_DELIMITER;
else {
parseUntilEOL(ctx);
return IGNORE_NO_DELIMITER;
}
}
private static final Query parseSetCatalog(ParserContext ctx) {

View File

@ -0,0 +1,80 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static org.jooq.impl.Keywords.K_SET;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Name;
import org.jooq.Param;
/**
* A <code>SET</code> command.
*
* @author Lukas Eder
*/
final class SetCommand extends AbstractQuery {
/**
* Generated UID
*/
private static final long serialVersionUID = -6018875346107141474L;
private final Name name;
private final Param<?> value;
SetCommand(Configuration configuration, Name name, Param<?> value) {
super(configuration);
this.name = name;
this.value = value;
}
final Name $name() { return name; }
final Param<?> $value() { return value; }
// ------------------------------------------------------------------------
// XXX: QueryPart API
// ------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
ctx.visit(K_SET).sql(' ').visit(name).sql(" = ").visit(value);
}
}

View File

@ -479,6 +479,10 @@ jOOQ queries, for which no specific fetchSize value was specified.]]></jxb:javad
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[[#8616] The search path to be used for unqualified table lookups by the parser.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="parseSetCommands" type="boolean" minOccurs="0" maxOccurs="1" default="false">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[[#9780] Whether commands of the type <code>SET key = value</code> should be parsed rather than ignored.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>
<element name="parseUnsupportedSyntax" type="jooq-runtime:ParseUnsupportedSyntax" minOccurs="0" maxOccurs="1" default="IGNORE">
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[[#5917] Whether the parser should accept unsupported (but known) syntax.]]></jxb:javadoc></jxb:property></appinfo></annotation>
</element>