@@ -563,113 +563,83 @@ public void datetimeTest(SqlQueries.JdbcQuery query) throws SQLException {
563563 }
564564 };
565565
566- private void assertDate (ResultSet rs , LocalDate ld , LocalDateTime ldt , Instant instant ) throws SQLException {
567- // TODO: NOT SUPPORTED YET
568- // Assert.assertEquals(ld, rs.getObject("c_Datetime", LocalDate.class));
569- // Assert.assertEquals(ldt, rs.getObject("c_Datetime", LocalDateTime.class));
570- Object obj = rs .getObject ("c_Date" );
571- Assert .assertTrue (obj instanceof LocalDate );
572- Assert .assertEquals (ld , obj );
573-
574- Assert .assertEquals (new Date (instant .toEpochMilli ()), rs .getDate ("c_Date" ));
575- Assert .assertEquals (new Timestamp (instant .toEpochMilli ()), rs .getTimestamp ("c_Date" ));
576- Assert .assertEquals (instant .toEpochMilli (), rs .getLong ("c_Date" ));
577- Assert .assertEquals (ld .toString (), rs .getString ("c_Date" ));
578- }
579-
580- @ Disabled
581566 @ ParameterizedTest (name = "with {0}" )
582567 @ EnumSource (SqlQueries .JdbcQuery .class )
583568 public void dateTest (SqlQueries .JdbcQuery query ) throws SQLException {
584569 String upsert = TEST_TABLE .upsertOne (query , "c_Date" , "Date" );
570+ boolean castingSupported = query != SqlQueries .JdbcQuery .IN_MEMORY ;
585571
586- try (PreparedStatement statement = jdbc .connection ().prepareStatement (upsert )) {
587- statement .setInt (1 , 1 );
588- statement .setObject (2 , LocalDate .of (2023 , Month .MARCH , 3 ));
589- statement .execute ();
572+ try (PreparedStatement ps = jdbc .connection ().prepareStatement (upsert )) {
573+ ps .setInt (1 , 1 );
574+ ps .setObject (2 , LocalDate .of (2025 , Month .AUGUST , 10 ));
575+ ps .execute ();
590576
591- statement .setInt (1 , 2 );
592- statement . setObject (2 , LocalDateTime . of ( 2023 , Month . MARCH , 3 , 14 , 56 , 59 , 123456789 ));
593- statement .execute ();
577+ ps .setInt (1 , 2 );
578+ ps . setDate (2 , new Date ( INSTANT . toEpochMilli () ));
579+ ps .execute ();
594580
595- statement .setInt (1 , 3 );
596- statement .setDate (2 , new Date (INSTANT .toEpochMilli ()));
597- statement .execute ();
581+ if (castingSupported ) {
582+ ps .setInt (1 , 3 );
583+ ps .setTimestamp (2 , new Timestamp (INSTANT .toEpochMilli ()));
584+ ps .execute ();
598585
599- statement .setInt (1 , 4 );
600- statement . setTimestamp (2 , new Timestamp ( INSTANT . toEpochMilli ()));
601- statement .execute ();
586+ ps .setInt (1 , 4 );
587+ ps . setInt (2 , 10 ); // Jan 11 1970
588+ ps .execute ();
602589
603- if (query != SqlQueries .JdbcQuery .IN_MEMORY ) { // IN MEMORY is not typed mode, casting is not supported
604- statement .setInt (1 , 5 );
605- statement .setLong (2 , 1585932011123l );
606- statement .execute ();
590+ ps .setInt (1 , 5 );
591+ ps .setLong (2 , 12345 ); // Oct 20 2003
592+ ps .execute ();
607593
608- statement .setInt (1 , 6 );
609- statement .setString (2 , "2011-12-03T10:15:30.456789123Z" );
610- statement .execute ();
594+ ps .setInt (1 , 6 );
595+ ps .setObject (2 , LocalDateTime .of (2023 , Month .MARCH , 3 , 14 , 56 , 59 , 123456789 ));
596+ ps .execute ();
597+
598+ ps .setInt (1 , 7 );
599+ ps .setString (2 , "2011-12-03" );
600+ ps .execute ();
611601 }
612602 }
613603
614- try (Statement statement = jdbc .connection ().createStatement ()) {
615- try (ResultSet rs = statement .executeQuery (TEST_TABLE .selectColumn ("c_Date" ))) {
616- Assert .assertTrue (rs .next ());
617- Assert .assertEquals (1 , rs .getInt ("key" ));
618- // LocalDate.of(2023, Month.MARCH, 3) UTC == 1677801600000l;
619- assertDate (rs ,
620- LocalDate .of (2023 , Month .MARCH , 3 ),
621- LocalDateTime .of (2023 , Month .MARCH , 3 , 0 , 0 , 0 ),
622- Instant .ofEpochSecond (1677801600l , 0 )
623- );
624-
625- Assert .assertTrue (rs .next ());
626- Assert .assertEquals (2 , rs .getInt ("key" ));
627- // LocalDateTime.of(2023, Month.MARCH, 3, 14, 56, 59, 123456789) UTC == 1677855419123l;
628- assertDate (rs ,
629- LocalDate .of (2023 , Month .MARCH , 3 ),
630- LocalDateTime .of (2023 , Month .MARCH , 3 , 0 , 0 , 0 ), // Timestamp supports only micros
631- Instant .ofEpochSecond (1677801600l )
632- );
633-
634- Assert .assertTrue (rs .next ());
635- Assert .assertEquals (3 , rs .getInt ("key" ));
636- assertDate (rs ,
637- LocalDate .of (2020 , Month .APRIL , 3 ),
638- LocalDateTime .of (2020 , Month .APRIL , 3 , 0 , 0 , 0 ),
639- Instant .ofEpochSecond (1585872000l ) // Friday, April 3, 2022 00:00:00 UTC
640- );
641-
642- Assert .assertTrue (rs .next ());
643- Assert .assertEquals (4 , rs .getInt ("key" ));
644- // Instant.ofEpochMilli(1585932011123l) == Friday, April 3, 2020 16:40:11.123 UTC
645- assertDate (rs ,
646- LocalDate .of (2020 , Month .APRIL , 3 ),
647- LocalDateTime .of (2020 , Month .APRIL , 3 , 0 , 0 , 0 ), // Timestamp supports only micros
648- Instant .ofEpochSecond (1585872000l )
649- );
650-
651- if (query != SqlQueries .JdbcQuery .IN_MEMORY ) { // IN MEMORY is not typed mode, casting is not supported
652- Assert .assertTrue (rs .next ());
653- Assert .assertEquals (5 , rs .getInt ("key" ));
654- // Instant.ofEpochMilli(1585932011123l) == Friday, April 3, 2020 16:40:11.123 UTC
655- assertDate (rs ,
656- LocalDate .of (2020 , Month .APRIL , 3 ),
657- LocalDateTime .of (2020 , Month .APRIL , 3 , 16 , 40 , 11 ),
658- Instant .ofEpochSecond (1585872000l )
659- );
660-
661- Assert .assertTrue (rs .next ());
662- Assert .assertEquals (6 , rs .getInt ("key" ));
663- // 2011-12-03T10:15:30.456789123Z
664- assertDate (rs ,
665- LocalDate .of (2011 , Month .DECEMBER , 3 ),
666- LocalDateTime .of (2011 , Month .DECEMBER , 3 , 10 , 15 , 30 ),
667- Instant .ofEpochSecond (1322870400l )
668- );
604+ try (Statement st = jdbc .connection ().createStatement ()) {
605+ try (ResultSet rs = st .executeQuery (TEST_TABLE .selectColumn ("c_Date" ))) {
606+ assertNextDate (rs , 1 , LocalDate .of (2025 , Month .AUGUST , 10 ));
607+ assertNextDate (rs , 2 , INSTANT .atZone (ZoneId .systemDefault ()).toLocalDate ());
608+
609+ if (castingSupported ) {
610+ assertNextDate (rs , 3 , INSTANT .atZone (ZoneId .systemDefault ()).toLocalDate ());
611+ assertNextDate (rs , 4 , LocalDate .of (1970 , Month .JANUARY , 11 ));
612+ assertNextDate (rs , 5 , LocalDate .of (2003 , Month .OCTOBER , 20 ));
613+ assertNextDate (rs , 6 , LocalDate .of (2023 , Month .MARCH , 3 ));
614+ assertNextDate (rs , 7 , LocalDate .of (2011 , Month .DECEMBER , 3 ));
669615 }
670616
671617 Assert .assertFalse (rs .next ());
672618 }
673619 }
674620 };
621+
622+ private void assertNextDate (ResultSet rs , int key , LocalDate ld ) throws SQLException {
623+ Assert .assertTrue (rs .next ());
624+ Assert .assertEquals (key , rs .getInt ("key" ));
625+
626+ Object obj = rs .getObject ("c_Date" );
627+ Assert .assertTrue (obj instanceof LocalDate );
628+ Assert .assertEquals (ld , obj );
629+
630+ Assert .assertEquals (ld .toEpochDay (), rs .getInt ("c_Date" ));
631+ Assert .assertEquals (ld .toEpochDay (), rs .getLong ("c_Date" ));
632+
633+ Assert .assertEquals (Date .valueOf (ld ), rs .getDate ("c_Date" ));
634+ Assert .assertEquals (Timestamp .valueOf (LocalDateTime .of (ld , LocalTime .MIN )), rs .getTimestamp ("c_Date" ));
635+ Assert .assertEquals (ld .toString (), rs .getString ("c_Date" ));
636+
637+ Assert .assertEquals (Long .valueOf (ld .toEpochDay ()), rs .getObject ("c_Date" , Long .class ));
638+ Assert .assertEquals (ld , rs .getObject ("c_Date" , LocalDate .class ));
639+ Assert .assertEquals (ld .atTime (LocalTime .MIN ), rs .getObject ("c_Date" , LocalDateTime .class ));
640+ Assert .assertEquals (ld .atTime (LocalTime .MIN )
641+ .atZone (ZoneId .systemDefault ())
642+ .toInstant (),
643+ rs .getObject ("c_Date" , Instant .class ));
644+ }
675645}
0 commit comments