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

org.jooq.impl.WithOrdinalityTable 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.SQLDialect.DERBY;
// ...
import static org.jooq.SQLDialect.FIREBIRD;
import static org.jooq.SQLDialect.H2;
// ...
import static org.jooq.SQLDialect.HSQLDB;
// ...
import static org.jooq.SQLDialect.MARIADB;
// ...
import static org.jooq.SQLDialect.MYSQL;
// ...
import static org.jooq.SQLDialect.POSTGRES;
// ...
// ...
import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
// ...
// ...
import static org.jooq.SQLDialect.YUGABYTEDB;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.DSL.rowNumber;
// ...
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.Keywords.K_ORDINALITY;
import static org.jooq.impl.Keywords.K_WITH;
import static org.jooq.impl.Names.N_OFFSET;
import static org.jooq.impl.Names.N_ORDINAL;
import static org.jooq.impl.SQLDataType.BIGINT;
import static org.jooq.impl.SubqueryCharacteristics.DERIVED_TABLE;
// ...
import static org.jooq.impl.Tools.visitSubquery;

import java.util.List;
import java.util.Set;

import org.jooq.Context;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Name;
// ...
import org.jooq.QueryPart;
import org.jooq.Record;
// ...
import org.jooq.SQLDialect;
import org.jooq.Select;
import org.jooq.Table;
// ...
import org.jooq.impl.QOM.Aliasable;

/**
 * @author Lukas Eder
 */
final class WithOrdinalityTable
extends
    AbstractTable
implements
    AutoAlias>,
    QOM.WithOrdinalityTable
{

    static final Set NO_SUPPORT_STANDARD          = SQLDialect.supportedBy(DERBY, FIREBIRD, MARIADB, MYSQL, SQLITE);
    static final Set NO_SUPPORT_TVF               = SQLDialect.supportedBy(H2, HSQLDB);
    static final Set NO_SUPPORT_TABLE_EXPRESSIONS = SQLDialect.supportedBy(POSTGRES, YUGABYTEDB);

    static {
        NO_SUPPORT_TVF.addAll(NO_SUPPORT_STANDARD);
        NO_SUPPORT_TABLE_EXPRESSIONS.addAll(NO_SUPPORT_TVF);
    }

    final AbstractTable       delegate;

    WithOrdinalityTable(AbstractTable delegate) {
        super(delegate.getOptions(), delegate.getQualifiedName(), delegate.getSchema());

        this.delegate = delegate;
    }

    // -------------------------------------------------------------------------
    // XXX: Table API
    // -------------------------------------------------------------------------

    // [#5799] TODO: Maybe share some logic with AbstractDelegatingTable

    @Override
    public final boolean declaresTables() {
        return true;
    }

    @SuppressWarnings("unchecked")
    @Override
    public final Class getRecordType() {
        // TODO: [#4695] Calculate the correct Record[B] type
        return (Class) RecordImplN.class;
    }

    @Override
    public final List> getReferences() {
        return (List) delegate.getReferences();
    }

    @Override
    final FieldsImpl fields0() {
        FieldsImpl r = new FieldsImpl<>(delegate.fields0().fields);

        // [#5799] If WITH ORDINALITY is emulated using a derived table, then
        //         we must not fully qualify the fields.
        for (int i = 0; i < r.fields.length; i++)
            r.fields[i] = DSL.field(delegate.getUnqualifiedName().append(r.fields[i].getUnqualifiedName()), r.fields[i].getDataType());

        r.add(DSL.field(N_ORDINAL, BIGINT));
        return r;
    }

    @SuppressWarnings("unchecked")
    @Override
    public final Table autoAlias(Context ctx, Table t) {
        if (t != this && t instanceof AutoAlias a) {
            return ((AutoAlias>) a).autoAlias(ctx, t);
        }
        else if (t instanceof Aliasable a) {
            Name alias = a.$alias();
            if (alias == null)
                alias = ((Table) a.$aliased()).getUnqualifiedName();

            Field[] fields = t.fields();
            if (Tools.isEmpty(fields))
                return t.as(alias);
            else
                return t.as(table(alias), fields);
        }
        else
            return null;
    }

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

    @Override
    public final void accept(Context ctx) {
        Select s;

        if (delegate instanceof ArrayTable || delegate instanceof ArrayOfValues) {
            if (NO_SUPPORT_STANDARD.contains(ctx.dialect()))
                acceptEmulation(ctx);
            else
                acceptStandard(ctx);
        }
        else if (delegate instanceof FunctionTable && NO_SUPPORT_TVF.contains(ctx.dialect())) {
            if (NO_SUPPORT_TVF.contains(ctx.dialect()))
                acceptEmulation(ctx);
            else
                acceptStandard(ctx);
        }
        else if (delegate instanceof TableImpl && ((TableImpl) delegate).parameters != null) {
            if (NO_SUPPORT_TVF.contains(ctx.dialect()))
                acceptEmulation(ctx);
            else
                acceptStandard(ctx);
        }
        else if (NO_SUPPORT_TABLE_EXPRESSIONS.contains(ctx.dialect()))
            acceptEmulation(ctx);








        else
            acceptStandard(ctx);
    }

    private final void acceptStandard(Context ctx) {
        ctx.visit(delegate).sql(' ').visit(K_WITH).sql(' ').visit(K_ORDINALITY);
    }

    private final void acceptEmulation(Context ctx) {
        Select s;

        switch (ctx.family()) {






            default:
                s = select(delegate.fields()).select(rowNumber().over().as(N_ORDINAL)).from(delegate);
                break;
        }

        visitSubquery(ctx, s, DERIVED_TABLE, true);
    }












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

    @Override
    public final Table $table() {
        return delegate;
    }

    @SuppressWarnings("unchecked")
    @Override
    public final WithOrdinalityTable $table(Table newTable) {
        return new WithOrdinalityTable<>((AbstractTable) newTable);
    }



















    @Override
    public final Table $aliased() {
        return new WithOrdinalityTable<>((AbstractTable) ((Aliasable>) delegate).$aliased());
    }

    @Override
    public final Name $alias() {
        return ((Aliasable>) delegate).$alias();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy