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

mybatis / ibatis-2 / 678

27 Dec 2025 01:13AM UTC coverage: 65.584% (+0.05%) from 65.532%
678

push

github

hazendaz
Merge remote-tracking branch 'upstream/master' into support-javax

1606 of 2810 branches covered (57.15%)

167 of 305 new or added lines in 90 files covered. (54.75%)

6 existing lines in 4 files now uncovered.

5067 of 7726 relevant lines covered (65.58%)

0.66 hits per line

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

88.89
/src/main/java/com/ibatis/sqlmap/engine/mapping/result/AutoResultMap.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.result;
17

18
import com.ibatis.common.beans.ClassInfo;
19
import com.ibatis.common.beans.Probe;
20
import com.ibatis.common.beans.ProbeFactory;
21
import com.ibatis.sqlmap.client.SqlMapException;
22
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
23
import com.ibatis.sqlmap.engine.scope.StatementScope;
24
import com.ibatis.sqlmap.engine.type.DomTypeMarker;
25

26
import java.sql.ResultSet;
27
import java.sql.ResultSetMetaData;
28
import java.sql.SQLException;
29
import java.util.ArrayList;
30
import java.util.HashMap;
31
import java.util.List;
32
import java.util.Map;
33

34
/**
35
 * An automatic result map for simple stuff.
36
 */
37
public class AutoResultMap extends ResultMap {
38

39
  /**
40
   * Constructor to pass in the SqlMapExecutorDelegate.
41
   *
42
   * @param delegate
43
   *          - the delegate
44
   * @param allowRemapping
45
   *          the allow remapping
46
   */
47
  public AutoResultMap(SqlMapExecutorDelegate delegate, boolean allowRemapping) {
48
    super(delegate);
1✔
49
    this.allowRemapping = allowRemapping;
1✔
50
  }
1✔
51

52
  @Override
53
  public synchronized Object[] getResults(StatementScope statementScope, ResultSet rs) throws SQLException {
54
    if (allowRemapping || getResultMappings() == null) {
1✔
55
      initialize(rs);
1✔
56
    }
57
    return super.getResults(statementScope, rs);
1✔
58
  }
59

60
  @Override
61
  public Object setResultObjectValues(StatementScope statementScope, Object resultObject, Object[] values) {
62
    // synchronization is only needed when remapping is enabled
63
    if (allowRemapping) {
1✔
64
      synchronized (this) {
1✔
65
        return super.setResultObjectValues(statementScope, resultObject, values);
1✔
66
      }
67
    }
68
    return super.setResultObjectValues(statementScope, resultObject, values);
1✔
69
  }
70

71
  /**
72
   * Initialize.
73
   *
74
   * @param rs
75
   *          the rs
76
   */
77
  private void initialize(ResultSet rs) {
78
    if (getResultClass() == null) {
1!
79
      throw new SqlMapException(
×
80
          "The automatic ResultMap named " + this.getId() + " had a null result class (not allowed).");
×
81
    }
82
    if (Map.class.isAssignableFrom(getResultClass())) {
1✔
83
      initializeMapResults(rs);
1✔
84
    } else if (getDelegate().getTypeHandlerFactory().getTypeHandler(getResultClass()) != null) {
1✔
85
      initializePrimitiveResults(rs);
1✔
86
    } else if (DomTypeMarker.class.isAssignableFrom(getResultClass())) {
1✔
87
      initializeXmlResults(rs);
1✔
88
    } else {
89
      initializeBeanResults(rs);
1✔
90
    }
91
  }
1✔
92

93
  /**
94
   * Initialize bean results.
95
   *
96
   * @param rs
97
   *          the rs
98
   */
99
  private void initializeBeanResults(ResultSet rs) {
100
    try {
101
      ClassInfo classInfo = ClassInfo.getInstance(getResultClass());
1✔
102
      String[] propertyNames = classInfo.getWriteablePropertyNames();
1✔
103

104
      Map propertyMap = new HashMap<>();
1✔
105
      for (String propertyName : propertyNames) {
1✔
106
        propertyMap.put(propertyName.toUpperCase(java.util.Locale.ENGLISH), propertyName);
1✔
107
      }
108

109
      List resultMappingList = new ArrayList<>();
1✔
110
      ResultSetMetaData rsmd = rs.getMetaData();
1✔
111
      for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
1✔
112
        String columnName = getColumnIdentifier(rsmd, i + 1);
1✔
113
        String upperColumnName = columnName.toUpperCase(java.util.Locale.ENGLISH);
1✔
114
        String matchedProp = (String) propertyMap.get(upperColumnName);
1✔
115
        Class type = null;
1✔
116
        if (matchedProp == null) {
1✔
117
          Probe p = ProbeFactory.getProbe(this.getResultClass());
1✔
118
          try {
119
            type = p.getPropertyTypeForSetter(this.getResultClass(), columnName);
1✔
120
          } catch (Exception e) {
1✔
121
            // TODO - add logging to this class?
122
          }
1✔
123
        } else {
1✔
124
          type = classInfo.getSetterType(matchedProp);
1✔
125
        }
126
        if (type != null || matchedProp != null) {
1!
127
          ResultMapping resultMapping = new ResultMapping();
1✔
128
          resultMapping.setPropertyName((matchedProp != null ? matchedProp : columnName));
1✔
129
          resultMapping.setColumnName(columnName);
1✔
130
          resultMapping.setColumnIndex(i + 1);
1✔
131
          resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(type)); // map
1✔
132
                                                                                                    // SQL
133
                                                                                                    // to
134
                                                                                                    // JDBC
135
                                                                                                    // type
136
          resultMappingList.add(resultMapping);
1✔
137
        }
138
      }
139
      setResultMappingList(resultMappingList);
1✔
140

141
    } catch (SQLException e) {
×
142
      throw new RuntimeException("Error automapping columns. Cause: " + e);
×
143
    }
