From 76254deab492ab8b80d862974b8d6d83d226ac88 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Fri, 11 Jan 2019 02:22:25 +0900 Subject: [PATCH] Updated OffsetTimeTypeHandler to use PreparedStatement#setObject() and ResultSet#getObject() Related to #1081 --- .../ibatis/type/OffsetTimeTypeHandler.java | 20 ++++------------ .../timestamp_with_timezone/Mapper.java | 5 +++- .../timestamp_with_timezone/Record.java | 11 +++++++++ .../TimestampWithTimezoneTypeHandlerTest.java | 24 +++++++++++++++++++ .../type/OffsetTimeTypeHandlerTest.java | 22 ++++++++--------- 5 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java index 2e52dfcabda..8eced0d5221 100644 --- a/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Time; import java.time.OffsetTime; /** @@ -31,31 +30,22 @@ public class OffsetTimeTypeHandler extends BaseTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, OffsetTime parameter, JdbcType jdbcType) throws SQLException { - ps.setTime(i, Time.valueOf(parameter.toLocalTime())); + ps.setObject(i, parameter); } @Override public OffsetTime getNullableResult(ResultSet rs, String columnName) throws SQLException { - Time time = rs.getTime(columnName); - return getOffsetTime(time); + return rs.getObject(columnName, OffsetTime.class); } @Override public OffsetTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { - Time time = rs.getTime(columnIndex); - return getOffsetTime(time); + return rs.getObject(columnIndex, OffsetTime.class); } @Override public OffsetTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { - Time time = cs.getTime(columnIndex); - return getOffsetTime(time); + return cs.getObject(columnIndex, OffsetTime.class); } - private static OffsetTime getOffsetTime(Time time) { - if (time != null) { - return time.toLocalTime().atOffset(OffsetTime.now().getOffset()); - } - return null; - } } diff --git a/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/Mapper.java b/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/Mapper.java index 0835d80af95..3a80d82b5cb 100644 --- a/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/Mapper.java +++ b/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/Mapper.java @@ -20,10 +20,13 @@ public interface Mapper { - @Select("select id, odt from records where id = #{id}") + @Select("select id, odt, odt ot from records where id = #{id}") Record selectById(Integer id); @Insert("insert into records (id, odt) values (#{id}, #{odt})") int insertOffsetDateTime(Record record); + @Insert("insert into records (id, odt) values (#{id}, #{ot})") + int insertOffsetTime(Record record); + } diff --git a/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/Record.java b/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/Record.java index b21aa6da23c..fe7c67d6788 100644 --- a/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/Record.java +++ b/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/Record.java @@ -16,6 +16,7 @@ package org.apache.ibatis.submitted.timestamp_with_timezone; import java.time.OffsetDateTime; +import java.time.OffsetTime; public class Record { @@ -23,6 +24,8 @@ public class Record { private OffsetDateTime odt; + private OffsetTime ot; + public Integer getId() { return id; } @@ -39,4 +42,12 @@ public void setOdt(OffsetDateTime odt) { this.odt = odt; } + public OffsetTime getOt() { + return ot; + } + + public void setOt(OffsetTime ot) { + this.ot = ot; + } + } diff --git a/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/TimestampWithTimezoneTypeHandlerTest.java b/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/TimestampWithTimezoneTypeHandlerTest.java index 38452824e79..bff34cda13f 100644 --- a/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/TimestampWithTimezoneTypeHandlerTest.java +++ b/src/test/java/org/apache/ibatis/submitted/timestamp_with_timezone/TimestampWithTimezoneTypeHandlerTest.java @@ -19,6 +19,7 @@ import java.io.Reader; import java.time.OffsetDateTime; +import java.time.OffsetTime; import java.time.ZoneOffset; import org.apache.ibatis.BaseDataTest; @@ -27,6 +28,7 @@ import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; public class TimestampWithTimezoneTypeHandlerTest { @@ -50,6 +52,8 @@ public void shouldSelectOffsetDateTime() { Record record = mapper.selectById(1); assertEquals(OffsetDateTime.of(2018, 1, 2, 11, 22, 33, 123456000, ZoneOffset.ofHoursMinutes(1, 23)), record.getOdt()); + // HSQLDB 2.4.1 truncates nano seconds. + assertEquals(OffsetTime.of(11, 22, 33, 0, ZoneOffset.ofHoursMinutes(1, 23)), record.getOt()); } } @@ -72,4 +76,24 @@ public void shouldInsertOffsetDateTime() { } } + @Disabled("HSQLDB 2.4.1 does not support this.") + @Test + public void shouldInsertOffsetTime() { + OffsetTime ot = OffsetTime.of(11, 22, 33, 123456000, ZoneOffset.ofHoursMinutes(1, 23)); + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + Record record = new Record(); + record.setId(3); + record.setOt(ot); + int result = mapper.insertOffsetTime(record); + assertEquals(1, result); + sqlSession.commit(); + } + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + Record record = mapper.selectById(3); + assertEquals(ot, record.getOt()); + } + } + } diff --git a/src/test/java/org/apache/ibatis/type/OffsetTimeTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/OffsetTimeTypeHandlerTest.java index abaa506dbcb..1aa6d954871 100644 --- a/src/test/java/org/apache/ibatis/type/OffsetTimeTypeHandlerTest.java +++ b/src/test/java/org/apache/ibatis/type/OffsetTimeTypeHandlerTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2018 the original author or authors. + * Copyright 2009-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; -import java.sql.Time; import java.time.OffsetTime; import org.junit.jupiter.api.Test; @@ -26,21 +25,20 @@ public class OffsetTimeTypeHandlerTest extends BaseTypeHandlerTest { private static final TypeHandler TYPE_HANDLER = new OffsetTimeTypeHandler(); - // java.sql.Time doesn't contain millis, so set nano to 0 - private static final OffsetTime OFFSET_TIME = OffsetTime.now().withNano(0); - private static final Time TIME = Time.valueOf(OFFSET_TIME.toLocalTime()); + + private static final OffsetTime OFFSET_TIME = OffsetTime.now(); @Override @Test public void shouldSetParameter() throws Exception { TYPE_HANDLER.setParameter(ps, 1, OFFSET_TIME, null); - verify(ps).setTime(1, TIME); + verify(ps).setObject(1, OFFSET_TIME); } @Override @Test public void shouldGetResultFromResultSetByName() throws Exception { - when(rs.getTime("column")).thenReturn(TIME); + when(rs.getObject("column", OffsetTime.class)).thenReturn(OFFSET_TIME); assertEquals(OFFSET_TIME, TYPE_HANDLER.getResult(rs, "column")); verify(rs, never()).wasNull(); } @@ -48,7 +46,7 @@ public void shouldGetResultFromResultSetByName() throws Exception { @Override @Test public void shouldGetResultNullFromResultSetByName() throws Exception { - when(rs.getTime("column")).thenReturn(null); + when(rs.getObject("column", OffsetTime.class)).thenReturn(null); assertNull(TYPE_HANDLER.getResult(rs, "column")); verify(rs, never()).wasNull(); } @@ -56,7 +54,7 @@ public void shouldGetResultNullFromResultSetByName() throws Exception { @Override @Test public void shouldGetResultFromResultSetByPosition() throws Exception { - when(rs.getTime(1)).thenReturn(TIME); + when(rs.getObject(1, OffsetTime.class)).thenReturn(OFFSET_TIME); assertEquals(OFFSET_TIME, TYPE_HANDLER.getResult(rs, 1)); verify(rs, never()).wasNull(); } @@ -64,7 +62,7 @@ public void shouldGetResultFromResultSetByPosition() throws Exception { @Override @Test public void shouldGetResultNullFromResultSetByPosition() throws Exception { - when(rs.getTime(1)).thenReturn(null); + when(rs.getObject(1, OffsetTime.class)).thenReturn(null); assertNull(TYPE_HANDLER.getResult(rs, 1)); verify(rs, never()).wasNull(); } @@ -72,7 +70,7 @@ public void shouldGetResultNullFromResultSetByPosition() throws Exception { @Override @Test public void shouldGetResultFromCallableStatement() throws Exception { - when(cs.getTime(1)).thenReturn(TIME); + when(cs.getObject(1, OffsetTime.class)).thenReturn(OFFSET_TIME); assertEquals(OFFSET_TIME, TYPE_HANDLER.getResult(cs, 1)); verify(cs, never()).wasNull(); } @@ -80,7 +78,7 @@ public void shouldGetResultFromCallableStatement() throws Exception { @Override @Test public void shouldGetResultNullFromCallableStatement() throws Exception { - when(cs.getTime(1)).thenReturn(null); + when(cs.getObject(1, OffsetTime.class)).thenReturn(null); assertNull(TYPE_HANDLER.getResult(cs, 1)); verify(cs, never()).wasNull(); }