Skip to content

Commit 46883bd

Browse files
committed
(fixes http://code.google.com/p/mybatis/issues/detail?id=291 ) Parse selectKey after other nodes, in case another node throws IncompleteElementException.
1 parent e163f35 commit 46883bd

File tree

5 files changed

+154
-3
lines changed

5 files changed

+154
-3
lines changed

src/main/java/org/apache/ibatis/builder/xml/XMLStatementBuilder.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public void parseStatementNode() {
8686
ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
8787

8888
List<SqlNode> contents = parseDynamicTags(context);
89+
// Parse selectKey after other nodes,
90+
// in case another node throws IncompleteElementException (issue #291)
91+
parseSelectKey(context);
8992
MixedSqlNode rootSqlNode = new MixedSqlNode(contents);
9093
SqlSource sqlSource = new DynamicSqlSource(configuration, rootSqlNode);
9194
String nodeName = context.getNode().getNodeName();
@@ -132,7 +135,7 @@ private boolean databaseIdMatchesCurrent(String id, String databaseId) {
132135
}
133136
return true;
134137
}
135-
138+
136139
private List<SqlNode> parseDynamicTags(XNode node) {
137140
List<SqlNode> contents = new ArrayList<SqlNode>();
138141
NodeList children = node.getNode().getChildNodes();
@@ -143,18 +146,29 @@ private List<SqlNode> parseDynamicTags(XNode node) {
143146
|| child.getNode().getNodeType() == Node.TEXT_NODE) {
144147
String data = child.getStringBody("");
145148
contents.add(new TextSqlNode(data));
146-
} else {
149+
} else if (!"selectKey".equals(nodeName)) {
147150
NodeHandler handler = nodeHandlers.get(nodeName);
148151
if (handler == null) {
149152
throw new BuilderException("Unknown element <" + nodeName + "> in SQL statement.");
150153
}
151154
handler.handleNode(child, contents);
152-
153155
}
154156
}
155157
return contents;
156158
}
157159

160+
private void parseSelectKey(XNode node) {
161+
NodeList children = node.getNode().getChildNodes();
162+
for (int i = 0; i < children.getLength(); i++) {
163+
XNode child = node.newXNode(children.item(i));
164+
String nodeName = child.getNode().getNodeName();
165+
if ("selectKey".equals(nodeName)) {
166+
NodeHandler handler = nodeHandlers.get(nodeName);
167+
handler.handleNode(child, null);
168+
}
169+
}
170+
}
171+
158172
private Map<String, NodeHandler> nodeHandlers = new HashMap<String, NodeHandler>() {
159173
private static final long serialVersionUID = 7123056019193266281L;
160174

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+
4+
Copyright 2009-2012 The MyBatis Team
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<!DOCTYPE mapper
21+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
22+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
23+
24+
<mapper namespace="org.apache.ibatis.submitted.refid_resolution.ExternalMapper1">
25+
<select id="select" resultType="map">
26+
select
27+
<include
28+
refid="org.apache.ibatis.submitted.refid_resolution.ExternalMapper2.externalColumnList" />
29+
from table1
30+
</select>
31+
<insert id="insert" parameterType="map">
32+
<selectKey resultType="java.lang.Integer" keyProperty="id">
33+
CALL IDENTITY()
34+
</selectKey>
35+
insert into table1 (
36+
<include
37+
refid="org.apache.ibatis.submitted.refid_resolution.ExternalMapper2.externalColumnList" />
38+
) values (#{field1},#{field2})
39+
</insert>
40+
</mapper>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2012 The MyBatis Team
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<!DOCTYPE mapper
21+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
22+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
23+
24+
<mapper namespace="org.apache.ibatis.submitted.refid_resolution.ExternalMapper2">
25+
<sql id="externalColumnList">
26+
field1, field2
27+
</sql>
28+
</mapper>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2012 The MyBatis Team
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<!DOCTYPE configuration
21+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
22+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
23+
24+
<configuration>
25+
26+
<mappers>
27+
<mapper resource="org/apache/ibatis/submitted/refid_resolution/ExternalMapper1.xml" />
28+
<mapper resource="org/apache/ibatis/submitted/refid_resolution/ExternalMapper2.xml" />
29+
</mappers>
30+
31+
</configuration>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2012 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.refid_resolution;
17+
18+
import java.io.Reader;
19+
20+
import org.apache.ibatis.io.Resources;
21+
import org.apache.ibatis.session.SqlSessionFactory;
22+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
23+
import org.junit.Test;
24+
25+
/**
26+
* @see http://code.google.com/p/mybatis/issues/detail?id=291
27+
*/
28+
public class ExternalRefidResolutionTest {
29+
@Test
30+
public void testExternalRefAfterSelectKey() throws Exception {
31+
String resource = "org/apache/ibatis/submitted/refid_resolution/ExternalMapperConfig.xml";
32+
Reader reader = Resources.getResourceAsReader(resource);
33+
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
34+
SqlSessionFactory sqlSessionFactory = builder.build(reader);
35+
reader.close();
36+
sqlSessionFactory.getConfiguration().getMappedStatementNames();
37+
}
38+
}

0 commit comments

Comments
 (0)