Skip to content

Commit 2de7f0d

Browse files
committed
The Tuple interface does not the storage of primitive arrays, while we are not encouraging this (because it will lead to duplicate conversion) we should allow it.
Support conversion of primitive array to the type required by the tuple API.
1 parent ccf2d79 commit 2de7f0d

File tree

4 files changed

+108
-4
lines changed

4 files changed

+108
-4
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,24 @@ public void testDecodeBooleanArray_(TestContext ctx) {
6363

6464
@Test
6565
public void testEncodeBooleanArray(TestContext ctx) {
66+
testEncodeBooleanArray(ctx, Tuple.tuple()
67+
.addArrayOfBoolean(new Boolean[]{Boolean.FALSE, Boolean.TRUE})
68+
.addInteger(2));
69+
}
70+
71+
@Test
72+
public void testEncodePrimitiveBooleanArray(TestContext ctx) {
73+
testEncodeBooleanArray(ctx, Tuple.tuple()
74+
.addValue(new boolean[]{false, true})
75+
.addInteger(2));
76+
}
77+
78+
private void testEncodeBooleanArray(TestContext ctx, Tuple tuple) {
6679
Async async = ctx.async();
6780
PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
6881
conn.prepare("UPDATE \"ArrayDataType\" SET \"Boolean\" = $1 WHERE \"id\" = $2 RETURNING \"Boolean\"",
6982
ctx.asyncAssertSuccess(p -> {
70-
p.query().execute(Tuple.tuple()
71-
.addArrayOfBoolean(new Boolean[]{Boolean.FALSE, Boolean.TRUE})
72-
.addInteger(2)
83+
p.query().execute(tuple
7384
, ctx.asyncAssertSuccess(result -> {
7485
ColumnChecker.checkColumn(0, "Boolean")
7586
.returns(Tuple::getValue, Row::getValue, ColumnChecker.toObjectArray(new boolean[]{Boolean.FALSE, Boolean.TRUE}))

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ protected <T> void testGenericArray(TestContext ctx, String sql, T[][] expected,
4545
}
4646

4747
protected <T> void testGeneric(TestContext ctx, String sql, T[] expected, BiFunction<Row, Integer, T> getter) {
48+
testGeneric(ctx, sql, Stream.of(expected).map(Tuple::of).collect(Collectors.toList()), expected, getter);
49+
}
50+
51+
protected <T> void testGeneric(TestContext ctx, String sql, List<Tuple> batch, T[] expected, BiFunction<Row, Integer, T> getter) {
4852
Async async = ctx.async();
4953
PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
50-
List<Tuple> batch = Stream.of(expected).map(Tuple::of).collect(Collectors.toList());
5154
conn.preparedQuery(sql).executeBatch(batch,
5255
ctx.asyncAssertSuccess(result -> {
5356
for (T n : expected) {

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.junit.Test;
1111

1212
import java.math.BigDecimal;
13+
import java.util.Collections;
1314

1415
public class NumericTypesExtendedCodecTest extends ExtendedQueryDataTypeCodecTestBase {
1516
@Test
@@ -477,27 +478,74 @@ public void testShortArray(TestContext ctx) {
477478
new Short[][]{new Short[]{0, -10, null, Short.MAX_VALUE}}, Tuple::getArrayOfShorts);
478479
}
479480

481+
@Test
482+
public void testPrimitiveShortArray(TestContext ctx) {
483+
testGeneric(ctx,
484+
"SELECT c FROM (VALUES ($1 :: INT2[])) AS t (c)",
485+
Collections.singletonList(Tuple.tuple().addValue(new short[]{0, -10, Short.MAX_VALUE})),
486+
new Short[][]{new Short[]{0, -10, Short.MAX_VALUE}}, Tuple::getArrayOfShorts);
487+
}
488+
480489
@Test
481490
public void testIntegerArray(TestContext ctx) {
482491
testGeneric(ctx,
483492
"SELECT c FROM (VALUES ($1 :: INT4[])) AS t (c)",
484493
new Integer[][]{new Integer[]{0, -10, null, Integer.MAX_VALUE}}, Tuple::getArrayOfIntegers);
485494
}
486495

496+
@Test
497+
public void testPrimitiveIntegerArray(TestContext ctx) {
498+
testGeneric(ctx,
499+
"SELECT c FROM (VALUES ($1 :: INT4[])) AS t (c)",
500+
Collections.singletonList(Tuple.tuple().addValue(new int[]{0, -10, Integer.MAX_VALUE})),
501+
new Integer[][]{new Integer[]{0, -10, Integer.MAX_VALUE}}, Tuple::getArrayOfIntegers);
502+
}
503+
487504
@Test
488505
public void testLongArray(TestContext ctx) {
489506
testGeneric(ctx,
490507
"SELECT c FROM (VALUES ($1 :: INT8[])) AS t (c)",
491508
new Long[][]{new Long[]{0L, -10L, null, Long.MAX_VALUE}}, Tuple::getArrayOfLongs);
492509
}
493510

511+
@Test
512+
public void testPrimitiveLongArray(TestContext ctx) {
513+
testGeneric(ctx,
514+
"SELECT c FROM (VALUES ($1 :: INT8[])) AS t (c)",
515+
Collections.singletonList(Tuple.tuple().addValue(new long[]{0L, -10L, Long.MAX_VALUE})),
516+
new Long[][]{new Long[]{0L, -10L, Long.MAX_VALUE}}, Tuple::getArrayOfLongs);
517+
}
518+
494519
@Test
495520
public void testFloatArray(TestContext ctx) {
496521
testGeneric(ctx,
497522
"SELECT c FROM (VALUES ($1 :: FLOAT4[])) AS t (c)",
498523
new Float[][]{new Float[]{0f, -10f, Float.MAX_VALUE}}, Tuple::getArrayOfFloats);
499524
}
500525

526+
@Test
527+
public void testPrimitiveFloatArray(TestContext ctx) {
528+
testGeneric(ctx,
529+
"SELECT c FROM (VALUES ($1 :: FLOAT4[])) AS t (c)",
530+
Collections.singletonList(Tuple.tuple().addValue(new float[]{0f, -10f, Float.MAX_VALUE})),
531+
new Float[][]{new Float[]{0f, -10f, Float.MAX_VALUE}}, Tuple::getArrayOfFloats);
532+
}
533+
534+
@Test
535+
public void testDoubleArray(TestContext ctx) {
536+
testGeneric(ctx,
537+
"SELECT c FROM (VALUES ($1 :: FLOAT8[])) AS t (c)",
538+
new Double[][]{new Double[]{0d, -10d, Double.MAX_VALUE}}, Tuple::getArrayOfDoubles);
539+
}
540+
541+
@Test
542+
public void testPrimitiveDoubleArray(TestContext ctx) {
543+
testGeneric(ctx,
544+
"SELECT c FROM (VALUES ($1 :: FLOAT8[])) AS t (c)",
545+
Collections.singletonList(Tuple.tuple().addValue(new double[]{0d, -10d, Double.MAX_VALUE})),
546+
new Double[][]{new Double[]{0d, -10d, Double.MAX_VALUE}}, Tuple::getArrayOfDoubles);
547+
}
548+
501549
@Test
502550
public void testDecodeShortArray(TestContext ctx) {
503551
Async async = ctx.async();

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,13 @@ default Boolean[] getArrayOfBooleans(int pos) {
649649
return null;
650650
} else if (val instanceof Boolean[]) {
651651
return (Boolean[]) val;
652+
} else if (val instanceof boolean[]) {
653+
boolean[] array = (boolean[]) val;
654+
Boolean[] booleanArray = new Boolean[array.length];
655+
for (int i = 0;i < array.length;i++) {
656+
booleanArray[i] = array[i];
657+
}
658+
return booleanArray;
652659
} else if (val.getClass() == Object[].class) {
653660
Object[] array = (Object[]) val;
654661
Boolean[] booleanArray = new Boolean[array.length];
@@ -677,6 +684,13 @@ default Short[] getArrayOfShorts(int pos) {
677684
return null;
678685
} else if (val instanceof Short[]) {
679686
return (Short[]) val;
687+
} else if (val instanceof short[]) {
688+
short[] array = (short[]) val;
689+
Short[] a = new Short[array.length];
690+
for (int i = 0;i < array.length;i++) {
691+
a[i] = array[i];
692+
}
693+
return a;
680694
} else if (val instanceof Number[]) {
681695
Number[] a = (Number[]) val;
682696
int len = a.length;
@@ -727,6 +741,13 @@ default Integer[] getArrayOfIntegers(int pos) {
727741
return null;
728742
} else if (val instanceof Integer[]) {
729743
return (Integer[]) val;
744+
} else if (val instanceof int[]) {
745+
int[] array = (int[]) val;
746+
Integer[] a = new Integer[array.length];
747+
for (int i = 0;i < array.length;i++) {
748+
a[i] = array[i];
749+
}
750+
return a;
730751
} else if (val instanceof Number[]) {
731752
Number[] a = (Number[]) val;
732753
int len = a.length;
@@ -777,6 +798,13 @@ default Long[] getArrayOfLongs(int pos) {
777798
return null;
778799
} else if (val instanceof Long[]) {
779800
return (Long[]) val;
801+
} else if (val instanceof long[]) {
802+
long[] array = (long[]) val;
803+
Long[] a = new Long[array.length];
804+
for (int i = 0;i < array.length;i++) {
805+
a[i] = array[i];
806+
}
807+
return a;
780808
} else if (val instanceof Number[]) {
781809
Number[] a = (Number[]) val;
782810
int len = a.length;
@@ -827,6 +855,13 @@ default Float[] getArrayOfFloats(int pos) {
827855
return null;
828856
} else if (val instanceof Float[]) {
829857
return (Float[]) val;
858+
} else if (val instanceof float[]) {
859+
float[] array = (float[]) val;
860+
Float[] a = new Float[array.length];
861+
for (int i = 0;i < array.length;i++) {
862+
a[i] = array[i];
863+
}
864+
return a;
830865
} else if (val instanceof Number[]) {
831866
Number[] a = (Number[]) val;
832867
int len = a.length;
@@ -877,6 +912,13 @@ default Double[] getArrayOfDoubles(int pos) {
877912
return null;
878913
} else if (val instanceof Double[]) {
879914
return (Double[]) val;
915+
} else if (val instanceof double[]) {
916+
double[] array = (double[]) val;
917+
Double[] a = new Double[array.length];
918+
for (int i = 0;i < array.length;i++) {
919+
a[i] = array[i];
920+
}
921+
return a;
880922
} else if (val instanceof Number[]) {
881923
Number[] a = (Number[]) val;
882924
int len = a.length;

0 commit comments

Comments
 (0)