Skip to content

Commit 007ae6c

Browse files
committed
fixed ip address conversion in JDBC. added more test for covner routine
1 parent f5d3281 commit 007ae6c

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ public static class ArrayValue {
668668

669669
int nextPos = 0;
670670

671-
ArrayValue(Class<?> itemType, int length) {
671+
public ArrayValue(Class<?> itemType, int length) {
672672
this.itemType = itemType;
673673
this.length = length;
674674

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

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

33
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
4+
import com.clickhouse.client.api.data_formats.internal.InetAddressConverter;
45
import com.clickhouse.data.ClickHouseColumn;
56
import com.clickhouse.data.ClickHouseDataType;
67
import com.clickhouse.data.Tuple;
8+
import com.clickhouse.data.format.BinaryStreamUtils;
79
import com.clickhouse.jdbc.types.Array;
810
import com.google.common.collect.ImmutableMap;
911

1012
import java.awt.*;
1113
import java.math.BigInteger;
1214
import java.net.Inet4Address;
1315
import java.net.Inet6Address;
16+
import java.net.InetAddress;
1417
import java.sql.Date;
1518
import java.sql.JDBCType;
1619
import java.sql.SQLException;
@@ -280,15 +283,15 @@ public static Object convert(Object value, Class<?> type, ClickHouseColumn colum
280283
return new Array(((List<?>) value).toArray(), "Unknown", JDBCType.OTHER.getVendorTypeNumber());
281284
} else if (type == Inet4Address.class && value instanceof Inet6Address) {
282285
// Convert Inet6Address to Inet4Address
283-
return Inet4Address.getByName(value.toString());
286+
return InetAddressConverter.convertToIpv4((InetAddress) value);
284287
} else if (type == Inet6Address.class && value instanceof Inet4Address) {
285288
// Convert Inet4Address to Inet6Address
286-
return Inet6Address.getByName(value.toString());
289+
return InetAddressConverter.convertToIpv6((InetAddress) value);
287290
} else if (type == Tuple.class && value.getClass().isArray()) {
288291
return new Tuple(true, value);
289292
}
290293
} catch (Exception e) {
291-
throw new SQLException("Failed to convert " + value + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION);
294+
throw new SQLException("Failed to convert " + value + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION, e);
292295
}
293296

294297
throw new SQLException("Unsupported conversion from " + value.getClass().getName() + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.clickhouse.client.api.ClientConfigProperties;
44
import com.clickhouse.client.api.DataTypeUtils;
5+
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
56
import com.clickhouse.client.api.internal.ServerSettings;
67
import com.clickhouse.data.ClickHouseVersion;
78
import com.clickhouse.data.Tuple;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
11+
import org.testng.Assert;
1012
import org.testng.annotations.BeforeClass;
1113
import org.testng.annotations.Test;
1214

@@ -724,7 +726,7 @@ public void testIpAddressTypes() throws SQLException, UnknownHostException {
724726
long seed = System.currentTimeMillis();
725727
Random rand = new Random(seed);
726728

727-
InetAddress ipv4AddressByIp = Inet4Address.getByName(rand.nextInt(256) + "." + rand.nextInt(256) + "." + rand.nextInt(256) + "." + rand.nextInt(256));
729+
InetAddress ipv4AddressByIp = Inet4Address.getByName("90.176.75.97");
728730
InetAddress ipv4AddressByName = Inet4Address.getByName("www.example.com");
729731
InetAddress ipv6Address = Inet6Address.getByName("2001:adb8:85a3:1:2:8a2e:370:7334");
730732
InetAddress ipv4AsIpv6 = Inet4Address.getByName("90.176.75.97");
@@ -745,11 +747,13 @@ public void testIpAddressTypes() throws SQLException, UnknownHostException {
745747
try (ResultSet rs = stmt.executeQuery("SELECT * FROM test_ips ORDER BY order")) {
746748
assertTrue(rs.next());
747749
assertEquals(rs.getObject("ipv4_ip"), ipv4AddressByIp);
750+
assertEquals(rs.getObject("ipv4_ip", Inet6Address.class).toString(), "/0:0:0:0:0:ffff:5ab0:4b61");
748751
assertEquals(rs.getString("ipv4_ip"), ipv4AddressByIp.toString());
749752
assertEquals(rs.getObject("ipv4_name"), ipv4AddressByName);
750753
assertEquals(rs.getObject("ipv6"), ipv6Address);
751754
assertEquals(rs.getString("ipv6"), ipv6Address.toString());
752755
assertEquals(rs.getObject("ipv4_as_ipv6"), ipv4AsIpv6);
756+
assertEquals(rs.getObject("ipv4_as_ipv6", Inet4Address.class), ipv4AsIpv6);
753757
assertFalse(rs.next());
754758
}
755759
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
package com.clickhouse.jdbc.internal;
22

3+
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
4+
import com.clickhouse.data.ClickHouseColumn;
5+
import org.testng.annotations.Test;
6+
7+
import java.math.BigDecimal;
8+
import java.sql.SQLException;
9+
10+
import static org.testng.Assert.assertEquals;
11+
312
public class JdbcUtilsTest {
413

14+
15+
@Test(groups = {"unit"})
16+
public void testConvertPrimitiveTypes() throws SQLException {
17+
assertEquals(JdbcUtils.convert(1, int.class), 1);
18+
assertEquals(JdbcUtils.convert(1L, long.class), 1L);
19+
assertEquals(JdbcUtils.convert("1", String.class), "1");
20+
assertEquals(JdbcUtils.convert(1.0f, float.class), 1.0f);
21+
assertEquals(JdbcUtils.convert(1.0, double.class), 1.0);
22+
assertEquals(JdbcUtils.convert(true, boolean.class), true);
23+
assertEquals(JdbcUtils.convert((short) 1, short.class), (short) 1);
24+
assertEquals(JdbcUtils.convert((byte) 1, byte.class), (byte) 1);
25+
assertEquals(JdbcUtils.convert(1.0d, BigDecimal.class), BigDecimal.valueOf(1.0d));
26+
}
27+
28+
29+
@Test(groups = {"unit"})
30+
public void testConvertToArray() throws Exception {
31+
ClickHouseColumn column = ClickHouseColumn.of("arr", "Array(Int32)");
32+
BinaryStreamReader.ArrayValue arrayValue = new BinaryStreamReader.ArrayValue(int.class, 2);
33+
arrayValue.set(0, 1);
34+
arrayValue.set(1, 2);
35+
java.sql.Array array = (java.sql.Array) JdbcUtils.convert(arrayValue, java.sql.Array.class, column);
36+
Object arr = array.getArray();
37+
assertEquals(array.getBaseTypeName(), "Int32");
38+
assertEquals(arr.getClass().getComponentType(), Object.class);
39+
Object[] arrs = (Object[]) arr;
40+
assertEquals(arrs[0], 1);
41+
assertEquals(arrs[1], 2);
42+
}
543
}

0 commit comments

Comments
 (0)