Skip to content

Support child nodes on bind element #1053

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -243,10 +243,10 @@ lang CDATA #IMPLIED
refid CDATA #REQUIRED
>

<!ELEMENT bind EMPTY>
<!ELEMENT bind (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>
<!ATTLIST bind
name CDATA #REQUIRED
value CDATA #REQUIRED
value CDATA #IMPLIED
>

<!ELEMENT sql (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
* Copyright 2009-2017 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.
@@ -53,6 +53,13 @@ public DynamicContext(Configuration configuration, Object parameterObject) {
bindings.put(DATABASE_ID_KEY, configuration.getDatabaseId());
}

/**
* @since 3.4.5
*/
public DynamicContext(DynamicContext source) {
this.bindings = source.bindings;
}

public Map<String, Object> getBindings() {
return bindings;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2009-2017 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.scripting.xmltags;

/**
* @author Kazuki Shimizu
* @since 3.4.5
*/
public class VarBindSqlNode implements SqlNode {

private final String name;
private final SqlNode contents;

public VarBindSqlNode(String name, SqlNode contents) {
this.name = name;
this.contents = contents;
}

@Override
public boolean apply(DynamicContext context) {
DynamicContext subContext = new DynamicContext(context);
contents.apply(subContext);
context.bind(name, subContext.getSql());
return true;
}

}
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.scripting.ScriptingException;
import org.apache.ibatis.scripting.defaults.RawSqlSource;
import org.apache.ibatis.session.Configuration;
import org.w3c.dom.Node;
@@ -114,7 +115,17 @@ public BindHandler() {
public void handleNode(XNode nodeToHandle, List<SqlNode> targetContents) {
final String name = nodeToHandle.getStringAttribute("name");
final String expression = nodeToHandle.getStringAttribute("value");
final VarDeclSqlNode node = new VarDeclSqlNode(name, expression);
if (expression != null && nodeToHandle.getNode().getChildNodes().getLength() > 0) {
throw new ScriptingException("Not support to specify value attribute and body with together on bind element.");
}
final SqlNode node;
if (expression != null) {
node = new VarDeclSqlNode(name, expression);
} else {
final List<SqlNode> contents = parseDynamicTags(nodeToHandle);
final MixedSqlNode mixedSqlNode = new MixedSqlNode(contents);
node = new VarBindSqlNode(name, mixedSqlNode);
}
targetContents.add(node);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
* Copyright 2009-2017 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.
@@ -19,4 +19,6 @@

public interface DynSqlMapper {
String selectDescription(@Param("p") String p);

String selectDescriptionById(@Param("id") Integer id);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2009-2015 the original author or authors.
Copyright 2009-2017 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.
@@ -30,4 +30,25 @@
WHERE id = 3
</if>
</select>
<select id="selectDescriptionById" resultType="string">
<bind name="conditions">
WHERE 1 = 1
<if test="id != null">
AND id = #{id}
</if>
<bind name="zero" value="0"/>
<bind name="innerCondition">
AND id > #{zero}
</bind>
${innerCondition}
</bind>
<include refid="selectDescriptionByConditions">
<property name="where" value="${conditions}"/>
</include>
</select>
<sql id="selectDescriptionByConditions">
SELECT description
FROM ibtest.names
${where}
</sql>
</mapper>
29 changes: 28 additions & 1 deletion src/test/java/org/apache/ibatis/submitted/dynsql/DynSqlTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
* Copyright 2009-2017 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.
@@ -15,17 +15,23 @@
*/
package org.apache.ibatis.submitted.dynsql;

import static com.googlecode.catchexception.apis.BDDCatchException.*;
import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.scripting.ScriptingException;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -170,4 +176,25 @@ public void testBindNull() {
}
}

@Test
public void testBindDynaSubNode() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
String description = mapper.selectDescriptionById(3);
assertEquals("Pebbles", description);
} finally {
sqlSession.close();
}
}

@Test
public void testInvalidBind() throws IOException {
Configuration configuration = new Configuration();
when(configuration).addMapper(InvalidBindSqlMapper.class);
then(caughtException()).isInstanceOf(PersistenceException.class)
.hasCauseInstanceOf(ScriptingException.class)
.hasMessageContaining("Not support to specify value attribute and body with together on bind element.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2009-2017 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.submitted.dynsql;

public interface InvalidBindSqlMapper {
String invalidBind();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2009-2017 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="org.apache.ibatis.submitted.dynsql.InvalidBindSqlMapper">
<select id="invalidBind">
<bind name="conditions" value="'WHERE 2 = 2'">
WHERE 1 = 1
</bind>
</select>
</mapper>