Skip to content

Commit d27ac08

Browse files
authored
Merge pull request #2523 from ClickHouse/jdbc_connection_create_Array
[JDBC] `Connection#createArray` & `Connection#createStruct`
2 parents c5c4ece + c52c85b commit d27ac08

File tree

13 files changed

+884
-237
lines changed

13 files changed

+884
-237
lines changed

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReader.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.LinkedHashMap;
3333
import java.util.List;
3434
import java.util.Map;
35+
import java.util.Objects;
3536
import java.util.Set;
3637
import java.util.TimeZone;
3738
import java.util.UUID;
@@ -667,7 +668,7 @@ public static class ArrayValue {
667668

668669
int nextPos = 0;
669670

670-
ArrayValue(Class<?> itemType, int length) {
671+
public ArrayValue(Class<?> itemType, int length) {
671672
this.itemType = itemType;
672673
this.length = length;
673674

@@ -721,6 +722,34 @@ public synchronized <T> List<T> asList() {
721722
}
722723
return (List<T>) list;
723724
}
725+
726+
/**
727+
* Returns internal array. This method is only useful to work with array of primitives (int[], boolean[]).
728+
* Otherwise use {@link #getArrayOfObjects()}
729+
*
730+
* @return
731+
*/
732+
public Object getArray() {
733+
return array;
734+
}
735+
736+
/**
737+
* Returns array of objects.
738+
* If item type is primitive then all elements will be converted into objects.
739+
*
740+
* @return
741+
*/
742+
public Object[] getArrayOfObjects() {
743+
if (itemType.isPrimitive()) {
744+
Object[] result = new Object[length];
745+
for (int i = 0; i < length; i++) {
746+
result[i] = Array.get(array, i);
747+
}
748+
return result;
749+
} else {
750+
return (Object[]) array;
751+
}
752+
}
724753
}
725754

726755
public static class EnumValue extends Number {

client-v2/src/test/java/com/clickhouse/client/api/data_formats/internal/BinaryStreamReaderTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
55
import java.io.IOException;
6+
import java.lang.reflect.Array;
67
import java.time.ZoneId;
78
import java.time.ZonedDateTime;
89
import java.time.temporal.ChronoUnit;
10+
import java.util.Arrays;
911
import java.util.TimeZone;
1012

1113
import org.testng.Assert;
@@ -176,4 +178,16 @@ private Object[][] provideDateTimeTestData() {
176178
};
177179
}
178180

181+
@Test
182+
public void testArrayValue() throws Exception {
183+
BinaryStreamReader.ArrayValue array = new BinaryStreamReader.ArrayValue(int.class, 10);
184+
185+
for (int i = 0; i < array.length(); i++) {
186+
array.set(i, i);
187+
}
188+
189+
int[] array1 = (int[]) array.getArray();
190+
Object[] array2 = array.getArrayOfObjects();
191+
Assert.assertEquals(array1.length, array2.length);
192+
}
179193
}

0 commit comments

Comments
 (0)