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

mybatis / mybatis-3 / #2968

pending completion
#2968

push

github

web-flow
Merge pull request #2792 from hazendaz/formatting

[ci] Formatting

84 of 84 new or added lines in 23 files covered. (100.0%)

9412 of 10781 relevant lines covered (87.3%)

0.87 hits per line

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

97.83
/src/main/java/org/apache/ibatis/executor/keygen/SelectKeyGenerator.java
1
/*
2
 *    Copyright 2009-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 org.apache.ibatis.executor.keygen;
17

18
import java.sql.Statement;
19
import java.util.List;
20

21
import org.apache.ibatis.executor.Executor;
22
import org.apache.ibatis.executor.ExecutorException;
23
import org.apache.ibatis.mapping.MappedStatement;
24
import org.apache.ibatis.reflection.MetaObject;
25
import org.apache.ibatis.session.Configuration;
26
import org.apache.ibatis.session.ExecutorType;
27
import org.apache.ibatis.session.RowBounds;
28

29
/**
30
 * @author Clinton Begin
31
 * @author Jeff Butler
32
 */
33
public class SelectKeyGenerator implements KeyGenerator {
34

35
  public static final String SELECT_KEY_SUFFIX = "!selectKey";
36
  private final boolean executeBefore;
37
  private final MappedStatement keyStatement;
38

39
  public SelectKeyGenerator(MappedStatement keyStatement, boolean executeBefore) {
1✔
40
    this.executeBefore = executeBefore;
1✔
41
    this.keyStatement = keyStatement;
1✔
42
  }
1✔
43

44
  @Override
45
  public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
46
    if (executeBefore) {
1✔
47
      processGeneratedKeys(executor, ms, parameter);
1✔
48
    }
49
  }
1✔
50

51
  @Override
52
  public void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
53
    if (!executeBefore) {
1✔
54
      processGeneratedKeys(executor, ms, parameter);
1✔
55
    }
56
  }
1✔
57

58
  private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
59
    try {
60
      if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) {
1✔
61
        String[] keyProperties = keyStatement.getKeyProperties();
1✔
62
        final Configuration configuration = ms.getConfiguration();
1✔
63
        final MetaObject metaParam = configuration.newMetaObject(parameter);
1✔
64
        // Do not close keyExecutor.
65
        // The transaction will be closed by parent executor.
66
        Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
1✔
67
        List<Object> values = keyExecutor.query(keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
1✔
68
        if (values.size() == 0) {
1✔
69
          throw new ExecutorException("SelectKey returned no data.");
1✔
70
        } else if (values.size() > 1) {
1✔
71
          throw new ExecutorException("SelectKey returned more than one value.");
1✔
72
        } else {
73
          MetaObject metaResult = configuration.newMetaObject(values.get(0));
1✔
74
          if (keyProperties.length == 1) {
1✔
75
            if (metaResult.hasGetter(keyProperties[0])) {
1✔
76
              setValue(metaParam, keyProperties[0], metaResult.getValue(keyProperties[0]));
1✔
77
            } else {
78
              // no getter for the property - maybe just a single value object
79
              // so try that
80
              setValue(metaParam, keyProperties[0], values.get(0));
1✔
81
            }
82
          } else {
83
            handleMultipleProperties(keyProperties, metaParam, metaResult);
1✔
84
          }
85
        }
86
      }
87
    } catch (ExecutorException e) {
1✔
88
      throw e;
1✔
89
    } catch (Exception e) {
1✔
90
      throw new ExecutorException("Error selecting key or setting result to parameter object. Cause: " + e, e);
1✔
91
    }
1✔
92
  }
1✔
93

94
  private void handleMultipleProperties(String[] keyProperties, MetaObject metaParam, MetaObject metaResult) {
95
    String[] keyColumns = keyStatement.getKeyColumns();
1✔
96

97
    if (keyColumns == null || keyColumns.length == 0) {
1✔
98
      // no key columns specified, just use the property names
99
      for (String keyProperty : keyProperties) {
1✔
100
        setValue(metaParam, keyProperty, metaResult.getValue(keyProperty));
1✔
101
      }
102
    } else {
103
      if (keyColumns.length != keyProperties.length) {
1✔
104
        throw new ExecutorException(
×
105
            "If SelectKey has key columns, the number must match the number of key properties.");
106
      }
107
      for (int i = 0; i < keyProperties.length; i++) {
1✔
108
        setValue(metaParam, keyProperties[i], metaResult.getValue(keyColumns[i]));
1✔
109
      }
110
    }
111
  }
1✔
112

113
  private void setValue(MetaObject metaParam, String property, Object value) {
114
    if (metaParam.hasSetter(property)) {
1✔
115
      metaParam.setValue(property, value);
1✔
116
    } else {
117
      throw new ExecutorException("No setter found for the keyProperty '" + property + "' in "
1✔
118
          + metaParam.getOriginalObject().getClass().getName() + ".");
1✔
119
    }
120
  }
1✔
121
}
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