Skip to content

Commit

Permalink
Fixes mybatis#33. Support includes with raw language
Browse files Browse the repository at this point in the history
  • Loading branch information
emacarron committed Apr 19, 2013
1 parent 1636009 commit 24d1430
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.Configuration;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class RawLanguageDriver implements LanguageDriver {

Expand All @@ -30,7 +32,16 @@ public ParameterHandler createParameterHandler(MappedStatement mappedStatement,
}

public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
return new RawSqlSource(configuration, script.getStringBody(), parameterType);
StringBuilder contents = new StringBuilder();
NodeList children = script.getNode().getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
XNode child = script.newXNode(children.item(i));
if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE || child.getNode().getNodeType() == Node.TEXT_NODE) {
String data = child.getStringBody("");
contents.append(data);
}
}
return new RawSqlSource(configuration, contents.toString(), parameterType);
}

public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ public void testLangRaw() {
}
}

@Test
public void testLangRawWithInclude() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Parameter p = new Parameter(true, "Fli%");
List<Name> answer = sqlSession.selectList("selectRawWithInclude", p);
assertEquals(3, answer.size());
for (Name n : answer) {
assertEquals("Flintstone", n.getLastName());
}
} finally {
sqlSession.close();
}
}

@Test
public void testLangXmlTags() {
SqlSession sqlSession = sqlSessionFactory.openSession();
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/language/Mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
WHERE lastName LIKE #{name}
</select>

<select id="selectRawWithInclude" resultType="Name" lang="raw">
SELECT firstName, lastName
<include refid="include"/>
WHERE lastName LIKE #{name}
</select>

<sql id="include">
FROM names
</sql>

<select id="selectXml" resultType="Name" lang="xml">
SELECT firstName
<if test="includeLastName != null">, lastName</if>
Expand Down

0 comments on commit 24d1430

Please sign in to comment.