Skip to content
Merged
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
32 changes: 11 additions & 21 deletions src/main/java/org/apache/ibatis/type/BaseTypeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@
import org.apache.ibatis.session.Configuration;

/**
* The base {@link TypeHandler} for references a generic type.
* <p>
* Important: Since 3.5.0, This class never call the {@link ResultSet#wasNull()} and
* {@link CallableStatement#wasNull()} method for handling the SQL {@code NULL} value.
* In other words, {@code null} value handling should be performed on subclass.
* </p>
*
* @author Clinton Begin
* @author Simone Tripodi
* @author Kzuki Shimizu
*/
public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> {

Expand Down Expand Up @@ -69,47 +77,29 @@ public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbc

@Override
public T getResult(ResultSet rs, String columnName) throws SQLException {
T result;
try {
result = getNullableResult(rs, columnName);
return getNullableResult(rs, columnName);
} 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;
}
}

@Override
public T getResult(ResultSet rs, int columnIndex) throws SQLException {
T result;
try {
result = getNullableResult(rs, columnIndex);
return getNullableResult(rs, columnIndex);
} 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;
}
}

@Override
public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
T result;
try {
result = getNullableResult(cs, columnIndex);
return getNullableResult(cs, columnIndex);
} catch (Exception e) {
throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from callable statement. Cause: " + e, e);
}
if (cs.wasNull()) {
return null;
} else {
return result;
}
}

public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/apache/ibatis/type/BooleanTypeHandler.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 Down Expand Up @@ -34,18 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter,
@Override
public Boolean getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getBoolean(columnName);
boolean result = rs.getBoolean(columnName);
return (!result && rs.wasNull()) ? null : result;
}

@Override
public Boolean getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getBoolean(columnIndex);
boolean result = rs.getBoolean(columnIndex);
return (!result && rs.wasNull()) ? null : result;
}

@Override
public Boolean getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getBoolean(columnIndex);
boolean result = cs.getBoolean(columnIndex);
return (!result && cs.wasNull()) ? null : result;
}
}
11 changes: 7 additions & 4 deletions src/main/java/org/apache/ibatis/type/ByteTypeHandler.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 Down Expand Up @@ -34,18 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Byte parameter, Jdb
@Override
public Byte getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getByte(columnName);
byte result = rs.getByte(columnName);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Byte getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getByte(columnIndex);
byte result = rs.getByte(columnIndex);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Byte getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getByte(columnIndex);
byte result = cs.getByte(columnIndex);
return (result == 0 && cs.wasNull()) ? null : result;
}
}
28 changes: 9 additions & 19 deletions src/main/java/org/apache/ibatis/type/ClobTypeHandler.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 Down Expand Up @@ -37,36 +37,26 @@ public void setNonNullParameter(PreparedStatement ps, int i, String parameter, J
@Override
public String getNullableResult(ResultSet rs, String columnName)
throws SQLException {
String value = "";
Clob clob = rs.getClob(columnName);
if (clob != null) {
int size = (int) clob.length();
value = clob.getSubString(1, size);
}
return value;
return toString(clob);
}

@Override
public String getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
String value = "";
Clob clob = rs.getClob(columnIndex);
if (clob != null) {
int size = (int) clob.length();
value = clob.getSubString(1, size);
}
return value;
return toString(clob);
}

@Override
public String getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
String value = "";
Clob clob = cs.getClob(columnIndex);
if (clob != null) {
int size = (int) clob.length();
value = clob.getSubString(1, size);
}
return value;
return toString(clob);
}

private String toString(Clob clob) throws SQLException {
return clob == null ? null : clob.getSubString(1, (int) clob.length());
}

}
11 changes: 7 additions & 4 deletions src/main/java/org/apache/ibatis/type/DoubleTypeHandler.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 Down Expand Up @@ -34,19 +34,22 @@ public void setNonNullParameter(PreparedStatement ps, int i, Double parameter, J
@Override
public Double getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getDouble(columnName);
double result = rs.getDouble(columnName);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Double getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getDouble(columnIndex);
double result = rs.getDouble(columnIndex);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Double getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getDouble(columnIndex);
double result = cs.getDouble(columnIndex);
return (result == 0 && cs.wasNull()) ? null : result;
}

}
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 @@ -47,7 +47,7 @@ public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcTy
@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
int i = rs.getInt(columnName);
if (rs.wasNull()) {
if (i == 0 && rs.wasNull()) {
return null;
} else {
try {
Expand All @@ -61,7 +61,7 @@ public E getNullableResult(ResultSet rs, String columnName) throws SQLException
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int i = rs.getInt(columnIndex);
if (rs.wasNull()) {
if (i == 0 && rs.wasNull()) {
return null;
} else {
try {
Expand All @@ -75,7 +75,7 @@ public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int i = cs.getInt(columnIndex);
if (cs.wasNull()) {
if (i == 0 && cs.wasNull()) {
return null;
} else {
try {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/apache/ibatis/type/FloatTypeHandler.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 Down Expand Up @@ -34,18 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Float parameter, Jd
@Override
public Float getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getFloat(columnName);
float result = rs.getFloat(columnName);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Float getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getFloat(columnIndex);
float result = rs.getFloat(columnIndex);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Float getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getFloat(columnIndex);
float result = cs.getFloat(columnIndex);
return (result == 0 && cs.wasNull()) ? null : result;
}
}
11 changes: 7 additions & 4 deletions src/main/java/org/apache/ibatis/type/IntegerTypeHandler.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 Down Expand Up @@ -34,18 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Integer parameter,
@Override
public Integer getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getInt(columnName);
int result = rs.getInt(columnName);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Integer getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getInt(columnIndex);
int result = rs.getInt(columnIndex);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Integer getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getInt(columnIndex);
int result = cs.getInt(columnIndex);
return (result == 0 && cs.wasNull()) ? null : result;
}
}
11 changes: 7 additions & 4 deletions src/main/java/org/apache/ibatis/type/LongTypeHandler.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 Down Expand Up @@ -34,18 +34,21 @@ public void setNonNullParameter(PreparedStatement ps, int i, Long parameter, Jdb
@Override
public Long getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getLong(columnName);
long result = rs.getLong(columnName);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Long getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getLong(columnIndex);
long result = rs.getLong(columnIndex);
return (result == 0 && rs.wasNull()) ? null : result;
}

@Override
public Long getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getLong(columnIndex);
long result = cs.getLong(columnIndex);
return (result == 0 && cs.wasNull()) ? null : result;
}
}
8 changes: 4 additions & 4 deletions src/main/java/org/apache/ibatis/type/MonthTypeHandler.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 @@ -39,19 +39,19 @@ public void setNonNullParameter(PreparedStatement ps, int i, Month month, JdbcTy
@Override
public Month getNullableResult(ResultSet rs, String columnName) throws SQLException {
int month = rs.getInt(columnName);
return month == 0 ? null : Month.of(month);
return (month == 0 && rs.wasNull()) ? null : Month.of(month);
}

@Override
public Month getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int month = rs.getInt(columnIndex);
return month == 0 ? null : Month.of(month);
return (month == 0 && rs.wasNull()) ? null : Month.of(month);
}

@Override
public Month getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int month = cs.getInt(columnIndex);
return month == 0 ? null : Month.of(month);
return (month == 0 && cs.wasNull()) ? null : Month.of(month);
}

}
Loading