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

mybatis / ibatis-2 / 730

28 Dec 2025 10:16PM UTC coverage: 65.615% (+0.5%) from 65.146%
730

push

github

web-flow
Update README.md

1602 of 2802 branches covered (57.17%)

5053 of 7701 relevant lines covered (65.61%)

0.66 hits per line

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

91.67
/src/main/java/com/ibatis/sqlmap/engine/mapping/sql/simple/SimpleDynamicSql.java
1
/*
2
 * Copyright 2004-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 com.ibatis.sqlmap.engine.mapping.sql.simple;
17

18
import com.ibatis.common.beans.Probe;
19
import com.ibatis.common.beans.ProbeFactory;
20
import com.ibatis.sqlmap.client.SqlMapException;
21
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
22
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
23
import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
24
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
25
import com.ibatis.sqlmap.engine.scope.StatementScope;
26

27
import java.util.StringTokenizer;
28

29
/**
30
 * The Class SimpleDynamicSql.
31
 */
32
public class SimpleDynamicSql implements Sql {
33

34
  /** The Constant PROBE. */
35
  private static final Probe PROBE = ProbeFactory.getProbe();
1✔
36

37
  /** The Constant ELEMENT_TOKEN. */
38
  private static final String ELEMENT_TOKEN = "$";
39

40
  /** The sql statement. */
41
  private String sqlStatement;
42

43
  /** The delegate. */
44
  private SqlMapExecutorDelegate delegate;
45

46
  /**
47
   * Instantiates a new simple dynamic sql.
48
   *
49
   * @param delegate
50
   *          the delegate
51
   * @param sqlStatement
52
   *          the sql statement
53
   */
54
  public SimpleDynamicSql(SqlMapExecutorDelegate delegate, String sqlStatement) {
1✔
55
    this.delegate = delegate;
1✔
56
    this.sqlStatement = sqlStatement;
1✔
57
  }
1✔
58

59
  @Override
60
  public String getSql(StatementScope statementScope, Object parameterObject) {
61
    return processDynamicElements(sqlStatement, parameterObject);
1✔
62
  }
63

64
  @Override
65
  public ParameterMap getParameterMap(StatementScope statementScope, Object parameterObject) {
66
    return statementScope.getParameterMap();
1✔
67
  }
68

69
  @Override
70
  public ResultMap getResultMap(StatementScope statementScope, Object parameterObject) {
71
    return statementScope.getResultMap();
1✔
72
  }
73

74
  @Override
75
  public void cleanup(StatementScope statementScope) {
76
  }
1✔
77

78
  /**
79
   * Checks if is simple dynamic sql.
80
   *
81
   * @param sql
82
   *          the sql
83
   *
84
   * @return true, if is simple dynamic sql
85
   */
86
  public static boolean isSimpleDynamicSql(String sql) {
87
    return sql != null && sql.indexOf(ELEMENT_TOKEN) > -1;
1!
88
  }
89

90
  /**
91
   * Process dynamic elements.
92
   *
93
   * @param sql
94
   *          the sql
95
   * @param parameterObject
96
   *          the parameter object
97
   *
98
   * @return the string
99
   */
100
  private String processDynamicElements(String sql, Object parameterObject) {
101
    StringTokenizer parser = new StringTokenizer(sql, ELEMENT_TOKEN, true);
1✔
102
    StringBuilder newSql = new StringBuilder();
1✔
103

104
    String token = null;
1✔
105
    String lastToken = null;
1✔
106
    while (parser.hasMoreTokens()) {
1✔
107
      token = parser.nextToken();
1✔
108

109
      if (ELEMENT_TOKEN.equals(lastToken)) {
1✔
110
        if (ELEMENT_TOKEN.equals(token)) {
1!
111
          newSql.append(ELEMENT_TOKEN);
×
112
          token = null;
×
113
        } else {
114

115
          Object value = null;
1✔
116
          if (parameterObject != null) {
1!
117
            if (delegate.getTypeHandlerFactory().hasTypeHandler(parameterObject.getClass())) {
1✔
118
              value = parameterObject;
1✔
119
            } else {
120
              value = PROBE.getObject(parameterObject, token);
1✔
121
            }
122
          }
123
          if (value != null) {
1!
124
            newSql.append(String.valueOf(value));
1✔
125
          }
126

127
          token = parser.nextToken();
1✔
128
          if (!ELEMENT_TOKEN.equals(token)) {
1!
129
            throw new SqlMapException("Unterminated dynamic element in sql (" + sql + ").");
×
130
          }
131
          token = null;
1✔
132
        }
1✔
133
      } else {
134
        if (!ELEMENT_TOKEN.equals(token)) {
1✔
135
          newSql.append(token);
1✔
136
        }
137
      }
138

139
      lastToken = token;
1✔
140
    }
141

142
    return newSql.toString();
1✔
143
  }
144

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