Skip to content

Commit 2a157ae

Browse files
authored
Merge pull request #1478 from kdubb/more_42_310
Update LocalDateTypeHandler, LocalTimeTypeHandler and LocalDateTimeTypeHandler to use `PreparedStatement#setObject()` and `ResultSet#getObject()`. This fixes the following potential issues. - LocalTimeTypeHandler : fractional seconds (nanoseconds) part is lost. - LocalDateTimeTypeHandler, LocalDateTypeHandler : value could be altered.
2 parents 555560b + 525b9c5 commit 2a157ae

16 files changed

+519
-77
lines changed
Lines changed: 5 additions & 16 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,7 +19,6 @@
1919
import java.sql.PreparedStatement;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22-
import java.sql.Timestamp;
2322
import java.time.LocalDateTime;
2423

2524
/**
@@ -31,31 +30,21 @@ public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {
3130
@Override
3231
public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType)
3332
throws SQLException {
34-
ps.setTimestamp(i, Timestamp.valueOf(parameter));
33+
ps.setObject(i, parameter);
3534
}
3635

3736
@Override
3837
public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
39-
Timestamp timestamp = rs.getTimestamp(columnName);
40-
return getLocalDateTime(timestamp);
38+
return rs.getObject(columnName, LocalDateTime.class);
4139
}
4240

4341
@Override
4442
public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
45-
Timestamp timestamp = rs.getTimestamp(columnIndex);
46-
return getLocalDateTime(timestamp);
43+
return rs.getObject(columnIndex, LocalDateTime.class);
4744
}
4845

4946
@Override
5047
public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
51-
Timestamp timestamp = cs.getTimestamp(columnIndex);
52-
return getLocalDateTime(timestamp);
53-
}
54-
55-
private static LocalDateTime getLocalDateTime(Timestamp timestamp) {
56-
if (timestamp != null) {
57-
return timestamp.toLocalDateTime();
58-
}
59-
return null;
48+
return cs.getObject(columnIndex, LocalDateTime.class);
6049
}
6150
}
Lines changed: 5 additions & 16 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.
@@ -16,7 +16,6 @@
1616
package org.apache.ibatis.type;
1717

1818
import java.sql.CallableStatement;
19-
import java.sql.Date;
2019
import java.sql.PreparedStatement;
2120
import java.sql.ResultSet;
2221
import java.sql.SQLException;
@@ -31,31 +30,21 @@ public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {
3130
@Override
3231
public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType)
3332
throws SQLException {
34-
ps.setDate(i, Date.valueOf(parameter));
33+
ps.setObject(i, parameter);
3534
}
3635

3736
@Override
3837
public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
39-
Date date = rs.getDate(columnName);
40-
return getLocalDate(date);
38+
return rs.getObject(columnName, LocalDate.class);
4139
}
4240

4341
@Override
4442
public LocalDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
45-
Date date = rs.getDate(columnIndex);
46-
return getLocalDate(date);
43+
return rs.getObject(columnIndex, LocalDate.class);
4744
}
4845

4946
@Override
5047
public LocalDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
51-
Date date = cs.getDate(columnIndex);
52-
return getLocalDate(date);
53-
}
54-
55-
private static LocalDate getLocalDate(Date date) {
56-
if (date != null) {
57-
return date.toLocalDate();
58-
}
59-
return null;
48+
return cs.getObject(columnIndex, LocalDate.class);
6049
}
6150
}
Lines changed: 5 additions & 16 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,7 +19,6 @@
1919
import java.sql.PreparedStatement;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22-
import java.sql.Time;
2322
import java.time.LocalTime;
2423

2524
/**
@@ -31,31 +30,21 @@ public class LocalTimeTypeHandler extends BaseTypeHandler<LocalTime> {
3130
@Override
3231
public void setNonNullParameter(PreparedStatement ps, int i, LocalTime parameter, JdbcType jdbcType)
3332
throws SQLException {
34-
ps.setTime(i, Time.valueOf(parameter));
33+
ps.setObject(i, parameter);
3534
}
3635

3736
@Override
3837
public LocalTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
39-
Time time = rs.getTime(columnName);
40-
return getLocalTime(time);
38+
return rs.getObject(columnName, LocalTime.class);
4139
}
4240

4341
@Override
4442
public LocalTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
45-
Time time = rs.getTime(columnIndex);
46-
return getLocalTime(time);
43+
return rs.getObject(columnIndex, LocalTime.class);
4744
}
4845

4946
@Override
5047
public LocalTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
51-
Time time = cs.getTime(columnIndex);
52-
return getLocalTime(time);
53-
}
54-
55-
private static LocalTime getLocalTime(Time time) {
56-
if (time != null) {
57-
return time.toLocalTime();
58-
}
59-
return null;
48+
return cs.getObject(columnIndex, LocalTime.class);
6049
}
6150
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2009-2019 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table records if exists;
18+
19+
create table records (
20+
id int,
21+
t time(9)
22+
);
23+
24+
insert into records (id, t) values
25+
(1, '11:22:33.123456789');
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.localtime;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import java.io.Reader;
21+
import java.time.LocalTime;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.Test;
30+
31+
public class LocalTimeTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeAll
36+
static void setUp() throws Exception {
37+
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/localtime/mybatis-config.xml")) {
38+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39+
}
40+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
41+
"org/apache/ibatis/submitted/localtime/CreateDB.sql");
42+
}
43+
44+
@Test
45+
void shouldSelectLocalTimeWithNanoseconds() {
46+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
47+
Mapper mapper = sqlSession.getMapper(Mapper.class);
48+
Record record = mapper.selectById(1);
49+
assertEquals(LocalTime.of(11, 22, 33, 123456789), record.getT());
50+
}
51+
}
52+
53+
@Test
54+
void shouldInsertLocalTimeWithNanoseconds() {
55+
LocalTime t = LocalTime.of(11, 22, 33, 123456789);
56+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
57+
Mapper mapper = sqlSession.getMapper(Mapper.class);
58+
Record record = new Record();
59+
record.setId(2);
60+
record.setT(t);
61+
int result = mapper.insertLocalTime(record);
62+
assertEquals(1, result);
63+
sqlSession.commit();
64+
}
65+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
66+
Mapper mapper = sqlSession.getMapper(Mapper.class);
67+
Record record = mapper.selectById(2);
68+
assertEquals(t, record.getT());
69+
}
70+
}
71+
72+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.localtime;
17+
18+
import org.apache.ibatis.annotations.Insert;
19+
import org.apache.ibatis.annotations.Select;
20+
21+
public interface Mapper {
22+
23+
@Select("select id, t from records where id = #{id}")
24+
Record selectById(Integer id);
25+
26+
@Insert("insert into records (id, t) values (#{id}, #{t})")
27+
int insertLocalTime(Record record);
28+
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.localtime;
17+
18+
import java.time.LocalTime;
19+
20+
public class Record {
21+
22+
private Integer id;
23+
24+
private LocalTime t;
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public LocalTime getT() {
35+
return t;
36+
}
37+
38+
public void setT(LocalTime t) {
39+
this.t = t;
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url" value="jdbc:hsqldb:mem:localtime" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<mappers>
39+
<mapper class="org.apache.ibatis.submitted.localtime.Mapper" />
40+
</mappers>
41+
42+
</configuration>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- Copyright 2009-2019 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table records if exists;
18+
19+
create table records (
20+
id int,
21+
ts timestamp(9),
22+
d date
23+
);
24+
25+
insert into records (id, ts, d) values
26+
(1, '2019-03-10 02:30:00', '2011-12-30');

0 commit comments

Comments
 (0)