Skip to content

Commit da9eee0

Browse files
committed
AutoClosable for SimpleRowWriter, Update Dependencies in pom
1 parent f9e8118 commit da9eee0

5 files changed

Lines changed: 52 additions & 77 deletions

File tree

PgBulkInsert/pgbulkinsert-rowwriter/src/main/java/de/bytefish/pgbulkinsert/row/SimpleRowWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.util.function.Function;
1818
import java.util.stream.Collectors;
1919

20-
public class SimpleRowWriter {
20+
public class SimpleRowWriter implements AutoCloseable {
2121

2222
public static class Table {
2323

PgBulkInsert/pgbulkinsert-rowwriter/src/test/java/de/bytefish/pgbulkinsert/test/row/NullTerminatingStringTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ public void testWriterThrowsErrorForNullCharacter() throws SQLException {
3838

3939
try {
4040
// Create the Writer:
41-
SimpleRowWriter writer = new SimpleRowWriter(table, pgConnection);
41+
try(SimpleRowWriter writer = new SimpleRowWriter(table, pgConnection)) {
4242

43-
writer.startRow((row) -> {
44-
row.setText("value_text", "Hi\0");
45-
});
46-
47-
// ... and make sure to close it:
48-
writer.close();
43+
writer.startRow((row) -> {
44+
row.setText("value_text", "Hi\0");
45+
});
46+
}
4947
} catch(Exception e) {
5048
exceptionHasBeenThrown = true;
5149
}

PgBulkInsert/pgbulkinsert-rowwriter/src/test/java/de/bytefish/pgbulkinsert/test/row/SimpleRowWriterTest.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,22 @@ public void rowBasedWriterTest() throws SQLException {
4040
SimpleRowWriter.Table table = new SimpleRowWriter.Table(schema, tableName, columnNames);
4141

4242
// Create the Writer:
43-
SimpleRowWriter writer = new SimpleRowWriter(table, pgConnection);
44-
45-
// ... write your data rows:
46-
for(int rowIdx = 0; rowIdx < 10000; rowIdx++) {
47-
48-
// ... using startRow and work with the row, see how the order doesn't matter:
49-
writer.startRow((row) -> {
50-
row.setText("value_text", "Hi");
51-
row.setInteger("value_int", 1);
52-
row.setTsTzRange("value_range", new Range<>(
53-
ZonedDateTime.of(2020, 3, 1, 0, 0, 0, 0, ZoneId.of("GMT")),
54-
ZonedDateTime.of(2020, 3, 1, 0, 0, 0, 0, ZoneId.of("GMT"))));
55-
});
43+
try(SimpleRowWriter writer = new SimpleRowWriter(table, pgConnection)) {
44+
45+
// ... write your data rows:
46+
for (int rowIdx = 0; rowIdx < 10000; rowIdx++) {
47+
48+
// ... using startRow and work with the row, see how the order doesn't matter:
49+
writer.startRow((row) -> {
50+
row.setText("value_text", "Hi");
51+
row.setInteger("value_int", 1);
52+
row.setTsTzRange("value_range", new Range<>(
53+
ZonedDateTime.of(2020, 3, 1, 0, 0, 0, 0, ZoneId.of("GMT")),
54+
ZonedDateTime.of(2020, 3, 1, 0, 0, 0, 0, ZoneId.of("GMT"))));
55+
});
56+
}
5657
}
5758

58-
// ... and make sure to close it:
59-
writer.close();
60-
6159
// Now assert, that we have written 10000 entities:
6260

6361
Assert.assertEquals(10000, getRowCount());

PgBulkInsert/pgbulkinsert-rowwriter/src/test/java/de/bytefish/pgbulkinsert/test/row/SimpleRowWriterWithQuotesTest.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,20 @@ public void rowBasedWriterTest() throws SQLException {
3636
SimpleRowWriter.Table table = new SimpleRowWriter.Table(schema, tableName, columnNames);
3737

3838
// Create the Writer:
39-
SimpleRowWriter writer = new SimpleRowWriter(table, pgConnection, true);
39+
try(SimpleRowWriter writer = new SimpleRowWriter(table, pgConnection, true)) {
4040

41-
// ... write your data rows:
42-
for(int rowIdx = 0; rowIdx < 10000; rowIdx++) {
43-
44-
// ... using startRow and work with the row, see how the order doesn't matter:
45-
writer.startRow((row) -> {
46-
row.setText("value_text", "Hi");
47-
row.setInteger("Value_int", 1);
48-
});
41+
// ... write your data rows:
42+
for (int rowIdx = 0; rowIdx < 10000; rowIdx++) {
4943

44+
// ... using startRow and work with the row, see how the order doesn't matter:
45+
writer.startRow((row) -> {
46+
row.setText("value_text", "Hi");
47+
row.setInteger("Value_int", 1);
48+
});
49+
}
5050
}
5151

52-
// ... and make sure to close it:
53-
writer.close();
54-
5552
// Now assert, that we have written 10000 entities:
56-
5753
Assert.assertEquals(10000, getRowCount());
5854
}
5955

README.md

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -112,42 +112,30 @@ String[] columnNames = new String[] {
112112

113113
// Create the Table Definition:
114114
SimpleRowWriter.Table table = new SimpleRowWriter.Table(schemaName, tableName, columnNames);
115-
116-
// Create the Writer:
117-
SimpleRowWriter writer = new SimpleRowWriter(table);
118115
```
119116

120-
Once created you are required to open the ``SimpleRowWriter`` by yourself using a ``PGConnection``:
117+
Once created you create the ``SimpleRowWriter`` by using the ``Table`` and a ``PGConnection``.
121118

122-
```java
123-
// ... open it:
124-
writer.open(pgConnection);
125-
```
126-
127-
To write a row to PostgreSQL you call the ``startRow`` method. It expects you to pass a ``Consumer<SimpleRow>`` into it,
128-
which defines what data to write to the row. The call to ``startRow`` is synchronized, so it is safe to be called from
129-
multiple threads.
119+
Now to write a row to PostgreSQL you call the ``startRow`` method. It expects you to pass a
120+
``Consumer<SimpleRow>`` into it, which defines what data to write to the row. The call to
121+
``startRow`` is synchronized, so it is safe to be called from multiple threads.
130122

131123
```java
132-
// ... write your data rows:
133-
for(int rowIdx = 0; rowIdx < 10000; rowIdx++) {
124+
// Create the Writer:
125+
try(SimpleRowWriter writer = new SimpleRowWriter(table, pgConnection)) {
134126

135-
// ... using startRow and work with the row, see how the order doesn't matter:
136-
writer.startRow((row) -> {
137-
row.setText("value_text", "Hi");
138-
row.setInteger("value_int", 1);
139-
});
127+
// ... write your data rows:
128+
for(int rowIdx = 0; rowIdx < 10000; rowIdx++) {
140129

130+
// ... using startRow and work with the row, see how the order doesn't matter:
131+
writer.startRow((row) -> {
132+
row.setText("value_text", "Hi");
133+
row.setInteger("value_int", 1);
134+
});
135+
}
141136
}
142137
```
143138

144-
And to finish the COPY operation, you need to close the ``SimpleRowWriter``:
145-
146-
```java
147-
// ... and make sure to close it:
148-
writer.close();
149-
```
150-
151139
So the complete example looks like this:
152140

153141
```java
@@ -177,25 +165,20 @@ public class SimpleRowWriterTest extends TransactionalTestBase {
177165
SimpleRowWriter.Table table = new SimpleRowWriter.Table(schemaName, tableName, columnNames);
178166

179167
// Create the Writer:
180-
SimpleRowWriter writer = new SimpleRowWriter(table);
181-
182-
// ... open it:
183-
writer.open(pgConnection);
168+
try(SimpleRowWriter writer = new SimpleRowWriter(table, pgConnection)) {
184169

185-
// ... write your data rows:
186-
for(int rowIdx = 0; rowIdx < 10000; rowIdx++) {
170+
// ... write your data rows:
171+
for(int rowIdx = 0; rowIdx < 10000; rowIdx++) {
187172

188-
// ... using startRow and work with the row, see how the order doesn't matter:
189-
writer.startRow((row) -> {
190-
row.setText("value_text", "Hi");
191-
row.setInteger("value_int", 1);
192-
});
173+
// ... using startRow and work with the row, see how the order doesn't matter:
174+
writer.startRow((row) -> {
175+
row.setText("value_text", "Hi");
176+
row.setInteger("value_int", 1);
177+
});
193178

179+
}
194180
}
195181

196-
// ... and make sure to close it:
197-
writer.close();
198-
199182
// Now assert, that we have written 10000 entities:
200183

201184
Assert.assertEquals(10000, getRowCount());

0 commit comments

Comments
 (0)