Skip to content

Commit c2fa5d8

Browse files
author
Paultagoras
committed
change-default-types
1 parent be52a87 commit c2fa5d8

File tree

3 files changed

+79
-29
lines changed

3 files changed

+79
-29
lines changed

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -608,18 +608,7 @@ public Object getObject(int columnIndex) throws SQLException {
608608

609609
@Override
610610
public Object getObject(String columnLabel) throws SQLException {
611-
checkClosed();
612-
try {
613-
if (reader.hasValue(columnLabel)) {
614-
wasNull = false;
615-
return reader.readValue(columnLabel);
616-
} else {
617-
wasNull = true;
618-
return null;
619-
}
620-
} catch (Exception e) {
621-
throw ExceptionUtils.toSqlState(String.format("SQL: [%s]; Method: getObject(%s)", parentStatement.getLastSql(), columnLabel), e);
622-
}
611+
return getObject(columnLabel, JdbcUtils.convertToJavaClass(getSchema().getColumnByName(columnLabel).getDataType()));
623612
}
624613

625614
@Override
@@ -1179,11 +1168,6 @@ public java.sql.Array getArray(int columnIndex) throws SQLException {
11791168
@Override
11801169
public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQLException {
11811170
checkClosed();
1182-
log.debug("getObject(columnLabel={}, map={})", columnLabel, map);
1183-
if (map == null) {
1184-
return getObject(columnLabel);
1185-
}
1186-
11871171
return getObject(columnLabel, map.get(JdbcUtils.convertToSqlType(getSchema().getColumnByName(columnLabel).getDataType()).getName()));
11881172
}
11891173

@@ -1647,7 +1631,17 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
16471631
public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
16481632
checkClosed();
16491633
try {
1650-
return (T) JdbcUtils.convert(getObject(columnLabel), type);
1634+
if (reader.hasValue(columnLabel)) {
1635+
wasNull = false;
1636+
if (type == null) {//As a fallback, try to get the value as is
1637+
return reader.readValue(columnLabel);
1638+
}
1639+
1640+
return (T) JdbcUtils.convert(reader.readValue(columnLabel), type);
1641+
} else {
1642+
wasNull = true;
1643+
return null;
1644+
}
16511645
} catch (Exception e) {
16521646
throw ExceptionUtils.toSqlState(e);
16531647
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.time.ZonedDateTime;
1414
import java.time.temporal.TemporalAccessor;
1515
import java.util.ArrayList;
16+
import java.util.HashMap;
1617
import java.util.List;
1718
import java.util.Map;
1819
import java.util.TreeMap;
@@ -53,6 +54,46 @@ private static Map<ClickHouseDataType, SQLType> generateTypeMap() {
5354
return map;
5455
}
5556

57+
private static final Map<SQLType, Class<?>> SQL_TYPE_TO_CLASS_MAP = generateClassMap();
58+
private static Map<SQLType, Class<?>> generateClassMap() {
59+
Map<SQLType, Class<?>> map = new HashMap<>();
60+
map.put(JDBCType.CHAR, String.class);
61+
map.put(JDBCType.VARCHAR, String.class);
62+
map.put(JDBCType.LONGVARCHAR, String.class);
63+
map.put(JDBCType.NUMERIC, java.math.BigDecimal.class);
64+
map.put(JDBCType.DECIMAL, java.math.BigDecimal.class);
65+
map.put(JDBCType.BIT, Boolean.class);
66+
map.put(JDBCType.BOOLEAN, Boolean.class);
67+
map.put(JDBCType.TINYINT, Integer.class);
68+
map.put(JDBCType.SMALLINT, Integer.class);
69+
map.put(JDBCType.INTEGER, Integer.class);
70+
map.put(JDBCType.BIGINT, Long.class);
71+
map.put(JDBCType.REAL, Float.class);
72+
map.put(JDBCType.FLOAT, Double.class);
73+
map.put(JDBCType.DOUBLE, Double.class);
74+
map.put(JDBCType.BINARY, byte[].class);
75+
map.put(JDBCType.VARBINARY, byte[].class);
76+
map.put(JDBCType.LONGVARBINARY, byte[].class);
77+
map.put(JDBCType.DATE, Date.class);
78+
map.put(JDBCType.TIME, java.sql.Time.class);
79+
map.put(JDBCType.TIMESTAMP, java.sql.Timestamp.class);
80+
map.put(JDBCType.TIME_WITH_TIMEZONE, java.sql.Time.class);
81+
map.put(JDBCType.TIMESTAMP_WITH_TIMEZONE, java.sql.Timestamp.class);
82+
map.put(JDBCType.CLOB, java.sql.Clob.class);
83+
map.put(JDBCType.BLOB, java.sql.Blob.class);
84+
map.put(JDBCType.ARRAY, java.sql.Array.class);
85+
map.put(JDBCType.STRUCT, java.sql.Struct.class);
86+
map.put(JDBCType.REF, java.sql.Ref.class);
87+
map.put(JDBCType.DATALINK, java.net.URL.class);
88+
map.put(JDBCType.ROWID, java.sql.RowId.class);
89+
map.put(JDBCType.NCHAR, String.class);
90+
map.put(JDBCType.NVARCHAR, String.class);
91+
map.put(JDBCType.LONGNVARCHAR, String.class);
92+
map.put(JDBCType.NCLOB, java.sql.NClob.class);
93+
map.put(JDBCType.SQLXML, java.sql.SQLXML.class);
94+
return map;
95+
}
96+
5697
public static SQLType convertToSqlType(ClickHouseDataType clickhouseType) {
5798
if (clickhouseType == null) {
5899
return JDBCType.NULL;
@@ -61,6 +102,10 @@ public static SQLType convertToSqlType(ClickHouseDataType clickhouseType) {
61102
return CLICKHOUSE_TO_SQL_TYPE_MAP.getOrDefault(clickhouseType, JDBCType.OTHER);
62103
}
63104

105+
public static Class<?> convertToJavaClass(ClickHouseDataType clickhouseType) {
106+
return SQL_TYPE_TO_CLASS_MAP.get(convertToSqlType(clickhouseType));
107+
}
108+
64109

65110
public static String generateSqlTypeEnum(String columnName) {
66111
StringBuilder sql = new StringBuilder("multiIf(");
@@ -157,6 +202,10 @@ public static Object convert(Object value, Class<?> type) throws SQLException {
157202
return Float.parseFloat(value.toString());
158203
} else if (type == Double.class || type == double.class) {
159204
return Double.parseDouble(value.toString());
205+
} else if (type == java.math.BigDecimal.class) {
206+
return new java.math.BigDecimal(value.toString());
207+
} else if (type == byte[].class) {
208+
return value.toString().getBytes();
160209
} else if (type == LocalDate.class && value instanceof TemporalAccessor) {
161210
return LocalDate.from((TemporalAccessor) value);
162211
} else if (type == LocalDateTime.class && value instanceof TemporalAccessor) {

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.time.LocalDateTime;
2424
import java.time.LocalTime;
2525
import java.time.OffsetDateTime;
26+
import java.time.ZoneId;
2627
import java.time.ZonedDateTime;
2728
import java.util.HashMap;
2829
import java.util.Map;
@@ -882,35 +883,41 @@ public void testTypeConversions() throws Exception {
882883
assertEquals(String.valueOf(rs.getObject(3, new HashMap<String, Class<?>>(){{put(JDBCType.FLOAT.getName(), Float.class);}})), "1.0");
883884

884885
assertEquals(rs.getDate(4), Date.valueOf("2024-12-01"));
885-
assertTrue(rs.getObject(4) instanceof ZonedDateTime);//ZonedDateTime
886-
assertEquals(String.valueOf(rs.getObject(4)), "2024-12-01T00:00Z[UTC]");//ZonedDateTime
886+
assertTrue(rs.getObject(4) instanceof Date);
887+
assertEquals(rs.getObject(4), Date.valueOf("2024-12-01"));
888+
assertEquals(rs.getString(4), "2024-12-01T00:00Z[UTC]");//Underlying object is ZonedDateTime
887889
assertEquals(rs.getObject(4, LocalDate.class), LocalDate.of(2024, 12, 1));
890+
assertEquals(rs.getObject(4, ZonedDateTime.class), ZonedDateTime.of(2024, 12, 1, 0, 0, 0, 0, ZoneId.of("UTC")));
888891
assertEquals(String.valueOf(rs.getObject(4, new HashMap<String, Class<?>>(){{put(JDBCType.DATE.getName(), LocalDate.class);}})), "2024-12-01");
889892

890893
assertEquals(rs.getTimestamp(5), Timestamp.valueOf("2024-12-01 12:34:56"));
891-
assertTrue(rs.getObject(5) instanceof ZonedDateTime);//ZonedDateTime
892-
assertEquals(String.valueOf(rs.getObject(5)), "2024-12-01T12:34:56Z[UTC]");//ZonedDateTime
894+
assertTrue(rs.getObject(5) instanceof Timestamp);
895+
assertEquals(rs.getObject(5), Timestamp.valueOf("2024-12-01 12:34:56"));
896+
assertEquals(rs.getString(5), "2024-12-01T12:34:56Z[UTC]");
893897
assertEquals(rs.getObject(5, LocalDateTime.class), LocalDateTime.of(2024, 12, 1, 12, 34, 56));
898+
assertEquals(rs.getObject(5, ZonedDateTime.class), ZonedDateTime.of(2024, 12, 1, 12, 34, 56, 0, ZoneId.of("UTC")));
894899
assertEquals(String.valueOf(rs.getObject(5, new HashMap<String, Class<?>>(){{put(JDBCType.TIMESTAMP_WITH_TIMEZONE.getName(), LocalDateTime.class);}})), "2024-12-01T12:34:56");
895900

896901
assertEquals(rs.getTimestamp(6), Timestamp.valueOf("2024-12-01 12:34:56.789"));
897-
assertTrue(rs.getObject(6) instanceof ZonedDateTime);//ZonedDateTime
898-
assertEquals(String.valueOf(rs.getObject(6)), "2024-12-01T12:34:56.789Z[UTC]");//ZonedDateTime
902+
assertTrue(rs.getObject(6) instanceof Timestamp);
903+
assertEquals(rs.getObject(6), Timestamp.valueOf("2024-12-01 12:34:56.789"));
904+
assertEquals(rs.getString(6), "2024-12-01T12:34:56.789Z[UTC]");
899905
assertEquals(rs.getObject(6, LocalDateTime.class), LocalDateTime.of(2024, 12, 1, 12, 34, 56, 789000000));
900906
assertEquals(String.valueOf(rs.getObject(6, new HashMap<String, Class<?>>(){{put(JDBCType.TIMESTAMP_WITH_TIMEZONE.getName(), LocalDateTime.class);}})), "2024-12-01T12:34:56.789");
901907

902908
assertEquals(rs.getTimestamp(7), Timestamp.valueOf("2024-12-01 12:34:56.789789"));
903-
assertTrue(rs.getObject(7) instanceof ZonedDateTime);//ZonedDateTime
904-
assertEquals(String.valueOf(rs.getObject(7)), "2024-12-01T12:34:56.789789Z[UTC]");//ZonedDateTime
909+
assertTrue(rs.getObject(7) instanceof Timestamp);
910+
assertEquals(rs.getObject(7), Timestamp.valueOf("2024-12-01 12:34:56.789789"));
911+
assertEquals(rs.getString(7), "2024-12-01T12:34:56.789789Z[UTC]");
905912
assertEquals(rs.getObject(7, LocalDateTime.class), LocalDateTime.of(2024, 12, 1, 12, 34, 56, 789789000));
906913
assertEquals(String.valueOf(rs.getObject(7, new HashMap<String, Class<?>>(){{put(JDBCType.TIMESTAMP_WITH_TIMEZONE.getName(), OffsetDateTime.class);}})), "2024-12-01T12:34:56.789789Z");
907914

908915
assertEquals(rs.getTimestamp(8), Timestamp.valueOf("2024-12-01 12:34:56.789789789"));
909-
assertTrue(rs.getObject(8) instanceof ZonedDateTime);//ZonedDateTime
910-
assertEquals(String.valueOf(rs.getObject(8)), "2024-12-01T12:34:56.789789789Z[UTC]");//ZonedDateTime
916+
assertTrue(rs.getObject(8) instanceof Timestamp);
917+
assertEquals(rs.getObject(8), Timestamp.valueOf("2024-12-01 12:34:56.789789789"));
918+
assertEquals(rs.getString(8), "2024-12-01T12:34:56.789789789Z[UTC]");
911919
assertEquals(rs.getObject(8, LocalDateTime.class), LocalDateTime.of(2024, 12, 1, 12, 34, 56, 789789789));
912920
assertEquals(String.valueOf(rs.getObject(8, new HashMap<String, Class<?>>(){{put(JDBCType.TIMESTAMP_WITH_TIMEZONE.getName(), ZonedDateTime.class);}})), "2024-12-01T12:34:56.789789789Z[UTC]");
913-
914921
}
915922
}
916923
}

0 commit comments

Comments
 (0)