Skip to content

Commit ce9d3a1

Browse files
author
Philipp Wagner
committed
Alternative Consructor
1 parent 18daa96 commit ce9d3a1

3 files changed

Lines changed: 54 additions & 7 deletions

File tree

PgBulkInsert/src/main/java/de/bytefish/pgbulkinsert/pgsql/handlers/utils/IntervalValueHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package de.bytefish.pgbulkinsert.pgsql.handlers.utils;
44

55
import de.bytefish.pgbulkinsert.pgsql.handlers.BaseValueHandler;
6-
import de.bytefish.pgbulkinsert.pgsql.model.geometric.Box;
76
import de.bytefish.pgbulkinsert.pgsql.model.interval.Interval;
87

98
import java.io.DataOutputStream;
@@ -14,7 +13,7 @@ public class IntervalValueHandler extends BaseValueHandler<Interval> {
1413
protected void internalHandle(DataOutputStream buffer, final Interval value) throws Exception {
1514
buffer.writeInt(16);
1615

17-
buffer.writeLong(value.getTime());
16+
buffer.writeLong(value.getTimeOfDay());
1817
buffer.writeInt(value.getDays());
1918
buffer.writeInt(value.getMonths());
2019
}
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package de.bytefish.pgbulkinsert.pgsql.model.interval;
22

3+
import java.util.concurrent.TimeUnit;
4+
35
public class Interval {
46

57
private final int months;
68
private final int days;
7-
private final long time;
9+
private final long timeOfDay;
810

9-
public Interval(int months, int days, long time) {
11+
public Interval(int months, int days, long timeOfDay) {
1012
this.months = months;
1113
this.days = days;
12-
this.time = time;
14+
this.timeOfDay = timeOfDay;
15+
}
16+
17+
public Interval(int months, int days, int hours, int minutes, int wholeSeconds, long microSeconds) {
18+
this.months = months;
19+
this.days = days;
20+
this.timeOfDay = calcTimeOfDay(hours, minutes, wholeSeconds, microSeconds);
1321
}
1422

1523
public int getMonths() {
@@ -20,7 +28,14 @@ public int getDays() {
2028
return days;
2129
}
2230

23-
public long getTime() {
24-
return time;
31+
public long getTimeOfDay() {
32+
return timeOfDay;
33+
}
34+
35+
private long calcTimeOfDay(int hours, int minutes, int wholeSeconds, long microSeconds) {
36+
return TimeUnit.HOURS.toMicros(hours)
37+
+ TimeUnit.MINUTES.toMicros(minutes)
38+
+ TimeUnit.SECONDS.toMicros(wholeSeconds)
39+
+ microSeconds;
2540
}
2641
}

PgBulkInsert/src/test/java/de/bytefish/pgbulkinsert/test/pgsql/handlers/PgBulkInsertTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,39 @@ public void saveAll_interval_Test() throws SQLException {
192192
}
193193
}
194194

195+
@Test
196+
public void saveAll_interval_AlternateConstructor_Test() throws SQLException {
197+
198+
// This list will be inserted.
199+
List<SampleEntity> entities = new ArrayList<>();
200+
201+
// Create the Entity to insert:
202+
SampleEntity entity = new SampleEntity();
203+
204+
// 2 mons 15 days 02:03:04.005
205+
entity.col_interval = new Interval(2, 15, 2, 3, 4, 5000);
206+
207+
entities.add(entity);
208+
209+
PgBulkInsert<SampleEntity> pgBulkInsert = new PgBulkInsert<>(new SampleEntityMapping());
210+
211+
pgBulkInsert.saveAll(PostgreSqlUtils.getPGConnection(connection), entities.stream());
212+
213+
ResultSet rs = getAll();
214+
215+
while (rs.next()) {
216+
PGInterval v = (PGInterval) rs.getObject("col_interval");
217+
218+
Assert.assertEquals(entity.col_interval.getDays(), v.getDays());
219+
Assert.assertEquals(entity.col_interval.getMonths(), v.getMonths());
220+
Assert.assertEquals(2, v.getHours());
221+
Assert.assertEquals(3, v.getMinutes());
222+
Assert.assertEquals(4, v.getWholeSeconds());
223+
Assert.assertEquals(5000, v.getMicroSeconds());
224+
}
225+
}
226+
227+
195228
@Test
196229
public void saveAll_numeric_Test() throws SQLException {
197230

0 commit comments

Comments
 (0)