[#2355] Add support for Postgres / HSQLDB's TRUNCATE [...] RESTART /
CONTINUE IDENTITY
This commit is contained in:
parent
32b6b787e6
commit
503b986f11
@ -45,6 +45,7 @@ import static org.jooq.SQLDialect.SQLITE;
|
||||
import java.sql.Date;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.jooq.InsertResultStep;
|
||||
import org.jooq.Record1;
|
||||
import org.jooq.Record2;
|
||||
import org.jooq.Record3;
|
||||
@ -105,7 +106,19 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
@Test
|
||||
public void testTruncateCascade() throws Exception {
|
||||
switch (dialect()) {
|
||||
case ASE:
|
||||
case CUBRID:
|
||||
case DB2:
|
||||
case DERBY:
|
||||
case FIREBIRD:
|
||||
case H2:
|
||||
case HSQLDB:
|
||||
case INGRES:
|
||||
case MYSQL:
|
||||
case ORACLE:
|
||||
case SQLITE:
|
||||
case SQLSERVER:
|
||||
case SYBASE:
|
||||
log.info("SKIPPING", "TRUNCATE CASCADE tests");
|
||||
return;
|
||||
}
|
||||
@ -127,4 +140,46 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
assertEquals(0, create().fetch(TAuthor()).size());
|
||||
assertEquals(0, create().fetch(TBook()).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTruncateRestartIdentity() throws Exception {
|
||||
switch (dialect()) {
|
||||
case ASE:
|
||||
case CUBRID:
|
||||
case DB2:
|
||||
case DERBY:
|
||||
case FIREBIRD:
|
||||
case H2:
|
||||
case INGRES:
|
||||
case MYSQL:
|
||||
case ORACLE:
|
||||
case SQLITE:
|
||||
case SQLSERVER:
|
||||
case SYBASE:
|
||||
log.info("SKIPPING", "RESTART IDENTITY tests");
|
||||
return;
|
||||
}
|
||||
|
||||
jOOQAbstractTest.reset = false;
|
||||
|
||||
InsertResultStep<I> insert =
|
||||
create().insertInto(TIdentity(), TIdentity_VAL())
|
||||
.values(1)
|
||||
.returning(TIdentity_ID());
|
||||
int id1 = insert.fetchOne().getValue(TIdentity_ID());
|
||||
|
||||
|
||||
create().truncate(TIdentity())
|
||||
.continueIdentity()
|
||||
.execute();
|
||||
int id2 = insert.fetchOne().getValue(TIdentity_ID());
|
||||
assertEquals(id1 + 1, id2);
|
||||
|
||||
|
||||
create().truncate(TIdentity())
|
||||
.restartIdentity()
|
||||
.execute();
|
||||
int id3 = insert.fetchOne().getValue(TIdentity_ID());
|
||||
assertEquals(1, id3);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1035,6 +1035,11 @@ public abstract class jOOQAbstractTest<
|
||||
new TruncateTests(this).testTruncateCascade();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTruncateRestartIdentity() throws Exception {
|
||||
new TruncateTests(this).testTruncateRestartIdentity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMetaModel() throws Exception {
|
||||
new MetaDataTests(this).testMetaModel();
|
||||
|
||||
@ -35,6 +35,9 @@
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
import static org.jooq.SQLDialect.HSQLDB;
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
|
||||
/**
|
||||
* A {@link Query} that can truncate a table in the database.
|
||||
*
|
||||
@ -42,4 +45,17 @@ package org.jooq;
|
||||
*/
|
||||
public interface TruncateIdentityStep<R extends Record> extends TruncateCascadeStep<R> {
|
||||
|
||||
/**
|
||||
* Add the <code>RESTART IDENTITY</code> clause to the <code>TRUNCATE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ HSQLDB, POSTGRES })
|
||||
TruncateCascadeStep<R> restartIdentity();
|
||||
|
||||
/**
|
||||
* Add the <code>CONTINUE IDENTITY</code> clause to the
|
||||
* <code>TRUNCATE</code> statement.
|
||||
*/
|
||||
@Support({ HSQLDB, POSTGRES })
|
||||
TruncateCascadeStep<R> continueIdentity();
|
||||
}
|
||||
|
||||
@ -41,6 +41,7 @@ import org.jooq.Record;
|
||||
import org.jooq.RenderContext;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TruncateCascadeStep;
|
||||
import org.jooq.TruncateFinalStep;
|
||||
import org.jooq.TruncateIdentityStep;
|
||||
|
||||
@ -59,6 +60,7 @@ class TruncateImpl<R extends Record> extends AbstractQuery implements
|
||||
|
||||
private final Table<R> table;
|
||||
private Boolean cascade;
|
||||
private Boolean restartIdentity;
|
||||
|
||||
public TruncateImpl(Configuration configuration, Table<R> table) {
|
||||
super(configuration);
|
||||
@ -78,6 +80,18 @@ class TruncateImpl<R extends Record> extends AbstractQuery implements
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final TruncateCascadeStep<R> restartIdentity() {
|
||||
restartIdentity = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final TruncateCascadeStep<R> continueIdentity() {
|
||||
restartIdentity = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void toSQL(RenderContext context) {
|
||||
switch (context.configuration().dialect()) {
|
||||
@ -103,8 +117,13 @@ class TruncateImpl<R extends Record> extends AbstractQuery implements
|
||||
}
|
||||
}
|
||||
|
||||
if (restartIdentity != null) {
|
||||
context.formatSeparator()
|
||||
.keyword(restartIdentity ? "restart identity" : "continue identity");
|
||||
}
|
||||
|
||||
if (cascade != null) {
|
||||
context.sql(" ")
|
||||
context.formatSeparator()
|
||||
.keyword(cascade ? "cascade" : "restrict");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user