Skip to content

Commit 4785167

Browse files
committed
moved ValueConverters instance to static init of ArrayResultSet. Added wrapping try-catch for exception while convertions
1 parent e722fd9 commit 4785167

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class ArrayResultSet implements ResultSet {
4747

4848
private static final ClickHouseColumn INDEX_COLUMN = ClickHouseColumn.of("INDEX", ClickHouseDataType.UInt32, false, 0, 0);
4949
private static final String VALUE_COLUMN = "VALUE";
50+
private static final ValueConverters defaultValueConverters = new ValueConverters();
51+
5052
private int fetchDirection = ResultSet.FETCH_FORWARD;
5153
private int fetchSize = 0;
5254
private boolean wasNull = false;
@@ -71,14 +73,13 @@ public ArrayResultSet(Object array, ClickHouseColumn column) {
7173
, "", "", "", JdbcUtils.DATA_TYPE_CLASS_MAP);
7274
this.componentDataType = valueColumn.getDataType();
7375
this.defaultClass = JdbcUtils.DATA_TYPE_CLASS_MAP.get(componentDataType);
74-
ValueConverters converters = new ValueConverters();
75-
indexConverterMap = converters.getConvertersForType(Integer.class);
76+
indexConverterMap = defaultValueConverters.getConvertersForType(Integer.class);
7677
if (this.length > 0) {
7778
Class<?> itemClass = array.getClass().getComponentType();
7879
if (itemClass == null) {
7980
itemClass = java.lang.reflect.Array.get(array, 0).getClass();
8081
}
81-
converterMap = converters.getConvertersForType(itemClass);
82+
converterMap = defaultValueConverters.getConvertersForType(itemClass);
8283
} else {
8384
// empty array - no values to convert
8485
converterMap = null;
@@ -127,7 +128,12 @@ private Object getValueAsObject(int columnIndex, Class<?> type, Object defaultVa
127128
// if there is something to convert. type == Object.class means no conversion
128129
Function<Object, Object> converter = valueConverterMap.get(type);
129130
if (converter != null) {
130-
value = converter.apply(value);
131+
try {
132+
value = converter.apply(value);
133+
} catch (Exception e) {
134+
throw new SQLException("Failed to convert value of " + value.getClass() + " to " + type,
135+
ExceptionUtils.SQL_STATE_DATA_EXCEPTION, e);
136+
}
131137
} else {
132138
throw new SQLException("Value of " + value.getClass() + " cannot be converted to " + type);
133139
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,19 @@ void testStringToURL() throws Exception {
505505
assertEquals(rs.getString(stringColumn), rs.getObject(stringColumn, String.class));
506506
assertEquals(rs.getURL(stringColumn), rs.getObject(stringColumn, URL.class));
507507
}
508+
509+
@Test
510+
void testInvalidStringConverts() throws Exception {
511+
String[] array = {"abc"};
512+
ArrayResultSet rs = new ArrayResultSet(array, ClickHouseColumn.parse("v Array(String)").get(0));
513+
514+
final String stringColumn = rs.getMetaData().getColumnName(2);
515+
rs.next();
516+
Assert.assertThrows(SQLException.class, () -> rs.getByte(stringColumn));
517+
Assert.assertThrows(SQLException.class, () -> rs.getShort(stringColumn));
518+
Assert.assertThrows(SQLException.class, () -> rs.getInt(stringColumn));
519+
Assert.assertThrows(SQLException.class, () -> rs.getLong(stringColumn));
520+
Assert.assertThrows(SQLException.class, () -> rs.getFloat(stringColumn));
521+
Assert.assertThrows(SQLException.class, () -> rs.getDouble(stringColumn));
522+
}
508523
}

0 commit comments

Comments
 (0)