Skip to content

Commit 5e83ba8

Browse files
author
Paultagoras
committed
Add IP conversion and encoding
1 parent 7a13970 commit 5e83ba8

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.InputStream;
99
import java.io.Reader;
1010
import java.math.BigDecimal;
11+
import java.net.InetAddress;
1112
import java.net.URL;
1213
import java.sql.Array;
1314
import java.sql.Blob;
@@ -517,6 +518,8 @@ private static String encodeObject(Object x) throws SQLException {
517518
return encodeObject(((ZonedDateTime) x).toInstant());
518519
} else if (x instanceof Instant) {
519520
return "fromUnixTimestamp64Nano(" + (((Instant) x).getEpochSecond() * 1_000_000_000L + ((Instant) x).getNano())+ ")";
521+
} else if (x instanceof InetAddress) {
522+
return "'" + ((InetAddress) x).getHostAddress() + "'";
520523
} else if (x instanceof Array) {
521524
StringBuilder listString = new StringBuilder();
522525
listString.append("[");

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.clickhouse.data.ClickHouseDataType;
55
import com.clickhouse.jdbc.types.Array;
66

7+
import java.net.Inet4Address;
8+
import java.net.Inet6Address;
79
import java.sql.Date;
810
import java.sql.JDBCType;
911
import java.sql.SQLException;
@@ -233,6 +235,12 @@ public static Object convert(Object value, Class<?> type) throws SQLException {
233235
return java.sql.Time.valueOf(LocalTime.from((TemporalAccessor) value));
234236
} else if (type == java.sql.Array.class && value instanceof BinaryStreamReader.ArrayValue) {//It's cleaner to use getList but this handles the more generic getObject
235237
return new Array(((BinaryStreamReader.ArrayValue) value).asList(), "Object", JDBCType.JAVA_OBJECT.getVendorTypeNumber());
238+
} else if (type == Inet4Address.class && value instanceof Inet6Address) {
239+
// Convert Inet6Address to Inet4Address
240+
return Inet4Address.getByName(value.toString());
241+
} else if (type == Inet6Address.class && value instanceof Inet4Address) {
242+
// Convert Inet4Address to Inet6Address
243+
return Inet6Address.getByName(value.toString());
236244
}
237245
} catch (Exception e) {
238246
throw new SQLException("Failed to convert " + value + " to " + type.getName(), ExceptionUtils.SQL_STATE_DATA_EXCEPTION);

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
import java.math.BigDecimal;
1111
import java.math.BigInteger;
12+
import java.net.Inet4Address;
13+
import java.net.Inet6Address;
14+
import java.net.InetAddress;
15+
import java.net.UnknownHostException;
1216
import java.sql.Connection;
1317
import java.sql.Date;
1418
import java.sql.JDBCType;
@@ -373,6 +377,43 @@ public void testStringTypes() throws SQLException {
373377
}
374378
}
375379

380+
@Test(groups = { "integration" })
381+
public void testIpAddressTypes() throws SQLException, UnknownHostException {
382+
runQuery("CREATE TABLE test_strings (order Int8, "
383+
+ "ipv4_ip IPv4, ipv4_name IPv4, ipv6 IPv6"
384+
+ ") ENGINE = MergeTree ORDER BY ()");
385+
386+
// Insert random (valid) values
387+
long seed = System.currentTimeMillis();
388+
Random rand = new Random(seed);
389+
390+
InetAddress ipv4AddressByIp = Inet4Address.getByName(rand.nextInt(256) + "." + rand.nextInt(256) + "." + rand.nextInt(256) + "." + rand.nextInt(256));
391+
InetAddress ipv4AddressByName = Inet4Address.getByName("www.example.com");
392+
InetAddress ipv6Address = Inet6Address.getByName("2001:adb8:85a3:1:2:8a2e:370:7334");
393+
394+
try (Connection conn = getConnection()) {
395+
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_strings VALUES ( 1, ?, ?, ? )")) {
396+
stmt.setObject(1, ipv4AddressByIp);
397+
stmt.setObject(2, ipv4AddressByName);
398+
stmt.setObject(3, ipv6Address);
399+
stmt.executeUpdate();
400+
}
401+
}
402+
403+
// Check the results
404+
try (Connection conn = getConnection()) {
405+
try (Statement stmt = conn.createStatement()) {
406+
try (ResultSet rs = stmt.executeQuery("SELECT * FROM test_strings ORDER BY order")) {
407+
assertTrue(rs.next());
408+
assertEquals(rs.getObject("ipv4_ip"), ipv4AddressByIp);
409+
assertEquals(rs.getObject("ipv4_name"), ipv4AddressByName);
410+
assertEquals(rs.getObject("ipv6"), ipv6Address);
411+
assertFalse(rs.next());
412+
}
413+
}
414+
}
415+
}
416+
376417
@Test(groups = { "integration" })
377418
public void testFloatTypes() throws SQLException {
378419
runQuery("CREATE TABLE test_floats (order Int8, "

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

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

3-
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
43
import org.apache.commons.lang3.RandomStringUtils;
54
import org.testng.annotations.Ignore;
65
import org.testng.annotations.Test;
@@ -10,11 +9,8 @@
109
import java.sql.PreparedStatement;
1110
import java.sql.ResultSet;
1211
import java.sql.Statement;
13-
import java.sql.Timestamp;
1412
import java.sql.Types;
15-
import java.time.ZoneId;
1613
import java.util.Arrays;
17-
import java.util.Calendar;
1814
import java.util.GregorianCalendar;
1915
import java.util.TimeZone;
2016

0 commit comments

Comments
 (0)