Skip to content

Commit efa1805

Browse files
authored
Merge pull request #1438 from harawata/zoneddatetimetypehandler-fix
ZonedDateTimeTypeHandler should use setObject(), getObject()
2 parents 39175d6 + 11a70d0 commit efa1805

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2018 the original author or authors.
2+
* Copyright 2009-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,6 @@
1919
import java.sql.PreparedStatement;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22-
import java.sql.Timestamp;
23-
import java.time.ZoneId;
2422
import java.time.ZonedDateTime;
2523

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

3836
@Override
3937
public ZonedDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
40-
Timestamp timestamp = rs.getTimestamp(columnName);
41-
return getZonedDateTime(timestamp);
38+
return rs.getObject(columnName, ZonedDateTime.class);
4239
}
4340

4441
@Override
4542
public ZonedDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
46-
Timestamp timestamp = rs.getTimestamp(columnIndex);
47-
return getZonedDateTime(timestamp);
43+
return rs.getObject(columnIndex, ZonedDateTime.class);
4844
}
4945

5046
@Override
5147
public ZonedDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
52-
Timestamp timestamp = cs.getTimestamp(columnIndex);
53-
return getZonedDateTime(timestamp);
54-
}
55-
56-
private static ZonedDateTime getZonedDateTime(Timestamp timestamp) {
57-
if (timestamp != null) {
58-
return ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
59-
}
60-
return null;
48+
return cs.getObject(columnIndex, ZonedDateTime.class);
6149
}
6250
}

src/test/java/org/apache/ibatis/type/ZonedDateTimeTypeHandlerTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2018 the original author or authors.
2+
* Copyright 2009-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818
import static org.junit.jupiter.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
2020

21-
import java.sql.Timestamp;
2221
import java.time.ZonedDateTime;
2322

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

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

3230
@Override
3331
@Test
3432
public void shouldSetParameter() throws Exception {
3533
TYPE_HANDLER.setParameter(ps, 1, ZONED_DATE_TIME, null);
36-
verify(ps).setTimestamp(1, TIMESTAMP);
34+
verify(ps).setObject(1, ZONED_DATE_TIME);
3735
}
3836

3937
@Override
4038
@Test
4139
public void shouldGetResultFromResultSetByName() throws Exception {
42-
when(rs.getTimestamp("column")).thenReturn(TIMESTAMP);
40+
when(rs.getObject("column", ZonedDateTime.class)).thenReturn(ZONED_DATE_TIME);
4341
assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(rs, "column"));
4442
verify(rs, never()).wasNull();
4543
}
4644

4745
@Override
4846
@Test
4947
public void shouldGetResultNullFromResultSetByName() throws Exception {
50-
when(rs.getTimestamp("column")).thenReturn(null);
48+
when(rs.getObject("column", ZonedDateTime.class)).thenReturn(null);
5149
assertNull(TYPE_HANDLER.getResult(rs, "column"));
5250
verify(rs, never()).wasNull();
5351
}
5452

5553
@Override
5654
@Test
5755
public void shouldGetResultFromResultSetByPosition() throws Exception {
58-
when(rs.getTimestamp(1)).thenReturn(TIMESTAMP);
56+
when(rs.getObject(1, ZonedDateTime.class)).thenReturn(ZONED_DATE_TIME);
5957
assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(rs, 1));
6058
verify(rs, never()).wasNull();
6159
}
6260

6361
@Override
6462
@Test
6563
public void shouldGetResultNullFromResultSetByPosition() throws Exception {
66-
when(rs.getTimestamp(1)).thenReturn(null);
64+
when(rs.getObject(1, ZonedDateTime.class)).thenReturn(null);
6765
assertNull(TYPE_HANDLER.getResult(rs, 1));
6866
verify(rs, never()).wasNull();
6967
}
7068

7169
@Override
7270
@Test
7371
public void shouldGetResultFromCallableStatement() throws Exception {
74-
when(cs.getTimestamp(1)).thenReturn(TIMESTAMP);
72+
when(cs.getObject(1, ZonedDateTime.class)).thenReturn(ZONED_DATE_TIME);
7573
assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(cs, 1));
7674
verify(cs, never()).wasNull();
7775
}
7876

7977
@Override
8078
@Test
8179
public void shouldGetResultNullFromCallableStatement() throws Exception {
82-
when(cs.getTimestamp(1)).thenReturn(null);
80+
when(cs.getObject(1, ZonedDateTime.class)).thenReturn(null);
8381
assertNull(TYPE_HANDLER.getResult(cs, 1));
8482
verify(cs, never()).wasNull();
8583
}

0 commit comments

Comments
 (0)