• 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

95.45
/src/main/java/com/ibatis/sqlmap/engine/builder/xml/SqlStatementParser.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.resources.Resources;
19
import com.ibatis.common.xml.NodeletUtils;
20
import com.ibatis.sqlmap.client.SqlMapException;
21
import com.ibatis.sqlmap.engine.config.MappedStatementConfig;
22
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
23

24
import java.util.Properties;
25

26
import org.w3c.dom.CharacterData;
27
import org.w3c.dom.Node;
28
import org.w3c.dom.NodeList;
29
import org.w3c.dom.CharacterData;
30

31
/**
32
 * The Class SqlStatementParser.
33
 */
34
public class SqlStatementParser {
35

36
  /** The state. */
37
  private XmlParserState state;
38

39
  /**
40
   * Instantiates a new sql statement parser.
41
   *
42
   * @param config
43
   *          the config
44
   */
1✔
45
  public SqlStatementParser(XmlParserState config) {
1✔
46
    this.state = config;
1✔
47
  }
48

49
  /**
50
   * Parses the general statement.
51
   *
52
   * @param node
53
   *          the node
54
   * @param statement
55
   *          the statement
56
   */
57
  public void parseGeneralStatement(Node node, MappedStatement statement) {
58

59
    // get attributes
1✔
60
    Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
61
    String id = attributes.getProperty("id");
1✔
62
    String parameterMapName = state.applyNamespace(attributes.getProperty("parameterMap"));
1✔
63
    String parameterClassName = attributes.getProperty("parameterClass");
1✔
64
    String resultMapName = attributes.getProperty("resultMap");
1✔
65
    String resultClassName = attributes.getProperty("resultClass");
1✔
66
    String cacheModelName = state.applyNamespace(attributes.getProperty("cacheModel"));
1✔
67
    String xmlResultName = attributes.getProperty("xmlResultName");
1✔
68
    String resultSetType = attributes.getProperty("resultSetType");
1✔
69
    String fetchSize = attributes.getProperty("fetchSize");
1✔
70
    String allowRemapping = attributes.getProperty("remapResults");
1✔
71
    String timeout = attributes.getProperty("timeout");
72

1✔
73
    if (state.isUseStatementNamespaces()) {
×
74
      id = state.applyNamespace(id);
75
    }
1✔
76
    String[] additionalResultMapNames = null;
1✔
77
    if (resultMapName != null) {
1✔
78
      additionalResultMapNames = state.getAllButFirstToken(resultMapName);
1✔
79
      resultMapName = state.getFirstToken(resultMapName);
1✔
80
      resultMapName = state.applyNamespace(resultMapName);
1✔
81
      for (int i = 0; i < additionalResultMapNames.length; i++) {
1✔
82
        additionalResultMapNames[i] = state.applyNamespace(additionalResultMapNames[i]);
83
      }
84
    }
85

1✔
86
    String[] additionalResultClassNames = null;
1✔
87
    if (resultClassName != null) {
1✔
88
      additionalResultClassNames = state.getAllButFirstToken(resultClassName);
1✔
89
      resultClassName = state.getFirstToken(resultClassName);
90
    }
1✔
91
    Class[] additionalResultClasses = null;
1✔
92
    if (additionalResultClassNames != null) {
1✔
93
      additionalResultClasses = new Class[additionalResultClassNames.length];
1✔
94
      for (int i = 0; i < additionalResultClassNames.length; i++) {
1✔
95
        additionalResultClasses[i] = resolveClass(additionalResultClassNames[i]);
96
      }
97
    }
98

1✔
99
    state.getConfig().getErrorContext().setMoreInfo("Check the parameter class.");
1✔
100
    Class parameterClass = resolveClass(parameterClassName);
101

1✔
102
    state.getConfig().getErrorContext().setMoreInfo("Check the result class.");
1✔
103
    Class resultClass = resolveClass(resultClassName);
104

1✔
105
    Integer timeoutInt = timeout == null ? null : Integer.valueOf(timeout);
1✔
106
    Integer fetchSizeInt = fetchSize == null ? null : Integer.valueOf(fetchSize);
1✔
107
    boolean allowRemappingBool = "true".equals(allowRemapping);
108

1✔
109
    MappedStatementConfig statementConf = state.getConfig().newMappedStatementConfig(id, statement,
110
        new XMLSqlSource(state, node), parameterMapName, parameterClass, resultMapName, additionalResultMapNames,
111
        resultClass, additionalResultClasses, resultSetType, fetchSizeInt, allowRemappingBool, timeoutInt,
112
        cacheModelName, xmlResultName);
113

1✔
114
    findAndParseSelectKey(node, statementConf);
1✔
115
  }
116

117
  /**
118
   * Resolve class.
119
   *
120
   * @param resultClassName
121
   *          the result class name
122
   *
123
   * @return the class
124
   */
125
  private Class resolveClass(String resultClassName) {
126
    try {
1✔
127
      if (resultClassName != null) {
1✔
128
        return Resources.classForName(state.getConfig().getTypeHandlerFactory().resolveAlias(resultClassName));
129
      } else {
1✔
130
        return null;
131
      }
×
132
    } catch (ClassNotFoundException e) {
×
133
      throw new SqlMapException("Error.  Could not initialize class.  Cause: " + e, e);
134
    }
135
  }
136

137
  /**
138
   * Find and parse select key.
139
   *
140
   * @param node
141
   *          the node
142
   * @param config
143
   *          the config
144
   */
145
  private void findAndParseSelectKey(Node node, MappedStatementConfig config) {
1✔
146
    state.getConfig().getErrorContext().setActivity("parsing select key tags");
1✔
147
    boolean foundSQLFirst = false;
1✔
148
    NodeList children = node.getChildNodes();
1✔
149
    for (int i = 0; i < children.getLength(); i++) {
1✔
150
      Node child = children.item(i);
1✔
151
      if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE) {
1✔
152
        String data = ((CharacterData) child).getData();
1✔
153
        if (data.trim().length() > 0) {
1✔
154
          foundSQLFirst = true;
155
        }
1✔
156
      } else if (child.getNodeType() == Node.ELEMENT_NODE && "selectKey".equals(child.getNodeName())) {
1✔
157
        Properties attributes = NodeletUtils.parseAttributes(child, state.getGlobalProps());
1✔
158
        String keyPropName = attributes.getProperty("keyProperty");
1✔
159
        String resultClassName = attributes.getProperty("resultClass");
1✔
160
        String type = attributes.getProperty("type");
1✔
161
        config.setSelectKeyStatement(new XMLSqlSource(state, child), resultClassName, keyPropName, foundSQLFirst, type);
1✔
162
        break;
163
      }
164
    }
1✔
165
    state.getConfig().getErrorContext().setMoreInfo(null);
166

1✔
167
  }
168

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