Skip to content

Commit bae02af

Browse files
committed
Clear 'item' and 'index' variables in the context at the end of foreach loop. Should fix #966 ( and #49 ).
1 parent a0308b1 commit bae02af

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

src/main/java/org/apache/ibatis/scripting/xmltags/ForEachSqlNode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,6 +87,8 @@ public boolean apply(DynamicContext context) {
8787
i++;
8888
}
8989
applyClose(context);
90+
context.getBindings().remove(item);
91+
context.getBindings().remove(index);
9092
return true;
9193
}
9294

src/test/java/org/apache/ibatis/submitted/foreach/ForEachTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,28 @@ public void shouldReportMissingPropertyName() {
143143
}
144144
}
145145

146+
@Test
147+
public void shouldRemoveItemVariableInTheContext() {
148+
SqlSession sqlSession = sqlSessionFactory.openSession();
149+
try {
150+
Mapper mapper = sqlSession.getMapper(Mapper.class);
151+
int result = mapper.itemVariableConflict(5, Arrays.asList(1, 2), Arrays.asList(3, 4));
152+
Assert.assertEquals(5, result);
153+
} finally {
154+
sqlSession.close();
155+
}
156+
}
157+
158+
@Test
159+
public void shouldRemoveIndexVariableInTheContext() {
160+
SqlSession sqlSession = sqlSessionFactory.openSession();
161+
try {
162+
Mapper mapper = sqlSession.getMapper(Mapper.class);
163+
int result = mapper.indexVariableConflict(4, Arrays.asList(6, 7), Arrays.asList(8, 9));
164+
Assert.assertEquals(4, result);
165+
} finally {
166+
sqlSession.close();
167+
}
168+
}
169+
146170
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717

1818
import java.util.List;
1919

20+
import org.apache.ibatis.annotations.Param;
21+
2022
public interface Mapper {
2123

2224
User getUser(User user);
@@ -28,4 +30,8 @@ public interface Mapper {
2830
String selectWithNullItemCheck(List<User> users);
2931

3032
int typoInItemProperty(List<User> users);
33+
34+
int itemVariableConflict(@Param("id") Integer id, @Param("ids") List<Integer> ids, @Param("ids2") List<Integer> ids2);
35+
36+
int indexVariableConflict(@Param("idx") Integer id, @Param("idxs") List<Integer> ids, @Param("idxs2") List<Integer> ids2);
3137
}

src/test/java/org/apache/ibatis/submitted/foreach/Mapper.xml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2009-2016 the original author or authors.
4+
Copyright 2009-2017 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -71,4 +71,25 @@
7171
</foreach>
7272
</insert>
7373

74+
<select id="itemVariableConflict" resultType="_int">
75+
select count(*) from users where id in
76+
<foreach collection="ids" item="id" open="(" close=")" separator=",">
77+
<foreach collection="ids2" item="id">
78+
#{id},
79+
</foreach>
80+
#{id}
81+
</foreach>
82+
or id = #{id}
83+
</select>
84+
85+
<select id="indexVariableConflict" resultType="_int">
86+
select count(*) from users where id in
87+
<foreach collection="idxs" index="idx" open="(" close=")" separator=",">
88+
<foreach collection="idxs2" index="idx">
89+
#{idx},
90+
</foreach>
91+
#{idx} + 2
92+
</foreach>
93+
or id = #{idx}
94+
</select>
7495
</mapper>

0 commit comments

Comments
 (0)