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

mybatis / ibatis-2 / #341

08 Sep 2023 11:16PM UTC coverage: 64.938% (+0.03%) from 64.913%
#341

push

github

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

fixes #174, update GHA, maven wrapper, fix EOL markers, do not use star imports

5047 of 7772 relevant lines covered (64.94%)

0.65 hits per line

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

94.29
/src/main/java/com/ibatis/sqlmap/engine/builder/xml/XMLSqlSource.java
1
/*
2
 * Copyright 2004-2023 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 com.ibatis.sqlmap.engine.builder.xml;
17

18
import com.ibatis.common.xml.NodeletUtils;
19
import com.ibatis.sqlmap.engine.config.SqlSource;
20
import com.ibatis.sqlmap.engine.mapping.parameter.InlineParameterMapParser;
21
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
22
import com.ibatis.sqlmap.engine.mapping.sql.SqlText;
23
import com.ibatis.sqlmap.engine.mapping.sql.dynamic.DynamicSql;
24
import com.ibatis.sqlmap.engine.mapping.sql.dynamic.elements.*;
25
import com.ibatis.sqlmap.engine.mapping.sql.raw.RawSql;
26

27
import java.util.Properties;
28

29
import org.w3c.dom.CharacterData;
30
import org.w3c.dom.Node;
31
import org.w3c.dom.NodeList;
32
import org.w3c.dom.CharacterData;
33

34
/**
35
 * The Class XMLSqlSource.
36
 */
