27
27
import com .google .cloud .Date ;
28
28
import com .google .cloud .Timestamp ;
29
29
import com .google .cloud .spanner .Database ;
30
+ import com .google .cloud .spanner .Interval ;
30
31
import com .google .cloud .spanner .KeySet ;
31
32
import com .google .cloud .spanner .Mutation ;
32
33
import com .google .cloud .spanner .Value ;
@@ -137,6 +138,8 @@ public void insertTestData() {
137
138
.to (new BigDecimal ("3.14" ))
138
139
.set ("col_timestamptz" )
139
140
.to (Timestamp .parseTimestamp ("2022-01-27T17:51:30+01:00" ))
141
+ .set ("col_interval" )
142
+ .to (Interval .parseFromString ("P1Y2M3DT4H5M6.789S" ))
140
143
.set ("col_date" )
141
144
.to (Date .parseDate ("2022-05-23" ))
142
145
.set ("col_varchar" )
@@ -164,6 +167,8 @@ public void insertTestData() {
164
167
Timestamp .parseTimestamp ("2000-01-01T00:00:00Z" ),
165
168
null ,
166
169
Timestamp .parseTimestamp ("1970-01-01T00:00:00Z" )))
170
+ .set ("col_array_interval" )
171
+ .toStringArray (Collections .singletonList ("P1Y2M3DT4H5M6.789S" ))
167
172
.set ("col_array_date" )
168
173
.toDateArray (
169
174
Arrays .asList (Date .parseDate ("2000-01-01" ), null , Date .parseDate ("1970-01-01" )))
@@ -426,9 +431,9 @@ public void testInsertWithParameters() throws SQLException {
426
431
try (PreparedStatement statement =
427
432
connection .prepareStatement (
428
433
"insert into all_types "
429
- + "(col_bigint, col_bool, col_bytea, col_float4, col_float8, col_int, col_numeric, col_timestamptz, col_date, col_varchar, col_jsonb, "
430
- + "col_array_bigint, col_array_bool, col_array_bytea, col_array_float4, col_array_float8, col_array_int, col_array_numeric, col_array_timestamptz, col_array_date, col_array_varchar, col_array_jsonb) "
431
- + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" )) {
434
+ + "(col_bigint, col_bool, col_bytea, col_float4, col_float8, col_int, col_numeric, col_timestamptz, col_interval, col_date, col_varchar, col_jsonb, "
435
+ + "col_array_bigint, col_array_bool, col_array_bytea, col_array_float4, col_array_float8, col_array_int, col_array_numeric, col_array_timestamptz, col_array_interval, col_array_date, col_array_varchar, col_array_jsonb) "
436
+ + "values (?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" )) {
432
437
int index = 0 ;
433
438
statement .setLong (++index , 2 );
434
439
statement .setBoolean (++index , true );
@@ -449,6 +454,7 @@ public void testInsertWithParameters() throws SQLException {
449
454
statement .setBigDecimal (++index , new BigDecimal ("6.626" ));
450
455
statement .setTimestamp (
451
456
++index , Timestamp .parseTimestamp ("2022-02-11T13:45:00.123456+01:00" ).toSqlTimestamp ());
457
+ statement .setObject (++index , "P1Y2M3DT4H5M6.789S" );
452
458
statement .setObject (++index , LocalDate .parse ("2000-02-29" ));
453
459
statement .setString (++index , "string_test" );
454
460
statement .setObject (++index , "{\" key1\" : \" value1\" , \" key2\" : \" value2\" }" , Types .OTHER );
@@ -492,6 +498,8 @@ public void testInsertWithParameters() throws SQLException {
492
498
null ,
493
499
Timestamp .parseTimestamp ("2000-01-01T00:00:00Z" ).toSqlTimestamp ()
494
500
}));
501
+ statement .setArray (
502
+ ++index , connection .createArrayOf ("varchar" , new String [] {"P1Y2M3DT4H5M6.789S" }));
495
503
statement .setArray (
496
504
++index ,
497
505
connection .createArrayOf (
@@ -530,7 +538,7 @@ public void testInsertWithParameters() throws SQLException {
530
538
connection .createStatement ().executeQuery ("select * from all_types where col_bigint=2" )) {
531
539
assertTrue (resultSet .next ());
532
540
533
- assertEquals (22 , resultSet .getMetaData ().getColumnCount ());
541
+ assertEquals (24 , resultSet .getMetaData ().getColumnCount ());
534
542
int index = 0 ;
535
543
assertEquals (2 , resultSet .getLong (++index ));
536
544
assertTrue (resultSet .getBoolean (++index ));
@@ -547,6 +555,7 @@ public void testInsertWithParameters() throws SQLException {
547
555
assertEquals (
548
556
Timestamp .parseTimestamp ("2022-02-11T13:45:00.123456+01:00" ).toSqlTimestamp (),
549
557
resultSet .getTimestamp (++index ));
558
+ assertEquals ("P1Y2M3DT4H5M6.789S" , resultSet .getString (++index ));
550
559
assertEquals (LocalDate .of (2000 , 2 , 29 ), resultSet .getObject (++index , LocalDate .class ));
551
560
assertEquals ("string_test" , resultSet .getString (++index ));
552
561
assertEquals ("{\" key1\" : \" value1\" , \" key2\" : \" value2\" }" , resultSet .getString (++index ));
@@ -592,6 +601,8 @@ public void testInsertWithParameters() throws SQLException {
592
601
},
593
602
(java .sql .Timestamp []) resultSet .getArray (++index ).getArray ());
594
603
}
604
+ assertArrayEquals (
605
+ new String [] {"P1Y2M3DT4H5M6.789S" }, (String []) resultSet .getArray (++index ).getArray ());
595
606
assertArrayEquals (
596
607
new java .sql .Date [] {
597
608
java .sql .Date .valueOf ("2000-01-01" ), null , java .sql .Date .valueOf ("1970-01-01" )
@@ -688,6 +699,7 @@ public void testUpdateWithParameters() throws SQLException {
688
699
assertEquals (
689
700
Timestamp .parseTimestamp ("2022-02-11T14:04:59.123457+01:00" ).toSqlTimestamp (),
690
701
resultSet .getTimestamp (++index ));
702
+ assertEquals ("P1Y2M3DT4H5M6.789S" , resultSet .getString (++index ));
691
703
assertEquals (LocalDate .parse ("2000-01-01" ), resultSet .getObject (++index , LocalDate .class ));
692
704
assertEquals ("updated" , resultSet .getString (++index ));
693
705
assertEquals (
@@ -1295,6 +1307,12 @@ public void testSelectTypes() throws SQLException {
1295
1307
assertEquals (Oid .TIMESTAMPTZ_ARRAY , types .getInt (1 ));
1296
1308
assertEquals ("_timestamptz" , types .getString (2 ));
1297
1309
assertTrue (types .next ());
1310
+ assertEquals (Oid .INTERVAL , types .getInt (1 ));
1311
+ assertEquals ("interval" , types .getString (2 ));
1312
+ assertTrue (types .next ());
1313
+ assertEquals (Oid .INTERVAL_ARRAY , types .getInt (1 ));
1314
+ assertEquals ("_interval" , types .getString (2 ));
1315
+ assertTrue (types .next ());
1298
1316
assertEquals (Oid .NUMERIC_ARRAY , types .getInt (1 ));
1299
1317
assertEquals ("_numeric" , types .getString (2 ));
1300
1318
assertTrue (types .next ());
@@ -1333,6 +1351,8 @@ private void writeExtraTestRows() {
1333
1351
.to (new BigDecimal ("-3.14" ))
1334
1352
.set ("col_timestamptz" )
1335
1353
.to (Timestamp .parseTimestamp ("2022-04-22T19:27:30+02:00" ))
1354
+ .set ("col_interval" )
1355
+ .to (Interval .parseFromString ("P1Y" ))
1336
1356
.set ("col_date" )
1337
1357
.to (Date .parseDate ("2000-01-01" ))
1338
1358
.set ("col_varchar" )
@@ -1360,6 +1380,8 @@ private void writeExtraTestRows() {
1360
1380
Timestamp .parseTimestamp ("2000-01-01T00:00:00Z" ),
1361
1381
null ,
1362
1382
Timestamp .parseTimestamp ("1970-01-01T00:00:00Z" )))
1383
+ .set ("col_array_interval" )
1384
+ .toIntervalArray (Collections .singletonList (Interval .parseFromString ("P1Y" )))
1363
1385
.set ("col_array_date" )
1364
1386
.toDateArray (
1365
1387
Arrays .asList (Date .parseDate ("2000-01-01" ), null , Date .parseDate ("1970-01-01" )))
@@ -1386,6 +1408,8 @@ private void writeExtraTestRows() {
1386
1408
.to ((BigDecimal ) null )
1387
1409
.set ("col_timestamptz" )
1388
1410
.to ((Timestamp ) null )
1411
+ .set ("col_interval" )
1412
+ .to ((Interval ) null )
1389
1413
.set ("col_date" )
1390
1414
.to ((Date ) null )
1391
1415
.set ("col_varchar" )
@@ -1409,6 +1433,8 @@ private void writeExtraTestRows() {
1409
1433
.set ("col_array_timestamptz" )
1410
1434
.toTimestampArray (null )
1411
1435
.set ("col_array_date" )
1436
+ .toIntervalArray (null )
1437
+ .set ("col_array_interval" )
1412
1438
.toDateArray (null )
1413
1439
.set ("col_array_varchar" )
1414
1440
.toStringArray (null )
@@ -1432,6 +1458,8 @@ private void writeExtraTestRows() {
1432
1458
.to (BigDecimal .ZERO )
1433
1459
.set ("col_timestamptz" )
1434
1460
.to (Timestamp .parseTimestamp ("0001-01-01T00:00:00Z" ))
1461
+ .set ("col_interval" )
1462
+ .to (Interval .parseFromString ("P0Y" ))
1435
1463
.set ("col_date" )
1436
1464
.to (Date .parseDate ("0001-01-01" ))
1437
1465
.set ("col_varchar" )
@@ -1454,6 +1482,8 @@ private void writeExtraTestRows() {
1454
1482
.toPgNumericArray (ImmutableList .of ())
1455
1483
.set ("col_array_timestamptz" )
1456
1484
.toTimestampArray (ImmutableList .of ())
1485
+ .set ("col_array_interval" )
1486
+ .toIntervalArray (ImmutableList .of ())
1457
1487
.set ("col_array_date" )
1458
1488
.toDateArray (ImmutableList .of ())
1459
1489
.set ("col_array_varchar" )
@@ -1478,6 +1508,8 @@ private void writeExtraTestRows() {
1478
1508
.to (BigDecimal .ZERO )
1479
1509
.set ("col_timestamptz" )
1480
1510
.to (Timestamp .parseTimestamp ("0001-01-01T00:00:00Z" ))
1511
+ .set ("col_interval" )
1512
+ .to (Interval .parseFromString ("P0Y" ))
1481
1513
.set ("col_date" )
1482
1514
.to (Date .parseDate ("0001-01-01" ))
1483
1515
.set ("col_varchar" )
@@ -1500,6 +1532,8 @@ private void writeExtraTestRows() {
1500
1532
.toPgNumericArray (ImmutableList .of ())
1501
1533
.set ("col_array_timestamptz" )
1502
1534
.toTimestampArray (ImmutableList .of ())
1535
+ .set ("col_array_interval" )
1536
+ .toIntervalArray (ImmutableList .of ())
1503
1537
.set ("col_array_date" )
1504
1538
.toDateArray (ImmutableList .of ())
1505
1539
.set ("col_array_varchar" )
0 commit comments