diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index a65f9a03f4..0bcdb86174 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -19320,6 +19320,46 @@ public class DSL { return new Xmlforest(fields); } + /** + * The XMLSERIALIZE_DOCUMENT function. + * + * @param value is wrapped as {@link #val(Object)}. + */ + @NotNull + @Support({ POSTGRES }) + public static Field xmlserializeDocument(XML value, DataType type) { + return new Xmlserialize(false, Tools.field(value), type); + } + + /** + * The XMLSERIALIZE_DOCUMENT function. + */ + @NotNull + @Support({ POSTGRES }) + public static Field xmlserializeDocument(Field value, DataType type) { + return new Xmlserialize(false, value, type); + } + + /** + * The XMLSERIALIZE_CONTENT function. + * + * @param value is wrapped as {@link #val(Object)}. + */ + @NotNull + @Support({ POSTGRES }) + public static Field xmlserializeContent(XML value, DataType type) { + return new Xmlserialize(true, Tools.field(value), type); + } + + /** + * The XMLSERIALIZE_CONTENT function. + */ + @NotNull + @Support({ POSTGRES }) + public static Field xmlserializeContent(Field value, DataType type) { + return new Xmlserialize(true, value, type); + } + // ------------------------------------------------------------------------- // JSON functions // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/Multiset.java b/jOOQ/src/main/java/org/jooq/impl/Multiset.java index 81f136a56c..08e6b09c08 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Multiset.java +++ b/jOOQ/src/main/java/org/jooq/impl/Multiset.java @@ -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 extends AbstractField> { Select> 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); diff --git a/jOOQ/src/main/java/org/jooq/impl/MultisetAgg.java b/jOOQ/src/main/java/org/jooq/impl/MultisetAgg.java index cdeda43206..be549cf5ca 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MultisetAgg.java +++ b/jOOQ/src/main/java/org/jooq/impl/MultisetAgg.java @@ -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 extends DefaultAggregateFunction order = xmlaggEmulation(row, multisetCondition); - Field f = xmlelement(N_RESULT, + Field 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); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index d244b94180..f57aa493fa 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -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 value = (Field) 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('('); diff --git a/jOOQ/src/main/java/org/jooq/impl/Xmlserialize.java b/jOOQ/src/main/java/org/jooq/impl/Xmlserialize.java new file mode 100644 index 0000000000..1df3a42b9a --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Xmlserialize.java @@ -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 XMLSERIALIZE DOCUMENT statement. + */ +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) +final class Xmlserialize +extends + AbstractField +{ + + private final boolean content; + private final Field value; + private final DataType type; + + Xmlserialize( + boolean content, + Field value, + DataType 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); + } +}