Skip to content

Commit

Permalink
Change to refers useColumnLabel at UnknownTypeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuki43zoo committed Oct 24, 2019
1 parent 8856384 commit 451440b
Show file tree
Hide file tree
Showing 12 changed files with 478 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/apache/ibatis/session/Configuration.java
Expand Up @@ -146,7 +146,7 @@ public class Configuration {

protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
protected final InterceptorChain interceptorChain = new InterceptorChain();
protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry(this);
protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry();

Expand Down
18 changes: 17 additions & 1 deletion src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
Expand Up @@ -46,6 +46,7 @@
import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;

/**
* @author Clinton Begin
Expand All @@ -55,14 +56,29 @@ public final class TypeHandlerRegistry {

private final Map<JdbcType, TypeHandler<?>> jdbcTypeHandlerMap = new EnumMap<>(JdbcType.class);
private final Map<Type, Map<JdbcType, TypeHandler<?>>> typeHandlerMap = new ConcurrentHashMap<>();
private final TypeHandler<Object> unknownTypeHandler = new UnknownTypeHandler(this);
private final TypeHandler<Object> unknownTypeHandler;
private final Map<Class<?>, TypeHandler<?>> allTypeHandlersMap = new HashMap<>();

private static final Map<JdbcType, TypeHandler<?>> NULL_TYPE_HANDLER_MAP = Collections.emptyMap();

private Class<? extends TypeHandler> defaultEnumTypeHandler = EnumTypeHandler.class;

/**
* The default constructor.
*/
public TypeHandlerRegistry() {
this(new Configuration());
}

/**
* The constructor that pass the MyBatis configuration.
*
* @param configuration a MyBatis configuration
* @since 3.5.4
*/
public TypeHandlerRegistry(Configuration configuration) {
this.unknownTypeHandler = new UnknownTypeHandler(configuration);

register(Boolean.class, new BooleanTypeHandler());
register(boolean.class, new BooleanTypeHandler());
register(JdbcType.BOOLEAN, new BooleanTypeHandler());
Expand Down
36 changes: 29 additions & 7 deletions src/main/java/org/apache/ibatis/type/UnknownTypeHandler.java
Expand Up @@ -22,20 +22,41 @@
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;

/**
* @author Clinton Begin
*/
public class UnknownTypeHandler extends BaseTypeHandler<Object> {

private static final ObjectTypeHandler OBJECT_TYPE_HANDLER = new ObjectTypeHandler();
private final Configuration configuration;
private final Supplier<TypeHandlerRegistry> typeHandlerRegistrySupplier;

private TypeHandlerRegistry typeHandlerRegistry;
/**
* The constructor that pass a MyBatis configuration.
*
* @param configuration a MyBatis configuration
* @since 3.5.4
*/
public UnknownTypeHandler(Configuration configuration) {
this.configuration = configuration;
this.typeHandlerRegistrySupplier = configuration::getTypeHandlerRegistry;
}

/**
* The constructor that pass the type handler registry.
*
* @param typeHandlerRegistry a type handler registry
* @deprecated Since 3.5.4, please use the {@link #UnknownTypeHandler(Configuration)}.
*/
@Deprecated
public UnknownTypeHandler(TypeHandlerRegistry typeHandlerRegistry) {
this.typeHandlerRegistry = typeHandlerRegistry;
this.configuration = new Configuration();
this.typeHandlerRegistrySupplier = () -> typeHandlerRegistry;
}

@Override
Expand Down Expand Up @@ -73,7 +94,7 @@ private TypeHandler<?> resolveTypeHandler(Object parameter, JdbcType jdbcType) {
if (parameter == null) {
handler = OBJECT_TYPE_HANDLER;
} else {
handler = typeHandlerRegistry.getTypeHandler(parameter.getClass(), jdbcType);
handler = typeHandlerRegistrySupplier.get().getTypeHandler(parameter.getClass(), jdbcType);
// check if handler is null (issue #270)
if (handler == null || handler instanceof UnknownTypeHandler) {
handler = OBJECT_TYPE_HANDLER;
Expand All @@ -88,8 +109,9 @@ private TypeHandler<?> resolveTypeHandler(ResultSet rs, String column) {
columnIndexLookup = new HashMap<>();
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
boolean useColumnLabel = configuration.isUseColumnLabel();
for (int i = 1; i <= count; i++) {
String name = rsmd.getColumnName(i);
String name = useColumnLabel ? rsmd.getColumnLabel(i) : rsmd.getColumnName(i);
columnIndexLookup.put(name,i);
}
Integer columnIndex = columnIndexLookup.get(column);
Expand All @@ -111,11 +133,11 @@ private TypeHandler<?> resolveTypeHandler(ResultSetMetaData rsmd, Integer column
JdbcType jdbcType = safeGetJdbcTypeForColumn(rsmd, columnIndex);
Class<?> javaType = safeGetClassForColumn(rsmd, columnIndex);
if (javaType != null && jdbcType != null) {
handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType);
handler = typeHandlerRegistrySupplier.get().getTypeHandler(javaType, jdbcType);
} else if (javaType != null) {
handler = typeHandlerRegistry.getTypeHandler(javaType);
handler = typeHandlerRegistrySupplier.get().getTypeHandler(javaType);
} else if (jdbcType != null) {
handler = typeHandlerRegistry.getTypeHandler(jdbcType);
handler = typeHandlerRegistrySupplier.get().getTypeHandler(jdbcType);
}
return handler;
}
Expand Down
@@ -0,0 +1,44 @@
--
-- Copyright 2009-2019 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.
--

drop table product_sku if exists;
drop table product_info if exists;
drop table product if exists;

create table product (
id varchar(32) not null,
code varchar(80) not null,
name varchar(240) not null
);

create table product_sku (
id varchar(32) not null,
product_id varchar(32) not null,
code varchar(80) not null,
color varchar(40),
size varchar(40)
);

create table product_info (
id int not null,
product_id varchar(32) not null,
other_info varchar(240)
);

insert into product(id, code, name) values('10000000000000000000000000000001', 'P001', 'Product 001');
insert into product_sku(id ,product_id, code, color, size) values('20000000000000000000000000000001', '10000000000000000000000000000001', 'S001', 'red', '80');
insert into product_sku(id ,product_id, code, color, size) values('20000000000000000000000000000002', '10000000000000000000000000000001', 'S001', 'blue', '10');
insert into product_info(id, product_id, other_info) values(1, '10000000000000000000000000000001', 'Sale 50% Off');
@@ -0,0 +1,82 @@
/**
* Copyright 2009-2019 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.submitted.nestedresulthandler_gh1551;

import java.io.Reader;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class NestedResultHandlerGh1551Test {
private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
// create a SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_gh1551/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}

// populate in-memory database
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/nestedresulthandler_gh1551/CreateDB.sql");
}

@Test
void useColumnLabelIsTrue() {
sqlSessionFactory.getConfiguration().setUseColumnLabel(true);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);

ProductResp productResp = mapper.selectAllInfo("P001").get(0);

Assertions.assertEquals("10000000000000000000000000000001", productResp.getId());
Assertions.assertEquals("P001", productResp.getCode());
Assertions.assertEquals("Product 001", productResp.getName());

Assertions.assertEquals(1, productResp.getProductInfo().getId());
Assertions.assertEquals("10000000000000000000000000000001", productResp.getProductInfo().getProductId());
Assertions.assertEquals("Sale 50% Off", productResp.getProductInfo().getOtherInfo());

Assertions.assertEquals("20000000000000000000000000000001", productResp.getSkus().get(0).getId());
Assertions.assertEquals("10000000000000000000000000000001", productResp.getSkus().get(0).getProductId());
Assertions.assertEquals("red", productResp.getSkus().get(0).getColor());
Assertions.assertEquals("80", productResp.getSkus().get(0).getSize());
Assertions.assertEquals("20000000000000000000000000000002", productResp.getSkus().get(1).getId());
Assertions.assertEquals("10000000000000000000000000000001", productResp.getSkus().get(1).getProductId());
Assertions.assertEquals("blue", productResp.getSkus().get(1).getColor());
Assertions.assertEquals("10", productResp.getSkus().get(1).getSize());
}
}

@Test
void useColumnLabelIsFalse() {
sqlSessionFactory.getConfiguration().setUseColumnLabel(false);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
PersistenceException exception = Assertions.assertThrows(PersistenceException.class, () -> mapper.selectAllInfo("P001"));
Assertions.assertTrue(exception.getMessage().contains("Error attempting to get column 'ID' from result set. Cause: java.sql.SQLSyntaxErrorException: incompatible data type in conversion: from SQL type VARCHAR to java.lang.Integer, value: 10000000000000000000000000000001"));
}
}

}
@@ -0,0 +1,48 @@
/**
* Copyright 2009-2019 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.submitted.nestedresulthandler_gh1551;

public class ProductInfo {

private Long id;
private String productId;
private String otherInfo;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getOtherInfo() {
return otherInfo;
}

public void setOtherInfo(String otherInfo) {
this.otherInfo = otherInfo;
}

}
@@ -0,0 +1,24 @@
/**
* Copyright 2009-2019 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.submitted.nestedresulthandler_gh1551;

import java.util.List;

import org.apache.ibatis.annotations.Param;

public interface ProductMapper {
List<ProductResp> selectAllInfo(@Param("code") String code);
}
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2019 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.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.apache.ibatis.submitted.nestedresulthandler_gh1551.ProductMapper">
<resultMap id="AllInfoResultMap" type="ProductResp">
<id column="ID" property="id"/>
<result column="NAME" property="name"/>
<result column="CODE" property="code"/>
<result column="INFO_ID" property="productInfo.id"/>
<result column="ID" property="productInfo.productId"/>
<result column="INFO_OTHER_INFO" property="productInfo.otherInfo"/>
<collection property="skus" select="selectSkuByPk1" column="ID"/>
</resultMap>
<select id="selectAllInfo" resultMap="AllInfoResultMap">
select
p.id,
p.code,
p.name,
i.id as INFO_ID,
i.other_info as INFO_OTHER_INFO
from
product p
left join product_info i on p.id = i.product_id
where p.code = #{code}
</select>
<resultMap id="SkuResultMap" type="ProductSku">
<id column="ID" property="id"/>
<result column="PRODUCT_ID" property="productId"/>
<result column="COLOR" property="color"/>
<result column="SIZE" property="size"/>
</resultMap>
<select id="selectSkuByPk1" resultMap="SkuResultMap">
select * from product_sku where product_id = #{id} ORDER BY id
</select>
</mapper>

0 comments on commit 451440b

Please sign in to comment.