• 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

85.9
/src/main/java/com/ibatis/sqlmap/engine/mapping/parameter/ParameterMap.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.parameter;
17

18
import com.ibatis.common.logging.Log;
19
import com.ibatis.common.logging.LogFactory;
20
import com.ibatis.sqlmap.engine.cache.CacheKey;
21
import com.ibatis.sqlmap.engine.exchange.DataExchange;
22
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
23
import com.ibatis.sqlmap.engine.scope.ErrorContext;
24
import com.ibatis.sqlmap.engine.scope.StatementScope;
25
import com.ibatis.sqlmap.engine.type.CustomTypeHandler;
26
import com.ibatis.sqlmap.engine.type.JdbcTypeRegistry;
27
import com.ibatis.sqlmap.engine.type.TypeHandler;
28

29
import java.sql.DatabaseMetaData;
30
import java.sql.PreparedStatement;
31
import java.sql.SQLException;
32
import java.sql.Types;
33
import java.util.HashMap;
34
import java.util.List;
35
import java.util.Map;
36

37
/**
38
 * The Class ParameterMap.
39
 */
40
public class ParameterMap {
41

42
  /** The Constant log. */
43
  private static final Log log = LogFactory.getLog(ParameterMap.class);
1✔
44

45
  /** The id. */
46
  private String id;
47

48
  /** The parameter class. */
49
  private Class parameterClass;
50

51
  /** The parameter mappings. */
52
  private ParameterMapping[] parameterMappings;
53

54
  /** The use set object for null value. */
55
  private Boolean useSetObjectForNullValue;
56

57
  /** The sql type to use for null value. */
58
  private int sqlTypeToUseForNullValue;
59

60
  /** The data exchange. */
61
  private DataExchange dataExchange;
62

63
  /** The resource. */
64
  private String resource;
65

66
  /** The parameter mapping index. */
67
  private Map parameterMappingIndex = new HashMap<>();
1✔
68

69
  /** The delegate. */
70
  private SqlMapExecutorDelegate delegate;
71

72
  /**
73
   * Instantiates a new parameter map.
74
   *
75
   * @param delegate
76
   *          the delegate
77
   */
78
  public ParameterMap(SqlMapExecutorDelegate delegate) {
1✔
79
    this.delegate = delegate;
1✔
80
  }
1✔
81

82
  /**
83
   * Gets the delegate.
84
   *
85
   * @return the delegate
86
   */
87
  public SqlMapExecutorDelegate getDelegate() {
88
    return delegate;
×
89
  }
90

91
  /**
92
   * Gets the id.
93
   *
94
   * @return the id
95
   */
96
  public String getId() {
97
    return id;
1✔
98
  }
99

100
  /**
101
   * Sets the id.
102
   *
103
   * @param id
104
   *          the new id
105
   */
106
  public void setId(String id) {
107
    this.id = id;
1✔
108
  }
1✔
109

110
  /**
111
   * Gets the parameter class.
112
   *
113
   * @return the parameter class
114
   */
115
  public Class getParameterClass() {
116
    return parameterClass;
1✔
117
  }
118

119
  /**
120
   * Sets the parameter class.
121
   *
122
   * @param parameterClass
123
   *          the new parameter class
124
   */
125
  public void setParameterClass(Class parameterClass) {
126
    this.parameterClass = parameterClass;
1✔
127
  }
1✔
128

129
  /**
130
   * Gets the data exchange.
131
   *
132
   * @return the data exchange
133
   */
134
  public DataExchange getDataExchange() {
135
    return dataExchange;
×
136
  }
137

138
  /**
139
   * Sets the data exchange.
140
   *
141
   * @param dataExchange
142
   *          the new data exchange
143
   */
144
  public void setDataExchange(DataExchange dataExchange) {
145
    this.dataExchange = dataExchange;
×
146
  }
×
147

148
  /**
149
   * Gets the parameter mappings.
150
   *
151
   * @return the parameter mappings
152
   */
153
  public ParameterMapping[] getParameterMappings() {
154
    return parameterMappings;
1✔
155
  }
156

157
  /**
158
   * Sets the parameter mapping list.
159
   *
160
   * @param parameterMappingList
161
   *          the new parameter mapping list
162
   */
163
  public void setParameterMappingList(List parameterMappingList) {
164
    this.parameterMappings = (ParameterMapping[]) parameterMappingList
1✔
165
        .toArray(new ParameterMapping[parameterMappingList.size()]);
1✔
166
    parameterMappingIndex.clear();
1✔
167
    for (int i = 0; i < parameterMappings.length; i++) {
1✔
168
      parameterMappingIndex.put(parameterMappings[i].getPropertyName(), Integer.valueOf(i));
1✔
169
    }
170
    Map props = new HashMap<>();
1✔
171
    props.put("map", this);
1✔
172

173
    dataExchange = delegate.getDataExchangeFactory().getDataExchangeForClass(parameterClass);
1✔
174
    dataExchange.initialize(props);
1✔
175
  }
1✔
176

177
  /**
178
   * Gets the parameter index.
179
   *
180
   * @param propertyName
181
   *          the property name
182
   *
183
   * @return the parameter index
184
   */
185
  public int getParameterIndex(String propertyName) {
186
    Integer idx = (Integer) parameterMappingIndex.get(propertyName);
×
187
    return idx == null ? -1 : idx.intValue();
×
188
  }
189

190
  /**
191
   * Gets the parameter count.
192
   *
193
   * @return the parameter count
194
   */
195
  public int getParameterCount() {
196
    return this.parameterMappings.length;
×
197
  }
198

199
  /**
200
   * Sets the parameters.
201
   *
202
   * @param statementScope
203
   *          the statement scope
204
   * @param ps
205
   *          the ps
206
   * @param parameters
207
   *          the parameters
208
   *
209
   * @throws SQLException
210
   *           the SQL exception
211
   */
212
  public void setParameters(StatementScope statementScope, PreparedStatement ps, Object[] parameters)
213
      throws SQLException {
214

215
    ErrorContext errorContext = statementScope.getErrorContext();
1✔
216
    errorContext.setActivity("applying a parameter map");
1✔
217
    errorContext.setObjectId(this.getId());
1✔
218
    errorContext.setResource(this.getResource());
1✔
219
    errorContext.setMoreInfo("Check the parameter map.");
1✔
220

221
    if (parameterMappings != null) {
1!
222
      for (int i = 0; i < parameterMappings.length; i++) {
1✔
223
        ParameterMapping mapping = parameterMappings[i];
1✔
224
        errorContext.setMoreInfo(mapping.getErrorString());
1✔
225
        if (mapping.isInputAllowed()) {
1!
226
          setParameter(ps, mapping, parameters, i);
1✔
227
        }
228
      }
229
    }
230
  }
1✔
231

232
  /**
233
   * Gets the parameter object values.
234
   *
235
   * @param statementScope
236
   *          the statement scope
237
   * @param parameterObject
238
   *          the parameter object
239
   *
240
   * @return the parameter object values
241
   */
242
  public Object[] getParameterObjectValues(StatementScope statementScope, Object parameterObject) {
243
    return dataExchange.getData(statementScope, this, parameterObject);
1✔
244
  }
245

246
  /**
247
   * Gets the cache key.
248
   *
249
   * @param statementScope
250
   *          the statement scope
251
   * @param parameterObject
252
   *          the parameter object
253
   *
254
   * @return the cache key
255
   */
256
  public CacheKey getCacheKey(StatementScope statementScope, Object parameterObject) {
257
    return dataExchange.getCacheKey(statementScope, this, parameterObject);
1✔
258
  }
259

260
  /**
261
   * Refresh parameter object values.
262
   *
263
   * @param statementScope
264
   *          the statement scope
265
   * @param parameterObject
266
   *          the parameter object
267
   * @param values
268
   *          the values
269
   */
270
  public void refreshParameterObjectValues(StatementScope statementScope, Object parameterObject, Object[] values) {
271
    dataExchange.setData(statementScope, this, parameterObject, values);
1✔
272
  }
1✔
273

274
  /**
275
   * Gets the resource.
276
   *
277
   * @return the resource
278
   */
279
  public String getResource() {
280
    return resource;
1✔
281
  }
282

283
  /**
284
   * Sets the resource.
285
   *
286
   * @param resource
287
   *          the new resource
288
   */
289
  public void setResource(String resource) {
290
    this.resource = resource;
1✔
291
  }
1✔
292

293
  /**
294
   * Sets the parameter.
295
   *
296
   * @param ps
297
   *          the ps
298
   * @param mapping
299
   *          the mapping
300
   * @param parameters
301
   *          the parameters
302
   * @param i
303
   *          the i
304
   *
305
   * @throws SQLException
306
   *           the SQL exception
307
   */
308
  protected void setParameter(PreparedStatement ps, ParameterMapping mapping, Object[] parameters, int i)
309
      throws SQLException {
310
    Object value = parameters[i];
1✔
311
    // Apply Null Value
312
    String nullValueString = mapping.getNullValue();
1✔
313
    if (nullValueString != null) {
1✔
314
      TypeHandler handler = mapping.getTypeHandler();
1✔
315
      if (handler.equals(value, nullValueString)) {
1✔
316
        value = null;
1✔
317
      }
318
    }
319

320
    // Set Parameter
321
    TypeHandler typeHandler = mapping.getTypeHandler();
1✔
322
    if ((value != null) || (typeHandler instanceof CustomTypeHandler)) {
1!
323
      typeHandler.setParameter(ps, i + 1, value, mapping.getJdbcTypeName());
1✔
324
    } else {
325
      int jdbcType = mapping.getJdbcType();
1✔
326
      if (jdbcType != JdbcTypeRegistry.UNKNOWN_TYPE) {
1✔
327
        ps.setNull(i + 1, jdbcType);
1✔
328
      } else {
329
        // Cloned from Spring StatementCreatorUtils.java (IBATIS-536)
330
        if (useSetObjectForNullValue == null) {
1✔
331
          // Keep current JDBC connection preferences for limiting introspections
332
          useSetObjectForNullValue = Boolean.FALSE;
1✔
333
          sqlTypeToUseForNullValue = Types.NULL;
1✔
334
          try {
335
            DatabaseMetaData dbmd = ps.getConnection().getMetaData();
1✔
336
            String databaseProductName = dbmd.getDatabaseProductName();
1✔
337
            String jdbcDriverName = dbmd.getDriverName();
1✔
338
            if (databaseProductName.startsWith("Informix") || databaseProductName.startsWith("Microsoft SQL Server")) {
1!
339
              useSetObjectForNullValue = Boolean.TRUE;
×
340
            } else if (databaseProductName.startsWith("DB2") || jdbcDriverName.startsWith("jConnect")
1!
341
                || jdbcDriverName.startsWith("SQLServer") || jdbcDriverName.startsWith("Apache Derby Embedded")) {
1!
342
              sqlTypeToUseForNullValue = Types.VARCHAR;
1✔
343
            }
344
          } catch (Throwable ex) {
×
345
            log.debug("Could not check database or driver name: " + ex.getMessage());
×
346
          }
1✔
347
        }
348
        if (useSetObjectForNullValue.booleanValue()) {
1!
349
          ps.setObject(i + 1, null);
×
350
        } else {
351
          ps.setNull(i + 1, sqlTypeToUseForNullValue);
1✔
352
        }
353
      }
354
    }
355
  }
1✔
356

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