Skip to content

Commit

Permalink
Updated OffsetTimeTypeHandler to use PreparedStatement#setObject() an…
Browse files Browse the repository at this point in the history
…d ResultSet#getObject()

Related to mybatis#1081
  • Loading branch information
harawata committed Jan 10, 2019
1 parent fa5e185 commit 76254de
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 28 deletions.
20 changes: 5 additions & 15 deletions src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,7 +19,6 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.time.OffsetTime;

/**
Expand All @@ -31,31 +30,22 @@ public class OffsetTimeTypeHandler extends BaseTypeHandler<OffsetTime> {
@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
package org.apache.ibatis.submitted.timestamp_with_timezone;

import java.time.OffsetDateTime;
import java.time.OffsetTime;

public class Record {

private Integer id;

private OffsetDateTime odt;

private OffsetTime ot;

public Integer getId() {
return id;
}
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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());
}
}

Expand All @@ -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());
}
}

}
22 changes: 10 additions & 12 deletions src/test/java/org/apache/ibatis/type/OffsetTimeTypeHandlerTest.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -18,69 +18,67 @@
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;

public class OffsetTimeTypeHandlerTest extends BaseTypeHandlerTest {

private static final TypeHandler<OffsetTime> 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();
}

@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();
}

@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();
}

@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();
}

@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();
}

@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();
}
Expand Down

0 comments on commit 76254de

Please sign in to comment.