9
9
10
10
import java .math .BigDecimal ;
11
11
import java .math .BigInteger ;
12
+ import java .net .Inet4Address ;
13
+ import java .net .Inet6Address ;
14
+ import java .net .InetAddress ;
15
+ import java .net .UnknownHostException ;
12
16
import java .sql .Connection ;
13
17
import java .sql .Date ;
14
18
import java .sql .JDBCType ;
@@ -317,35 +321,29 @@ public void testStringTypes() throws SQLException {
317
321
runQuery ("CREATE TABLE test_strings (order Int8, "
318
322
+ "str String, fixed FixedString(6), "
319
323
+ "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 "
322
325
+ ") ENGINE = MergeTree ORDER BY ()" );
323
326
324
327
// Insert random (valid) values
325
328
long seed = System .currentTimeMillis ();
326
329
Random rand = new Random (seed );
327
- log .info ("Random seed was: {}" , seed );
328
330
329
331
String str = "string" + rand .nextInt (1000 );
330
332
String fixed = "fixed" + rand .nextInt (10 );
331
333
String enum8 = "a" ;
332
334
String enum16 = "b" ;
333
335
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" ;
336
336
String escaped = "\\ xA3\\ xA3\\ x12\\ xA0\\ xDF\\ x13\\ x4E\\ x8C\\ x87\\ x74\\ xD4\\ x53\\ xDB\\ xFC\\ x34\\ x95" ;
337
337
338
338
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, ?, ?, ?, ?, ?, ?, ? )" )) {
340
340
stmt .setString (1 , str );
341
341
stmt .setString (2 , fixed );
342
342
stmt .setString (3 , enum8 );
343
343
stmt .setString (4 , enum8 );
344
344
stmt .setString (5 , enum16 );
345
345
stmt .setString (6 , uuid );
346
- stmt .setString (7 , ipv4 );
347
- stmt .setString (8 , ipv6 );
348
- stmt .setString (9 , escaped );
346
+ stmt .setString (7 , escaped );
349
347
stmt .executeUpdate ();
350
348
}
351
349
}
@@ -364,15 +362,52 @@ public void testStringTypes() throws SQLException {
364
362
assertEquals (rs .getString ("enum16" ), "b" );
365
363
assertEquals (rs .getInt ("enum16" ), 2 );
366
364
assertEquals (rs .getString ("uuid" ), uuid );
367
- assertEquals (rs .getString ("ipv4" ), "/" + ipv4 );
368
- assertEquals (rs .getString ("ipv6" ), "/" + ipv6 );
369
365
assertEquals (rs .getString ("escaped" ), escaped );
370
366
assertFalse (rs .next ());
371
367
}
372
368
}
373
369
}
374
370
}
375
371
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
+
376
411
@ Test (groups = { "integration" })
377
412
public void testFloatTypes () throws SQLException {
378
413
runQuery ("CREATE TABLE test_floats (order Int8, "
0 commit comments