37
public class XMLSqlSource implements SqlSource {
38

39
  /** The Constant PARAM_PARSER. */
1✔
40
  private static final InlineParameterMapParser PARAM_PARSER = new InlineParameterMapParser();
41

42
  /** The state. */
43
  private XmlParserState state;
44

45
  /** The parent node. */
46
  private Node parentNode;
47

48
  /**
49
   * Instantiates a new XML sql source.
50
   *
51
   * @param config
52
   *          the config
53
   * @param parentNode
54
   *          the parent node
55
   */
1✔
56
  public XMLSqlSource(XmlParserState config, Node parentNode) {
1✔
57
    this.state = config;
1✔
58
    this.parentNode = parentNode;
1✔
59
  }
60

61
  public Sql getSql() {
1✔
62
    state.getConfig().getErrorContext().setActivity("processing an SQL statement");
63

1✔
64
    boolean isDynamic = false;
1✔
65
    StringBuilder sqlBuffer = new StringBuilder();
1✔
66
    DynamicSql dynamic = new DynamicSql(state.getConfig().getClient().getDelegate());
1✔
67
    isDynamic = parseDynamicTags(parentNode, dynamic, sqlBuffer, isDynamic, false);
1✔
68
    String sqlStatement = sqlBuffer.toString();
1✔
69
    if (isDynamic) {
1✔
70
      return dynamic;
71
    } else {
1✔
72
      return new RawSql(sqlStatement);
73
    }
74
  }
75

76
  /**
77
   * Parses the dynamic tags.
78
   *
79
   * @param node
80
   *          the node
81
   * @param dynamic
82
   *          the dynamic
83
   * @param sqlBuffer
84
   *          the sql buffer
85
   * @param isDynamic
86
   *          the is dynamic
87
   * @param postParseRequired
88
   *          the post parse required
89
   *
90
   * @return true, if successful
91
   */
92
  private boolean parseDynamicTags(Node node, DynamicParent dynamic, StringBuilder sqlBuffer, boolean isDynamic,
93
      boolean postParseRequired) {
1✔
94
    state.getConfig().getErrorContext().setActivity("parsing dynamic SQL tags");
95

1✔
96
    NodeList children = node.getChildNodes();
1✔
97
    for (int i = 0; i < children.getLength(); i++) {
1✔
98
      Node child = children.item(i);
1✔
99
      String nodeName = child.getNodeName();
1✔
100
      if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE) {
101

1✔
102
        String data = ((CharacterData) child).getData();
1✔
103
        data = NodeletUtils.parsePropertyTokens(data, state.getGlobalProps());
104

105
        SqlText sqlText;
106

1✔
107
        if (postParseRequired) {
1✔
108
          sqlText = new SqlText();
1✔
109
          sqlText.setPostParseRequired(postParseRequired);
1✔
110
          sqlText.setText(data);
111
        } else {
1✔
112
          sqlText = PARAM_PARSER
1✔
113
              .parseInlineParameterMap(state.getConfig().getClient().getDelegate().getTypeHandlerFactory(), data, null);
1✔
114
          sqlText.setPostParseRequired(postParseRequired);
115
        }
116

1✔
117
        dynamic.addChild(sqlText);
118

1✔
119
        sqlBuffer.append(data);
1✔
120
      } else if ("include".equals(nodeName)) {
1✔
121
        Properties attributes = NodeletUtils.parseAttributes(child, state.getGlobalProps());
1✔
122
        String refid = (String) attributes.get("refid");
1✔
123
        Node includeNode = (Node) state.getSqlIncludes().get(refid);
1✔
124
        if (includeNode == null) {
×
125
          String nsrefid = state.applyNamespace(refid);
×
126
          includeNode = (Node) state.getSqlIncludes().get(nsrefid);
×
127
          if (includeNode == null) {
×
128
            throw new RuntimeException("Could not find SQL statement to include with refid '" + refid + "'");
129
          }
130
        }
1✔
131
        isDynamic = parseDynamicTags(includeNode, dynamic, sqlBuffer, isDynamic, false);
1✔
132
      } else {
1✔
133
        state.getConfig().getErrorContext().setMoreInfo("Check the dynamic tags.");
134

1✔
135
        SqlTagHandler handler = SqlTagHandlerFactory.getSqlTagHandler(nodeName);
1✔
136
        if (handler != null) {
1✔
137
          isDynamic = true;
138

1✔
139
          SqlTag tag = new SqlTag();
1✔
140
          tag.setName(nodeName);
1✔
141
          tag.setHandler(handler);
142

1✔
143
          Properties attributes = NodeletUtils.parseAttributes(child, state.getGlobalProps());
144

1✔
145
          tag.setPrependAttr(attributes.getProperty("prepend"));
1✔
146
          tag.setPropertyAttr(attributes.getProperty("property"));
1✔
147
          tag.setRemoveFirstPrepend(attributes.getProperty("removeFirstPrepend"));
148

1✔
149
          tag.setOpenAttr(attributes.getProperty("open"));
1✔
150
          tag.setCloseAttr(attributes.getProperty("close"));
151

1✔
152
          tag.setComparePropertyAttr(attributes.getProperty("compareProperty"));
1✔
153
          tag.setCompareValueAttr(attributes.getProperty("compareValue"));
1✔
154
          tag.setConjunctionAttr(attributes.getProperty("conjunction"));
155

156
          // an iterate ancestor requires a post parse
157

1✔
158
          if (dynamic instanceof SqlTag) {
1✔
159
            SqlTag parentSqlTag = (SqlTag) dynamic;
1✔
160
            if (parentSqlTag.isPostParseRequired() || tag.getHandler() instanceof IterateTagHandler) {
1✔
161
              tag.setPostParseRequired(true);
162
            }
1✔
163
          } else if (dynamic instanceof DynamicSql) {
1✔
164
            if (tag.getHandler() instanceof IterateTagHandler) {
1✔
165
              tag.setPostParseRequired(true);
166
            }
167
          }
168

1✔
169
          dynamic.addChild(tag);
170

1✔
171
          if (child.hasChildNodes()) {
1✔
172
            isDynamic = parseDynamicTags(child, tag, sqlBuffer, isDynamic, tag.isPostParseRequired());
173
          }
174
        }
175
      }
176
    }
1✔
177
    state.getConfig().getErrorContext().setMoreInfo(null);
1✔
178
    return isDynamic;
179
  }
180

181
}
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