From 766fe2400d4d41640224a300abd117499996ae4b Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Tue, 7 Jan 2025 04:36:32 +0900 Subject: [PATCH] Added tests that maps nested cursor using type handler This probably worked in earlier versions, so these tests are to ensure backward compatibility. https://github.com/mybatis/mybatis-3/issues/566#issuecomment-557084907 --- .../org/apache/ibatis/mapping/ResultMap.java | 2 +- .../oracle_cursor/BooksTypeHandler.java | 62 +++++++++++++++++++ .../submitted/oracle_cursor/Mapper.java | 4 ++ .../oracle_cursor/OracleCursorTest.java | 10 +++ .../ibatis/submitted/oracle_cursor/Mapper.xml | 30 +++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/apache/ibatis/submitted/oracle_cursor/BooksTypeHandler.java diff --git a/src/main/java/org/apache/ibatis/mapping/ResultMap.java b/src/main/java/org/apache/ibatis/mapping/ResultMap.java index b70c7bf96af..a45d4ca47be 100644 --- a/src/main/java/org/apache/ibatis/mapping/ResultMap.java +++ b/src/main/java/org/apache/ibatis/mapping/ResultMap.java @@ -117,7 +117,7 @@ public ResultMap build() { // #101 Class javaType = resultMapping.getJavaType(); resultMap.hasResultMapsUsingConstructorCollection = resultMap.hasResultMapsUsingConstructorCollection - || (resultMapping.getNestedQueryId() == null && javaType != null + || (resultMapping.getNestedQueryId() == null && resultMapping.getTypeHandler() == null && javaType != null && resultMap.configuration.getObjectFactory().isCollection(javaType)); if (resultMapping.getProperty() != null) { diff --git a/src/test/java/org/apache/ibatis/submitted/oracle_cursor/BooksTypeHandler.java b/src/test/java/org/apache/ibatis/submitted/oracle_cursor/BooksTypeHandler.java new file mode 100644 index 00000000000..42a107c0426 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/oracle_cursor/BooksTypeHandler.java @@ -0,0 +1,62 @@ +/* + * Copyright 2009-2025 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 + * + * https://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.oracle_cursor; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; + +public class BooksTypeHandler extends BaseTypeHandler> { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType) + throws SQLException { + // n/a + } + + @Override + public List getNullableResult(ResultSet rs, String columnName) throws SQLException { + List list = new ArrayList<>(); + try (ResultSet nestedCursor = rs.getObject(columnName, ResultSet.class)) { + while (nestedCursor.next()) { + Integer id = nestedCursor.getInt("id"); + String name = nestedCursor.getString("name"); + list.add(new Book(id, name)); + } + } + return list; + } + + @Override + public List getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + // n/a + return null; + } + + @Override + public List getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + // n/a + return null; + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/oracle_cursor/Mapper.java b/src/test/java/org/apache/ibatis/submitted/oracle_cursor/Mapper.java index a7d6f4201d2..ff05d3c2eaa 100644 --- a/src/test/java/org/apache/ibatis/submitted/oracle_cursor/Mapper.java +++ b/src/test/java/org/apache/ibatis/submitted/oracle_cursor/Mapper.java @@ -35,6 +35,10 @@ public interface Mapper { List selectNestedCursorConstructorCollection(); + List selectNestedCursorTypeHandler(); + + List selectNestedCursorTypeHandlerConstructor(); + List selectNestedCursorOfStrings(); List selectNestedCursorAssociation(); diff --git a/src/test/java/org/apache/ibatis/submitted/oracle_cursor/OracleCursorTest.java b/src/test/java/org/apache/ibatis/submitted/oracle_cursor/OracleCursorTest.java index 03d7313338b..da3d2237048 100644 --- a/src/test/java/org/apache/ibatis/submitted/oracle_cursor/OracleCursorTest.java +++ b/src/test/java/org/apache/ibatis/submitted/oracle_cursor/OracleCursorTest.java @@ -93,6 +93,16 @@ void nestedCursorsConstructorCollection() { doTest(Mapper::selectNestedCursorConstructorCollection); } + @Test + void nestedCursorsTypeHandler() { + doTest(Mapper::selectNestedCursorTypeHandler); + } + + @Test + void nestedCursorsTypeHandlerConstructor() { + doTest(Mapper::selectNestedCursorTypeHandlerConstructor); + } + private void doTest(Function> query) { doTest(query, ArrayList.class); } diff --git a/src/test/resources/org/apache/ibatis/submitted/oracle_cursor/Mapper.xml b/src/test/resources/org/apache/ibatis/submitted/oracle_cursor/Mapper.xml index cfbffd11466..cfd81175282 100644 --- a/src/test/resources/org/apache/ibatis/submitted/oracle_cursor/Mapper.xml +++ b/src/test/resources/org/apache/ibatis/submitted/oracle_cursor/Mapper.xml @@ -128,6 +128,36 @@ + + + + + + + + + + + + + + + + + + + +