Skip to content

Commit

Permalink
Move wasNull to subclass from BaseTypeHandler mybatis#1242
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuki43zoo committed Apr 5, 2018
1 parent d30f647 commit 67d1b59
Show file tree
Hide file tree
Showing 61 changed files with 581 additions and 342 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/apache/ibatis/type/ArrayTypeHandler.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 @@ -38,19 +38,19 @@ public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, J
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
Array array = rs.getArray(columnName);
return array == null ? null : array.getArray();
return (array == null || rs.wasNull()) ? null : array.getArray();
}

@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Array array = rs.getArray(columnIndex);
return array == null ? null : array.getArray();
return (array == null || rs.wasNull()) ? null : array.getArray();
}

@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Array array = cs.getArray(columnIndex);
return array == null ? null : array.getArray();
return (array == null || cs.wasNull()) ? null : array.getArray();
}

}
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
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,28 +34,25 @@ public void setNonNullParameter(PreparedStatement ps, int i, Byte[] parameter, J
public Byte[] getNullableResult(ResultSet rs, String columnName)
throws SQLException {
Blob blob = rs.getBlob(columnName);
return getBytes(blob);
return (blob == null || rs.wasNull()) ? null : getBytes(blob);
}

@Override
public Byte[] getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
Blob blob = rs.getBlob(columnIndex);
return getBytes(blob);
return (blob == null || rs.wasNull()) ? null : getBytes(blob);
}

@Override
public Byte[] getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
Blob blob = cs.getBlob(columnIndex);
return getBytes(blob);
return (blob == null || cs.wasNull()) ? null : getBytes(blob);
}

private Byte[] getBytes(Blob blob) throws SQLException {
Byte[] returnValue = null;
if (blob != null) {
returnValue = ByteArrayUtils.convertToObjectArray(blob.getBytes(1, (int) blob.length()));
}
return returnValue;
return ByteArrayUtils.convertToObjectArray(blob.getBytes(1, (int) blob.length()));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2016 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 @@ -42,7 +42,8 @@ public void setNonNullParameter(PreparedStatement ps, int i, InputStream paramet
@Override
public InputStream getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return toInputStream(rs.getBlob(columnName));
Blob blob = rs.getBlob(columnName);
return (blob == null || rs.wasNull()) ? null : blob.getBinaryStream();
}

/**
Expand All @@ -52,7 +53,8 @@ public InputStream getNullableResult(ResultSet rs, String columnName)
@Override
public InputStream getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return toInputStream(rs.getBlob(columnIndex));
Blob blob = rs.getBlob(columnIndex);
return (blob == null || rs.wasNull()) ? null : blob.getBinaryStream();
}

/**
Expand All @@ -62,15 +64,8 @@ public InputStream getNullableResult(ResultSet rs, int columnIndex)
@Override
public InputStream getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return toInputStream(cs.getBlob(columnIndex));
}

private InputStream toInputStream(Blob blob) throws SQLException {
if (blob == null) {
return null;
} else {
return blob.getBinaryStream();
}
Blob blob = cs.getBlob(columnIndex);
return (blob == null || cs.wasNull()) ? null : blob.getBinaryStream();
}

}
20 changes: 4 additions & 16 deletions src/main/java/org/apache/ibatis/type/BlobTypeHandler.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 @@ -38,32 +38,20 @@ public void setNonNullParameter(PreparedStatement ps, int i, byte[] parameter, J
public byte[] getNullableResult(ResultSet rs, String columnName)
throws SQLException {
Blob blob = rs.getBlob(columnName);
byte[] returnValue = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
return returnValue;
return (blob == null || rs.wasNull()) ? null : blob.getBytes(1, (int) blob.length());
}

@Override
public byte[] getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
Blob blob = rs.getBlob(columnIndex);
byte[] returnValue = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
return returnValue;
return (blob == null || rs.wasNull()) ? null : blob.getBytes(1, (int) blob.length());
}

@Override
public byte[] getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
Blob blob = cs.getBlob(columnIndex);
byte[] returnValue = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
return returnValue;
return (blob == null || cs.wasNull()) ? null : blob.getBytes(1, (int) blob.length());
}
}
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;
}
}
19 changes: 7 additions & 12 deletions src/main/java/org/apache/ibatis/type/ClobReaderTypeHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2016 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 @@ -42,7 +42,8 @@ public void setNonNullParameter(PreparedStatement ps, int i, Reader parameter, J
@Override
public Reader getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return toReader(rs.getClob(columnName));
Clob clob = rs.getClob(columnName);
return (clob == null || rs.wasNull()) ? null : clob.getCharacterStream();
}

/**
Expand All @@ -52,7 +53,8 @@ public Reader getNullableResult(ResultSet rs, String columnName)
@Override
public Reader getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return toReader(rs.getClob(columnIndex));
Clob clob = rs.getClob(columnIndex);
return (clob == null || rs.wasNull()) ? null : clob.getCharacterStream();
}

/**
Expand All @@ -62,15 +64,8 @@ public Reader getNullableResult(ResultSet rs, int columnIndex)
@Override
public Reader getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return toReader(cs.getClob(columnIndex));
}

private Reader toReader(Clob clob) throws SQLException {
if (clob == null) {
return null;
} else {
return clob.getCharacterStream();
}
Clob clob = cs.getClob(columnIndex);
return (clob == null || cs.wasNull()) ? null : clob.getCharacterStream();
}

}
23 changes: 4 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,21 @@ 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 (clob == null || rs.wasNull()) ? null : clob.getSubString(1, (int) clob.length());
}

@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 (clob == null || rs.wasNull()) ? null : clob.getSubString(1, (int) clob.length());
}

@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 (clob == null || cs.wasNull()) ? null : clob.getSubString(1, (int) clob.length());
}
}
Loading

0 comments on commit 67d1b59

Please sign in to comment.