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

org.jooq.impl.GenerateSeries 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
 *
 *  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
 * Apache-2.0 license 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.SQLDialect.CUBRID;
// ...
// ...
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.SQLITE;
// ...
// ...
// ...
import static org.jooq.conf.ParamType.INLINED;
import static org.jooq.impl.DSL.function;
import static org.jooq.impl.DSL.inline;
// ...
import static org.jooq.impl.DSL.one;
// ...
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.DSL.withRecursive;
import static org.jooq.impl.Internal.iadd;
import static org.jooq.impl.Internal.idiv;
import static org.jooq.impl.Internal.imul;
import static org.jooq.impl.Internal.isub;
import static org.jooq.impl.Keywords.K_TABLE;
import static org.jooq.impl.Names.N_GENERATE_ARRAY;
import static org.jooq.impl.Names.N_GENERATE_SERIES;
import static org.jooq.impl.Names.N_GENERATOR;
import static org.jooq.impl.Names.N_SYSTEM_RANGE;
import static org.jooq.impl.Names.N_UNNEST;
import static org.jooq.impl.SQLDataType.INTEGER;
import static org.jooq.impl.SubqueryCharacteristics.DERIVED_TABLE;
import static org.jooq.impl.Tools.apply;
import static org.jooq.impl.Tools.visitSubquery;

import java.util.Set;

import org.jooq.Clause;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Function3;
import org.jooq.Name;
// ...
import org.jooq.Record1;
import org.jooq.SQLDialect;
import org.jooq.Table;
import org.jooq.TableOptions;

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

    private static final Set EMULATE_WITH_RECURSIVE = SQLDialect.supportedBy(FIREBIRD, HSQLDB, MARIADB, MYSQL, SQLITE);
    private static final Set EMULATE_SYSTEM_RANGE   = SQLDialect.supportedBy(H2);











    private final Field         from;
    private final Field         to;
    private final Field         step;
    private final Name                   name;

    GenerateSeries(Field from, Field to) {
        this(from, to, null);
    }

    GenerateSeries(Field from, Field to, Field step) {
        this(from, to, step, N_GENERATE_SERIES);
    }

    GenerateSeries(Field from, Field to, Field step, Name name) {
        super(TableOptions.expression(), name);

        this.from = from;
        this.to = to;
        this.step = step;
        this.name = name;
    }

    @Override
    public Table> as(Name alias) {
        return new TableAlias<>(new GenerateSeries(from, to, step, alias), alias);
    }

    @Override
    public Table> as(Name alias, Name... fieldAliases) {
        return new TableAlias<>(new GenerateSeries(from, to, step, alias), alias, fieldAliases);
    }

    @Override
    public final void accept(Context ctx) {
        if (EMULATE_WITH_RECURSIVE.contains(ctx.dialect())) {
            Field f = DSL.field(name, INTEGER);
            visitSubquery(
                ctx,
                withRecursive(name, name)
                    .as(select(from).unionAll(select(iadd(f, step == null ? inline(1) : step)).from(name).where(f.lt(to))))
                    .select(f).from(name),
                DERIVED_TABLE
            );
        }
        else if (EMULATE_SYSTEM_RANGE.contains(ctx.dialect())) {
            if (step == null) {
                ctx.visit(N_SYSTEM_RANGE).sql('(').visit(from).sql(", ").visit(to).sql(')');
            }

            // Work around https://github.com/h2database/h2database/issues/3046
            else {
                ctx.visit(N_SYSTEM_RANGE).sql('(').visit(from).sql(", ").visit(to).sql(", ");
                ctx.paramType(INLINED, c -> c.visit(step));
                ctx.sql(')');
            }
        }







































        else {
            if (step == null)
                ctx.visit(N_GENERATE_SERIES).sql('(').visit(from).sql(", ").visit(to).sql(')');
            else
                ctx.visit(N_GENERATE_SERIES).sql('(').visit(from).sql(", ").visit(to).sql(", ").visit(step).sql(')');
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public final Class> getRecordType() {
        return (Class) RecordImpl1.class;
    }

    @Override
    final FieldsImpl> fields0() {
        return new FieldsImpl<>(DSL.field(DSL.name(name, N_GENERATE_SERIES), Integer.class));
    }

    @Override // Avoid AbstractTable implementation
    public final Clause[] clauses(Context ctx) {
        return null;
    }

    @Override
    public final Table> autoAlias(Context ctx) {
        if (EMULATE_WITH_RECURSIVE.contains(ctx.dialect()))
            return as(name);
        else if (EMULATE_SYSTEM_RANGE.contains(ctx.dialect()))
            return as(name, name);








        else
            return null;
    }

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

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

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

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

    @Override
    public final Function3, ? super Field, ? super Field, ? extends Table>> $constructor() {
        return (f1, f2, f3) -> new GenerateSeries(f1, f2, f3, name);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy