package com.elab.core.dao.params;

import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import java.sql.Types;
import java.util.Map;

/**
 * @author : liukx
 * @create : 2018/8/10 14:17
 * @email : liukx@elab-plus.com
 */
public class BeanSqlParameterSource extends BeanPropertySqlParameterSource {

    private Map<String, String> propertyMap;

    public void setPropertyMap(Map<String, String> propertyMap) {
        this.propertyMap = propertyMap;
    }

    /**
     * Create a new BeanPropertySqlParameterSource for the given bean.
     *
     * @param object the bean instance to wrap
     */
    public BeanSqlParameterSource(Object object) {
        super(object);
    }

    @Override
    public Object getValue(String paramName) throws IllegalArgumentException {

        if (this.propertyMap != null && this.propertyMap.get(paramName) != null) {
            paramName = this.propertyMap.get(paramName);
        }

        try {
            return super.getValue(paramName);
        } catch (IllegalArgumentException ex) {
            return null;
        }
    }


    @Override
    public int getSqlType(String paramName) {
        try {
            return super.getSqlType(paramName);
        } catch (NullPointerException ex) {
            return Types.VARCHAR;
        }
    }
}