1✔
144

145
  }
1✔
146

147
  /**
148
   * Initialize xml results.
149
   *
150
   * @param rs
151
   *          the rs
152
   */
153
  private void initializeXmlResults(ResultSet rs) {
154
    try {
155
      List resultMappingList = new ArrayList<>();
1✔
156
      ResultSetMetaData rsmd = rs.getMetaData();
1✔
157
      for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
1✔
158
        String columnName = getColumnIdentifier(rsmd, i + 1);
1✔
159
        ResultMapping resultMapping = new ResultMapping();
1✔
160
        resultMapping.setPropertyName(columnName);
1✔
161
        resultMapping.setColumnName(columnName);
1✔
162
        resultMapping.setColumnIndex(i + 1);
1✔
163
        resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(String.class));
1✔
164
        resultMappingList.add(resultMapping);
1✔
165
      }
166
      setResultMappingList(resultMappingList);
1✔
167
    } catch (SQLException e) {
×
168
      throw new RuntimeException("Error automapping columns. Cause: " + e);
×
169
    }
1✔
170
  }
1✔
171

172
  /**
173
   * Initialize map results.
174
   *
175
   * @param rs
176
   *          the rs
177
   */
178
  private void initializeMapResults(ResultSet rs) {
179
    try {
180
      List resultMappingList = new ArrayList<>();
1✔
181
      ResultSetMetaData rsmd = rs.getMetaData();
1✔
182
      for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
1✔
183
        String columnName = getColumnIdentifier(rsmd, i + 1);
1✔
184
        ResultMapping resultMapping = new ResultMapping();
1✔
185
        resultMapping.setPropertyName(columnName);
1✔
186
        resultMapping.setColumnName(columnName);
1✔
187
        resultMapping.setColumnIndex(i + 1);
1✔
188
        resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(Object.class));
1✔
189
        resultMappingList.add(resultMapping);
1✔
190
      }
191

192
      setResultMappingList(resultMappingList);
1✔
193

194
    } catch (SQLException e) {
×
195
      throw new RuntimeException("Error automapping columns. Cause: " + e);
×
196
    }
1✔
197
  }
1✔
198

199
  /**
200
   * Initialize primitive results.
201
   *
202
   * @param rs
203
   *          the rs
204
   */
205
  private void initializePrimitiveResults(ResultSet rs) {
206
    try {
207
      ResultSetMetaData rsmd = rs.getMetaData();
1✔
208
      String columnName = getColumnIdentifier(rsmd, 1);
1✔
209
      ResultMapping resultMapping = new ResultMapping();
1✔
210
      resultMapping.setPropertyName(columnName);
1✔
211
      resultMapping.setColumnName(columnName);
1✔
212
      resultMapping.setColumnIndex(1);
1✔
213
      resultMapping.setTypeHandler(getDelegate().getTypeHandlerFactory().getTypeHandler(getResultClass()));
1✔
214

215
      List resultMappingList = new ArrayList<>();
1✔
216
      resultMappingList.add(resultMapping);
1✔
217

218
      setResultMappingList(resultMappingList);
1✔
219

220
    } catch (SQLException e) {
×
221
      throw new RuntimeException("Error automapping columns. Cause: " + e);
×
222
    }
1✔
223
  }
1✔
224

225
  /**
226
   * Gets the column identifier.
227
   *
228
   * @param rsmd
229
   *          the rsmd
230
   * @param i
231
   *          the i
232
   *
233
   * @return the column identifier
234
   *
235
   * @throws SQLException
236
   *           the SQL exception
237
   */
238
  private String getColumnIdentifier(ResultSetMetaData rsmd, int i) throws SQLException {
239
    if (delegate.isUseColumnLabel()) {
1!
240
      return rsmd.getColumnLabel(i);
1✔
241
    }
NEW
242
    return rsmd.getColumnName(i);
×
243
  }
244

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

© 2026 Coveralls, Inc