feat: add QueryWrapper.toDebugSQL() method for debug

This commit is contained in:
开源海哥 2023-05-30 13:43:19 +08:00
parent 832c161cfb
commit d35155ea22
3 changed files with 47 additions and 33 deletions

View File

@ -17,20 +17,17 @@ package com.mybatisflex.core.audit;
import com.mybatisflex.core.mybatis.TypeHandlerObject;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.DateUtil;
import com.mybatisflex.core.util.SqlUtil;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Proxy;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
public class AuditMessage implements Serializable {
@ -147,37 +144,11 @@ public class AuditMessage implements Serializable {
}
public String getFullSql() {
String sql = getQuery();
List<Object> params = getQueryParams();
if (params != null) {
for (Object value : params) {
// null
if (value == null) {
sql = sql.replaceFirst("\\?", "null");
}
// number
else if (value instanceof Number || value instanceof Boolean) {
sql = sql.replaceFirst("\\?", value.toString());
}
// other
else {
StringBuilder sb = new StringBuilder();
sb.append("'");
if (value instanceof Date) {
sb.append(DateUtil.toDateTimeString((Date) value));
} else if (value instanceof LocalDateTime) {
sb.append(DateUtil.toDateTimeString(DateUtil.toDate((LocalDateTime) value)));
} else {
sb.append(value);
}
sb.append("'");
sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(sb.toString()));
}
}
}
return sql;
List<Object> queryParams = getQueryParams();
return SqlUtil.replaceSqlParams(getQuery(), queryParams == null ? null : queryParams.toArray());
}
private PreparedStatement createPreparedStatement() {
return (PreparedStatement) Proxy.newProxyInstance(
AuditMessage.class.getClassLoader(),

View File

@ -15,10 +15,12 @@
*/
package com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.DialectFactory;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.table.TableDef;
import com.mybatisflex.core.util.ArrayUtil;
import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.SqlUtil;
import com.mybatisflex.core.util.StringUtil;
import java.util.*;
@ -526,4 +528,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
}
public String toDebugSQL() {
String sql = DialectFactory.getDialect().forSelectByQuery(this);
return SqlUtil.replaceSqlParams(sql, getValueArray());
}
}

View File

@ -15,6 +15,10 @@
*/
package com.mybatisflex.core.util;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.regex.Matcher;
public class SqlUtil {
@ -72,4 +76,35 @@ public class SqlUtil {
return result != null && result.longValue() > 0;
}
public static String replaceSqlParams(String sql, Object[] params){
if (params != null && params.length > 0) {
for (Object value : params) {
// null
if (value == null) {
sql = sql.replaceFirst("\\?", "null");
}
// number
else if (value instanceof Number || value instanceof Boolean) {
sql = sql.replaceFirst("\\?", value.toString());
}
// other
else {
StringBuilder sb = new StringBuilder();
sb.append("'");
if (value instanceof Date) {
sb.append(DateUtil.toDateTimeString((Date) value));
} else if (value instanceof LocalDateTime) {
sb.append(DateUtil.toDateTimeString(DateUtil.toDate((LocalDateTime) value)));
} else {
sb.append(value);
}
sb.append("'");
sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(sb.toString()));
}
}
}
return sql;
}
}