1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.scripting.defaults;
17
18 import java.sql.PreparedStatement;
19 import java.sql.SQLException;
20 import java.util.List;
21
22 import org.apache.ibatis.executor.ErrorContext;
23 import org.apache.ibatis.executor.parameter.ParameterHandler;
24 import org.apache.ibatis.mapping.BoundSql;
25 import org.apache.ibatis.mapping.MappedStatement;
26 import org.apache.ibatis.mapping.ParameterMapping;
27 import org.apache.ibatis.mapping.ParameterMode;
28 import org.apache.ibatis.reflection.MetaObject;
29 import org.apache.ibatis.session.Configuration;
30 import org.apache.ibatis.type.JdbcType;
31 import org.apache.ibatis.type.TypeException;
32 import org.apache.ibatis.type.TypeHandler;
33 import org.apache.ibatis.type.TypeHandlerRegistry;
34
35
36
37
38
39 public class DefaultParameterHandler implements ParameterHandler {
40
41 private final TypeHandlerRegistry typeHandlerRegistry;
42
43 private final MappedStatement mappedStatement;
44 private final Object parameterObject;
45 private final BoundSql boundSql;
46 private final Configuration configuration;
47
48 public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
49 this.mappedStatement = mappedStatement;
50 this.configuration = mappedStatement.getConfiguration();
51 this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
52 this.parameterObject = parameterObject;
53 this.boundSql = boundSql;
54 }
55
56 @Override
57 public Object getParameterObject() {
58 return parameterObject;
59 }
60
61 @Override
62 public void setParameters(PreparedStatement ps) {
63 ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
64 List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
65 if (parameterMappings != null) {
66 for (int i = 0; i < parameterMappings.size(); i++) {
67 ParameterMapping parameterMapping = parameterMappings.get(i);
68 if (parameterMapping.getMode() != ParameterMode.OUT) {
69 Object value;
70 String propertyName = parameterMapping.getProperty();
71 if (boundSql.hasAdditionalParameter(propertyName)) {
72 value = boundSql.getAdditionalParameter(propertyName);
73 } else if (parameterObject == null) {
74 value = null;
75 } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
76 value = parameterObject;
77 } else {
78 MetaObject metaObject = configuration.newMetaObject(parameterObject);
79 value = metaObject.getValue(propertyName);
80 }
81 TypeHandler typeHandler = parameterMapping.getTypeHandler();
82 JdbcType jdbcType = parameterMapping.getJdbcType();
83 if (value == null && jdbcType == null) {
84 jdbcType = configuration.getJdbcTypeForNull();
85 }
86 try {
87 typeHandler.setParameter(ps, i + 1, value, jdbcType);
88 } catch (TypeException | SQLException e) {
89 throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
90 }
91 }
92 }
93 }
94 }
95
96 }