Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add NullableBaseTypeHandler for avoid unnecessary wasNull() call #1242

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 4 additions & 53 deletions src/main/java/org/apache/ibatis/type/BaseTypeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends TypeReference<T> implements TypeHandler<T> {
public abstract class BaseTypeHandler<T> extends NullableBaseTypeHandler<T> {

/**
* @deprecated Since 3.5.0 - See https://github.com/mybatis/mybatis-3/issues/1203. This field will remove future.
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -99,25 +63,12 @@ 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 {
return result;
}
}

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;

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,7 +24,7 @@
/**
* @author Clinton Begin
*/
public class BigDecimalTypeHandler extends BaseTypeHandler<BigDecimal> {
public class BigDecimalTypeHandler extends NullableBaseTypeHandler<BigDecimal> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,7 +25,7 @@
/**
* @author Paul Krause
*/
public class BigIntegerTypeHandler extends BaseTypeHandler<BigInteger> {
public class BigIntegerTypeHandler extends NullableBaseTypeHandler<BigInteger> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, BigInteger parameter, JdbcType jdbcType) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,7 +23,7 @@
/**
* @author Clinton Begin
*/
public class ByteArrayTypeHandler extends BaseTypeHandler<byte[]> {
public class ByteArrayTypeHandler extends NullableBaseTypeHandler<byte[]> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, byte[] parameter, JdbcType jdbcType)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,7 +23,7 @@
/**
* @author Clinton Begin
*/
public class ByteObjectArrayTypeHandler extends BaseTypeHandler<Byte[]> {
public class ByteObjectArrayTypeHandler extends NullableBaseTypeHandler<Byte[]> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Byte[] parameter, JdbcType jdbcType) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,7 +23,7 @@
/**
* @author Clinton Begin
*/
public class CharacterTypeHandler extends BaseTypeHandler<Character> {
public class CharacterTypeHandler extends NullableBaseTypeHandler<Character> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Character parameter, JdbcType jdbcType) throws SQLException {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/type/DateOnlyTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,7 +24,7 @@
/**
* @author Clinton Begin
*/
public class DateOnlyTypeHandler extends BaseTypeHandler<Date> {
public class DateOnlyTypeHandler extends NullableBaseTypeHandler<Date> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/type/DateTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,7 +25,7 @@
/**
* @author Clinton Begin
*/
public class DateTypeHandler extends BaseTypeHandler<Date> {
public class DateTypeHandler extends NullableBaseTypeHandler<Date> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/type/EnumTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,7 +23,7 @@
/**
* @author Clinton Begin
*/
public class EnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
public class EnumTypeHandler<E extends Enum<E>> extends NullableBaseTypeHandler<E> {

private final Class<E> type;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/type/InstantTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,7 @@
* @author Tomas Rohovsky
*/
@UsesJava8
public class InstantTypeHandler extends BaseTypeHandler<Instant> {
public class InstantTypeHandler extends NullableBaseTypeHandler<Instant> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Instant parameter, JdbcType jdbcType) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -32,7 +32,7 @@
* @author Kazuki Shimizu
*/
@UsesJava8
public class JapaneseDateTypeHandler extends BaseTypeHandler<JapaneseDate> {
public class JapaneseDateTypeHandler extends NullableBaseTypeHandler<JapaneseDate> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, JapaneseDate parameter, JdbcType jdbcType)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,7 @@
* @author Tomas Rohovsky
*/
@UsesJava8
public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {
public class LocalDateTimeTypeHandler extends NullableBaseTypeHandler<LocalDateTime> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,7 @@
* @author Tomas Rohovsky
*/
@UsesJava8
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {
public class LocalDateTypeHandler extends NullableBaseTypeHandler<LocalDate> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,7 @@
* @author Tomas Rohovsky
*/
@UsesJava8
public class LocalTimeTypeHandler extends BaseTypeHandler<LocalTime> {
public class LocalTimeTypeHandler extends NullableBaseTypeHandler<LocalTime> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalTime parameter, JdbcType jdbcType)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/type/NStringTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,7 +23,7 @@
/**
* @author Clinton Begin
*/
public class NStringTypeHandler extends BaseTypeHandler<String> {
public class NStringTypeHandler extends NullableBaseTypeHandler<String> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
Expand Down
Loading