Skip to content

Commit 95d2fbf

Browse files
authored
* #67: Add Table#truncate() (#74)
* #67: Add Table#truncate()
1 parent db1a670 commit 95d2fbf

File tree

7 files changed

+50
-1
lines changed

7 files changed

+50
-1
lines changed

doc/changes/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changes
22

3+
* [3.2.0](changes_3.2.0.md)
34
* [3.1.1](changes_3.1.1.md)
45
* [3.1.0](changes_3.1.0.md)
56
* [3.0.0](changes_3.0.0.md)

doc/changes/changes_3.2.0.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Test Database Builder 3.2.0, released 2021-03-XX
2+
3+
Code name: Truncating tables
4+
5+
## Summary
6+
7+
In this release we added support for truncating tables.
8+
9+
## Features
10+
11+
* #67: Add Table#truncate()

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.exasol</groupId>
66
<artifactId>test-db-builder-java</artifactId>
7-
<version>3.1.1</version>
7+
<version>3.2.0</version>
88
<name>Test Database Builder for Java</name>
99
<description>pom.xml</description>
1010
<url>https://github.com/exasol/test-db-builder</url>

src/main/java/com/exasol/dbbuilder/dialects/AbstractImmediateDatabaseObjectWriter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public void write(final Table table) {
5858
writeToObject(table, builder.toString());
5959
}
6060

61+
@Override
62+
public void truncate(final Table table) {
63+
writeToObject(table, "TRUNCATE TABLE " + table.getFullyQualifiedName());
64+
}
65+
6166
/**
6267
* Get a quoted column name.
6368
*

src/main/java/com/exasol/dbbuilder/dialects/DatabaseObjectWriter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public interface DatabaseObjectWriter {
3030
*/
3131
void write(final Table table, final Stream<List<Object>> rows);
3232

33+
/**
34+
* Truncate a table.
35+
*
36+
* @param table table to truncate
37+
*/
38+
void truncate(Table table);
39+
3340
/**
3441
* Create a user in the database.
3542
*

src/main/java/com/exasol/dbbuilder/dialects/Table.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ public Table insert(final Object... values) {
7373
return this;
7474
}
7575

76+
/**
77+
* Remove all rows from this table.
78+
*/
79+
public void truncate() {
80+
this.writer.truncate(this);
81+
}
82+
7683
/**
7784
* Insert multiple rows at once. Compared to inserting each row using {@link #insert(Object...)} this is a lot
7885
* faster.

src/test/java/com/exasol/dbbuilder/dialects/AbstractDatabaseObjectCreationAndDeletionIT.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.exasol.dbbuilder.dialects;
22

3+
import static org.hamcrest.CoreMatchers.equalTo;
34
import static org.hamcrest.CoreMatchers.not;
45
import static org.hamcrest.MatcherAssert.assertThat;
56
import static org.hamcrest.Matchers.empty;
@@ -85,6 +86,23 @@ void testDropTable() {
8586
assertThat(table, not(existsInDatabase()));
8687
}
8788

89+
@Test
90+
void testTruncateTable() throws SQLException {
91+
final Schema schema = this.factory.createSchema("PARENT_SCHEMA_FOR_TRUNCATE_TABLE");
92+
final Table table = schema.createTable("MY_TABLE", "COL1", "INTEGER").insert(1);
93+
table.truncate();
94+
assertThat(getTableSize(table), equalTo(0));
95+
}
96+
97+
private int getTableSize(final Table table) throws SQLException {
98+
try (final Statement statement = getAdminConnection().createStatement();
99+
final ResultSet resultSet = statement
100+
.executeQuery("SELECT COUNT(*) FROM " + table.getFullyQualifiedName())) {
101+
resultSet.next();
102+
return resultSet.getInt(1);
103+
}
104+
}
105+
88106
@Test
89107
protected void testCreateTableIsSqlInjectionSafe() {
90108
final Table table = this.factory.createSchema("INJECTION_TEST").createTable("INJECTION_TEST_TABLE " + QUOTES,

0 commit comments

Comments
 (0)