Skip to content

Commit c0ab206

Browse files
committed
Add SP multiple resultset test/example
1 parent 7f4ccce commit c0ab206

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

src/test/java/org/apache/ibatis/submitted/sptests/CreateDB.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ go
1313
drop table sptest.names if exists
1414
go
1515

16+
drop table sptest.items if exists
17+
go
18+
1619
drop schema sptest if exists
1720
go
1821

@@ -45,6 +48,22 @@ go
4548
insert into sptest.names (first_name, last_name) values('Betty', 'Rubble')
4649
go
4750

51+
create table sptest.items (
52+
id integer generated by default as identity not null,
53+
item varchar(20),
54+
primary key(id)
55+
)
56+
go
57+
58+
insert into sptest.items (item) values('Brontosaurus Burger')
59+
go
60+
61+
insert into sptest.items (item) values('Lunch Box')
62+
go
63+
64+
insert into sptest.items (item) values('Helmet')
65+
go
66+
4867
-- note that these create procedure statements will fail until hsqldb 2.0.1
4968
create procedure sptest.getname(in nameId integer)
5069
reads sql data
@@ -55,6 +74,17 @@ BEGIN ATOMIC
5574
END
5675
go
5776

77+
create procedure sptest.getnamesanditems()
78+
reads sql data
79+
dynamic result sets 2
80+
BEGIN ATOMIC
81+
declare cur1 cursor for select * from sptest.names;
82+
declare cur2 cursor for select * from sptest.items;
83+
open cur1;
84+
open cur2;
85+
END
86+
go
87+
5888
create procedure sptest.getnames(in lowestId int, out totalrows integer)
5989
reads sql data
6090
dynamic result sets 1
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.apache.ibatis.submitted.sptests;
2+
3+
public class Item {
4+
private Integer id;
5+
private String item;
6+
public Integer getId() {
7+
return id;
8+
}
9+
public void setId(Integer id) {
10+
this.id = id;
11+
}
12+
public String getItem() {
13+
return item;
14+
}
15+
public void setItem(String item) {
16+
this.item = item;
17+
}
18+
}

src/test/java/org/apache/ibatis/submitted/sptests/SPMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public interface SPMapper {
1010
Name getName(Integer id);
1111
List<Name> getNames(Map<String, Object> parms);
1212
List<Name> getNamesWithArray(Map<String, Object> parms);
13+
List<List<?>> getNamesAndItems();
1314
}

src/test/java/org/apache/ibatis/submitted/sptests/SPMapper.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<result column="LAST_NAME" property="lastName"/>
88
</resultMap>
99

10+
<resultMap type="org.apache.ibatis.submitted.sptests.Item" id="itemResult">
11+
<result column="ID" property="id"/>
12+
<result column="ITEM" property="item"/>
13+
</resultMap>
14+
1015
<parameterMap type="map" id="testParameterMap">
1116
<parameter property="addend1" jdbcType="INTEGER" mode="IN"/>
1217
<parameter property="addend2" jdbcType="INTEGER" mode="IN"/>
@@ -70,4 +75,9 @@
7075
#{requestedRows,jdbcType=INTEGER,mode=OUT},
7176
#{returnedIds,mode=OUT,jdbcType=ARRAY,typeHandler=org.apache.ibatis.submitted.sptests.ArrayTypeHandler})}
7277
</select>
78+
79+
<select id="getNamesAndItems" statementType="CALLABLE"
80+
resultMap="nameResult,itemResult">
81+
{call sptest.getnamesanditems()}
82+
</select>
7383
</mapper>

src/test/java/org/apache/ibatis/submitted/sptests/SPTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,26 @@ public void testGetNamesWithArray() throws SQLException {
332332
sqlSession.close();
333333
}
334334
}
335+
336+
/**
337+
* This test shows how to call procedures that return
338+
* multiple result sets
339+
*
340+
* @throws SQLException
341+
*/
342+
@Test
343+
@Ignore("until hsqldb 2.0.1 is released")
344+
public void testGetNamesAndItems() throws SQLException {
345+
SqlSession sqlSession = sqlSessionFactory.openSession();
346+
try {
347+
SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
348+
349+
List<List<?>> results = spMapper.getNamesAndItems();
350+
assertEquals(2, results.size());
351+
assertEquals(4, results.get(0).size());
352+
assertEquals(3, results.get(1).size());
353+
} finally {
354+
sqlSession.close();
355+
}
356+
}
335357
}

0 commit comments

Comments
 (0)