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

mybatis / ibatis-2 / #363

04 Nov 2023 06:48PM UTC coverage: 64.938%. Remained the same
#363

push

github

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

Update GHA and prepare for new site distribution

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

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

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

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

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

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

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

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

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

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

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

104
        SqlText sqlText;
105

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

116
        dynamic.addChild(sqlText);
1✔
117

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

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

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

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

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

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

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

155
          // an iterate ancestor requires a post parse
156

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

168
          dynamic.addChild(tag);
1✔
169

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

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