All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jooq.impl.XMLQuery Maven / Gradle / Ivy

There is a newer version: 3.19.15
Show newest version
/*
 * 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
 *
 *  https://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: https://www.jooq.org/legal/licensing
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package org.jooq.impl;

// ...
import static org.jooq.conf.ParamType.INLINED;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.unnest;
import static org.jooq.impl.DSL.xmlagg;
import static org.jooq.impl.Keywords.K_CONTENT;
import static org.jooq.impl.Keywords.K_EXISTS;
import static org.jooq.impl.Keywords.K_QUERY;
import static org.jooq.impl.Keywords.K_RETURNING;
import static org.jooq.impl.Names.N_XMLQUERY;
import static org.jooq.impl.QOM.XMLPassingMechanism.BY_REF;
import static org.jooq.impl.QOM.XMLPassingMechanism.BY_VALUE;
import static org.jooq.impl.SQLDataType.XML;
import static org.jooq.impl.XMLTable.acceptPassing;
import static org.jooq.impl.XMLTable.acceptXPath;

import java.util.function.BiFunction;
import java.util.function.Predicate;

import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Function1;
import org.jooq.QueryPart;
// ...
// ...
import org.jooq.XML;
import org.jooq.XMLQueryPassingStep;
import org.jooq.impl.QOM.XMLPassingMechanism;

/**
 * @author Lukas Eder
 */
final class XMLQuery extends AbstractField implements XMLQueryPassingStep, QOM.XMLQuery {
    private final Field       xpath;
    private final Field          passing;
    private final XMLPassingMechanism passingMechanism;

    XMLQuery(Field xpath) {
        this(xpath, null, null);
    }

    private XMLQuery(Field xpath, Field passing, XMLPassingMechanism passingMechanism) {
        super(N_XMLQUERY, SQLDataType.XML);

        this.xpath = xpath;
        this.passing = passing;
        this.passingMechanism = passingMechanism;
    }

    // -------------------------------------------------------------------------
    // XXX: DSL API
    // -------------------------------------------------------------------------
    @Override
    public final Field passing(XML xml) {
        return passing(Tools.field(xml));
    }

    @Override
    public final Field passing(Field xml) {
        return new XMLQuery(xpath, xml, null);
    }

    @Override
    public final Field passingByRef(XML xml) {
        return passingByRef(Tools.field(xml));
    }

    @Override
    public final Field passingByRef(Field xml) {
        return new XMLQuery(xpath, xml, BY_REF);
    }















    // -------------------------------------------------------------------------
    // XXX: QueryPart API
    // -------------------------------------------------------------------------

    @Override
    public final void accept(Context ctx) {
        switch (ctx.family()) {
            case POSTGRES:
                Field x = DSL.field(DSL.name("x"), XML);

                ctx.sql('(').visit(
                        select(xmlagg(x))
                        .from(unnest(DSL.field(
                            "xpath({0}, {1})", XML.getArrayDataType(), xpath, passing
                        )).as("t", x.getName())))
                   .sql(')');

                break;







            default:
                ctx.visit(N_XMLQUERY).sqlIndentStart('(');
                acceptXPath(ctx, xpath);
                acceptPassing(ctx, passing, passingMechanism);






                ctx.sqlIndentEnd(')');
                break;
        }
    }

    // -------------------------------------------------------------------------
    // XXX: Query Object Model
    // -------------------------------------------------------------------------

    @Override
    public final Field $xpath() {
        return xpath;
    }

    @Override
    public final Field $passing() {
        return passing;
    }

    @Override
    public final XMLPassingMechanism $passingMechanism() {
        return passingMechanism;
    }














}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy