[jOOQ/jOOQ#9925] Add support for XMLPARSE()

This commit is contained in:
Lukas Eder 2020-03-20 12:24:56 +01:00
parent dd54a05d17
commit bd53d19855
5 changed files with 138 additions and 0 deletions

View File

@ -394,6 +394,7 @@ import org.jooq.XMLAttributes;
import org.jooq.XMLExistsPassingStep;
import org.jooq.conf.Settings;
import org.jooq.exception.SQLDialectNotSupportedException;
import org.jooq.impl.XMLParse.DocumentOrContent;
import org.jooq.tools.Convert;
import org.jooq.tools.StringUtils;
import org.jooq.tools.jdbc.JDBCUtils;
@ -18094,6 +18095,38 @@ public class DSL {
// XXX XML functions
// -------------------------------------------------------------------------
/**
* The XML parse function.
*/
@Support({ POSTGRES })
public static Field<XML> xmlparseDocument(String content) {
return xmlparseDocument(Tools.field(content));
}
/**
* The XML parse function.
*/
@Support({ POSTGRES })
public static Field<XML> xmlparseDocument(Field<String> content) {
return new XMLParse(content, DocumentOrContent.DOCUMENT);
}
/**
* The XML parse function.
*/
@Support({ POSTGRES })
public static Field<XML> xmlparseContent(String content) {
return xmlparseContent(Tools.field(content));
}
/**
* The XML parse function.
*/
@Support({ POSTGRES })
public static Field<XML> xmlparseContent(Field<String> content) {
return new XMLParse(content, DocumentOrContent.CONTENT);
}
/**
* The XML comment constructor.
*/

View File

@ -98,6 +98,7 @@ final class Keywords {
static final Keyword K_CONSTRAINT = keyword("constraint");
static final Keyword K_CONSTRAINTS = keyword("constraints");
static final Keyword K_CONTAINED = keyword("contained");
static final Keyword K_CONTENT = keyword("content");
static final Keyword K_CONTINUE = keyword("continue");
static final Keyword K_CONTINUE_IDENTITY = keyword("continue identity");
static final Keyword K_CREATE = keyword("create");
@ -129,6 +130,7 @@ final class Keywords {
static final Keyword K_DO = keyword("do");
static final Keyword K_DO_NOTHING = keyword("do nothing");
static final Keyword K_DO_UPDATE = keyword("do update");
static final Keyword K_DOCUMENT = keyword("document");
static final Keyword K_DROP = keyword("drop");
static final Keyword K_DROP_COLUMN = keyword("drop column");
static final Keyword K_DROP_CONSTRAINT = keyword("drop constraint");

View File

@ -160,6 +160,7 @@ final class Names {
static final Name N_XMLCONCAT = DSL.unquotedName("xmlconcat");
static final Name N_XMLELEMENT = DSL.unquotedName("xmlelement");
static final Name N_XMLFOREST = DSL.unquotedName("xmlforest");
static final Name N_XMLPARSE = DSL.unquotedName("xmlparse");
static final Name N_XMLPI = DSL.unquotedName("xmlpi");
static final Name N_XMLROOT = DSL.unquotedName("xmlroot");
static final Name N_XPATH = DSL.unquotedName("xpath");

View File

@ -298,6 +298,8 @@ import static org.jooq.impl.DSL.xmlconcat;
import static org.jooq.impl.DSL.xmlelement;
import static org.jooq.impl.DSL.xmlexists;
import static org.jooq.impl.DSL.xmlforest;
import static org.jooq.impl.DSL.xmlparseContent;
import static org.jooq.impl.DSL.xmlparseDocument;
import static org.jooq.impl.DSL.xmlpi;
import static org.jooq.impl.DSL.year;
import static org.jooq.impl.DSL.zero;
@ -522,6 +524,7 @@ import org.jooq.conf.RenderQuotedNames;
import org.jooq.conf.Settings;
import org.jooq.conf.SettingsTools;
import org.jooq.impl.XMLExists.PassingMechanism;
import org.jooq.impl.XMLParse.DocumentOrContent;
import org.jooq.tools.StringUtils;
import org.jooq.tools.reflect.Reflect;
import org.jooq.types.DayToSecond;
@ -6439,6 +6442,8 @@ final class ParserImpl implements Parser {
return field;
else if ((field = parseFieldXMLForestIf(ctx)) != null)
return field;
else if ((field = parseFieldXMLParseIf(ctx)) != null)
return field;
break;
@ -6809,6 +6814,29 @@ final class ParserImpl implements Parser {
return null;
}
private static final Field<?> parseFieldXMLParseIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "XMLPARSE")) {
parse(ctx, '(');
DocumentOrContent documentOrContent;
if (parseKeywordIf(ctx, "DOCUMENT"))
documentOrContent = DocumentOrContent.DOCUMENT;
else if (parseKeywordIf(ctx, "CONTENT"))
documentOrContent = DocumentOrContent.CONTENT;
else
throw ctx.expected("CONTENT", "DOCUMENT");
Field<String> xml = (Field<String>) parseField(ctx);
parse(ctx, ')');
return documentOrContent == DocumentOrContent.DOCUMENT
? xmlparseDocument(xml)
: xmlparseContent(xml);
}
return null;
}
private static final List<Field<?>> parseAliasedXMLContent(ParserContext ctx) {
List<Field<?>> result = new ArrayList<>();

View File

@ -0,0 +1,74 @@
/*
* 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_CONTENT;
import static org.jooq.impl.Keywords.K_DOCUMENT;
import static org.jooq.impl.Names.N_XMLPARSE;
import static org.jooq.impl.XMLParse.DocumentOrContent.DOCUMENT;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.XML;
/**
* @author Lukas Eder
*/
final class XMLParse extends AbstractField<XML> {
/**
* Generated UID
*/
private static final long serialVersionUID = 4505809303211506197L;
private final Field<String> content;
private final DocumentOrContent documentOrContent;
XMLParse(Field<String> content, DocumentOrContent documentOrContent) {
super(N_XMLPARSE, SQLDataType.XML);
this.content = content;
this.documentOrContent = documentOrContent;
}
@Override
public final void accept(Context<?> ctx) {
ctx.visit(N_XMLPARSE).sql('(').visit(documentOrContent == DOCUMENT ? K_DOCUMENT : K_CONTENT).sql(' ').visit(content).sql(')');
}
enum DocumentOrContent { DOCUMENT, CONTENT }
}