Skip to content

Commit 8d78c63

Browse files
alexpoletaevtsegismont
authored andcommitted
Pg Client: BigDecimal support for reading numeric values (#1509)
* Fixes BigDecimal casting for the postgres client * Add tests. Fix row conversion for an array of BigDecimal elements Signed-off-by: Thomas Segismont <[email protected]>
1 parent a225c8a commit 8d78c63

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/RowImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.vertx.sqlclient.impl.RowInternal;
3030

3131
import java.lang.reflect.Array;
32+
import java.math.BigDecimal;
3233
import java.time.*;
3334
import java.util.List;
3435
import java.util.UUID;
@@ -77,6 +78,8 @@ public <T> T get(Class<T> type, int position) {
7778
return type.cast(getArrayOfFloats(position));
7879
} else if (componentType == Double.class) {
7980
return type.cast(getArrayOfDoubles(position));
81+
} else if (componentType == BigDecimal.class) {
82+
return type.cast(getArrayOfBigDecimals(position));
8083
} else if (componentType == String.class) {
8184
return type.cast(getArrayOfStrings(position));
8285
} else if (componentType == Buffer.class) {
@@ -131,6 +134,8 @@ public <T> T get(Class<T> type, int position) {
131134
return type.cast(getFloat(position));
132135
} else if (type == Double.class) {
133136
return type.cast(getDouble(position));
137+
} else if (type == BigDecimal.class) {
138+
return type.cast(getBigDecimal(position));
134139
} else if (type == Numeric.class) {
135140
return type.cast(getNumeric(position));
136141
} else if (type == String.class) {

vertx-pg-client/src/test/java/io/vertx/pgclient/data/ExtendedQueryDataTypeCodecTestBase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ protected <T> void testGeneric(TestContext ctx, String sql, List<Tuple> batch, T
6767
}));
6868
}
6969

70+
protected <T> void testGetter(TestContext ctx, String sql, List<Tuple> batch, T[] expected, BiFunction<Row, Integer, T> getter) {
71+
Async async = ctx.async();
72+
PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(conn -> {
73+
conn
74+
.preparedQuery(sql)
75+
.executeBatch(batch)
76+
.onComplete(ctx.asyncAssertSuccess(result -> {
77+
for (T n : expected) {
78+
ctx.assertEquals(result.size(), 1);
79+
Iterator<Row> it = result.iterator();
80+
Row row = it.next();
81+
compare(ctx, n, getter.apply(row, 0));
82+
result = result.next();
83+
}
84+
ctx.assertNull(result);
85+
async.complete();
86+
}));
87+
}));
88+
}
89+
7090
protected <T> void testDecode(TestContext ctx, String sql, BiFunction<Row, Integer, T> getter, T... expected) {
7191
Async async = ctx.async();
7292
PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {

vertx-pg-client/src/test/java/io/vertx/pgclient/data/NumericTypesExtendedCodecTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import java.math.BigDecimal;
1313
import java.util.Collections;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
1416

1517
public class NumericTypesExtendedCodecTest extends ExtendedQueryDataTypeCodecTestBase {
1618
@Test
@@ -471,6 +473,26 @@ public void testNumericArray(TestContext ctx) {
471473
}
472474
*/
473475

476+
@Test
477+
public void testNumericToBigDecimalRowConverter(TestContext ctx) {
478+
BigDecimal[] expected = {new BigDecimal("2.22")};
479+
testGetter(ctx,
480+
"SELECT c FROM (VALUES ($1 :: NUMERIC)) AS t (c)",
481+
Stream.of(expected).map(Tuple::of).collect(Collectors.toList()),
482+
expected,
483+
(row, index) -> row.get(BigDecimal.class, index));
484+
}
485+
486+
@Test
487+
public void testNumericArrayToBigDecimalArrayRowConverter(TestContext ctx) {
488+
BigDecimal[] expected = {new BigDecimal("2.22"), new BigDecimal("3.33")};
489+
testGetter(ctx,
490+
"SELECT c FROM (VALUES ($1 :: NUMERIC[])) AS t (c)",
491+
Collections.singletonList(Tuple.tuple().addValue(expected)),
492+
new BigDecimal[][]{expected},
493+
(row, index) -> row.get(BigDecimal[].class, index));
494+
}
495+
474496
@Test
475497
public void testShortArray(TestContext ctx) {
476498
testGeneric(ctx,

vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,25 @@ default UUID[] getArrayOfUUIDs(int pos) {
12871287
*/
12881288
@GenIgnore(GenIgnore.PERMITTED_TYPE)
12891289
default BigDecimal[] getArrayOfBigDecimals(int pos) {
1290-
return (BigDecimal[]) getValue(pos);
1290+
Object val = getValue(pos);
1291+
if (val == null) {
1292+
return null;
1293+
} else if (val instanceof BigDecimal[]) {
1294+
return (BigDecimal[]) val;
1295+
} else if (val instanceof Number[]) {
1296+
Number[] a = (Number[]) val;
1297+
int len = a.length;
1298+
BigDecimal[] arr = new BigDecimal[len];
1299+
for (int i = 0; i < len; i++) {
1300+
Number elt = a[i];
1301+
if (elt != null) {
1302+
arr[i] = new BigDecimal(elt.toString());
1303+
}
1304+
}
1305+
return arr;
1306+
} else {
1307+
return (BigDecimal[]) val; // Throw CCE
1308+
}
12911309
}
12921310

12931311
/**

0 commit comments

Comments
 (0)