Skip to content

Commit

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

Related to mybatis#1081
  • Loading branch information
harawata committed Jan 11, 2019
1 parent 6267204 commit 11a70d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
22 changes: 5 additions & 17 deletions src/main/java/org/apache/ibatis/type/ZonedDateTimeTypeHandler.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,8 +19,6 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
Expand All @@ -32,31 +30,21 @@ public class ZonedDateTimeTypeHandler extends BaseTypeHandler<ZonedDateTime> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, ZonedDateTime parameter, JdbcType jdbcType)
throws SQLException {
ps.setTimestamp(i, Timestamp.from(parameter.toInstant()));
ps.setObject(i, parameter);
}

@Override
public ZonedDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName);
return getZonedDateTime(timestamp);
return rs.getObject(columnName, ZonedDateTime.class);
}

@Override
public ZonedDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex);
return getZonedDateTime(timestamp);
return rs.getObject(columnIndex, ZonedDateTime.class);
}

@Override
public ZonedDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp timestamp = cs.getTimestamp(columnIndex);
return getZonedDateTime(timestamp);
}

private static ZonedDateTime getZonedDateTime(Timestamp timestamp) {
if (timestamp != null) {
return ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
}
return null;
return cs.getObject(columnIndex, ZonedDateTime.class);
}
}
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,7 +18,6 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.sql.Timestamp;
import java.time.ZonedDateTime;

import org.junit.jupiter.api.Test;
Expand All @@ -27,59 +26,58 @@ public class ZonedDateTimeTypeHandlerTest extends BaseTypeHandlerTest {

private static final TypeHandler<ZonedDateTime> TYPE_HANDLER = new ZonedDateTimeTypeHandler();
private static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.now();
private static final Timestamp TIMESTAMP = Timestamp.from(ZONED_DATE_TIME.toInstant());

@Override
@Test
public void shouldSetParameter() throws Exception {
TYPE_HANDLER.setParameter(ps, 1, ZONED_DATE_TIME, null);
verify(ps).setTimestamp(1, TIMESTAMP);
verify(ps).setObject(1, ZONED_DATE_TIME);
}

@Override
@Test
public void shouldGetResultFromResultSetByName() throws Exception {
when(rs.getTimestamp("column")).thenReturn(TIMESTAMP);
when(rs.getObject("column", ZonedDateTime.class)).thenReturn(ZONED_DATE_TIME);
assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(rs, "column"));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromResultSetByName() throws Exception {
when(rs.getTimestamp("column")).thenReturn(null);
when(rs.getObject("column", ZonedDateTime.class)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(rs, "column"));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultFromResultSetByPosition() throws Exception {
when(rs.getTimestamp(1)).thenReturn(TIMESTAMP);
when(rs.getObject(1, ZonedDateTime.class)).thenReturn(ZONED_DATE_TIME);
assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(rs, 1));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromResultSetByPosition() throws Exception {
when(rs.getTimestamp(1)).thenReturn(null);
when(rs.getObject(1, ZonedDateTime.class)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(rs, 1));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultFromCallableStatement() throws Exception {
when(cs.getTimestamp(1)).thenReturn(TIMESTAMP);
when(cs.getObject(1, ZonedDateTime.class)).thenReturn(ZONED_DATE_TIME);
assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(cs, 1));
verify(cs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromCallableStatement() throws Exception {
when(cs.getTimestamp(1)).thenReturn(null);
when(cs.getObject(1, ZonedDateTime.class)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(cs, 1));
verify(cs, never()).wasNull();
}
Expand Down

0 comments on commit 11a70d0

Please sign in to comment.