Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.vertx.sqlclient.impl.RowInternal;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.time.*;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -77,6 +78,8 @@ public <T> T get(Class<T> type, int position) {
return type.cast(getArrayOfFloats(position));
} else if (componentType == Double.class) {
return type.cast(getArrayOfDoubles(position));
} else if (componentType == BigDecimal.class) {
return type.cast(getArrayOfBigDecimals(position));
} else if (componentType == String.class) {
return type.cast(getArrayOfStrings(position));
} else if (componentType == Buffer.class) {
Expand Down Expand Up @@ -131,6 +134,8 @@ public <T> T get(Class<T> type, int position) {
return type.cast(getFloat(position));
} else if (type == Double.class) {
return type.cast(getDouble(position));
} else if (type == BigDecimal.class) {
return type.cast(getBigDecimal(position));
} else if (type == Numeric.class) {
return type.cast(getNumeric(position));
} else if (type == String.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ protected <T> void testGeneric(TestContext ctx, String sql, List<Tuple> batch, T
}));
}

protected <T> void testGetter(TestContext ctx, String sql, List<Tuple> batch, T[] expected, BiFunction<Row, Integer, T> getter) {
Async async = ctx.async();
PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(conn -> {
conn
.preparedQuery(sql)
.executeBatch(batch)
.onComplete(ctx.asyncAssertSuccess(result -> {
for (T n : expected) {
ctx.assertEquals(result.size(), 1);
Iterator<Row> it = result.iterator();
Row row = it.next();
compare(ctx, n, getter.apply(row, 0));
result = result.next();
}
ctx.assertNull(result);
async.complete();
}));
}));
}

protected <T> void testDecode(TestContext ctx, String sql, BiFunction<Row, Integer, T> getter, T... expected) {
Async async = ctx.async();
PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.math.BigDecimal;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class NumericTypesExtendedCodecTest extends ExtendedQueryDataTypeCodecTestBase {
@Test
Expand Down Expand Up @@ -471,6 +473,26 @@ public void testNumericArray(TestContext ctx) {
}
*/

@Test
public void testNumericToBigDecimalRowConverter(TestContext ctx) {
BigDecimal[] expected = {new BigDecimal("2.22")};
testGetter(ctx,
"SELECT c FROM (VALUES ($1 :: NUMERIC)) AS t (c)",
Stream.of(expected).map(Tuple::of).collect(Collectors.toList()),
expected,
(row, index) -> row.get(BigDecimal.class, index));
}

@Test
public void testNumericArrayToBigDecimalArrayRowConverter(TestContext ctx) {
BigDecimal[] expected = {new BigDecimal("2.22"), new BigDecimal("3.33")};
testGetter(ctx,
"SELECT c FROM (VALUES ($1 :: NUMERIC[])) AS t (c)",
Collections.singletonList(Tuple.tuple().addValue(expected)),
new BigDecimal[][]{expected},
(row, index) -> row.get(BigDecimal[].class, index));
}

@Test
public void testShortArray(TestContext ctx) {
testGeneric(ctx,
Expand Down
20 changes: 19 additions & 1 deletion vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,25 @@ default UUID[] getArrayOfUUIDs(int pos) {
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default BigDecimal[] getArrayOfBigDecimals(int pos) {
return (BigDecimal[]) getValue(pos);
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof BigDecimal[]) {
return (BigDecimal[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
BigDecimal[] arr = new BigDecimal[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = new BigDecimal(elt.toString());
}
}
return arr;
} else {
return (BigDecimal[]) val; // Throw CCE
}
}

/**
Expand Down