Skip to content

Commit b1d9b85

Browse files
committed
(fixes #10) Index variable is not reset when multiple foreach loops are used in a single statement. Thanks to jgorinsky for the test case.
1 parent 75d2cbe commit b1d9b85

File tree

7 files changed

+273
-1
lines changed

7 files changed

+273
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public boolean apply(DynamicContext context) {
7070
applyIndex(context, mapEntry.getKey(), uniqueNumber);
7171
applyItem(context, mapEntry.getValue(), uniqueNumber);
7272
} else {
73-
applyIndex(context, i, uniqueNumber);
73+
applyIndex(context, uniqueNumber, i);
7474
applyItem(context, o, uniqueNumber);
7575
}
7676
contents.apply(new FilteredDynamicContext(configuration, context, index, item, uniqueNumber));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--
2+
-- Copyright 2009-2013 The MyBatis Team
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20),
22+
first_attr_1 varchar(3),
23+
first_attr_2 varchar(3),
24+
second_attr_1 varchar(3),
25+
second_attr_2 varchar(3),
26+
);
27+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2009-2013 The MyBatis Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.multipleiterates;
17+
18+
public interface Mapper {
19+
20+
User getUser(Integer id);
21+
22+
void insertUser(User user);
23+
24+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2013 The MyBatis Team
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE mapper
18+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
21+
<mapper namespace="org.apache.ibatis.submitted.multipleiterates.Mapper">
22+
23+
<select id="getUser" resultType="org.apache.ibatis.submitted.multipleiterates.User">
24+
select * from users
25+
</select>
26+
27+
<insert id="insertUser" parameterType="org.apache.ibatis.submitted.multipleiterates.User">
28+
insert into users
29+
(id,
30+
name,
31+
<foreach item="attr" index="index" collection="firstAttr"
32+
separator=",">
33+
first_attr_${index + 1}
34+
</foreach>
35+
,
36+
<foreach item="attr" index="index" collection="secondAttr"
37+
separator=",">
38+
second_attr_${index + 1}
39+
</foreach>
40+
)
41+
values(
42+
1,
43+
'User1',
44+
<foreach item="attr" index="index" collection="firstAttr"
45+
separator=",">
46+
#{attr}
47+
</foreach>
48+
,
49+
<foreach item="attr" index="index" collection="secondAttr"
50+
separator=",">
51+
#{attr}
52+
</foreach>
53+
)
54+
</insert>
55+
56+
</mapper>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2009-2013 The MyBatis Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.multipleiterates;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.jdbc.ScriptRunner;
23+
import org.apache.ibatis.session.SqlSession;
24+
import org.apache.ibatis.session.SqlSessionFactory;
25+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
29+
public class MultipleIteratesTest {
30+
31+
private static SqlSessionFactory sqlSessionFactory;
32+
33+
@BeforeClass
34+
public static void setUp() throws Exception {
35+
// create a SqlSessionFactory
36+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleiterates/mybatis-config.xml");
37+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
38+
reader.close();
39+
40+
// populate in-memory database
41+
SqlSession session = sqlSessionFactory.openSession();
42+
Connection conn = session.getConnection();
43+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleiterates/CreateDB.sql");
44+
ScriptRunner runner = new ScriptRunner(conn);
45+
runner.setLogWriter(null);
46+
runner.runScript(reader);
47+
reader.close();
48+
session.close();
49+
}
50+
51+
@Test
52+
public void shouldGetAUser() {
53+
SqlSession sqlSession = sqlSessionFactory.openSession();
54+
try {
55+
Mapper mapper = sqlSession.getMapper(Mapper.class);
56+
User user = new User();
57+
user.setId(1);
58+
user.setName("Justin");
59+
user.setFirstAttr(new String[] { "asd", "asd" });
60+
user.setSecondAttr(new String[] { "fds", "fds" });
61+
mapper.insertUser(user);
62+
} finally {
63+
sqlSession.close();
64+
}
65+
}
66+
67+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2009-2013 The MyBatis Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
15+
package org.apache.ibatis.submitted.multipleiterates;
16+
17+
public class User {
18+
19+
private Integer id;
20+
21+
private String name;
22+
23+
private String[] firstAttr;
24+
25+
private String[] secondAttr;
26+
27+
public Integer getId() {
28+
return id;
29+
}
30+
31+
public void setId(Integer id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public String[] getFirstAttr() {
44+
return firstAttr;
45+
}
46+
47+
public void setFirstAttr(String[] firstAttr) {
48+
this.firstAttr = firstAttr;
49+
}
50+
51+
public String[] getSecondAttr() {
52+
return secondAttr;
53+
}
54+
55+
public void setSecondAttr(String[] secondAttr) {
56+
this.secondAttr = secondAttr;
57+
}
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Copyright 2009-2013 The MyBatis Team
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE configuration
18+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
23+
<environments default="development">
24+
<environment id="development">
25+
<transactionManager type="JDBC">
26+
<property name="" value="" />
27+
</transactionManager>
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver" />
30+
<property name="url" value="jdbc:hsqldb:mem:multipleiterates" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper resource="org/apache/ibatis/submitted/multipleiterates/Mapper.xml" />
38+
</mappers>
39+
40+
</configuration>

0 commit comments

Comments
 (0)