diff --git a/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java b/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java index 8cb07cc2ec6..195b5d435f5 100644 --- a/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java +++ b/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java @@ -16,18 +16,16 @@ package org.apache.ibatis.type; import java.sql.CallableStatement; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import org.apache.ibatis.executor.result.ResultMapException; import org.apache.ibatis.session.Configuration; /** * @author Clinton Begin * @author Simone Tripodi */ -public abstract class BaseTypeHandler extends TypeReference implements TypeHandler { +public abstract class BaseTypeHandler extends NullableBaseTypeHandler { /** * @deprecated Since 3.5.0 - See https://github.com/mybatis/mybatis-3/issues/1203. This field will remove future. @@ -43,38 +41,9 @@ public void setConfiguration(Configuration c) { this.configuration = c; } - @Override - public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException { - if (parameter == null) { - if (jdbcType == null) { - throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters."); - } - try { - ps.setNull(i, jdbcType.TYPE_CODE); - } catch (SQLException e) { - throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . " + - "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. " + - "Cause: " + e, e); - } - } else { - try { - setNonNullParameter(ps, i, parameter, jdbcType); - } catch (Exception e) { - throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . " + - "Try setting a different JdbcType for this parameter or a different configuration property. " + - "Cause: " + e, e); - } - } - } - @Override public T getResult(ResultSet rs, String columnName) throws SQLException { - T result; - try { - result = getNullableResult(rs, columnName); - } catch (Exception e) { - throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e); - } + T result = super.getResult(rs, columnName); if (rs.wasNull()) { return null; } else { @@ -84,12 +53,7 @@ public T getResult(ResultSet rs, String columnName) throws SQLException { @Override public T getResult(ResultSet rs, int columnIndex) throws SQLException { - T result; - try { - result = getNullableResult(rs, columnIndex); - } catch (Exception e) { - throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from result set. Cause: " + e, e); - } + T result = super.getResult(rs, columnIndex); if (rs.wasNull()) { return null; } else { @@ -99,12 +63,7 @@ public T getResult(ResultSet rs, int columnIndex) throws SQLException { @Override public T getResult(CallableStatement cs, int columnIndex) throws SQLException { - T result; - try { - result = getNullableResult(cs, columnIndex); - } catch (Exception e) { - throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from callable statement. Cause: " + e, e); - } + T result = super.getResult(cs, columnIndex); if (cs.wasNull()) { return null; } else { @@ -112,12 +71,4 @@ public T getResult(CallableStatement cs, int columnIndex) throws SQLException { } } - public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; - - public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException; - - public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException; - - public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException; - } diff --git a/src/main/java/org/apache/ibatis/type/BigDecimalTypeHandler.java b/src/main/java/org/apache/ibatis/type/BigDecimalTypeHandler.java index 25cb73e99e8..d776ecefedb 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 NullableBaseTypeHandler { @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..1a2c3cef59a 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 NullableBaseTypeHandler { @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..c9ab0792f05 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 NullableBaseTypeHandler { @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..2767c6eeac3 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 NullableBaseTypeHandler { @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..37b617bf544 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 NullableBaseTypeHandler { @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..8cff7fbf675 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 NullableBaseTypeHandler { @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..5e5c4569df2 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 NullableBaseTypeHandler { @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..287b0ccb0be 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 NullableBaseTypeHandler { 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..5c972b51cea 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 NullableBaseTypeHandler { @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..e9b2669eac1 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 NullableBaseTypeHandler { @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..0483d1e57a8 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 NullableBaseTypeHandler { @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..2f48619d42a 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 NullableBaseTypeHandler { @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..b0a65ab0e5f 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 NullableBaseTypeHandler { @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..9d2b96a1d4c 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 NullableBaseTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) diff --git a/src/main/java/org/apache/ibatis/type/NullableBaseTypeHandler.java b/src/main/java/org/apache/ibatis/type/NullableBaseTypeHandler.java new file mode 100644 index 00000000000..06774ffe85d --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/NullableBaseTypeHandler.java @@ -0,0 +1,90 @@ +/** + * 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.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.apache.ibatis.executor.result.ResultMapException; + +/** + * @author Clinton Begin + * @author Simone Tripodi + */ +public abstract class NullableBaseTypeHandler extends TypeReference implements TypeHandler { + + @Override + public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException { + if (parameter == null) { + if (jdbcType == null) { + throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters."); + } + try { + ps.setNull(i, jdbcType.TYPE_CODE); + } catch (SQLException e) { + throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . " + + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. " + + "Cause: " + e, e); + } + } else { + try { + setNonNullParameter(ps, i, parameter, jdbcType); + } catch (Exception e) { + throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . " + + "Try setting a different JdbcType for this parameter or a different configuration property. " + + "Cause: " + e, e); + } + } + } + + @Override + public T getResult(ResultSet rs, String columnName) throws SQLException { + try { + return getNullableResult(rs, columnName); + } catch (Exception e) { + throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e); + } + } + + @Override + public T getResult(ResultSet rs, int columnIndex) throws SQLException { + try { + return getNullableResult(rs, columnIndex); + } catch (Exception e) { + throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from result set. Cause: " + e, e); + } + } + + @Override + public T getResult(CallableStatement cs, int columnIndex) throws SQLException { + try { + return getNullableResult(cs, columnIndex); + } catch (Exception e) { + throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from callable statement. Cause: " + e, e); + } + } + + public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; + + public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException; + + public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException; + + public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException; + +} diff --git a/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java index ecee2bd9379..a19793f7f12 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 NullableBaseTypeHandler { @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..0dbe465e6bf 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 NullableBaseTypeHandler { @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..e238522d0de 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 NullableBaseTypeHandler { @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..ab9caa72542 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