Skip to content

Commit fefa236

Browse files
authored
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
1 parent ac1814a commit fefa236

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
@@ -26,6 +26,7 @@
2626
import io.vertx.sqlclient.internal.RowDesc;
2727

2828
import java.lang.reflect.Array;
29+
import java.math.BigDecimal;
2930
import java.time.*;
3031
import java.util.List;
3132
import java.util.UUID;
@@ -74,6 +75,8 @@ public <T> T get(Class<T> type, int position) {
7475
return type.cast(getArrayOfFloats(position));
7576
} else if (componentType == Double.class) {
7677
return type.cast(getArrayOfDoubles(position));
78+
} else if (componentType == BigDecimal.class) {
79+
return type.cast(getArrayOfBigDecimals(position));
7780
} else if (componentType == String.class) {
7881
return type.cast(getArrayOfStrings(position));
7982
} else if (componentType == Buffer.class) {
@@ -128,6 +131,8 @@ public <T> T get(Class<T> type, int position) {
128131
return type.cast(getFloat(position));
129132
} else if (type == Double.class) {
130133
return type.cast(getDouble(position));
134+
} else if (type == BigDecimal.class) {
135+
return type.cast(getBigDecimal(position));
131136
} else if (type == Numeric.class) {
132137
return type.cast(getNumeric(position));
133138
} else if (type == String.class) {

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

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

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

vertx-pg-client/src/test/java/io/vertx/tests/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
@@ -513,6 +515,26 @@ public void testNumericArray(TestContext ctx) {
513515
}
514516
*/
515517

518+
@Test
519+
public void testNumericToBigDecimalRowConverter(TestContext ctx) {
520+
BigDecimal[] expected = {new BigDecimal("2.22")};
521+
testGetter(ctx,
522+
"SELECT c FROM (VALUES ($1 :: NUMERIC)) AS t (c)",
523+
Stream.of(expected).map(Tuple::of).collect(Collectors.toList()),
524+
expected,
525+
(row, index) -> row.get(BigDecimal.class, index));
526+
}
527+
528+
@Test
529+
public void testNumericArrayToBigDecimalArrayRowConverter(TestContext ctx) {
530+
BigDecimal[] expected = {new BigDecimal("2.22"), new BigDecimal("3.33")};
531+
testGetter(ctx,
532+
"SELECT c FROM (VALUES ($1 :: NUMERIC[])) AS t (c)",
533+
Collections.singletonList(Tuple.tuple().addValue(expected)),
534+
new BigDecimal[][]{expected},
535+
(row, index) -> row.get(BigDecimal[].class, index));
536+
}
537+
516538
@Test
517539
public void testShortArray(TestContext ctx) {
518540
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
@@ -1288,7 +1288,25 @@ default UUID[] getArrayOfUUIDs(int pos) {
12881288
*/
12891289
@GenIgnore(GenIgnore.PERMITTED_TYPE)
12901290
default BigDecimal[] getArrayOfBigDecimals(int pos) {
1291-
return (BigDecimal[]) getValue(pos);
1291+
Object val = getValue(pos);
1292+
if (val == null) {
1293+
return null;
1294+
} else if (val instanceof BigDecimal[]) {
1295+
return (BigDecimal[]) val;
1296+
} else if (val instanceof Number[]) {
1297+
Number[] a = (Number[]) val;
1298+
int len = a.length;
1299+
BigDecimal[] arr = new BigDecimal[len];
1300+
for (int i = 0; i < len; i++) {
1301+
Number elt = a[i];
1302+
if (elt != null) {
1303+
arr[i] = new BigDecimal(elt.toString());
1304+
}
1305+
}
1306+
return arr;
1307+
} else {
1308+
return (BigDecimal[]) val; // Throw CCE
1309+
}
12921310
}
12931311

12941312
/**

0 commit comments

Comments
 (0)