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

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

The 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
 * Apache-2.0 license 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.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.ExtendedDataKey.*;
import static org.jooq.impl.Tools.SimpleDataKey.*;
import static org.jooq.SQLDialect.*;

import org.jooq.*;
import org.jooq.Function1;
import org.jooq.Record;
import org.jooq.conf.ParamType;
import org.jooq.impl.QOM.CycleOption;
import org.jooq.tools.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;



/**
 * The ALTER SEQUENCE statement.
 */
@SuppressWarnings({ "hiding", "rawtypes", "unchecked", "unused" })
final class AlterSequenceImpl
extends
    AbstractDDLQuery
implements
    QOM.AlterSequence,
    AlterSequenceStep,
    AlterSequenceFlagsStep,
    AlterSequenceFinalStep
{

    final Sequence sequence;
    final boolean     ifExists;
          Sequence renameTo;
          boolean     restart;
          Field    restartWith;
          Field    startWith;
          Field    incrementBy;
          Field    minvalue;
          boolean     noMinvalue;
          Field    maxvalue;
          boolean     noMaxvalue;
          CycleOption cycle;
          Field    cache;
          boolean     noCache;

    AlterSequenceImpl(
        Configuration configuration,
        Sequence sequence,
        boolean ifExists
    ) {
        this(
            configuration,
            sequence,
            ifExists,
            null,
            false,
            null,
            null,
            null,
            null,
            false,
            null,
            false,
            null,
            null,
            false
        );
    }

    AlterSequenceImpl(
        Configuration configuration,
        Sequence sequence,
        boolean ifExists,
        Sequence renameTo,
        boolean restart,
        Field restartWith,
        Field startWith,
        Field incrementBy,
        Field minvalue,
        boolean noMinvalue,
        Field maxvalue,
        boolean noMaxvalue,
        CycleOption cycle,
        Field cache,
        boolean noCache
    ) {
        super(configuration);

        this.sequence = sequence;
        this.ifExists = ifExists;
        this.renameTo = renameTo;
        this.restart = restart;
        this.restartWith = restartWith;
        this.startWith = startWith;
        this.incrementBy = incrementBy;
        this.minvalue = minvalue;
        this.noMinvalue = noMinvalue;
        this.maxvalue = maxvalue;
        this.noMaxvalue = noMaxvalue;
        this.cycle = cycle;
        this.cache = cache;
        this.noCache = noCache;
    }

    // -------------------------------------------------------------------------
    // XXX: DSL API
    // -------------------------------------------------------------------------

    @Override
    public final AlterSequenceImpl renameTo(String renameTo) {
        return renameTo(DSL.sequence(DSL.name(renameTo)));
    }

    @Override
    public final AlterSequenceImpl renameTo(Name renameTo) {
        return renameTo(DSL.sequence(renameTo));
    }

    @Override
    public final AlterSequenceImpl renameTo(Sequence renameTo) {
        this.renameTo = renameTo;
        return this;
    }

    @Override
    public final AlterSequenceImpl restart() {
        this.restart = true;
        return this;
    }

    @Override
    public final AlterSequenceImpl restartWith(T restartWith) {
        return restartWith(Tools.field(restartWith));
    }

    @Override
    public final AlterSequenceImpl restartWith(Field restartWith) {
        this.restartWith = restartWith;
        return this;
    }

    @Override
    public final AlterSequenceImpl startWith(T startWith) {
        return startWith(Tools.field(startWith));
    }

    @Override
    public final AlterSequenceImpl startWith(Field startWith) {
        this.startWith = startWith;
        return this;
    }

    @Override
    public final AlterSequenceImpl incrementBy(T incrementBy) {
        return incrementBy(Tools.field(incrementBy));
    }

    @Override
    public final AlterSequenceImpl incrementBy(Field incrementBy) {
        this.incrementBy = incrementBy;
        return this;
    }

    @Override
    public final AlterSequenceImpl minvalue(T minvalue) {
        return minvalue(Tools.field(minvalue));
    }

    @Override
    public final AlterSequenceImpl minvalue(Field minvalue) {
        this.minvalue = minvalue;
        return this;
    }

    @Override
    public final AlterSequenceImpl noMinvalue() {
        this.noMinvalue = true;
        return this;
    }

    @Override
    public final AlterSequenceImpl maxvalue(T maxvalue) {
        return maxvalue(Tools.field(maxvalue));
    }

    @Override
    public final AlterSequenceImpl maxvalue(Field maxvalue) {
        this.maxvalue = maxvalue;
        return this;
    }

    @Override
    public final AlterSequenceImpl noMaxvalue() {
        this.noMaxvalue = true;
        return this;
    }

    @Override
    public final AlterSequenceImpl cycle() {
        this.cycle = CycleOption.CYCLE;
        return this;
    }

    @Override
    public final AlterSequenceImpl noCycle() {
        this.cycle = CycleOption.NO_CYCLE;
        return this;
    }

    @Override
    public final AlterSequenceImpl cache(T cache) {
        return cache(Tools.field(cache));
    }

    @Override
    public final AlterSequenceImpl cache(Field cache) {
        this.cache = cache;
        return this;
    }

    @Override
    public final AlterSequenceImpl noCache() {
        this.noCache = true;
        return this;
    }

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



    private static final Clause[]        CLAUSES                     = { Clause.ALTER_SEQUENCE };
    private static final Set NO_SUPPORT_IF_EXISTS        = SQLDialect.supportedUntil(CUBRID, DERBY, FIREBIRD);
    private static final Set NO_SUPPORT_RENAME_IF_EXISTS = SQLDialect.supportedBy(CUBRID, DERBY, FIREBIRD);
    private static final Set NO_SEPARATOR                = SQLDialect.supportedBy(CUBRID, MARIADB);
    private static final Set NO_SUPPORT_CACHE            = SQLDialect.supportedBy(DERBY, FIREBIRD, HSQLDB);
    private static final Set EMULATE_NO_CACHE            = SQLDialect.supportedBy(POSTGRES, YUGABYTEDB);





    private final boolean supportsIfExists(Context ctx) {
        if (renameTo != null)
            return !NO_SUPPORT_RENAME_IF_EXISTS.contains(ctx.dialect());
        else
            return !NO_SUPPORT_IF_EXISTS.contains(ctx.dialect());
    }

    @Override
    public final void accept(Context ctx) {
        if (ifExists && !supportsIfExists(ctx))
            tryCatch(ctx, DDLStatementType.ALTER_SEQUENCE, c -> accept0(c));
        else
            accept0(ctx);
    }

    private final void accept0(Context ctx) {
        switch (ctx.family()) {











            case MARIADB:
                if (renameTo != null)
                    acceptRenameTable(ctx);
                else
                    accept1(ctx);

                break;

            default:
                accept1(ctx);
                break;
        }
    }

    private final void acceptRenameTable(Context ctx) {
        ctx.start(Clause.ALTER_SEQUENCE_SEQUENCE)
           .start(Clause.ALTER_SEQUENCE_RENAME)
           .visit(K_ALTER_TABLE)
           .sql(' ')
           .visit(sequence)
           .sql(' ')
           .visit(K_RENAME_TO)
           .sql(' ')
           .qualifySchema(false, c -> c.visit(renameTo))
           .end(Clause.ALTER_SEQUENCE_RENAME)
           .end(Clause.ALTER_SEQUENCE_SEQUENCE);
    }


















    private final void accept1(Context ctx) {
        ctx.start(Clause.ALTER_SEQUENCE_SEQUENCE)
           .visit(K_ALTER)
           .sql(' ')
           .visit(ctx.family() == CUBRID ? K_SERIAL : K_SEQUENCE);

        if (ifExists && supportsIfExists(ctx))
            ctx.sql(' ').visit(K_IF_EXISTS);






        ctx.sql(' ').visit(sequence);

        ctx.end(Clause.ALTER_SEQUENCE_SEQUENCE);

        if (renameTo != null) {
            ctx.start(Clause.ALTER_SEQUENCE_RENAME)
               .sql(' ').visit(K_RENAME_TO)
               .sql(' ')
               .qualify(false, c -> c.visit(renameTo))
               .end(Clause.ALTER_SEQUENCE_RENAME);
        }
        else {
            ctx.start(Clause.ALTER_SEQUENCE_RESTART);

            String noSeparator = NO_SEPARATOR.contains(ctx.dialect()) ? "" : " ";

            if (incrementBy != null) {
                ctx.sql(' ').visit(K_INCREMENT_BY)
                   .sql(' ').visit(incrementBy);
            }

            if (minvalue != null)
                ctx.sql(' ').visit(K_MINVALUE).sql(' ').visit(minvalue);
            else if (noMinvalue)
                ctx.sql(' ').visit(K_NO).sql(noSeparator).visit(K_MINVALUE);

            if (maxvalue != null)
                ctx.sql(' ').visit(K_MAXVALUE).sql(' ').visit(maxvalue);
            else if (noMaxvalue)
                ctx.sql(' ').visit(K_NO).sql(noSeparator).visit(K_MAXVALUE);

            if (startWith != null) {
                ctx.sql(' ').visit(K_START_WITH)
                   .sql(' ').visit(startWith);
            }

            if (restart) {










                    ctx.sql(' ').visit(K_RESTART);
            }
            else if (restartWith != null) {
                if (ctx.family() == CUBRID)
                    ctx.sql(' ').visit(K_START_WITH)
                       .sql(' ').visit(restartWith);
                else
                    ctx.sql(' ').visit(K_RESTART_WITH)
                       .sql(' ').visit(restartWith);
            }

            if (!NO_SUPPORT_CACHE.contains(ctx.dialect()))
                if (cache != null)
                    ctx.sql(' ').visit(K_CACHE).sql(' ').visit(cache);
                else if (noCache)
                    if (EMULATE_NO_CACHE.contains(ctx.dialect()))
                        ctx.sql(' ').visit(K_CACHE).sql(' ').sql(1);
                    else
                        ctx.sql(' ').visit(K_NO).sql(noSeparator).visit(K_CACHE);

            if (cycle == CycleOption.CYCLE)
                ctx.sql(' ').visit(K_CYCLE);
            else if (cycle == CycleOption.NO_CYCLE)
                ctx.sql(' ').visit(K_NO).sql(noSeparator).visit(K_CYCLE);

            ctx.end(Clause.ALTER_SEQUENCE_RESTART);
        }
    }

    @Override
    public final Clause[] clauses(Context ctx) {
        return CLAUSES;
    }



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

    @Override
    public final Sequence $sequence() {
        return sequence;
    }

    @Override
    public final boolean $ifExists() {
        return ifExists;
    }

    @Override
    public final Sequence $renameTo() {
        return renameTo;
    }

    @Override
    public final boolean $restart() {
        return restart;
    }

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

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

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

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

    @Override
    public final boolean $noMinvalue() {
        return noMinvalue;
    }

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

    @Override
    public final boolean $noMaxvalue() {
        return noMaxvalue;
    }

    @Override
    public final CycleOption $cycle() {
        return cycle;
    }

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

    @Override
    public final boolean $noCache() {
        return noCache;
    }

    @Override
    public final QOM.AlterSequence $sequence(Sequence newValue) {
        return $constructor().apply(newValue, $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $ifExists(boolean newValue) {
        return $constructor().apply($sequence(), newValue, $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $renameTo(Sequence newValue) {
        return $constructor().apply($sequence(), $ifExists(), newValue, $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $restart(boolean newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), newValue, $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $restartWith(Field newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), newValue, $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $startWith(Field newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), newValue, $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $incrementBy(Field newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), newValue, $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $minvalue(Field newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), newValue, $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $noMinvalue(boolean newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), newValue, $maxvalue(), $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $maxvalue(Field newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), newValue, $noMaxvalue(), $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $noMaxvalue(boolean newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), newValue, $cycle(), $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $cycle(CycleOption newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), newValue, $cache(), $noCache());
    }

    @Override
    public final QOM.AlterSequence $cache(Field newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), newValue, $noCache());
    }

    @Override
    public final QOM.AlterSequence $noCache(boolean newValue) {
        return $constructor().apply($sequence(), $ifExists(), $renameTo(), $restart(), $restartWith(), $startWith(), $incrementBy(), $minvalue(), $noMinvalue(), $maxvalue(), $noMaxvalue(), $cycle(), $cache(), newValue);
    }

    public final Function14, ? super Boolean, ? super Sequence, ? super Boolean, ? super Field, ? super Field, ? super Field, ? super Field, ? super Boolean, ? super Field, ? super Boolean, ? super CycleOption, ? super Field, ? super Boolean, ? extends QOM.AlterSequence> $constructor() {
        return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) -> new AlterSequenceImpl(configuration(), a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
    }









































}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy