mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
feat: add QueryWrapper.toDebugSQL() method for debug
This commit is contained in:
parent
832c161cfb
commit
d35155ea22
@ -17,20 +17,17 @@ package com.mybatisflex.core.audit;
|
|||||||
|
|
||||||
import com.mybatisflex.core.mybatis.TypeHandlerObject;
|
import com.mybatisflex.core.mybatis.TypeHandlerObject;
|
||||||
import com.mybatisflex.core.util.ClassUtil;
|
import com.mybatisflex.core.util.ClassUtil;
|
||||||
import com.mybatisflex.core.util.DateUtil;
|
import com.mybatisflex.core.util.SqlUtil;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
public class AuditMessage implements Serializable {
|
public class AuditMessage implements Serializable {
|
||||||
|
|
||||||
@ -147,37 +144,11 @@ public class AuditMessage implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getFullSql() {
|
public String getFullSql() {
|
||||||
String sql = getQuery();
|
List<Object> queryParams = getQueryParams();
|
||||||
List<Object> params = getQueryParams();
|
return SqlUtil.replaceSqlParams(getQuery(), queryParams == null ? null : queryParams.toArray());
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private PreparedStatement createPreparedStatement() {
|
private PreparedStatement createPreparedStatement() {
|
||||||
return (PreparedStatement) Proxy.newProxyInstance(
|
return (PreparedStatement) Proxy.newProxyInstance(
|
||||||
AuditMessage.class.getClassLoader(),
|
AuditMessage.class.getClassLoader(),
|
||||||
|
|||||||
@ -15,10 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.query;
|
package com.mybatisflex.core.query;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.dialect.DialectFactory;
|
||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
import com.mybatisflex.core.table.TableDef;
|
import com.mybatisflex.core.table.TableDef;
|
||||||
import com.mybatisflex.core.util.ArrayUtil;
|
import com.mybatisflex.core.util.ArrayUtil;
|
||||||
import com.mybatisflex.core.util.CollectionUtil;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
|
import com.mybatisflex.core.util.SqlUtil;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
import java.util.*;
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.util;
|
package com.mybatisflex.core.util;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
public class SqlUtil {
|
public class SqlUtil {
|
||||||
|
|
||||||
|
|
||||||
@ -72,4 +76,35 @@ public class SqlUtil {
|
|||||||
return result != null && result.longValue() > 0;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user