工具类
package top.jiqfu.commom.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
@Data
public class Param {
private static final Logger logger = LoggerFactory.getLogger(Param.class);
public static final String SQL_START = "==> Preparing: ";
public static final String SQL_END = "\n";
public static final String PARAM_START = "==> Parameters: ";
public static final String PARAM_END = "\n";
public static final String PARAM_STRING_TYPE = "String";
public static final String PARAM_TIMESTAMP = "Timestamp";
public static final List<String> ADD_SINGLE_QUOTES_LIST = Arrays.asList(PARAM_STRING_TYPE, PARAM_TIMESTAMP);
public static final String REPLACE_REG = "[?]";
public static final String SPLIT = ", ";
public static final String SINGLE_QUOTES = "'";
private String value;
private String type;
public Param() {
}
public Param(String param) {
if (param.contains("(")) {
this.value = param.substring(0, param.lastIndexOf("("));
this.type = param.substring(param.indexOf("(") + 1, param.lastIndexOf(")"));
} else {
this.value = null;
this.type = null;
}
}
public static String restore(String sourceSql) {
if (ObjectUtils.isEmpty(sourceSql)) {
return sourceSql;
}
if (!sourceSql.contains(SQL_START)) {
return sourceSql;
}
String sql = sourceSql.substring(sourceSql.indexOf(SQL_START) + SQL_START.length(), sourceSql.indexOf(SQL_END, sourceSql.indexOf(SQL_START) + SQL_START.length()));
String param = sourceSql.substring(sourceSql.indexOf(PARAM_START) + PARAM_START.length(), sourceSql.indexOf(PARAM_END, sourceSql.indexOf(PARAM_START) + PARAM_START.length()));
String[] paramArray = param.split(SPLIT);
for (int i = 0; i < paramArray.length; i++) {
Param paramTemp = new Param(paramArray[i]);
if (ADD_SINGLE_QUOTES_LIST.contains(paramTemp.getType())) {
sql = sql.replaceFirst(REPLACE_REG, SINGLE_QUOTES + paramTemp.getValue() + SINGLE_QUOTES);
} else {
sql = sql.replaceFirst(REPLACE_REG, ObjectUtils.isEmpty(paramTemp.getValue())?"null":paramTemp.getValue());
}
}
return sql;
}
public static void main(String[] args) {
StringBuilder sql = new StringBuilder();
do {
Scanner input = new Scanner(System.in);
do {
String temp = input.nextLine();
if (temp.equals("")) {
break;
}
sql.append(temp).append("\n");
} while (true);
logger.info("=================\n{}\n=================", Param.restore(sql.toString()));
sql=new StringBuilder();
} while (!"end".equals(sql.toString()));
}
}
使用
@Test
public void restoreTest() {
String sql = "roductItemRefInfo : ==> Preparing: SELECT * FROM student st where st.name= ? and st.age=? \n" +
"2020-05-15 10:27:15.210 DEBUG 20608 --- [nio-8476-exec-5] .m.p.i.m.P.groupSelectProductItemRefInfo : ==> Parameters: %小明%(String), 20(Long)\n" +
"2020-05-15 10:27:15.254 DEBUG 20608 --- [nio-8476-exec-5] .m.p.i.m.P.groupSelectProductItemRefInfo : <== Total: 3";
logger.info("=================\n{}\n=================",restore(sql));
}