Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.apache.ibatis.executor.result;

public class ResultClassTypeHolder {

// hold the the class type of result
private static final ThreadLocal<Class> resultTypeTl = new ThreadLocal();

public static void setResultType(Class clazz) {
resultTypeTl.set(clazz);
}

public static Class getResultType() {
return resultTypeTl.get();
}

public static void clean() {
resultTypeTl.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.result.DefaultResultContext;
import org.apache.ibatis.executor.result.DefaultResultHandler;
import org.apache.ibatis.executor.result.ResultClassTypeHolder;
import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.Discriminator;
Expand Down Expand Up @@ -293,6 +294,7 @@ private void validateResultMapsCount(ResultSetWrapper rsw, int resultMapCount) {

private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
try {
ResultClassTypeHolder.setResultType(resultMap.getType());
if (parentMapping != null) {
handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
} else {
Expand All @@ -307,6 +309,7 @@ private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Obj
} finally {
// issue #228 (close resultsets)
closeResultSet(rsw.getResultSet());
ResultClassTypeHolder.clean();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import java.util.List;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.builder.typehandler.WidthTypeHandler;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -73,6 +76,9 @@ void annotatedSubject() {

@Test
void badSubject() {
Configuration configuration = sqlSessionFactory.getConfiguration();
final TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
registry.register(BadSubject.Width.class, WidthTypeHandler.class);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
final AutoConstructorMapper mapper = sqlSession.getMapper(AutoConstructorMapper.class);
assertThrows(PersistenceException.class, mapper::getBadSubjects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ public class BadSubject {
private final int age;
private final Height height;
private final Double weight;
private final Width width;

public BadSubject(final int id, final String name, final int age, final Height height, final Double weight) {
public BadSubject(final int id, final String name, final int age, final Height height, final Double weight, final Width width) {
this.id = id;
this.name = name;
this.age = age;
this.height = height;
this.weight = weight == null ? 0 : weight;
this.width = width;
}

private class Height {

}

public static class Width {

}
}
11 changes: 6 additions & 5 deletions src/test/java/org/apache/ibatis/autoconstructor/CreateDB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ CREATE TABLE subject (
height INT,
weight INT,
active BIT,
dt TIMESTAMP
dt TIMESTAMP,
width INT
);

CREATE TABLE extensive_subject (
Expand All @@ -47,12 +48,12 @@ CREATE TABLE extensive_subject (
);

INSERT INTO subject VALUES
(1, 'a', 10, 100, 45, 1, CURRENT_TIMESTAMP),
(2, 'b', 10, NULL, 45, 1, CURRENT_TIMESTAMP),
(2, 'c', 10, NULL, NULL, 0, CURRENT_TIMESTAMP);
(1, 'a', 10, 100, 45, 1, CURRENT_TIMESTAMP, 50),
(2, 'b', 10, NULL, 45, 1, CURRENT_TIMESTAMP, 50),
(2, 'c', 10, NULL, NULL, 0, CURRENT_TIMESTAMP, 50);

INSERT INTO extensive_subject
VALUES
(1, 1, 'a', 1, 1, 1, 1.0, 1, 'a', 'AVALUE', 'ACLOB', 'aaaaaabbbbbb', CURRENT_TIMESTAMP),
(2, 2, 'b', 2, 2, 2, 2.0, 2, 'b', 'BVALUE', 'BCLOB', '010101010101', CURRENT_TIMESTAMP),
(3, 3, 'c', 3, 3, 3, 3.0, 3, 'c', 'CVALUE', 'CCLOB', '777d010078da', CURRENT_TIMESTAMP);
(3, 3, 'c', 3, 3, 3, 3.0, 3, 'c', 'CVALUE', 'CCLOB', '777d010078da', CURRENT_TIMESTAMP);
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package org.apache.ibatis.builder;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

@MappedTypes(Long.class)
public class CustomLongTypeHandler implements TypeHandler<Long> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.apache.ibatis.builder.typehandler;

import org.apache.ibatis.autoconstructor.BadSubject;
import org.apache.ibatis.executor.result.ResultClassTypeHolder;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.junit.Assert;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class WidthTypeHandler extends BaseTypeHandler<BadSubject.Width> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, BadSubject.Width parameter, JdbcType jdbcType) throws SQLException {

}

@Override
public BadSubject.Width getNullableResult(ResultSet rs, String columnName) throws SQLException {
Assert.assertNotNull(ResultClassTypeHolder.getResultType());
return new BadSubject.Width();
}

@Override
public BadSubject.Width getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return new BadSubject.Width();
}

@Override
public BadSubject.Width getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return new BadSubject.Width();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.apache.ibatis.executor;

import org.apache.ibatis.executor.result.ResultClassTypeHolder;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

public class ResultClassTypeHolderTest {

@Test
public void getResultClassFromTl() {
try {
ResultClassTypeHolder.setResultType(Object.class);
getResultClass();
}finally {
ResultClassTypeHolder.clean();
}

}

private void getResultClass() {
Class aClass = ResultClassTypeHolder.getResultType();
Assert.assertNotNull(aClass);
}

}