Skip to content

Commit

Permalink
Added tests that maps nested cursor using type handler
Browse files Browse the repository at this point in the history
This probably worked in earlier versions, so these tests are to ensure backward compatibility.

mybatis#566 (comment)
  • Loading branch information
harawata committed Jan 7, 2025
1 parent f19408b commit 7a1bfad
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<Book>> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, List<Book> parameter, JdbcType jdbcType)
throws SQLException {
// n/a
}

@Override
public List<Book> getNullableResult(ResultSet rs, String columnName) throws SQLException {
List<Book> 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<Book> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
// n/a
return null;
}

@Override
public List<Book> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
// n/a
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public interface Mapper {

List<Author> selectNestedCursorConstructorCollection();

List<Author> selectNestedCursorTypeHandler();

List<Author> selectNestedCursorTypeHandlerConstructor();

List<Author2> selectNestedCursorOfStrings();

List<Book2> selectNestedCursorAssociation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mapper, List<Author>> query) {
doTest(query, ArrayList.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,36 @@
<include refid="nestedCursor" />
</select>

<!-- Map nested cursor to property using type handler. -->
<resultMap id="authorRM_TypeHandler"
type="org.apache.ibatis.submitted.oracle_cursor.Author">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="books" column="books"
typeHandler="org.apache.ibatis.submitted.oracle_cursor.BooksTypeHandler" />
</resultMap>

<select id="selectNestedCursorTypeHandler"
resultMap="authorRM_TypeHandler">
<include refid="nestedCursor" />
</select>

<!-- Map nested cursor to constructor arg using type handler. -->
<resultMap id="authorRM_TypeHandlerConstructor"
type="org.apache.ibatis.submitted.oracle_cursor.Author">
<constructor>
<idArg column="id" javaType="int" />
<arg column="name" javaType="string" />
<arg column="books" javaType="list"
typeHandler="org.apache.ibatis.submitted.oracle_cursor.BooksTypeHandler" />
</constructor>
</resultMap>

<select id="selectNestedCursorTypeHandlerConstructor"
resultMap="authorRM_TypeHandlerConstructor">
<include refid="nestedCursor" />
</select>

<!-- Use inline result map to map nested cursor to collection. -->
<resultMap id="author2RM_InlineRM"
type="org.apache.ibatis.submitted.oracle_cursor.Author2">
Expand Down

0 comments on commit 7a1bfad

Please sign in to comment.