001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.mybatis;
017
018import com.mybatisflex.core.FlexConsts;
019import com.mybatisflex.core.exception.FlexExceptions;
020import org.apache.ibatis.mapping.BoundSql;
021import org.apache.ibatis.mapping.MappedStatement;
022import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
023import org.apache.ibatis.type.TypeHandler;
024import org.apache.ibatis.type.TypeHandlerRegistry;
025
026import java.sql.PreparedStatement;
027import java.sql.SQLException;
028import java.sql.Types;
029import java.util.Map;
030
031/**
032 * 向 {@link PreparedStatement} 中的占位符设置值。
033 *
034 * @author michael
035 * @author 王帅
036 */
037public class SqlArgsParameterHandler extends DefaultParameterHandler {
038
039    private final TypeHandlerRegistry typeHandlerRegistry;
040
041    public SqlArgsParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
042        super(mappedStatement, parameterObject, boundSql);
043        this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
044    }
045
046    @Override
047    public void setParameters(PreparedStatement ps) {
048        try {
049            doSetParameters(ps);
050        } catch (SQLException e) {
051            throw FlexExceptions.wrap(e);
052        }
053    }
054
055    @SuppressWarnings({"rawtypes", "unchecked"})
056    private void doSetParameters(PreparedStatement ps) throws SQLException {
057        Object[] sqlArgs = (Object[]) ((Map) getParameterObject()).get(FlexConsts.SQL_ARGS);
058        if (sqlArgs == null || sqlArgs.length == 0) {
059            super.setParameters(ps);
060            return;
061        }
062
063        int index = 1;
064        for (Object value : sqlArgs) {
065            // 设置 NULL 值
066            if (value == null) {
067                ps.setNull(index++, Types.NULL);
068                continue;
069            }
070
071            // 通过配置的 TypeHandler 去设置值
072            if (value instanceof TypeHandlerObject) {
073                ((TypeHandlerObject) value).setParameter(ps, index++);
074                continue;
075            }
076
077            TypeHandler typeHandler = typeHandlerRegistry.getTypeHandler(value.getClass());
078            if (typeHandler == null) {
079                typeHandler = typeHandlerRegistry.getUnknownTypeHandler();
080            }
081
082            // 此处的 jdbcType 可以为 null 的,原因是 value 不为 null,
083            // 只有 value 为 null 时, jdbcType 不允许为 null
084            typeHandler.setParameter(ps, index++, value, null);
085        }
086    }
087
088}