Skip to content

includeColumns element for select #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public void applyIncludes(Node source) {
toInclude.getParentNode().insertBefore(toInclude.getFirstChild(), toInclude);
}
toInclude.getParentNode().removeChild(toInclude);
} else if (source.getNodeName().equals("includeColumns")) {
Node toInclude = findSqlFragment(getStringAttribute(source, "refid"));
String tableAlias = getStringAttribute(source, "tableAlias");
String columnAliasPrefix = getOptionalStringAttribute(source, "columnAliasPrefix");
applyIncludes(toInclude);
if (toInclude.getOwnerDocument() != source.getOwnerDocument()) {
toInclude = source.getOwnerDocument().importNode(toInclude, true);
}
source.getParentNode().replaceChild(toInclude, source);
while (toInclude.hasChildNodes()) {
Node firstChild = toInclude.getFirstChild();
String transformedColumnList = transformColumnList(firstChild.getTextContent(), tableAlias, columnAliasPrefix);
firstChild.setTextContent(transformedColumnList);
toInclude.getParentNode().insertBefore(firstChild, toInclude);
}
toInclude.getParentNode().removeChild(toInclude);
} else if (source.getNodeType() == Node.ELEMENT_NODE) {
NodeList children = source.getChildNodes();
for (int i=0; i<children.getLength(); i++) {
Expand All @@ -68,4 +84,35 @@ private Node findSqlFragment(String refid) {
private String getStringAttribute(Node node, String name) {
return node.getAttributes().getNamedItem(name).getNodeValue();
}

private String getOptionalStringAttribute(Node node, String name) {
Node namedItem = node.getAttributes().getNamedItem(name);
if (namedItem == null) {
return null;
}

return namedItem.getNodeValue();
}

private String transformColumnList(String columns, String tableAlias, String columnAliasPrefix) {
if (columnAliasPrefix == null) {
columnAliasPrefix = tableAlias;
}
StringBuilder stringBuilder = new StringBuilder();
String textContent = columns.trim();
String[] tokens = textContent.split(",");
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
stringBuilder.append(tableAlias).append(".").append(token);
if (!token.toLowerCase().contains(" ")) {
stringBuilder.append(" as ").append(columnAliasPrefix).append("_").append(token);
}

if (i < tokens.length - 1) {
stringBuilder.append(", ");
}
}

return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ alias CDATA #REQUIRED
type CDATA #REQUIRED
>

<!ELEMENT select (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>
<!ELEMENT select (#PCDATA | include | includeColumns | trim | where | set | foreach | choose | if | bind)*>
<!ATTLIST select
id CDATA #REQUIRED
parameterMap CDATA #IMPLIED
Expand Down Expand Up @@ -236,6 +236,13 @@ lang CDATA #IMPLIED
refid CDATA #REQUIRED
>

<!ELEMENT includeColumns EMPTY>
<!ATTLIST includeColumns
refid CDATA #REQUIRED
tableAlias CDATA #REQUIRED
columnAliasPrefix CDATA #IMPLIED
>

<!ELEMENT bind EMPTY>
<!ATTLIST bind
name CDATA #REQUIRED
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/org/apache/ibatis/builder/IncludeMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--

Copyright 2009-2012 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.domain.IncludeMapper">

<sql id="Base_Column_List">
a, b, c
</sql>

<sql id="Second_Base_Column_List">
p as z, q as y, r
</sql>

<select id="selectInclude">
select
<includeColumns refid="Base_Column_List" tableAlias="x"/>,
<includeColumns refid="Base_Column_List" tableAlias="y" columnAliasPrefix="q"/>,
<includeColumns refid="Second_Base_Column_List" tableAlias="z"/>
from dual x
join dual y
</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2009-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.builder;

import java.io.InputStream;

import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.junit.Assert;
import org.junit.Test;

public class IncludeMapperXmlBuilderTest {

@Test
public void shouldSuccessfullyLoadIncludeXMLMapperFile() throws Exception {
Configuration configuration = new Configuration();
String resource = "org/apache/ibatis/builder/IncludeMapper.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
builder.parse();

String sqlOutput = configuration.getMappedStatement("com.domain.IncludeMapper.selectInclude").getSqlSource().getBoundSql(null).getSql();

String[] lines = sqlOutput.split("\n");
Assert.assertEquals("x.a as x_a, x.b as x_b, x.c as x_c ,", lines[1].trim());
Assert.assertEquals("y.a as q_a, y.b as q_b, y.c as q_c ,", lines[2].trim());
Assert.assertEquals("z.p as z, z.q as y, z.r as z_r", lines[3].trim());
}

}