Skip to content

Commit f19408b

Browse files
committed
Mapping nested cursor to constructor arg
1 parent 0803167 commit f19408b

File tree

4 files changed

+87
-13
lines changed

4 files changed

+87
-13
lines changed

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,9 @@ private Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObject
569569
return getNestedQueryMappingValue(rs, metaResultObject, propertyMapping, lazyLoader, columnPrefix);
570570
}
571571
if (JdbcType.CURSOR.equals(propertyMapping.getJdbcType())) {
572-
return getNestedCursorValue(rs, metaResultObject, propertyMapping, columnPrefix);
572+
List<Object> results = getNestedCursorValue(rs, propertyMapping, columnPrefix);
573+
linkObjects(metaResultObject, propertyMapping, results.get(0), true);
574+
return metaResultObject.getValue(propertyMapping.getProperty());
573575
}
574576
if (propertyMapping.getResultSet() != null) {
575577
addPendingChildRelation(rs, metaResultObject, propertyMapping); // TODO is that OK?
@@ -581,18 +583,16 @@ private Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObject
581583
}
582584
}
583585

584-
private Object getNestedCursorValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping,
585-
String parentColumnPrefix) throws SQLException {
586+
private List<Object> getNestedCursorValue(ResultSet rs, ResultMapping propertyMapping, String parentColumnPrefix)
587+
throws SQLException {
586588
final String column = prependPrefix(propertyMapping.getColumn(), parentColumnPrefix);
587-
final String property = propertyMapping.getProperty();
588589
ResultMap nestedResultMap = resolveDiscriminatedResultMap(rs,
589590
configuration.getResultMap(propertyMapping.getNestedResultMapId()),
590591
getColumnPrefix(parentColumnPrefix, propertyMapping));
591592
ResultSetWrapper rsw = new ResultSetWrapper(rs.getObject(column, ResultSet.class), configuration);
592593
List<Object> results = new ArrayList<>();
593594
handleResultSet(rsw, nestedResultMap, results, null);
594-
linkObjects(metaResultObject, propertyMapping, results.get(0), true);
595-
return metaResultObject.getValue(property);
595+
return results;
596596
}
597597

598598
private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap,
@@ -778,6 +778,15 @@ Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> resultType
778778
try {
779779
if (constructorMapping.getNestedQueryId() != null) {
780780
value = getNestedQueryConstructorValue(rsw.getResultSet(), constructorMapping, columnPrefix);
781+
} else if (JdbcType.CURSOR.equals(constructorMapping.getJdbcType())) {
782+
List<?> result = (List<?>) getNestedCursorValue(rsw.getResultSet(), constructorMapping, columnPrefix).get(0);
783+
if (objectFactory.isCollection(parameterType)) {
784+
MetaObject collection = configuration.newMetaObject(objectFactory.create(parameterType));
785+
collection.addAll((List<?>) result);
786+
value = collection.getOriginalObject();
787+
} else {
788+
value = toSingleObj(result);
789+
}
781790
} else if (constructorMapping.getNestedResultMapId() != null) {
782791
final String constructorColumnPrefix = getColumnPrefix(columnPrefix, constructorMapping);
783792
final ResultMap resultMap = resolveDiscriminatedResultMap(rsw.getResultSet(),

src/test/java/org/apache/ibatis/submitted/oracle_cursor/Mapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public interface Mapper {
3333

3434
List<Author> selectNestedCursor_Automap();
3535

36+
List<Author> selectNestedCursorConstructorCollection();
37+
3638
List<Author2> selectNestedCursorOfStrings();
3739

3840
List<Book2> selectNestedCursorAssociation();
3941

42+
List<Book2> selectNestedCursorConstructorAssociation();
43+
4044
}

src/test/java/org/apache/ibatis/submitted/oracle_cursor/OracleCursorTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ void nestedCursors_Automap() {
8888
doTest(Mapper::selectNestedCursor_Automap);
8989
}
9090

91+
@Test
92+
void nestedCursorsConstructorCollection() {
93+
doTest(Mapper::selectNestedCursorConstructorCollection);
94+
}
95+
9196
private void doTest(Function<Mapper, List<Author>> query) {
9297
doTest(query, ArrayList.class);
9398
}
@@ -116,9 +121,18 @@ void nestedCursorOfStrings() {
116121

117122
@Test
118123
void nestedCursorAssociation() {
124+
doTestBook2(Mapper::selectNestedCursorAssociation);
125+
}
126+
127+
@Test
128+
void nestedCursorConstructorAssociation() {
129+
doTestBook2(Mapper::selectNestedCursorConstructorAssociation);
130+
}
131+
132+
private void doTestBook2(Function<Mapper, List<Book2>> query) {
119133
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
120134
Mapper mapper = sqlSession.getMapper(Mapper.class);
121-
List<Book2> books = mapper.selectNestedCursorAssociation();
135+
List<Book2> books = query.apply(mapper);
122136
assertIterableEquals(List.of(new Book2(1, "C#", new Author(1, "John")),
123137
new Book2(2, "Python", new Author(1, "John")), new Book2(3, "SQL", new Author(2, "Jane")),
124138
new Book2(4, "Java", new Author(2, "Jane")), new Book2(5, "Ruby", new Author(1, "John"))), books);

src/test/resources/org/apache/ibatis/submitted/oracle_cursor/Mapper.xml

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@
112112
<include refid="nestedCursor" />
113113
</select>
114114

115+
<!-- Map nested cursor to constructor arg (collection). -->
116+
<resultMap id="authorRM_ConstructorCollection"
117+
type="org.apache.ibatis.submitted.oracle_cursor.Author">
118+
<constructor>
119+
<idArg column="id" javaType="int" />
120+
<arg column="name" javaType="string" />
121+
<arg column="books" jdbcType="CURSOR"
122+
javaType="list" resultMap="bookRM" />
123+
</constructor>
124+
</resultMap>
125+
126+
<select id="selectNestedCursorConstructorCollection"
127+
resultMap="authorRM_ConstructorCollection">
128+
<include refid="nestedCursor" />
129+
</select>
130+
115131
<!-- Use inline result map to map nested cursor to collection. -->
116132
<resultMap id="author2RM_InlineRM"
117133
type="org.apache.ibatis.submitted.oracle_cursor.Author2">
@@ -130,21 +146,52 @@
130146
) book_names from author a
131147
]]></select>
132148

149+
<sql id="bookNestedCursor"><![CDATA[
150+
select b.id, b.name, cursor(
151+
select a.id, a.name from author a where b.author_id = a.id
152+
) author from book b
153+
]]></sql>
154+
133155
<!-- Map nested cursor to association. -->
134156
<resultMap id="book2RM_Association"
135157
type="org.apache.ibatis.submitted.oracle_cursor.Book2">
136158
<id property="id" column="id" />
137159
<result property="name" column="name" />
138-
<association property="author" column="author" jdbcType="CURSOR">
160+
<association property="author" column="author"
161+
jdbcType="CURSOR" resultMap="authorRM">
139162
<id property="id" column="id" />
140163
<result property="name" column="name" />
141164
</association>
142165
</resultMap>
143166

144167
<select id="selectNestedCursorAssociation"
145-
resultMap="book2RM_Association"><![CDATA[
146-
select b.id, b.name, cursor(
147-
select a.id, a.name from author a where b.author_id = a.id
148-
) author from book b
149-
]]></select>
168+
resultMap="book2RM_Association">
169+
<include refid="bookNestedCursor" />
170+
</select>
171+
172+
<resultMap id="authorRM_ConstructorIdName"
173+
type="org.apache.ibatis.submitted.oracle_cursor.Author">
174+
<constructor>
175+
<idArg column="id" javaType="int" />
176+
<arg column="name" javaType="string" />
177+
</constructor>
178+
</resultMap>
179+
180+
<!-- Map nested cursor to constructor arg (single). -->
181+
<resultMap id="book2RM_ConstructorAssociation"
182+
type="org.apache.ibatis.submitted.oracle_cursor.Book2">
183+
<constructor>
184+
<idArg column="id" javaType="int" />
185+
<arg column="name" javaType="string" />
186+
<arg column="author" jdbcType="CURSOR"
187+
javaType="org.apache.ibatis.submitted.oracle_cursor.Author"
188+
resultMap="authorRM_ConstructorIdName" />
189+
</constructor>
190+
</resultMap>
191+
192+
<select id="selectNestedCursorConstructorAssociation"
193+
resultMap="book2RM_ConstructorAssociation">
194+
<include refid="bookNestedCursor" />
195+
</select>
196+
150197
</mapper>

0 commit comments

Comments
 (0)