Skip to content

Commit 5fd504d

Browse files
author
Paultagoras
committed
Adjust conversion for subclass
1 parent 36d52d7 commit 5fd504d

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ public java.sql.Clob getClob(int columnIndex) throws SQLException {
10061006

10071007
@Override
10081008
public java.sql.Array getArray(int columnIndex) throws SQLException {
1009-
return getArray(columnIndexToName(columnIndex));
1009+
return getObject(columnIndex, java.sql.Array.class);
10101010
}
10111011

10121012
@Override
@@ -1047,17 +1047,7 @@ public Clob getClob(String columnLabel) throws SQLException {
10471047

10481048
@Override
10491049
public java.sql.Array getArray(String columnLabel) throws SQLException {
1050-
checkClosed();
1051-
try {
1052-
ClickHouseColumn column = getSchema().getColumnByName(columnLabel);
1053-
List<Object> lstObj = reader.getList(columnLabel);
1054-
wasNull = lstObj == null;
1055-
return new Array(lstObj,
1056-
column.getArrayBaseColumn().getDataType().name(),
1057-
JdbcUtils.convertToSqlType(column.getArrayBaseColumn().getDataType()).getVendorTypeNumber());
1058-
} catch (Exception e) {
1059-
throw ExceptionUtils.toSqlState(String.format("Method: getArray(\"%s\") encountered an exception.", columnLabel), String.format("SQL: [%s]", parentStatement.getLastStatementSql()), e);
1060-
}
1050+
return getObject(columnLabel, java.sql.Array.class);
10611051
}
10621052

10631053
@Override
@@ -1530,7 +1520,7 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
15301520
return reader.readValue(columnIndex);
15311521
}
15321522

1533-
return (T) JdbcUtils.convert(reader.readValue(columnIndex), type);
1523+
return (T) JdbcUtils.convert(reader.readValue(columnIndex), type, getSchema().getColumnByIndex(columnIndex));
15341524
} else {
15351525
wasNull = true;
15361526
return null;
@@ -1552,7 +1542,7 @@ public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
15521542
return reader.readValue(columnLabel);
15531543
}
15541544

