• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / freemarker-scripting / 377

05 May 2025 02:52AM CUT coverage: 93.536%. Remained the same
377

push

github

web-flow
Merge pull request #227 from hazendaz/master

Build updates

78 of 86 branches covered (90.7%)

246 of 263 relevant lines covered (93.54%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.12
/src/main/java/org/mybatis/scripting/freemarker/FreeMarkerSqlSource.java
1
/*
2
 *    Copyright 2015-2025 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.scripting.freemarker;
17

18
import java.io.CharArrayWriter;
19
import java.io.IOException;
20
import java.util.ArrayList;
21
import java.util.HashMap;
22
import java.util.List;
23
import java.util.Map;
24

25
import org.apache.ibatis.builder.SqlSourceBuilder;
26
import org.apache.ibatis.mapping.BoundSql;
27
import org.apache.ibatis.mapping.SqlSource;
28
import org.apache.ibatis.session.Configuration;
29

30
import freemarker.template.SimpleScalar;
31
import freemarker.template.Template;
32
import freemarker.template.TemplateException;
33
import freemarker.template.Version;
34

35
/**
36
 * Applies provided parameter(s) to FreeMarker template. Then passes the result into default MyBatis engine (and it
37
 * finally replaces #{}-params to '?'-params). So, FreeMarker is used as preprocessor for MyBatis engine.
38
 *
39
 * @author elwood
40
 */
41
public class FreeMarkerSqlSource implements SqlSource {
42
  private final Template template;
43
  private final Configuration configuration;
44
  private final Version incompatibleImprovementsVersion;
45
  private final String databaseId;
46

47
  public static final String GENERATED_PARAMS_KEY = "__GENERATED__";
48

49
  public FreeMarkerSqlSource(Template template, Configuration configuration, Version incompatibleImprovementsVersion) {
1✔
50
    this.template = template;
1✔
51
    this.configuration = configuration;
1✔
52
    this.incompatibleImprovementsVersion = incompatibleImprovementsVersion;
1✔
53
    this.databaseId = configuration.getDatabaseId();
1✔
54
  }
1✔
55

56
  /**
57
   * Populates additional parameters to data context. Data context can be {@link java.util.Map} or
58
   * {@link org.mybatis.scripting.freemarker.ParamObjectAdapter} instance.
59
   */
60
  protected Object preProcessDataContext(Object dataContext, boolean isMap) {
61
    if (isMap) {
1✔
62
      ((Map<String, Object>) dataContext).put(MyBatisParamDirective.DEFAULT_KEY, new MyBatisParamDirective());
1✔
63
      ((Map<String, Object>) dataContext).put(MyBatisParamDirective.DATABASE_ID_KEY, new SimpleScalar(this.databaseId));
1✔
64
    } else {
65
      ((ParamObjectAdapter) dataContext).putAdditionalParam(MyBatisParamDirective.DEFAULT_KEY,
1✔
66
          new MyBatisParamDirective());
67
      ((ParamObjectAdapter) dataContext).putAdditionalParam(MyBatisParamDirective.DATABASE_ID_KEY,
1✔
68
          new SimpleScalar(this.databaseId));
69
    }
70
    return dataContext;
1✔
71
  }
72

73
  @Override
74
  public BoundSql getBoundSql(Object parameterObject) {
75
    // Add to passed parameterObject our predefined directive - MyBatisParamDirective
76
    // It will be available as "p" inside templates
77
    Object dataContext;
78
    List generatedParams = new ArrayList<>();
1✔
79
    if (parameterObject != null) {
1✔
80
      if (parameterObject instanceof Map) {
1✔
81
        HashMap<String, Object> map = new HashMap<>((Map<String, Object>) parameterObject);
1✔
82
        map.put(GENERATED_PARAMS_KEY, generatedParams);
1✔
83
        dataContext = preProcessDataContext(map, true);
1✔
84
      } else {
1✔
85
        ParamObjectAdapter adapter = new ParamObjectAdapter(parameterObject, generatedParams,
1✔
86
            incompatibleImprovementsVersion);
87
        dataContext = preProcessDataContext(adapter, false);
1✔
88
      }
1✔
89
    } else {
90
      HashMap<Object, Object> map = new HashMap<>();
1✔
91
      map.put(GENERATED_PARAMS_KEY, generatedParams);
1✔
92
      dataContext = preProcessDataContext(map, true);
1✔
93
    }
94

95
    CharArrayWriter writer = new CharArrayWriter();
1✔
96
    try {
97
      template.process(dataContext, writer);
1✔
98
    } catch (TemplateException | IOException e) {
×
99
      throw new RuntimeException(e);
×
100
    }
1✔
101

102
    // We got SQL ready for MyBatis here. This SQL contains
103
    // params declarations like "#{param}",
104
    // they will be replaced to '?' by MyBatis engine further
105
    String sql = writer.toString();
1✔
106

107
    if (!generatedParams.isEmpty()) {
1✔
108
      if (!(parameterObject instanceof Map)) {
1✔
109
        throw new UnsupportedOperationException("Auto-generated prepared statements parameters"
1✔
110
            + " are not available if using parameters object. Use @Param-annotated parameters" + " instead.");
111
      }
112

113
      Map<String, Object> parametersMap = (Map<String, Object>) parameterObject;
1✔
114
      for (int i = 0; i < generatedParams.size(); i++) {
1✔
115
        parametersMap.put("_p" + i, generatedParams.get(i));
1✔
116
      }
117
    }
118

119
    // Pass retrieved SQL into MyBatis engine, it will substitute prepared-statements parameters
120
    SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
1✔
121
    Class<?> parameterType1 = parameterObject == null ? Object.class : parameterObject.getClass();
1✔
122
    SqlSource sqlSource = sqlSourceParser.parse(sql, parameterType1, new HashMap<>());
1✔
123
    return sqlSource.getBoundSql(parameterObject);
1✔
124
  }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc