mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 23:28:55 +00:00
#1782 - ENH: Add truncate(Class<?>... types) and truncate(String... tables)
This commit is contained in:
@@ -1237,4 +1237,18 @@ public class DB {
|
||||
return getDefault().json();
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate the base tables for the given bean types.
|
||||
*/
|
||||
public static void truncate(Class<?>... types) {
|
||||
getDefault().truncate(types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate the given tables.
|
||||
*/
|
||||
public static void truncate(String... tables) {
|
||||
getDefault().truncate(tables);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1580,4 +1580,15 @@ public interface Database {
|
||||
* </p>
|
||||
*/
|
||||
<T> Set<String> validateQuery(Query<T> query);
|
||||
|
||||
/**
|
||||
* Truncate all the given tables.
|
||||
*/
|
||||
void truncate(String... tables);
|
||||
|
||||
/**
|
||||
* Truncate the base tables for the given bean types.
|
||||
*/
|
||||
void truncate(Class<?>... tables);
|
||||
|
||||
}
|
||||
|
||||
@@ -139,6 +139,10 @@ public class DatabasePlatform {
|
||||
*/
|
||||
protected Platform platform = Platform.GENERIC;
|
||||
|
||||
protected String truncateTable = "truncate table %s";
|
||||
protected String truncateTableBefore;
|
||||
protected String truncateTableAfter;
|
||||
|
||||
protected String columnAliasPrefix = "c";
|
||||
|
||||
protected String tableAliasPlaceHolder = "${ta}";
|
||||
@@ -708,6 +712,27 @@ public class DatabasePlatform {
|
||||
return persistBatchOnCascade;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a statement to truncate a table.
|
||||
*/
|
||||
public String truncateStatement(String table) {
|
||||
return String.format(truncateTable, table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a statement to execute prior to truncate (like disable foreign keys for mysql).
|
||||
*/
|
||||
public String truncateStatementBefore() {
|
||||
return truncateTableBefore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a statement to execute after truncate (like enable foreign keys for mysql).
|
||||
*/
|
||||
public String truncateStatementAfter() {
|
||||
return truncateTableAfter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the DB schema if it does not exist.
|
||||
*/
|
||||
|
||||
@@ -22,6 +22,7 @@ public class DB2Platform extends DatabasePlatform {
|
||||
this.platform = Platform.DB2;
|
||||
this.maxTableNameLength = 18;
|
||||
this.maxConstraintNameLength = 18;
|
||||
this.truncateTable = "truncate table %s reuse storage ignore delete triggers immediate";
|
||||
this.sqlLimiter = new Db2SqlLimiter();
|
||||
|
||||
this.dbIdentity.setSupportsGetGeneratedKeys(true);
|
||||
|
||||
@@ -26,7 +26,8 @@ public class H2Platform extends DatabasePlatform {
|
||||
this.supportsDeleteTableAlias = true;
|
||||
this.dbDefaultValue.setNow("now()");
|
||||
this.columnAliasPrefix = null;
|
||||
|
||||
//this.truncateTableBefore = "set referential_integrity false";
|
||||
//this.truncateTableAfter = "set referential_integrity true";
|
||||
this.exceptionTranslator =
|
||||
new SqlErrorCodes()
|
||||
.addAcquireLock("50200","HYT00")
|
||||
|
||||
@@ -18,7 +18,6 @@ public class HanaPlatform extends DatabasePlatform {
|
||||
this.persistBatchOnCascade = PersistBatch.NONE;
|
||||
this.supportsResultSetConcurrencyModeUpdatable = false;
|
||||
this.columnAliasPrefix = null;
|
||||
|
||||
this.historySupport = new HanaHistorySupport();
|
||||
this.basicSqlLimiter = new HanaBasicSqlLimiter();
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class HsqldbPlatform extends DatabasePlatform {
|
||||
super();
|
||||
this.platform = Platform.HSQLDB;
|
||||
this.dbEncrypt = new H2DbEncrypt();
|
||||
|
||||
this.truncateTable = "delete from %s";
|
||||
this.dbIdentity.setIdType(IdType.IDENTITY);
|
||||
this.dbIdentity.setSupportsGetGeneratedKeys(true);
|
||||
this.dbIdentity.setSupportsSequence(true);
|
||||
|
||||
@@ -31,7 +31,8 @@ public class MySqlPlatform extends DatabasePlatform {
|
||||
this.dbEncrypt = new MySqlDbEncrypt();
|
||||
this.historySupport = new MySqlHistorySupport();
|
||||
this.columnAliasPrefix = null;
|
||||
|
||||
//this.truncateTableBefore = "set foreign_key_checks = 0";
|
||||
//this.truncateTableAfter = "set foreign_key_checks = 1";
|
||||
this.dbIdentity.setIdType(IdType.IDENTITY);
|
||||
this.dbIdentity.setSupportsGetGeneratedKeys(true);
|
||||
this.dbIdentity.setSupportsIdentity(true);
|
||||
|
||||
@@ -30,7 +30,7 @@ public class OraclePlatform extends DatabasePlatform {
|
||||
this.sqlLimiter = new RownumSqlLimiter();
|
||||
this.basicSqlLimiter = new BasicSqlAnsiLimiter();
|
||||
this.historySupport = new OracleDbHistorySupport();
|
||||
|
||||
this.truncateTable = "truncate table %s cascade";
|
||||
dbIdentity.setIdType(IdType.SEQUENCE);
|
||||
dbIdentity.setSupportsSequence(true);
|
||||
dbIdentity.setSupportsIdentity(true);
|
||||
|
||||
@@ -36,7 +36,7 @@ public class PostgresPlatform extends DatabasePlatform {
|
||||
this.clobDbType = Types.VARCHAR;
|
||||
this.nativeUuidType = true;
|
||||
this.columnAliasPrefix = null;
|
||||
|
||||
this.truncateTable = "truncate table %s cascade";
|
||||
this.dbEncrypt = new PostgresDbEncrypt();
|
||||
this.historySupport = new PostgresHistorySupport();
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ public class SQLitePlatform extends DatabasePlatform {
|
||||
this.dbIdentity.setSupportsGetGeneratedKeys(false);
|
||||
this.dbIdentity.setSupportsSequence(false);
|
||||
this.dbIdentity.setSelectLastInsertedIdTemplate("select last_insert_rowid()");
|
||||
|
||||
this.truncateTable = "delete from %s";
|
||||
this.booleanDbType = Types.INTEGER;
|
||||
this.likeClauseRaw = "like ?";
|
||||
this.likeClauseEscaped = "like ?";
|
||||
|
||||
@@ -124,8 +124,10 @@ import javax.persistence.OptimisticLockException;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Clock;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -639,6 +641,38 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
||||
externalModification(evt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void truncate(Class<?>... types) {
|
||||
List<String> tableNames = new ArrayList<>();
|
||||
for (Class<?> type : types) {
|
||||
tableNames.add(getBeanDescriptor(type).getBaseTable());
|
||||
}
|
||||
truncate(tableNames.toArray(new String[0]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void truncate(String... tables) {
|
||||
try (Connection connection = getDataSource().getConnection()) {
|
||||
executeSql(connection, databasePlatform.truncateStatementBefore());
|
||||
for (String table : tables) {
|
||||
executeSql(connection, databasePlatform.truncateStatement(table));
|
||||
}
|
||||
executeSql(connection, databasePlatform.truncateStatementAfter());
|
||||
connection.commit();
|
||||
} catch(SQLException e) {
|
||||
throw new PersistenceException("Error executing truncate", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void executeSql(Connection connection, String sql) throws SQLException {
|
||||
if (sql != null) {
|
||||
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
|
||||
transactionManager.log().sql().debug(sql);
|
||||
stmt.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the query execution statistics.
|
||||
*/
|
||||
|
||||
@@ -94,7 +94,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void setClock(Clock clock) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +103,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void shutdownManaged() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -113,8 +111,15 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scopedTransactionEnter(TxScope txScope) {
|
||||
public void truncate(String... tables) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void truncate(Class<?>... tables) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scopedTransactionEnter(TxScope txScope) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -229,7 +234,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void clearServerTransaction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -249,7 +253,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void remoteTransactionEvent(RemoteTransactionEvent event) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -274,17 +277,14 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void merge(Object bean) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void merge(Object bean, MergeOptions options) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void merge(Object bean, MergeOptions options, Transaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -304,12 +304,10 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void loadBean(LoadBeanRequest loadRequest) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadMany(LoadManyRequest loadRequest) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -324,27 +322,22 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void collectQueryStats(ObjectGraphNode objectGraphNode, long loadedBeanCount, long timeMicros) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMetrics(MetricVisitor visitor) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadMany(BeanCollection<?> collection, boolean onlyIds) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBean(EntityBeanIntercept ebi) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown(boolean shutdownDataSource, boolean deregisterDriver) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -439,7 +432,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public <T> void sort(List<T> list, String sortByClause) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -449,12 +441,10 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public <T> void findDtoEach(SpiDtoQuery<T> query, Consumer<T> consumer) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void findDtoEachWhile(SpiDtoQuery<T> query, Predicate<T> consumer) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -509,7 +499,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void findEachRow(SpiSqlQuery query, RowConsumer consumer) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -539,7 +528,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void register(TransactionCallback transactionCallback) throws PersistenceException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -614,32 +602,26 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void commitTransaction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollbackTransaction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endTransaction() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh(Object bean) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshMany(Object bean, String propertyName) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -679,12 +661,10 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public <T> void findEach(Query<T> query, Consumer<T> consumer, Transaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void findEachWhile(Query<T> query, Predicate<T> consumer, Transaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -757,7 +737,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void save(Object bean) throws OptimisticLockException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -807,7 +786,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void externalModification(String tableName, boolean inserted, boolean updated, boolean deleted) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -817,37 +795,30 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void save(Object bean, Transaction transaction) throws OptimisticLockException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markAsDirty(Object bean) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Object bean) throws OptimisticLockException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Object bean, Transaction t) throws OptimisticLockException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Object bean, Transaction transaction, boolean deleteMissingChildren) throws OptimisticLockException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insert(Object bean) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insert(Object bean, Transaction t) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -887,7 +858,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void addBatch(SpiSqlUpdate sqlUpdate, SpiTransaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -902,12 +872,10 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void execute(TxScope scope, Runnable r) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Runnable r) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -982,27 +950,22 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
@Override
|
||||
public void updateAll(Collection<?> beans) throws OptimisticLockException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAll(Collection<?> beans, Transaction transaction) throws OptimisticLockException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertAll(Collection<?> beans) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertAll(Collection<?> beans, Transaction transaction) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void slowQueryCheck(long executionTimeMicros, int rowCount, SpiQuery<?> query) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package main;
|
||||
|
||||
import io.ebean.docker.commands.CockroachConfig;
|
||||
import io.ebean.docker.commands.CockroachContainer;
|
||||
|
||||
public class StartCockroach {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
CockroachConfig config = new CockroachConfig();
|
||||
config.setDbName("unit");
|
||||
|
||||
CockroachContainer container = new CockroachContainer(config);
|
||||
container.startWithDropCreate();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.tests.model.nofk;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
@@ -21,9 +22,8 @@ public class Test2NoFk extends BaseTestCase {
|
||||
@Before
|
||||
public void setup() {
|
||||
// Reset t
|
||||
Ebean.find(EFile2NoFk.class).delete();
|
||||
Ebean.find(EUserNoFk.class).delete();
|
||||
Ebean.find(EUserNoFkSoftDel.class).delete();
|
||||
DB.truncate(EFile2NoFk.class, EUserNoFk.class, EUserNoFkSoftDel.class);
|
||||
assertThat(DB.find(EFile2NoFk.class).findCount()).isEqualTo(0);
|
||||
|
||||
// There are two user accounts persisted in our database
|
||||
EUserNoFk root = new EUserNoFk();
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.tests.model.nofk;
|
||||
|
||||
import io.ebean.DB;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestTruncate {
|
||||
|
||||
@Test
|
||||
public void test_truncateByClass() {
|
||||
DB.truncate(EUserNoFkSoftDel.class, EUserNoFk.class);
|
||||
|
||||
insertRows();
|
||||
assertRowCounts(1);
|
||||
|
||||
DB.truncate(EUserNoFkSoftDel.class, EUserNoFk.class);
|
||||
assertRowCounts(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_truncateByTable() {
|
||||
DB.truncate(EUserNoFkSoftDel.class, EUserNoFk.class);
|
||||
|
||||
insertRows();
|
||||
assertRowCounts(1);
|
||||
|
||||
DB.truncate("euser_no_fk_soft_del", "euser_no_fk");
|
||||
assertRowCounts(0);
|
||||
}
|
||||
|
||||
private void insertRows() {
|
||||
EUserNoFk root = new EUserNoFk();
|
||||
root.setUserName("root");
|
||||
DB.save(root);
|
||||
|
||||
EUserNoFkSoftDel rootSoftDel = new EUserNoFkSoftDel();
|
||||
rootSoftDel.setUserName("root");
|
||||
DB.save(rootSoftDel);
|
||||
}
|
||||
|
||||
private void assertRowCounts(int expected) {
|
||||
assertThat(DB.find(EUserNoFk.class).findCount()).isEqualTo(expected);
|
||||
assertThat(DB.find(EUserNoFkSoftDel.class).findCount()).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@
|
||||
<logger name="io.ebean.docker" level="TRACE"/>
|
||||
<!--<logger name="io.ebean.DDL" level="DEBUG"/>-->
|
||||
|
||||
<!-- <logger name="io.ebean.SQL" level="TRACE"/>-->
|
||||
<logger name="io.ebean.SQL" level="TRACE"/>
|
||||
<!-- <logger name="io.ebean.TXN" level="TRACE"/>-->
|
||||
<!-- <logger name="io.ebean.SUM" level="TRACE"/>-->
|
||||
|
||||
|
||||
Reference in New Issue
Block a user