diff --git a/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java b/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java index 8cb07cc2ec6..0878aafbc41 100644 --- a/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java @@ -24,8 +24,11 @@ import org.apache.ibatis.session.Configuration; /** + * The base {@link TypeHandler} for references a generic type. + * * @author Clinton Begin * @author Simone Tripodi + * @author Kzuki Shimizu */ public abstract class BaseTypeHandler extends TypeReference implements TypeHandler { @@ -75,11 +78,7 @@ public T getResult(ResultSet rs, String columnName) throws SQLException { } catch (Exception e) { throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e); } - if (rs.wasNull()) { - return null; - } else { - return result; - } + return handleResult(rs, result); } @Override @@ -90,11 +89,7 @@ public T getResult(ResultSet rs, int columnIndex) throws SQLException { } catch (Exception e) { throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from result set. Cause: " + e, e); } - if (rs.wasNull()) { - return null; - } else { - return result; - } + return handleResult(rs, result); } @Override @@ -105,6 +100,40 @@ public T getResult(CallableStatement cs, int columnIndex) throws SQLException { } catch (Exception e) { throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from callable statement. Cause: " + e, e); } + return handleResult(cs, result); + } + + /** + * Handle a fetched result value from {@link ResultSet}. + * + * @param rs a {@link ResultSet} + * @param result a fetched value + * @return If {@link ResultSet#wasNull()} return {@code true}, it return {@code null}. Otherwise + * return a fetched result value. + * @exception SQLException if a database access error occurs or this method is + * called on a closed result set + * @since 3.5.0 + */ + protected T handleResult(ResultSet rs, T result) throws SQLException { + if (rs.wasNull()) { + return null; + } else { + return result; + } + } + + /** + * Handle a fetched result value + * + * @param cs a {@link CallableStatement} + * @param result a fetched value + * @return If {@link ResultSet#wasNull()} return {@code true}, it return {@code null}. Otherwise + * return a fetched result value. + * @exception SQLException if a database access error occurs or this method is + * called on a closed result set + * @since 3.5.0 + */ + protected T handleResult(CallableStatement cs, T result) throws SQLException { if (cs.wasNull()) { return null; } else { diff --git a/src/main/java/org/apache/ibatis/type/BigDecimalTypeHandler.java b/src/main/java/org/apache/ibatis/type/BigDecimalTypeHandler.java index 25cb73e99e8..e9db8b3ad54 100644 --- a/src/main/java/org/apache/ibatis/type/BigDecimalTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/BigDecimalTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -24,7 +24,7 @@ /** * @author Clinton Begin */ -public class BigDecimalTypeHandler extends BaseTypeHandler { +public class BigDecimalTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/BigIntegerTypeHandler.java b/src/main/java/org/apache/ibatis/type/BigIntegerTypeHandler.java index ec3bd8b09bb..2c6f53cf53a 100644 --- a/src/main/java/org/apache/ibatis/type/BigIntegerTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/BigIntegerTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -25,7 +25,7 @@ /** * @author Paul Krause */ -public class BigIntegerTypeHandler extends BaseTypeHandler { +public class BigIntegerTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, BigInteger parameter, JdbcType jdbcType) throws SQLException { diff --git a/src/main/java/org/apache/ibatis/type/ByteArrayTypeHandler.java b/src/main/java/org/apache/ibatis/type/ByteArrayTypeHandler.java index a29690c1e05..a199da19d40 100644 --- a/src/main/java/org/apache/ibatis/type/ByteArrayTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/ByteArrayTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -23,7 +23,7 @@ /** * @author Clinton Begin */ -public class ByteArrayTypeHandler extends BaseTypeHandler { +public class ByteArrayTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, byte[] parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/ByteObjectArrayTypeHandler.java b/src/main/java/org/apache/ibatis/type/ByteObjectArrayTypeHandler.java index a1e379a2c11..b901212c73c 100644 --- a/src/main/java/org/apache/ibatis/type/ByteObjectArrayTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/ByteObjectArrayTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -23,7 +23,7 @@ /** * @author Clinton Begin */ -public class ByteObjectArrayTypeHandler extends BaseTypeHandler { +public class ByteObjectArrayTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, Byte[] parameter, JdbcType jdbcType) throws SQLException { diff --git a/src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java b/src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java index 4dec7fe73d4..4b26b390df6 100644 --- a/src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/CharacterTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -23,7 +23,7 @@ /** * @author Clinton Begin */ -public class CharacterTypeHandler extends BaseTypeHandler { +public class CharacterTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, Character parameter, JdbcType jdbcType) throws SQLException { diff --git a/src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java b/src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java index 21e542331e6..b9d70b795db 100644 --- a/src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -24,7 +24,7 @@ /** * @author Clinton Begin */ -public class DateOnlyTypeHandler extends BaseTypeHandler { +public class DateOnlyTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/DateTypeHandler.java b/src/main/java/org/apache/ibatis/type/DateTypeHandler.java index 92b74fdbff8..3dda37be645 100644 --- a/src/main/java/org/apache/ibatis/type/DateTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/DateTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -25,7 +25,7 @@ /** * @author Clinton Begin */ -public class DateTypeHandler extends BaseTypeHandler { +public class DateTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/EnumTypeHandler.java b/src/main/java/org/apache/ibatis/type/EnumTypeHandler.java index 510fd2650a9..8ae9853473a 100644 --- a/src/main/java/org/apache/ibatis/type/EnumTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/EnumTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -23,7 +23,7 @@ /** * @author Clinton Begin */ -public class EnumTypeHandler> extends BaseTypeHandler { +public class EnumTypeHandler> extends NullableApiBasedTypeHandler { private final Class type; diff --git a/src/main/java/org/apache/ibatis/type/InstantTypeHandler.java b/src/main/java/org/apache/ibatis/type/InstantTypeHandler.java index 3dee9c35ffa..9318be88816 100644 --- a/src/main/java/org/apache/ibatis/type/InstantTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/InstantTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -29,7 +29,7 @@ * @author Tomas Rohovsky */ @UsesJava8 -public class InstantTypeHandler extends BaseTypeHandler { +public class InstantTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, Instant parameter, JdbcType jdbcType) throws SQLException { diff --git a/src/main/java/org/apache/ibatis/type/JapaneseDateTypeHandler.java b/src/main/java/org/apache/ibatis/type/JapaneseDateTypeHandler.java index 729aa9b4cce..8961ae7726d 100644 --- a/src/main/java/org/apache/ibatis/type/JapaneseDateTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/JapaneseDateTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -32,7 +32,7 @@ * @author Kazuki Shimizu */ @UsesJava8 -public class JapaneseDateTypeHandler extends BaseTypeHandler { +public class JapaneseDateTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, JapaneseDate parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/LocalDateTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/LocalDateTimeTypeHandler.java index a7b377ab3c9..edfa804a646 100644 --- a/src/main/java/org/apache/ibatis/type/LocalDateTimeTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/LocalDateTimeTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -29,7 +29,7 @@ * @author Tomas Rohovsky */ @UsesJava8 -public class LocalDateTimeTypeHandler extends BaseTypeHandler { +public class LocalDateTimeTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/LocalDateTypeHandler.java b/src/main/java/org/apache/ibatis/type/LocalDateTypeHandler.java index 896d7657053..3df89ac1e72 100644 --- a/src/main/java/org/apache/ibatis/type/LocalDateTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/LocalDateTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -29,7 +29,7 @@ * @author Tomas Rohovsky */ @UsesJava8 -public class LocalDateTypeHandler extends BaseTypeHandler { +public class LocalDateTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/LocalTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/LocalTimeTypeHandler.java index c17a21f5493..4837fed9a5c 100644 --- a/src/main/java/org/apache/ibatis/type/LocalTimeTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/LocalTimeTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -29,7 +29,7 @@ * @author Tomas Rohovsky */ @UsesJava8 -public class LocalTimeTypeHandler extends BaseTypeHandler { +public class LocalTimeTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, LocalTime parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/NStringTypeHandler.java b/src/main/java/org/apache/ibatis/type/NStringTypeHandler.java index 91c5b632de7..fba76a70999 100644 --- a/src/main/java/org/apache/ibatis/type/NStringTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/NStringTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -23,7 +23,7 @@ /** * @author Clinton Begin */ -public class NStringTypeHandler extends BaseTypeHandler { +public class NStringTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/NullableApiBasedTypeHandler.java b/src/main/java/org/apache/ibatis/type/NullableApiBasedTypeHandler.java new file mode 100644 index 00000000000..60c58182c25 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/NullableApiBasedTypeHandler.java @@ -0,0 +1,76 @@ +/** + * Copyright 2009-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.type; + +import java.sql.CallableStatement; +import java.sql.ResultSet; + +/** + * The base {@link TypeHandler} for using nullable JDBC APIs (APIs that it return null, + * if the value is SQL NULL). + *

+ * Nullable JDBC APIs are follows on {@link ResultSet} or {@link CallableStatement}: + *

+ *
    + *
  • getAsciiStream()
  • + *
  • getBigDecimal()
  • + *
  • getBinaryStream()
  • + *
  • getBytes()
  • + *
  • getCharacterStream()
  • + *
  • getDate()
  • + *
  • getNCharacterStream()
  • + *
  • getNString()
  • + *
  • getRowId()
  • + *
  • getString()
  • + *
  • getTime()
  • + *
  • getTimestamp()
  • + *
  • getUnicodeStream()
  • + *
  • getURL()
  • + *
+ *

+ * If you create a custom {@link TypeHandler} using above APIs, we propose to use this class instead + * of the {@link BaseTypeHandler}. This class is efficient than the {@link BaseTypeHandler} because + * it does not call the wasNull() on {@link ResultSet} or {@link CallableStatement} for + * handling null value. + *

+ * + * @param the referenced type + * @author Kazuki Shimizu + * @since 3.5.0 + */ +public abstract class NullableApiBasedTypeHandler extends BaseTypeHandler { + + /** + * {@inheritDoc} + * + * @return Always return a fetched result + */ + @Override + protected T handleResult(ResultSet rs, T result) { + return result; + } + + /** + * {@inheritDoc} + * + * @return Always return a fetched result + */ + @Override + protected T handleResult(CallableStatement cs, T result) { + return result; + } + +} diff --git a/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java index ecee2bd9379..ef5e0db75f6 100644 --- a/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -30,7 +30,7 @@ * @author Tomas Rohovsky */ @UsesJava8 -public class OffsetDateTimeTypeHandler extends BaseTypeHandler { +public class OffsetDateTimeTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, OffsetDateTime parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java index 22104eceaf0..7277dc6e07e 100644 --- a/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -29,7 +29,7 @@ * @author Tomas Rohovsky */ @UsesJava8 -public class OffsetTimeTypeHandler extends BaseTypeHandler { +public class OffsetTimeTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, OffsetTime parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/SqlDateTypeHandler.java b/src/main/java/org/apache/ibatis/type/SqlDateTypeHandler.java index ef96d0a4ea4..7bdb4188235 100644 --- a/src/main/java/org/apache/ibatis/type/SqlDateTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/SqlDateTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -24,7 +24,7 @@ /** * @author Clinton Begin */ -public class SqlDateTypeHandler extends BaseTypeHandler { +public class SqlDateTypeHandler extends NullableApiBasedTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/SqlTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/SqlTimeTypeHandler.java index 4aa0b5f848f..3214385e3a3 100644 --- a/src/main/java/org/apache/ibatis/type/SqlTimeTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/SqlTimeTypeHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -24,7 +24,7 @@ /** * @author Clinton Begin */ -public class SqlTimeTypeHandler extends BaseTypeHandler