[jOOQ/jOOQ#9437] Add flag clauses to ALTER SEQUENCE to DSL API

Adds the new `AlterSequenceFlagsStep` as a supertype to
`AlterSequenceStep`, which allows setting and clearing various flags
like `MINVALUE` and `CACHE`.
This commit is contained in:
Knut Wannheden 2019-10-30 17:28:32 +01:00
parent bf15048959
commit b8cbde22cc
4 changed files with 373 additions and 50 deletions

View File

@ -575,11 +575,9 @@ public abstract class AbstractDatabase implements Database {
@Override
public final CatalogDefinition getCatalog(String inputName) {
for (CatalogDefinition catalog : getCatalogs()) {
if (catalog.getName().equals(inputName)) {
for (CatalogDefinition catalog : getCatalogs())
if (catalog.getName().equals(inputName))
return catalog;
}
}
return null;
}
@ -674,7 +672,7 @@ public abstract class AbstractDatabase implements Database {
inputSchemata = new ArrayList<>();
inputSchemataPerCatalog = new LinkedHashMap<>();
// [#1312] Allow for ommitting inputSchema configuration. Generate all schemata instead.
// [#1312] Allow for omitting inputSchema configuration. Generate all schemata instead.
if (configuredSchemata.size() == 1 && StringUtils.isBlank(configuredSchemata.get(0).getInputSchema())) {
initAllSchemata();
}

View File

@ -0,0 +1,206 @@
/*
* 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;
// ...
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.POSTGRES;
// ...
// ...
// ...
// ...
/**
* The step in the <code>ALTER SEQUENCE</code> statement where the flags of the
* sequence are modified or cleared.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
* It is usually not recommended to reference any <code>XYZ*Step</code> types
* directly from client code, or assign them to local variables. When writing
* dynamic SQL, creating a statement's components dynamically, and passing them
* to the DSL API statically is usually a better choice. See the manual's
* section about dynamic SQL for details: <a href=
* "https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql">https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql</a>.
* <p>
* Drawbacks of referencing the <code>XYZ*Step</code> types directly:
* <ul>
* <li>They're operating on mutable implementations (as of jOOQ 3.x)</li>
* <li>They're less composable and not easy to get right when dynamic SQL gets
* complex</li>
* <li>They're less readable</li>
* <li>They might have binary incompatible changes between minor releases</li>
* </ul>
*
* @author Knut Wannheden
*/
public interface AlterSequenceFlagsStep<T extends Number> extends AlterSequenceFinalStep {
/**
* Restart the sequence at its initial value.
*/
@Support({ HSQLDB, MARIADB, POSTGRES })
AlterSequenceStep<T> restart();
/**
* Restart the sequence at a given value.
*/
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceStep<T> restartWith(T value);
/**
* Restart the sequence at a given value.
*/
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceStep<T> restartWith(Field<? extends T> value);
/**
* Add a <code>START WITH</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> startWith(Number constant);
/**
* Add a <code>START WITH</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> startWith(Field<? extends Number> constant);
/**
* Add a <code>INCREMENT BY</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> incrementBy(Number constant);
/**
* Add a <code>INCREMENT BY</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> incrementBy(Field<? extends Number> constant);
/**
* Add a <code>MINVALUE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> minvalue(Number constant);
/**
* Add a <code>MINVALUE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> minvalue(Field<? extends Number> constant);
/**
* Add a <code>NO MINVALUE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> noMinvalue();
/**
* Add a <code>MINVALUE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> maxvalue(Number constant);
/**
* Add a <code>MINVALUE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> maxvalue(Field<? extends Number> constant);
/**
* Add a <code>NO MINVALUE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> noMaxvalue();
/**
* Add a <code>CYCLE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> cycle();
/**
* Add a <code>NO CYCLE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> noCycle();
/**
* Add a <code>CACHE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> cache(Number constant);
/**
* Add a <code>CACHE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> cache(Field<? extends Number> constant);
/**
* Add a <code>NO CACHE</code> clause to the <code>ALTER SEQUENCE</code>
* statement.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFlagsStep<T> noCache();
}

View File

@ -37,21 +37,12 @@
*/
package org.jooq;
// ...
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.POSTGRES;
// ...
// ...
// ...
/**
* A {@link Query} that can alter sequences.
@ -76,25 +67,7 @@ import static org.jooq.SQLDialect.POSTGRES;
*
* @author Lukas Eder
*/
public interface AlterSequenceStep<T extends Number> {
/**
* Restart the sequence at its initial value.
*/
@Support({ HSQLDB, MARIADB, POSTGRES })
AlterSequenceFinalStep restart();
/**
* Restart the sequence at a given value.
*/
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFinalStep restartWith(T value);
/**
* Restart the sequence at a given value.
*/
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterSequenceFinalStep restartWith(Field<? extends T> value);
public interface AlterSequenceStep<T extends Number> extends AlterSequenceFlagsStep<T> {
/**
* Add a <code>RENAME TO</code> clause to the <code>ALTER SEQUENCE</code>

View File

@ -48,13 +48,21 @@ import static org.jooq.SQLDialect.CUBRID;
import static org.jooq.SQLDialect.DERBY;
import static org.jooq.SQLDialect.FIREBIRD;
// ...
import static org.jooq.SQLDialect.HSQLDB;
// ...
import static org.jooq.SQLDialect.MARIADB;
// ...
// ...
// ...
import static org.jooq.impl.Keywords.K_ALTER;
import static org.jooq.impl.Keywords.K_ALTER_TABLE;
import static org.jooq.impl.Keywords.K_CACHE;
import static org.jooq.impl.Keywords.K_CYCLE;
import static org.jooq.impl.Keywords.K_IF_EXISTS;
import static org.jooq.impl.Keywords.K_INCREMENT_BY;
import static org.jooq.impl.Keywords.K_MAXVALUE;
import static org.jooq.impl.Keywords.K_MINVALUE;
import static org.jooq.impl.Keywords.K_NO;
import static org.jooq.impl.Keywords.K_RENAME;
import static org.jooq.impl.Keywords.K_RENAME_SEQUENCE;
import static org.jooq.impl.Keywords.K_RENAME_TO;
@ -86,24 +94,35 @@ import org.jooq.Sequence;
final class AlterSequenceImpl<T extends Number> extends AbstractRowCountQuery implements
// Cascading interface implementations for AlterSequence behaviour
AlterSequenceStep<T>,
AlterSequenceFinalStep {
AlterSequenceStep<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = 8904572826501186329L;
private static final Clause[] CLAUSES = { ALTER_SEQUENCE };
private static final Set<SQLDialect> NO_SUPPORT_IF_EXISTS = SQLDialect.supported(CUBRID, DERBY, FIREBIRD);
private static final long serialVersionUID = 8904572826501186329L;
private static final Clause[] CLAUSES = { ALTER_SEQUENCE };
private static final Set<SQLDialect> NO_SUPPORT_IF_EXISTS = SQLDialect.supported(CUBRID, DERBY, FIREBIRD);
private static final Set<SQLDialect> NO_SEPARATOR = SQLDialect.supported(CUBRID, MARIADB);
private static final Set<SQLDialect> NO_SUPPORT_CACHE = SQLDialect.supported(DERBY, FIREBIRD, HSQLDB);
private final Sequence<T> sequence;
private final boolean ifExists;
private Field<?> restartWith;
private Sequence<?> renameTo;
private final Sequence<T> sequence;
private final boolean ifExists;
private Sequence<?> renameTo;
private boolean restart;
private Field<?> restartWith;
private Field<? extends Number> startWith;
private Field<? extends Number> incrementBy;
private Field<? extends Number> minvalue;
private boolean noMinvalue;
private Field<? extends Number> maxvalue;
private boolean noMaxvalue;
private Boolean cycle;
private Field<? extends Number> cache;
private boolean noCache;
AlterSequenceImpl(Configuration configuration, Sequence<T> sequence) {
this(configuration, sequence, false);
@ -125,17 +144,20 @@ final class AlterSequenceImpl<T extends Number> extends AbstractRowCountQuery im
// ------------------------------------------------------------------------
@Override
public final AlterSequenceFinalStep restart() {
public final AlterSequenceStep<T> restart() {
restart = true;
restartWith = null;
return this;
}
@Override
public final AlterSequenceFinalStep restartWith(T value) {
public final AlterSequenceStep<T> restartWith(T value) {
return restartWith(Tools.field(value, sequence.getDataType()));
}
@Override
public final AlterSequenceFinalStep restartWith(Field<? extends T> value) {
public final AlterSequenceStep<T> restartWith(Field<? extends T> value) {
restart = false;
restartWith = value;
return this;
}
@ -156,6 +178,97 @@ final class AlterSequenceImpl<T extends Number> extends AbstractRowCountQuery im
return renameTo(DSL.name(newName));
}
@Override
public AlterSequenceStep<T> startWith(Number constant) {
return startWith(Tools.field(constant, sequence.getDataType()));
}
@Override
public AlterSequenceStep<T> startWith(Field<? extends Number> constant) {
startWith = constant;
return this;
}
@Override
public AlterSequenceStep<T> incrementBy(Number constant) {
return incrementBy(Tools.field(constant, sequence.getDataType()));
}
@Override
public AlterSequenceStep<T> incrementBy(Field<? extends Number> constant) {
incrementBy = constant;
return this;
}
@Override
public AlterSequenceStep<T> minvalue(Number constant) {
return minvalue(Tools.field(constant, sequence.getDataType()));
}
@Override
public AlterSequenceStep<T> minvalue(Field<? extends Number> constant) {
minvalue = constant;
noMinvalue = false;
return this;
}
@Override
public AlterSequenceStep<T> noMinvalue() {
minvalue = null;
noMinvalue = true;
return this;
}
@Override
public AlterSequenceStep<T> maxvalue(Number constant) {
return maxvalue(Tools.field(constant, sequence.getDataType()));
}
@Override
public AlterSequenceStep<T> maxvalue(Field<? extends Number> constant) {
maxvalue = constant;
noMaxvalue = false;
return this;
}
@Override
public AlterSequenceStep<T> noMaxvalue() {
maxvalue = null;
noMaxvalue = true;
return this;
}
@Override
public AlterSequenceStep<T> cycle() {
cycle = true;
return this;
}
@Override
public AlterSequenceStep<T> noCycle() {
cycle = false;
return this;
}
@Override
public AlterSequenceStep<T> cache(Number constant) {
return cache(Tools.field(constant, sequence.getDataType()));
}
@Override
public AlterSequenceStep<T> cache(Field<? extends Number> constant) {
cache = constant;
noCache = false;
return this;
}
@Override
public AlterSequenceStep<T> noCache() {
cache = null;
noCache = true;
return this;
}
// ------------------------------------------------------------------------
// XXX: QueryPart API
// ------------------------------------------------------------------------
@ -286,9 +399,29 @@ final class AlterSequenceImpl<T extends Number> extends AbstractRowCountQuery im
else {
ctx.start(ALTER_SEQUENCE_RESTART);
Field<?> with = restartWith;
if (with == null) {
String noSeparator = NO_SEPARATOR.contains(ctx.family()) ? "" : " ";
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) {
@ -296,15 +429,27 @@ final class AlterSequenceImpl<T extends Number> extends AbstractRowCountQuery im
ctx.sql(' ').visit(K_RESTART);
}
else {
else if (restartWith != null) {
if (ctx.family() == CUBRID)
ctx.sql(' ').visit(K_START_WITH)
.sql(' ').visit(with);
.sql(' ').visit(restartWith);
else
ctx.sql(' ').visit(K_RESTART_WITH)
.sql(' ').visit(with);
.sql(' ').visit(restartWith);
}
if (!NO_SUPPORT_CACHE.contains(ctx.family()))
if (cache != null)
ctx.sql(' ').visit(K_CACHE).sql(' ').visit(cache);
else if (noCache)
// TODO: Postgres requires CACHE 1 here
ctx.sql(' ').visit(K_NO).sql(noSeparator).visit(K_CACHE);
if (Boolean.TRUE.equals(cycle))
ctx.sql(' ').visit(K_CYCLE);
else if (Boolean.FALSE.equals(cycle))
ctx.sql(' ').visit(K_NO).sql(noSeparator).visit(K_CYCLE);
ctx.end(ALTER_SEQUENCE_RESTART);
}
}
@ -313,4 +458,5 @@ final class AlterSequenceImpl<T extends Number> extends AbstractRowCountQuery im
public final Clause[] clauses(Context<?> ctx) {
return CLAUSES;
}
}