From d35155ea220ea690d500bb24334810b9d191d2c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 30 May 2023 13:43:19 +0800 Subject: [PATCH] feat: add QueryWrapper.toDebugSQL() method for debug --- .../mybatisflex/core/audit/AuditMessage.java | 37 ++----------------- .../mybatisflex/core/query/QueryWrapper.java | 8 ++++ .../com/mybatisflex/core/util/SqlUtil.java | 35 ++++++++++++++++++ 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AuditMessage.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AuditMessage.java index 6d69a496..4d8792b4 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AuditMessage.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AuditMessage.java @@ -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 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 queryParams = getQueryParams(); + return SqlUtil.replaceSqlParams(getQuery(), queryParams == null ? null : queryParams.toArray()); } + private PreparedStatement createPreparedStatement() { return (PreparedStatement) Proxy.newProxyInstance( AuditMessage.class.getClassLoader(), diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index 4905172a..f4014c21 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -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 { } + public String toDebugSQL() { + String sql = DialectFactory.getDialect().forSelectByQuery(this); + return SqlUtil.replaceSqlParams(sql, getValueArray()); + } + + } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/SqlUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/SqlUtil.java index cf7878b8..b36b2d93 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/SqlUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/SqlUtil.java @@ -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; + } + }