Skip to content

Commit da5cad5

Browse files
committed
Fix for issue #3. Existing mapper statements should never be parsed as
XML.
1 parent 9b84acb commit da5cad5

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2012 The MyBatis Team
2+
* Copyright 2009-2013 The MyBatis Team
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.
@@ -15,6 +15,9 @@
1515
*/
1616
package org.apache.ibatis.scripting.xmltags;
1717

18+
import java.util.ArrayList;
19+
import java.util.List;
20+
1821
import org.apache.ibatis.executor.parameter.ParameterHandler;
1922
import org.apache.ibatis.mapping.BoundSql;
2023
import org.apache.ibatis.mapping.MappedStatement;
@@ -36,8 +39,15 @@ public SqlSource createSqlSource(Configuration configuration, XNode script, Clas
3639
}
3740

3841
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
39-
XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script);
40-
return builder.parseScriptNode();
42+
if (script.startsWith("<script>")) { // issue #3
43+
XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script);
44+
return builder.parseScriptNode();
45+
} else {
46+
List<SqlNode> contents = new ArrayList<SqlNode>();
47+
contents.add(new TextSqlNode(script.toString()));
48+
MixedSqlNode rootSqlNode = new MixedSqlNode(contents);
49+
return new DynamicSqlSource(configuration, rootSqlNode);
50+
}
4151
}
42-
52+
4353
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2012 The MyBatis Team
2+
* Copyright 2009-2013 The MyBatis Team
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.
@@ -41,11 +41,7 @@ public XMLScriptBuilder(Configuration configuration, XNode context) {
4141

4242
public XMLScriptBuilder(Configuration configuration, String context) {
4343
super(configuration);
44-
XPathParser parser = new XPathParser(
45-
"<script>" + context + "</script>",
46-
false,
47-
configuration.getVariables(),
48-
new XMLMapperEntityResolver());
44+
XPathParser parser = new XPathParser(context, false, configuration.getVariables(), new XMLMapperEntityResolver());
4945
this.context = parser.evalNode("/script");
5046
}
5147

src/test/java/org/apache/ibatis/submitted/language/LanguageTest.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,21 @@ public void testDynamicSelectWithIteration() {
132132
SqlSession sqlSession = sqlSessionFactory.openSession();
133133
try {
134134

135-
int[] ids = {2,4,5};
135+
int[] ids = { 2, 4, 5 };
136136
Map<String, Object> param = new HashMap<String, Object>();
137137
param.put("ids", ids);
138138
List<Name> answer = sqlSession.selectList("selectNamesWithIteration", param);
139139
assertEquals(3, answer.size());
140-
for (int i=0; i<ids.length; i++) {
140+
for (int i = 0; i < ids.length; i++) {
141141
assertEquals(ids[i], answer.get(i).getId());
142142
}
143143

144144
} finally {
145145
sqlSession.close();
146146
}
147147
}
148-
149-
@Test
148+
149+
@Test
150150
public void testLangRaw() {
151151
SqlSession sqlSession = sqlSessionFactory.openSession();
152152
try {
@@ -158,11 +158,10 @@ public void testLangRaw() {
158158
}
159159
} finally {
160160
sqlSession.close();
161-
}
161+
}
162162
}
163163

164-
165-
@Test
164+
@Test
166165
public void testLangXmlTags() {
167166
SqlSession sqlSession = sqlSessionFactory.openSession();
168167
try {
@@ -174,10 +173,10 @@ public void testLangXmlTags() {
174173
}
175174
} finally {
176175
sqlSession.close();
177-
}
176+
}
178177
}
179178

180-
@Test
179+
@Test
181180
public void testLangRawWithMapper() {
182181
SqlSession sqlSession = sqlSessionFactory.openSession();
183182
try {
@@ -190,10 +189,10 @@ public void testLangRawWithMapper() {
190189
}
191190
} finally {
192191
sqlSession.close();
193-
}
192+
}
194193
}
195194

196-
@Test
195+
@Test
197196
public void testLangVelocityWithMapper() {
198197
SqlSession sqlSession = sqlSessionFactory.openSession();
199198
try {
@@ -206,10 +205,10 @@ public void testLangVelocityWithMapper() {
206205
}
207206
} finally {
208207
sqlSession.close();
209-
}
208+
}
210209
}
211210

212-
@Test
211+
@Test
213212
public void testLangXmlWithMapper() {
214213
SqlSession sqlSession = sqlSessionFactory.openSession();
215214
try {
@@ -222,7 +221,23 @@ public void testLangXmlWithMapper() {
222221
}
223222
} finally {
224223
sqlSession.close();
225-
}
224+
}
225+
}
226+
227+
@Test
228+
public void testLangXmlWithMapperAndSqlSymbols() {
229+
SqlSession sqlSession = sqlSessionFactory.openSession();
230+
try {
231+
Parameter p = new Parameter(true, "Fli%");
232+
Mapper m = sqlSession.getMapper(Mapper.class);
233+
List<Name> answer = m.selectXmlWithMapperAndSqlSymbols(p);
234+
assertEquals(3, answer.size());
235+
for (Name n : answer) {
236+
assertEquals("Flintstone", n.getLastName());
237+
}
238+
} finally {
239+
sqlSession.close();
240+
}
226241
}
227242

228243
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ public interface Mapper {
1414
List<Name> selectRawWithMapper(Parameter p);
1515

1616
@Lang(XMLLanguageDriver.class)
17-
@Select("SELECT firstName <if test=\"includeLastName != null\">, lastName</if> FROM names WHERE lastName LIKE #{name}")
17+
@Select("<script>SELECT firstName <if test=\"includeLastName != null\">, lastName</if> FROM names WHERE lastName LIKE #{name}</script>")
1818
List<Name> selectXmlWithMapper(Parameter p);
1919

2020
@Select("SELECT firstName #if($_parameter.includeLastName), lastName#end FROM names WHERE lastName LIKE @{name}")
2121
List<Name> selectVelocityWithMapper(Parameter p);
2222

23+
@Lang(XMLLanguageDriver.class)
24+
@Select("SELECT firstName, lastName FROM names WHERE lastName LIKE #{name} and 0 < 1")
25+
List<Name> selectXmlWithMapperAndSqlSymbols(Parameter p);
26+
2327
}

0 commit comments

Comments
 (0)