[jOOQ/jOOQ#9925] Added support for xmlforest

This commit is contained in:
Lukas Eder 2020-03-18 17:15:17 +01:00
parent b4abf5ada9
commit 04c61c0fca
3 changed files with 123 additions and 24 deletions

View File

@ -18238,6 +18238,22 @@ public class DSL {
return new XMLPI(target, content);
}
/**
* The XML forest constructor.
*/
@Support({ POSTGRES })
public static Field<XML> xmlforest(Field<?>... fields) {
return xmlforest(asList(fields));
}
/**
* The XML forest constructor.
*/
@Support({ POSTGRES })
public static Field<XML> xmlforest(Collection<? extends Field<?>> fields) {
return new XMLForest(fields);
}
// -------------------------------------------------------------------------
// XXX JSON functions
// -------------------------------------------------------------------------

View File

@ -295,6 +295,7 @@ import static org.jooq.impl.DSL.xmlattributes;
import static org.jooq.impl.DSL.xmlcomment;
import static org.jooq.impl.DSL.xmlconcat;
import static org.jooq.impl.DSL.xmlelement;
import static org.jooq.impl.DSL.xmlforest;
import static org.jooq.impl.DSL.xmlpi;
import static org.jooq.impl.DSL.year;
import static org.jooq.impl.DSL.zero;
@ -6403,6 +6404,8 @@ final class ParserImpl implements Parser {
return field;
else if ((field = parseFieldXMLPIIf(ctx)) != null)
return field;
else if ((field = parseFieldXMLForestIf(ctx)) != null)
return field;
break;
@ -6730,25 +6733,8 @@ final class ParserImpl implements Parser {
while (parseIf(ctx, ',')) {
if (attr == null && parseKeywordIf(ctx, "XMLATTRIBUTES")) {
List<Field<?>> attrs = new ArrayList<>();
parse(ctx, '(');
do {
Name alias = null;
Field<?> field = null;
if (field == null) {
field = parseField(ctx);
if (parseKeywordIf(ctx, "AS"))
alias = parseIdentifier(ctx, true);
}
attrs.add(alias == null ? field : field.as(alias));
}
while (parseIf(ctx, ','));
List<Field<?>> attrs = parseAliasedXMLContent(ctx);
parse(ctx, ')');
attr = xmlattributes(attrs);
}
@ -6770,12 +6756,7 @@ final class ParserImpl implements Parser {
parse(ctx, '(');
parseKeyword(ctx, "NAME");
Name target = parseIdentifier(ctx);
Field<?> content = null;
if (parseIf(ctx, ',')) {
content = parseField(ctx);
}
Field<?> content = parseIf(ctx, ',') ? parseField(ctx) : null;
parse(ctx, ')');
return content == null ? xmlpi(target) : xmlpi(target, content);
}
@ -6783,6 +6764,33 @@ final class ParserImpl implements Parser {
return null;
}
private static final Field<?> parseFieldXMLForestIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "XMLFOREST")) {
parse(ctx, '(');
List<Field<?>> content = parseAliasedXMLContent(ctx);
parse(ctx, ')');
return xmlforest(content);
}
return null;
}
private static final List<Field<?>> parseAliasedXMLContent(ParserContext ctx) {
List<Field<?>> result = new ArrayList<>();
do {
Field<?> field = parseField(ctx);
if (parseKeywordIf(ctx, "AS"))
field = field.as(parseIdentifier(ctx, true));
result.add(field);
}
while (parseIf(ctx, ','));
return result;
}
private static final Field<?> parseFieldJSONArrayConstructorIf(ParserContext ctx) {
if (parseFunctionNameIf(ctx, "JSON_ARRAY")) {
parse(ctx, '(');

View File

@ -0,0 +1,75 @@
/*
* 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.Names.N_XMLFOREST;
import java.util.Collection;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.XML;
/**
* @author Lukas Eder
*/
final class XMLForest extends AbstractField<XML> {
/**
* Generated UID
*/
private static final long serialVersionUID = 4505809303211506197L;
private final SelectFieldList<Field<?>> args;
XMLForest(Collection<? extends Field<?>> args) {
super(N_XMLFOREST, SQLDataType.XML);
this.args = new SelectFieldList<>(args);
}
@Override
public final void accept(Context<?> ctx) {
boolean declareFields = ctx.declareFields();
ctx.visit(N_XMLFOREST).sql('(')
.declareFields(true)
.visit(args)
.declareFields(declareFields)
.sql(')');
}
}