Skip to content

Commit 5e40e5a

Browse files
authored
Merge pull request #45 from kazuki43zoo/gh-16-change-method-signature-on-SqlGenerator
Change method signature of SqlGenerator#generate
2 parents 9bec74e + fb75dce commit 5e40e5a

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

src/main/asciidoc/user-guide.adoc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ String sql = sqlGenerator.generate(
11131113

11141114
==== Receiving custom bind variables
11151115

1116-
You can receiving custom bind variables that created during template processing via user define `Map` reference as follow:
1116+
You can receiving custom bind variables that created during template processing via a `BiConsumer` function as follow:
11171117

11181118
[NOTE]
11191119
====
@@ -1129,16 +1129,14 @@ Map<String, Object> conditionsMap = new HashMap<>();
11291129
conditionsMap.put("name", "Yamada");
11301130
11311131
// sql = "SELECT * FROM accounts WHERE name = #{patternName}"
1132-
// customBindVariablesStore = {"patternName":"Yamada%"}
1133-
Map<String, Object> customBindVariablesStore = new HashMap<>(); // <1>
1132+
// conditionsMap = {"name":"Yamada", "patternName":"Yamada%"}
11341133
String sql = sqlGenerator.generate(
11351134
"/*[# mb:bind='patternName=|${#likes.escapeWildcard(name)}%|' /]*/" +
11361135
"SELECT * FROM accounts WHERE name = /*[# mb:p='patternName']*/ 'Sato' /*[/]*/",
1137-
conditionsMap, null, customBindVariablesStore); // <2>
1136+
conditionsMap, null, conditionsMap::put); // <1>
11381137
----
11391138

1140-
<1> Define a `Map` reference for receiving custom bind variables
1141-
<2> Specify(Pass) a `Map` reference for receiving custom bind variables at 4th argument of `generate` method
1139+
<1> Specify(Pass) a `BiConsumer` reference for receiving custom bind variables at 4th argument of `generate` method
11421140

11431141
=== Advanced Usage
11441142

src/main/java/org/mybatis/scripting/thymeleaf/SqlGenerator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424
import java.util.Optional;
2525
import java.util.Set;
26+
import java.util.function.BiConsumer;
2627
import java.util.function.BiFunction;
2728
import java.util.stream.Collectors;
2829

@@ -199,12 +200,12 @@ public String generate(CharSequence sqlTemplate, Object parameter, Map<String, O
199200
* a parameter object
200201
* @param customVariable
201202
* a custom variables for passing to template engine
202-
* @param customBindVariableStore
203-
* a store for saving a custom bind variable that generated with {@code mb:bind} or {@code mb:param}
203+
* @param customBindVariableBinder
204+
* a binder for a custom bind variable that generated with {@code mb:bind} or {@code mb:param}
204205
* @return a processed SQL by template engine
205206
*/
206207
public String generate(CharSequence sqlTemplate, Object parameter, Map<String, Object> customVariable,
207-
Map<String, Object> customBindVariableStore) {
208+
BiConsumer<String, Object> customBindVariableBinder) {
208209

209210
Map<String, Object> processingCustomVariables = new HashMap<>(defaultCustomVariables);
210211
Optional.ofNullable(customVariable).ifPresent(processingCustomVariables::putAll);
@@ -213,8 +214,8 @@ public String generate(CharSequence sqlTemplate, Object parameter, Map<String, O
213214
String sql = templateEngine.process(sqlTemplate.toString(), context);
214215

215216
MyBatisBindingContext bindingContext = MyBatisBindingContext.load(context);
216-
if (bindingContext != null && customBindVariableStore != null) {
217-
customBindVariableStore.putAll(bindingContext.getCustomBindVariables());
217+
if (bindingContext != null && customBindVariableBinder != null) {
218+
bindingContext.getCustomBindVariables().forEach(customBindVariableBinder);
218219
}
219220

220221
return sql;

src/main/java/org/mybatis/scripting/thymeleaf/ThymeleafSqlSource.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.lang.reflect.InvocationTargetException;
1919
import java.util.Arrays;
2020
import java.util.Collection;
21-
import java.util.HashMap;
2221
import java.util.HashSet;
2322
import java.util.Locale;
2423
import java.util.Map;
@@ -95,10 +94,8 @@ public BoundSql getBoundSql(Object parameterObject) {
9594
customVariables.put(TemporaryTakeoverKeys.CONFIGURATION, configuration);
9695
customVariables.put(TemporaryTakeoverKeys.DYNAMIC_CONTEXT, dynamicContext);
9796
customVariables.put(TemporaryTakeoverKeys.PROCESSING_PARAMETER_TYPE, processingParameterType);
98-
Map<String, Object> customBindVariableStore = new HashMap<>();
99-
String sql = sqlGenerator.generate(sqlTemplate, parameterObject, customVariables, customBindVariableStore);
97+
String sql = sqlGenerator.generate(sqlTemplate, parameterObject, customVariables, dynamicContext::bind);
10098

101-
customBindVariableStore.forEach(dynamicContext::bind);
10299
SqlSource sqlSource = sqlSourceBuilder.parse(sql, processingParameterType, dynamicContext.getBindings());
103100
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
104101
dynamicContext.getBindings().forEach(boundSql::setAdditionalParameter);

src/test/java/org/mybatis/scripting/thymeleaf/SqlGeneratorTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,17 @@ void processWithCustomBindVariablesStore() {
248248
// @formatter: on
249249
{
250250
Map<String, Object> param = Collections.singletonMap("name", "Be%");
251-
Map<String, Object> store = new HashMap<>();
251+
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(param);
252252

253-
String sql = sqlGenerator.generate(sqlTemplate, param, null, store);
253+
String sql = sqlGenerator.generate(sqlTemplate, param, null, mapSqlParameterSource::addValue);
254254

255-
Map<String, Object> record = jdbcOperations.queryForMap(sql, new MapSqlParameterSource(param).addValues(store));
255+
Map<String, Object> record = jdbcOperations.queryForMap(sql, mapSqlParameterSource);
256256

257257
Assertions.assertEquals(6, record.get("ID"));
258258
Assertions.assertEquals("Be%ty", record.get("FIRSTNAME"));
259259
Assertions.assertEquals("Ab_le", record.get("LASTNAME"));
260260

261-
Assertions.assertEquals(1, store.size());
262-
Assertions.assertEquals("Be\\%%", store.get("patternName"));
261+
Assertions.assertEquals("Be\\%%", mapSqlParameterSource.getValue("patternName"));
263262
}
264263
}
265264

0 commit comments

Comments
 (0)