package com.foreverwin.mesnac.dataimport.util;

import com.foreverwin.modular.core.meext.ExtBusinessException;
import com.sap.me.frame.domain.BusinessException;

import java.util.Map;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

public class SqlUtil {
    private static final String reg = "\\:\\s*[a-zA-Z\\.0-9\\_\\-\\?\\*\\/]+\\s*";
    private static final String mreg = "\\/\\*([\\S\\s]*?)\\*\\/";

    public static Object parseSql(final Map<String, Object> queryParam, String sql) throws BusinessException {

        sql = StringUtils.trim(RegUtils.findAndReplace(mreg, sql, new RegUtils.FindCallback() {
            public String execute(String source) {
                return " ";
            }
        }));
        try {
            RegUtils.findAndReplace(reg, sql, new RegUtils.FindCallback() {
                public String execute(String source) {
                    String s = StringUtils.trim(StringUtils.replace(source, ":", ""));
                    if (Objects.isNull(queryParam.get(s))) queryParam.put(s, "");
                    return source;
                }

            });
            return sql;
        } catch (Exception ex) {
            throw new ExtBusinessException("初始化参数失败!\n>>> PARAM >>>:\n" + queryParam + sql, ex);
        }
    }

}
