Skip to content

Commit 01966c4

Browse files
authored
Merge pull request #136 from ilyshkafox/Fix_write_big_integer
Fix Write BigInteger
2 parents f459efe + f06b50d commit 01966c4

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

PgBulkInsert/src/main/java/de/bytefish/pgbulkinsert/pgsql/handlers/BigDecimalValueHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ public int getLength(final T value) {
5555
}
5656

5757
private static BigDecimal getNumericAsBigDecimal(final Number source) {
58-
if (!(source instanceof BigDecimal)) {
59-
return BigDecimalUtils.toBigDecimal(source.doubleValue());
58+
if (source instanceof BigDecimal) {
59+
return (BigDecimal) source;
6060
}
61-
62-
return (BigDecimal) source;
61+
if (source instanceof BigInteger) {
62+
return new BigDecimal((BigInteger) source);
63+
}
64+
return BigDecimalUtils.toBigDecimal(source.doubleValue());
6365
}
6466

6567
private List<Integer> digits(final BigDecimal value) {

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.postgresql.util.PGInterval;
1313

1414
import java.math.BigDecimal;
15+
import java.math.BigInteger;
1516
import java.net.Inet4Address;
1617
import java.net.Inet6Address;
1718
import java.net.UnknownHostException;
@@ -46,6 +47,7 @@ private static class SampleEntity {
4647
public List<Double> col_double_array;
4748
public String col_jsonb;
4849
public BigDecimal col_numeric;
50+
public BigInteger col_bignumeric;
4951
public Interval col_interval;
5052

5153
public Interval get_col_interval() {
@@ -124,6 +126,10 @@ public BigDecimal getCol_numeric() {
124126
return col_numeric;
125127
}
126128

129+
public BigInteger getCol_bignumeric() {
130+
return col_bignumeric;
131+
}
132+
127133
}
128134

129135
@Override
@@ -159,6 +165,7 @@ public SampleEntityMapping() {
159165
mapDoubleArray("col_double_array", SampleEntity::getCol_double_array);
160166
mapJsonb("col_jsonb", SampleEntity::getCol_jsonb);
161167
mapNumeric("col_numeric", SampleEntity::getCol_numeric);
168+
mapNumeric("col_bignumeric", SampleEntity::getCol_bignumeric);
162169
mapInterval("col_interval", SampleEntity::get_col_interval);
163170
}
164171
}
@@ -251,6 +258,32 @@ public void saveAll_numeric_Test() throws SQLException {
251258
}
252259
}
253260

261+
262+
@Test
263+
public void saveAll_BigNumeric_Test() throws SQLException {
264+
265+
// This list will be inserted.
266+
List<SampleEntity> entities = new ArrayList<>();
267+
268+
// Create the Entity to insert:
269+
SampleEntity entity = new SampleEntity();
270+
entity.col_bignumeric = new BigInteger("999999999999999999999999999999999999");
271+
272+
entities.add(entity);
273+
274+
PgBulkInsert<SampleEntity> pgBulkInsert = new PgBulkInsert<>(new SampleEntityMapping());
275+
276+
pgBulkInsert.saveAll(PostgreSqlUtils.getPGConnection(connection), entities.stream());
277+
278+
ResultSet rs = getAll();
279+
280+
while (rs.next()) {
281+
BigDecimal v = rs.getBigDecimal("col_bignumeric");
282+
283+
Assert.assertEquals(new BigInteger("999999999999999999999999999999999999"), v.toBigInteger());
284+
}
285+
}
286+
254287
@Test
255288
public void saveAll_boolean_Test() throws SQLException {
256289

@@ -645,7 +678,7 @@ public void saveAll_ByteArray_Test() throws SQLException {
645678
byte byte1 = 1;
646679
byte byte2 = 2;
647680

648-
entity.col_bytearray = new byte[]{ byte1, byte2 };
681+
entity.col_bytearray = new byte[]{byte1, byte2};
649682

650683
entities.add(entity);
651684

@@ -786,7 +819,8 @@ private boolean createTable() throws SQLException {
786819
" col_int_array integer[], \n" +
787820
" col_double_array double precision[], \n" +
788821
" col_jsonb jsonb, \n" +
789-
" col_numeric numeric(50, 20) \n" +
822+
" col_numeric numeric(50, 20), \n" +
823+
" col_bignumeric numeric(38) \n" +
790824
" );";
791825

792826
Statement statement = connection.createStatement();

PgBulkInsert/src/test/java/de/bytefish/pgbulkinsert/test/utils/TransactionalTestBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void setUp() throws Exception {
2929
schema = properties.getProperty("db.schema");
3030

3131
onSetUpBeforeTransaction();
32+
connection.createStatement().execute("SET timezone='UTC'");
3233
connection.setAutoCommit(false); // Start the Transaction:
3334
onSetUpInTransaction();
3435
}

0 commit comments

Comments
 (0)