[jOOQ/jOOQ#12046] Add support for XMLSERIALIZE

This also fixes [jOOQ/jOOQ#12030] MULTISET predicates for Db2
This commit is contained in:
Lukas Eder 2021-06-23 13:09:41 +02:00
parent 339ce915c5
commit 55ae3dcd31
5 changed files with 217 additions and 3 deletions

View File

@ -19320,6 +19320,46 @@ public class DSL {
return new Xmlforest(fields);
}
/**
* The <code>XMLSERIALIZE_DOCUMENT</code> function.
*
* @param value is wrapped as {@link #val(Object)}.
*/
@NotNull
@Support({ POSTGRES })
public static <T> Field<T> xmlserializeDocument(XML value, DataType<T> type) {
return new Xmlserialize(false, Tools.field(value), type);
}
/**
* The <code>XMLSERIALIZE_DOCUMENT</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static <T> Field<T> xmlserializeDocument(Field<XML> value, DataType<T> type) {
return new Xmlserialize(false, value, type);
}
/**
* The <code>XMLSERIALIZE_CONTENT</code> function.
*
* @param value is wrapped as {@link #val(Object)}.
*/
@NotNull
@Support({ POSTGRES })
public static <T> Field<T> xmlserializeContent(XML value, DataType<T> type) {
return new Xmlserialize(true, Tools.field(value), type);
}
/**
* The <code>XMLSERIALIZE_CONTENT</code> function.
*/
@NotNull
@Support({ POSTGRES })
public static <T> Field<T> xmlserializeContent(Field<XML> value, DataType<T> type) {
return new Xmlserialize(true, value, type);
}
// -------------------------------------------------------------------------
// JSON functions
// -------------------------------------------------------------------------

View File

@ -39,6 +39,7 @@ package org.jooq.impl;
import static java.lang.Boolean.TRUE;
// ...
// ...
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.impl.DSL.jsonArray;
import static org.jooq.impl.DSL.jsonArrayAgg;
@ -51,6 +52,7 @@ import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.selectFrom;
import static org.jooq.impl.DSL.xmlagg;
import static org.jooq.impl.DSL.xmlelement;
import static org.jooq.impl.DSL.xmlserializeContent;
import static org.jooq.impl.Keywords.K_MULTISET;
import static org.jooq.impl.Names.N_MULTISET;
import static org.jooq.impl.Names.N_RECORD;
@ -223,7 +225,7 @@ final class Multiset<R extends Record> extends AbstractField<Result<R>> {
Select<Record1<XML>> s = select(xmlelement(N_RESULT, filter)).from(t);
if (multisetCondition && NO_SUPPORT_XML_COMPARE.contains(ctx.dialect()))
ctx.visit(DSL.field(s).cast(VARCHAR));
ctx.visit(xmlserializeContent(DSL.field(s), VARCHAR));
else
visitSubquery(ctx, s, true);

View File

@ -44,6 +44,7 @@ import static org.jooq.impl.DSL.jsonObject;
import static org.jooq.impl.DSL.jsonbArrayAgg;
import static org.jooq.impl.DSL.xmlagg;
import static org.jooq.impl.DSL.xmlelement;
import static org.jooq.impl.DSL.xmlserializeContent;
import static org.jooq.impl.Multiset.NO_SUPPORT_JSON_COMPARE;
import static org.jooq.impl.Multiset.NO_SUPPORT_XML_COMPARE;
import static org.jooq.impl.Multiset.jsonArrayaggEmulation;
@ -140,14 +141,14 @@ final class MultisetAgg<R extends Record> extends DefaultAggregateFunction<Resul
case XML: {
XMLAggOrderByStep<XML> order = xmlaggEmulation(row, multisetCondition);
Field<?> f = xmlelement(N_RESULT,
Field<XML> f = xmlelement(N_RESULT,
multisetCondition
? fo((AbstractAggregateFunction<?>) order.orderBy(row.fields()))
: ofo((AbstractAggregateFunction<?>) order)
);
if (multisetCondition && NO_SUPPORT_XML_COMPARE.contains(ctx.dialect()))
ctx.visit(f.cast(VARCHAR));
ctx.visit(xmlserializeContent(f, VARCHAR));
else
ctx.visit(f);

View File

@ -7827,6 +7827,8 @@ final class DefaultParseContext extends AbstractScope implements ParseContext {
return field;
else if ((field = parseFieldXMLQueryIf()) != null)
return field;
else if ((field = parseFieldXMLSerializeIf()) != null)
return field;
break;
@ -8216,6 +8218,24 @@ final class DefaultParseContext extends AbstractScope implements ParseContext {
return null;
}
private final Field<?> parseFieldXMLSerializeIf() {
if (parseFunctionNameIf("XMLSERIALIZE")) {
parse('(');
boolean content = parseKeywordIf("CONTENT");
if (!content)
parseKeywordIf("DOCUMENT");
Field<XML> value = (Field<XML>) parseField();
parseKeyword("AS");
DataType<?> type = parseCastDataType();
parse(')');
return content ? xmlserializeContent(value, type) : xmlserializeDocument(value, type);
}
return null;
}
private final Field<?> parseFieldXMLConcatIf() {
if (parseFunctionNameIf("XMLCONCAT")) {
parse('(');

View File

@ -0,0 +1,151 @@
/*
* 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.DSL.*;
import static org.jooq.impl.Internal.*;
import static org.jooq.impl.Keywords.*;
import static org.jooq.impl.Names.*;
import static org.jooq.impl.SQLDataType.*;
import static org.jooq.impl.Tools.*;
import static org.jooq.impl.Tools.BooleanDataKey.*;
import static org.jooq.impl.Tools.DataExtendedKey.*;
import static org.jooq.impl.Tools.DataKey.*;
import static org.jooq.SQLDialect.*;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.conf.*;
import org.jooq.impl.*;
import org.jooq.tools.*;
import java.util.*;
/**
* The <code>XMLSERIALIZE DOCUMENT</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class Xmlserialize<T>
extends
AbstractField<T>
{
private final boolean content;
private final Field<XML> value;
private final DataType<T> type;
Xmlserialize(
boolean content,
Field<XML> value,
DataType<T> type
) {
super(
N_XMLSERIALIZE,
type
);
this.content = content;
this.value = value;
this.type = type;
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
ctx.visit(N_XMLSERIALIZE).sql('(');
if (content)
ctx.visit(K_CONTENT).sql(' ');
else
ctx.visit(K_DOCUMENT).sql(' ');
ctx.visit(value).sql(' ').visit(K_AS).sql(' ')
.sql(type.getCastTypeName(ctx.configuration()))
.sql(')');
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof Xmlserialize) {
return
StringUtils.equals(content, ((Xmlserialize) that).content) &&
StringUtils.equals(value, ((Xmlserialize) that).value) &&
StringUtils.equals(type, ((Xmlserialize) that).type)
;
}
else
return super.equals(that);
}
}