Skip to content

Commit 4fec820

Browse files
committed
Add tests. Fix row conversion for an array of BigDecimal elements
1 parent 413b608 commit 4fec820

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public <T> T get(Class<T> type, int position) {
7575
return type.cast(getArrayOfFloats(position));
7676
} else if (componentType == Double.class) {
7777
return type.cast(getArrayOfDoubles(position));
78-
} else if (type == BigDecimal.class) {
78+
} else if (componentType == BigDecimal.class) {
7979
return type.cast(getArrayOfBigDecimals(position));
8080
} else if (componentType == String.class) {
8181
return type.cast(getArrayOfStrings(position));

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)