data-dump/src/main/java/com/celnet/datadump/util/SqlUtil.java

113 lines
4.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.celnet.datadump.util;
import com.celnet.datadump.global.Const;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author Red
* @description salesforce 执行sql生成工具
* @date 2022/04/13
*/
@Component
@Slf4j
public class SqlUtil {
private static SqlSessionFactory sqlSessionFactory = null;
@Autowired
private void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
SqlUtil.sqlSessionFactory = sqlSessionFactory;
}
/**
* 生成salesforce 执行sql语句
*
* @param method 方法全名称 com.celnet.xxxx.select
* @param param 参数 没有则传null
* @return sql语句
*/
public static String showSql(String method, Map<String, Object> param) {
Configuration configuration = sqlSessionFactory.getConfiguration();
BoundSql boundSql = configuration
.getMappedStatement(method)
.getBoundSql(param);
return showSql(configuration, boundSql);
}
/**
* 解析BoundSql生成不含占位符的SQL语句
*
* @param configuration configuration
* @param boundSql boundSql
* @return sql语句
*/
private static String showSql(Configuration configuration, BoundSql boundSql) {
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
// 去多余空格 换行符
String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
if (parameterMappings.size() > 0 && parameterObject != null) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
} else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
for (ParameterMapping parameterMapping : parameterMappings) {
String propertyName = parameterMapping.getProperty();
if (metaObject.hasGetter(propertyName)) {
Object obj = metaObject.getValue(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
} else if (boundSql.hasAdditionalParameter(propertyName)) {
Object obj = boundSql.getAdditionalParameter(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
}
}
}
}
return sql;
}
/**
* 判断参数类型 若为字符串或者日期类型,则在参数两边添加''
*
* @param obj 参数
* @return 替换参数str
*/
private static String getParameterValue(Object obj) {
StringBuilder value = new StringBuilder();
if (obj instanceof String) {
value.append("'").append(obj).append("'");
} else if (obj instanceof Date) {
// 日期转为sf格式
value.append(DateFormatUtils.formatUTC((Date) obj, Const.SF_DATE_FORMAT));
} else if (obj instanceof Calendar) {
// 日期转为sf格式
value.append(DateFormatUtils.formatUTC(((Calendar) obj).getTime(), Const.SF_DATE_FORMAT));
} else {
if (obj != null) {
value.append(obj);
} else {
value.append(StringUtils.EMPTY);
}
}
return value.toString();
}
}