99
1010import java .math .BigDecimal ;
1111import java .math .BigInteger ;
12+ import java .net .Inet4Address ;
13+ import java .net .Inet6Address ;
14+ import java .net .InetAddress ;
15+ import java .net .UnknownHostException ;
1216import java .sql .Connection ;
1317import java .sql .Date ;
1418import java .sql .JDBCType ;
@@ -317,35 +321,29 @@ public void testStringTypes() throws SQLException {
317321 runQuery ("CREATE TABLE test_strings (order Int8, "
318322 + "str String, fixed FixedString(6), "
319323 + "enum Enum8('a' = 6, 'b' = 7, 'c' = 8), enum8 Enum8('a' = 1, 'b' = 2, 'c' = 3), enum16 Enum16('a' = 1, 'b' = 2, 'c' = 3), "
320- + "uuid UUID, ipv4 IPv4, ipv6 IPv6, "
321- + "escaped String "
324+ + "uuid UUID, escaped String "
322325 + ") ENGINE = MergeTree ORDER BY ()" );
323326
324327 // Insert random (valid) values
325328 long seed = System .currentTimeMillis ();
326329 Random rand = new Random (seed );
327- log .info ("Random seed was: {}" , seed );
328330
329331 String str = "string" + rand .nextInt (1000 );
330332 String fixed = "fixed" + rand .nextInt (10 );
331333 String enum8 = "a" ;
332334 String enum16 = "b" ;
333335 String uuid = UUID .randomUUID ().toString ();
334- String ipv4 = rand .nextInt (256 ) + "." + rand .nextInt (256 ) + "." + rand .nextInt (256 ) + "." + rand .nextInt (256 );
335- String ipv6 = "2001:adb8:85a3:1:2:8a2e:370:7334" ;
336336 String escaped = "\\ xA3\\ xA3\\ x12\\ xA0\\ xDF\\ x13\\ x4E\\ x8C\\ x87\\ x74\\ xD4\\ x53\\ xDB\\ xFC\\ x34\\ x95" ;
337337
338338 try (Connection conn = getConnection ()) {
339- try (PreparedStatement stmt = conn .prepareStatement ("INSERT INTO test_strings VALUES ( 1, ?, ?, ?, ?, ?, ?, ?, ?, ? )" )) {
339+ try (PreparedStatement stmt = conn .prepareStatement ("INSERT INTO test_strings VALUES ( 1, ?, ?, ?, ?, ?, ?, ? )" )) {
340340 stmt .setString (1 , str );
341341 stmt .setString (2 , fixed );
342342 stmt .setString (3 , enum8 );
343343 stmt .setString (4 , enum8 );
344344 stmt .setString (5 , enum16 );
345345 stmt .setString (6 , uuid );
346- stmt .setString (7 , ipv4 );
347- stmt .setString (8 , ipv6 );
348- stmt .setString (9 , escaped );
346+ stmt .setString (7 , escaped );
349347 stmt .executeUpdate ();
350348 }
351349 }
@@ -364,15 +362,52 @@ public void testStringTypes() throws SQLException {
364362 assertEquals (rs .getString ("enum16" ), "b" );
365363 assertEquals (rs .getInt ("enum16" ), 2 );
366364 assertEquals (rs .getString ("uuid" ), uuid );
367- assertEquals (rs .getString ("ipv4" ), "/" + ipv4 );
368- assertEquals (rs .getString ("ipv6" ), "/" + ipv6 );
369365 assertEquals (rs .getString ("escaped" ), escaped );
370366 assertFalse (rs .next ());
371367 }
372368 }
373369 }
374370 }
375371
372+ @ Test (groups = { "integration" })
373+ public void testIpAddressTypes () throws SQLException , UnknownHostException {
374+ runQuery ("CREATE TABLE test_ips (order Int8, "
375+ + "ipv4_ip IPv4, ipv4_name IPv4, ipv6 IPv6"
376+ + ") ENGINE = MergeTree ORDER BY ()" );
377+
378+ // Insert random (valid) values
379+ long seed = System .currentTimeMillis ();
380+ Random rand = new Random (seed );
381+
382+ InetAddress ipv4AddressByIp = Inet4Address .getByName (rand .nextInt (256 ) + "." + rand .nextInt (256 ) + "." + rand .nextInt (256 ) + "." + rand .nextInt (256 ));
383+ InetAddress ipv4AddressByName = Inet4Address .getByName ("www.example.com" );
384+ InetAddress ipv6Address = Inet6Address .getByName ("2001:adb8:85a3:1:2:8a2e:370:7334" );
385+
386+ try (Connection conn = getConnection ()) {
387+ try (PreparedStatement stmt = conn .prepareStatement ("INSERT INTO test_ips VALUES ( 1, ?, ?, ? )" )) {
388+ stmt .setObject (1 , ipv4AddressByIp );
389+ stmt .setObject (2 , ipv4AddressByName );
390+ stmt .setObject (3 , ipv6Address );
391+ stmt .executeUpdate ();
392+ }
393+ }
394+
395+ // Check the results
396+ try (Connection conn = getConnection ()) {
397+ try (Statement stmt = conn .createStatement ()) {
398+ try (ResultSet rs = stmt .executeQuery ("SELECT * FROM test_ips ORDER BY order" )) {
399+ assertTrue (rs .next ());
400+ assertEquals (rs .getObject ("ipv4_ip" ), ipv4AddressByIp );
401+ assertEquals (rs .getString ("ipv4_ip" ), ipv4AddressByIp .toString ());
402+ assertEquals (rs .getObject ("ipv4_name" ), ipv4AddressByName );
403+ assertEquals (rs .getObject ("ipv6" ), ipv6Address );
404+ assertEquals (rs .getString ("ipv6" ), ipv6Address .toString ());
405+ assertFalse (rs .next ());
406+ }
407+ }
408+ }
409+ }
410+
376411 @ Test (groups = { "integration" })
377412 public void testFloatTypes () throws SQLException {
378413 runQuery ("CREATE TABLE test_floats (order Int8, "
0 commit comments