1555-
return (T) JdbcUtils.convert(reader.readValue(columnLabel), type);
1545+
return (T) JdbcUtils.convert(reader.readValue(columnLabel), type, getSchema().getColumnByName(columnLabel));
15561546
} else {
15571547
wasNull = true;
15581548
return null;

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.clickhouse.jdbc.internal;
22

33
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
4+
import com.clickhouse.client.api.metadata.TableSchema;
5+
import com.clickhouse.data.ClickHouseColumn;
46
import com.clickhouse.data.ClickHouseDataType;
57
import com.clickhouse.jdbc.types.Array;
68
import com.google.common.collect.ImmutableMap;
@@ -17,8 +19,10 @@
1719
import java.time.OffsetDateTime;
1820
import java.time.ZonedDateTime;
1921
import java.time.temporal.TemporalAccessor;
22+
import java.util.ArrayList;
2023
import java.util.Collections;
2124
import java.util.HashMap;
25+
import java.util.List;
2226
import java.util.Map;
2327
import java.util.TreeMap;
2428

@@ -131,6 +135,10 @@ public static Class<?> convertToJavaClass(ClickHouseDataType clickhouseType) {
131135
}
132136

133137
public static Object convert(Object value, Class<?> type) throws SQLException {
138+
return convert(value, type, null);
139+
}
140+
141+
public static Object convert(Object value, Class<?> type, ClickHouseColumn column) throws SQLException {
134142
if (value == null || type == null) {
135143
return value;
136144
}
@@ -172,6 +180,9 @@ public static Object convert(Object value, Class<?> type) throws SQLException {
172180
} else if (type == java.sql.Time.class && value instanceof TemporalAccessor) {
173181
return java.sql.Time.valueOf(LocalTime.from((TemporalAccessor) value));
174182
} else if (type == java.sql.Array.class && value instanceof BinaryStreamReader.ArrayValue) {//It's cleaner to use getList but this handles the more generic getObject
183+
if (column != null) {
184+
return new Array(convertList(((BinaryStreamReader.ArrayValue) value).asList(), JdbcUtils.convertToJavaClass(column.getArrayBaseColumn().getDataType())), "Object", JDBCType.JAVA_OBJECT.getVendorTypeNumber());
185+
}
175186
return new Array(((BinaryStreamReader.ArrayValue) value).asList(), "Object", JDBCType.JAVA_OBJECT.getVendorTypeNumber());
176187
} else if (type == Inet4Address.class && value instanceof Inet6Address) {
177188
// Convert Inet6Address to Inet4Address
@@ -186,4 +197,16 @@ public static Object convert(Object value, Class<?> type) throws SQLException {
186197

187198
throw new SQLException("Unsupported conversion from " + value.getClass().getName() + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION);
188199
}
200+
201+
public static List<Object> convertList(List<Object> values, Class<?> type) throws SQLException {
202+
if (values == null || type == null) {
203+
return values;
204+
}
205+
206+
List<Object> convertedValues = new ArrayList<>(values.size());
207+
for (Object value : values) {
208+
convertedValues.add(convert(value, type));
209+
}
210+
return convertedValues;
211+
}
189212
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ public void testBooleanTypes() throws SQLException {
676676
@Test(groups = { "integration" })
677677
public void testArrayTypes() throws SQLException {
678678
runQuery("CREATE TABLE test_arrays (order Int8, "
679-
+ "array Array(Int8), arraystr Array(String), arraytuple Array(Tuple(Int8, String))"
679+
+ "array Array(Int8), arraystr Array(String), arraytuple Array(Tuple(Int8, String)), arraydate Array(Date)"
680680
+ ") ENGINE = MergeTree ORDER BY ()");
681681

682682
// Insert random (valid) values
@@ -699,12 +699,18 @@ public void testArrayTypes() throws SQLException {
699699
arraytuple[i] = new Tuple(rand.nextInt(256) - 128, "string" + rand.nextInt(1000));
700700
}
701701

702+
Date[] arraydate = new Date[rand.nextInt(10)];
703+
for (int i = 0; i < arraydate.length; i++) {
704+
arraydate[i] = Date.valueOf(LocalDate.now().plusDays(rand.nextInt(100)));
705+
}
706+
702707
// Insert random (valid) values
703708
try (Connection conn = getConnection()) {
704-
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_arrays VALUES ( 1, ?, ?, ?)")) {
709+
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_arrays VALUES ( 1, ?, ?, ?, ?)")) {
705710
stmt.setArray(1, conn.createArrayOf("Int8", array));
706711
stmt.setArray(2, conn.createArrayOf("String", arraystr));
707712
stmt.setArray(3, conn.createArrayOf("Tuple", arraytuple));
713+
stmt.setArray(4, conn.createArrayOf("Date", arraydate));
708714
stmt.executeUpdate();
709715
}
710716
}
@@ -733,6 +739,12 @@ public void testArrayTypes() throws SQLException {
733739
assertEquals(String.valueOf(tupleResult.getValue(0)), String.valueOf(tuple.getValue(0)));
734740
assertEquals(String.valueOf(tupleResult.getValue(1)), String.valueOf(tuple.getValue(1)));
735741
}
742+
743+
Object[] arraydateResult = (Object[]) rs.getArray("arraydate").getArray();
744+
assertEquals(arraydateResult.length, arraydate.length);
745+
for (int i = 0; i < arraydate.length; i++) {
746+
assertEquals(String.valueOf(arraydateResult[i]), String.valueOf(arraydate[i]));
747+
}
736748
assertFalse(rs.next());
737749
}
738750
}
@@ -762,6 +774,12 @@ public void testArrayTypes() throws SQLException {
762774
assertEquals(String.valueOf(tupleResult.getValue(0)), String.valueOf(tuple.getValue(0)));
763775
assertEquals(String.valueOf(tupleResult.getValue(1)), String.valueOf(tuple.getValue(1)));
764776
}
777+
778+
Object[] arraydateResult = (Object[]) ((Array) rs.getObject("arraydate")).getArray();
779+
assertEquals(arraydateResult.length, arraydate.length);
780+
for (int i = 0; i < arraydate.length; i++) {
781+
assertEquals(String.valueOf(arraydateResult[i]), String.valueOf(arraydate[i]));
782+
}
765783
assertFalse(rs.next());
766784
}
767785
}

0 commit comments

Comments
 (0)