Skip to content

Commit b36cb7e

Browse files
committed
Add new base type handler for using nullable JDBC APIs
The original PR is #1242
1 parent d30f647 commit b36cb7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+317
-169
lines changed

src/main/java/org/apache/ibatis/type/BaseTypeHandler.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import org.apache.ibatis.session.Configuration;
2525

2626
/**
27+
* The base {@link TypeHandler} for references a generic type.
28+
*
2729
* @author Clinton Begin
2830
* @author Simone Tripodi
31+
* @author Kzuki Shimizu
2932
*/
3033
public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> {
3134

@@ -75,11 +78,7 @@ public T getResult(ResultSet rs, String columnName) throws SQLException {
7578
} catch (Exception e) {
7679
throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e);
7780
}
78-
if (rs.wasNull()) {
79-
return null;
80-
} else {
81-
return result;
82-
}
81+
return handleResult(rs, result);
8382
}
8483

8584
@Override
@@ -90,11 +89,7 @@ public T getResult(ResultSet rs, int columnIndex) throws SQLException {
9089
} catch (Exception e) {
9190
throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from result set. Cause: " + e, e);
9291
}
93-
if (rs.wasNull()) {
94-
return null;
95-
} else {
96-
return result;
97-
}
92+
return handleResult(rs, result);
9893
}
9994

10095
@Override
@@ -105,6 +100,40 @@ public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
105100
} catch (Exception e) {
106101
throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from callable statement. Cause: " + e, e);
107102
}
103+
return handleResult(cs, result);
104+
}
105+
106+
/**
107+
* Handle a fetched result value from {@link ResultSet}.
108+
*
109+
* @param rs a {@link ResultSet}
110+
* @param result a fetched value
111+
* @return If {@link ResultSet#wasNull()} return {@code true}, it return {@code null}. Otherwise
112+
* return a fetched result value.
113+
* @exception SQLException if a database access error occurs or this method is
114+
* called on a closed result set
115+
* @since 3.5.0
116+
*/
117+
protected T handleResult(ResultSet rs, T result) throws SQLException {
118+
if (rs.wasNull()) {
119+
return null;
120+
} else {
121+
return result;
122+
}
123+
}
124+
125+
/**
126+
* Handle a fetched result value
127+
*
128+
* @param cs a {@link CallableStatement}
129+
* @param result a fetched value
130+
* @return If {@link ResultSet#wasNull()} return {@code true}, it return {@code null}. Otherwise
131+
* return a fetched result value.
132+
* @exception SQLException if a database access error occurs or this method is
133+
* called on a closed result set
134+
* @since 3.5.0
135+
*/
136+
protected T handleResult(CallableStatement cs, T result) throws SQLException {
108137
if (cs.wasNull()) {
109138
return null;
110139
} else {

src/main/java/org/apache/ibatis/type/BigDecimalTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -24,7 +24,7 @@
2424
/**
2525
* @author Clinton Begin
2626
*/
27-
public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal> {
27+
public class BigDecimalTypeHandler extends NullableApiBasedTypeHandler<BigDecimal> {
2828

2929
@Override
3030
public void setNonNullParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType)

src/main/java/org/apache/ibatis/type/BigIntegerTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -25,7 +25,7 @@
2525
/**
2626
* @author Paul Krause
2727
*/
28-
public class BigIntegerTypeHandler extends BaseTypeHandler<BigInteger> {
28+
public class BigIntegerTypeHandler extends NullableApiBasedTypeHandler<BigInteger> {
2929

3030
@Override
3131
public void setNonNullParameter(PreparedStatement ps, int i, BigInteger parameter, JdbcType jdbcType) throws SQLException {

src/main/java/org/apache/ibatis/type/ByteArrayTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -23,7 +23,7 @@
2323
/**
2424
* @author Clinton Begin
2525
*/
26-
public class ByteArrayTypeHandler extends BaseTypeHandler<byte[]> {
26+
public class ByteArrayTypeHandler extends NullableApiBasedTypeHandler<byte[]> {
2727

2828
@Override
2929
public void setNonNullParameter(PreparedStatement ps, int i, byte[] parameter, JdbcType jdbcType)

src/main/java/org/apache/ibatis/type/ByteObjectArrayTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -23,7 +23,7 @@
2323
/**
2424
* @author Clinton Begin
2525
*/
26-
public class ByteObjectArrayTypeHandler extends BaseTypeHandler<Byte[]> {
26+
public class ByteObjectArrayTypeHandler extends NullableApiBasedTypeHandler<Byte[]> {
2727

2828
@Override
2929
public void setNonNullParameter(PreparedStatement ps, int i, Byte[] parameter, JdbcType jdbcType) throws SQLException {

src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -23,7 +23,7 @@
2323
/**
2424
* @author Clinton Begin
2525
*/
26-
public class CharacterTypeHandler extends BaseTypeHandler<Character> {
26+
public class CharacterTypeHandler extends NullableApiBasedTypeHandler<Character> {
2727

2828
@Override
2929
public void setNonNullParameter(PreparedStatement ps, int i, Character parameter, JdbcType jdbcType) throws SQLException {

src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -24,7 +24,7 @@
2424
/**
2525
* @author Clinton Begin
2626
*/
27-
public class DateOnlyTypeHandler extends BaseTypeHandler<Date> {
27+
public class DateOnlyTypeHandler extends NullableApiBasedTypeHandler<Date> {
2828

2929
@Override
3030
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType)

src/main/java/org/apache/ibatis/type/DateTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -25,7 +25,7 @@
2525
/**
2626
* @author Clinton Begin
2727
*/
28-
public class DateTypeHandler extends BaseTypeHandler<Date> {
28+
public class DateTypeHandler extends NullableApiBasedTypeHandler<Date> {
2929

3030
@Override
3131
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType)

src/main/java/org/apache/ibatis/type/EnumTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -23,7 +23,7 @@
2323
/**
2424
* @author Clinton Begin
2525
*/
26-
public class EnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
26+
public class EnumTypeHandler<E extends Enum<E>> extends NullableApiBasedTypeHandler<E> {
2727

2828
private final Class<E> type;
2929

src/main/java/org/apache/ibatis/type/InstantTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -29,7 +29,7 @@
2929
* @author Tomas Rohovsky
3030
*/
3131
@UsesJava8
32-
public class InstantTypeHandler extends BaseTypeHandler<Instant> {
32+
public class InstantTypeHandler extends NullableApiBasedTypeHandler<Instant> {
3333

3434
@Override
3535
public void setNonNullParameter(PreparedStatement ps, int i, Instant parameter, JdbcType jdbcType) throws SQLException {

0 commit comments

Comments
 (0)