Skip to content

Commit

Permalink
fix: 改进分隔判断,由单纯通过,判断会存在无法对文本正常分隔的情况下。
Browse files Browse the repository at this point in the history
  • Loading branch information
lk committed Nov 20, 2020
1 parent a9f8548 commit 132290c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'java'


group 'com.linkkou'
version '1.0.6.1'
version '1.0.7'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
Expand Down
48 changes: 43 additions & 5 deletions src/main/java/com/plugins/mybaitslog/util/SqlProUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 用于SQL的还原
Expand All @@ -17,6 +19,16 @@ public class SqlProUtil {

private final static BasicFormatter BASIC_FORMATTER = new BasicFormatter();

/**
* 匹配 '
*/
private final static Pattern PSingleQuotationMark = Pattern.compile("'");
/**
* 匹配 (String),
*/
private final static String Separate = "\\(.*?\\),\\s";
private final static Pattern PSeparate = Pattern.compile(Separate);

/**
* 获取Sql语句类型
*
Expand Down Expand Up @@ -101,12 +113,19 @@ private static String[] getPreparing(String[] preparingLineSplit) {
*/
private static Object[] getParameters(String[] parametersLineSplit) {
if (parametersLineSplit.length == 2) {
final String[] split = parametersLineSplit[1].split(",");
//Mybatis的特性一定保证参数数量是一致的
final String[] split = parametersLineSplit[1].split(Separate);
final Matcher matcher = PSeparate.matcher(parametersLineSplit[1]);
final List<String> params = new ArrayList<>();
for (String item : split) {
String group = "";
if (matcher.find()) {
group = matcher.group().replaceAll(",", "").trim();
}
final String s = item.trim();
final String[] parametersTypeOrValue = getParametersTypeOrValue(s);
final String stringformat = stringformat(parametersTypeOrValue);
final String[] parametersTypeOrValue = getParametersTypeOrValue(s + group);
//final String[] specialTypesOfEscapeFormat = specialTypesOfEscapeFormat(parametersTypeOrValue);
final String stringformat = quotationTypeFormat(parametersTypeOrValue);
params.add(stringformat);
}
return params.toArray(new String[0]);
Expand All @@ -117,10 +136,11 @@ private static Object[] getParameters(String[] parametersLineSplit) {

/**
* 对特点类型进行格式化
* 这里的String值由Mybatis输入的格式来解决决定的
*
* @return String
*/
private static String stringformat(String[] parametersTypeOrValue) {
private static String quotationTypeFormat(String[] parametersTypeOrValue) {
String[] d = {"String", "Timestamp", "Date", "Time", "LocalDate", "LocalTime", "LocalDateTime"};
for (String s : d) {
if (s.equals(parametersTypeOrValue[1])) {
Expand All @@ -130,6 +150,24 @@ private static String stringformat(String[] parametersTypeOrValue) {
return parametersTypeOrValue[0];
}

/**
* 特殊字符串类型进行转义
* 由于一些JSON测试中只能对单引号进行处理,否者无法保证最后的值是能正确转义的
* 占时保留代码,后续在考虑如何解决问题
*
* @return String
*/
private static String[] specialTypesOfEscapeFormat(String[] parametersTypeOrValue) {
final String type = parametersTypeOrValue[1];
final String value = parametersTypeOrValue[0];
String s = value;
if ("String".equals(type)) {
final Matcher matcher1 = PSingleQuotationMark.matcher(value);
s = matcher1.replaceAll("\\\\'");
}
return new String[]{s, type};
}

/**
* 获取到参数值与类型
*
Expand All @@ -147,7 +185,7 @@ private static String[] getParametersTypeOrValue(String s) {
break;
}
}
if (find > 0) {
if (find >= 0) {
final String val = s.substring(0, find);
final String type = s.substring(find + 1, value.length - 1);
return new String[]{val, type};
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
<vendor email="[email protected]" url="https://github.com/Link-Kou/intellij-mybaitslog">linkkou</vendor>

<!-- 插件版本 -->
<version>1.0.3</version>
<version>1.0.7</version>
<!-- please see https://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description -->
<idea-version since-build="172"/>
<!-- 插件的描述 -->
<description><![CDATA[
<ul>
<h3>English:</h3>
<li>Copy with mybatis-log-plugin</li>
<li>Format output mybatis-log</li>
<h3>中文:</h3>
<li>拷贝 mybatis-log-plugin</li>
<li>格式化输出 mybatis-log</li>
</ul>
]]></description>

<!-- 插件的功能 -->
<change-notes><![CDATA[
<ul>
<p>1.0.0</p>
<li> 1. 修正并运行 mybatis-log-plugin </li>
<li> 1. Fix and run mybatis-log-plugin </li>
<p>1.X.X</p>
<li> 1. 构建新的插件</li>
<li> 1. Building new plug-ins</li>
</ul>
]]></change-notes>

Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/plugins/mybaitslog/SqlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,31 @@ public void format() {
System.out.println(strings[0]);
System.out.println(strings[1]);
}

@Test
public void format2() {
String preparingLine = "2020.09.19 11:12:40 CST DEBUG com.cms.dao.OrgAdminRoleDao.queryInForParentId - ==> Preparing: select `rows`, `f_id`, `f_parent`, `f_role_id`, `createtime`, `updatedtime` from cms.t_org_admin_role WHERE f_parent in ( ? , ? , ? ) \n";
String parametersLine = "2020.09.19 11:12:40 CST DEBUG com.cms.dao.OrgAdminRoleDao.queryInForParentId - ==> Parameters: 4378097006489370624(String), 43829'9488411451,3'920(String), 4383415103714754575(String)\n";
final String[] strings = SqlProUtil.restoreSql(null, preparingLine, parametersLine);
System.out.println(strings[0]);
System.out.println(strings[1]);
}

@Test
public void format3() {
String preparingLine = "2020.09.19 11:12:40 CST DEBUG com.cms.dao.OrgAdminRoleDao.queryInForParentId - ==> Preparing: select `rows`, `f_id`, `f_parent`, `f_role_id`, `createtime`, `updatedtime` from cms.t_org_admin_role WHERE f_parent in ( ? , ? , ? ) \n";
String parametersLine = "2020.09.19 11:12:40 CST DEBUG com.cms.dao.OrgAdminRoleDao.queryInForParentId - ==> Parameters: (String), 43829'9488411451,3'920(String), 4383415103714754575(String)\n";
final String[] strings = SqlProUtil.restoreSql(null, preparingLine, parametersLine);
System.out.println(strings[0]);
System.out.println(strings[1]);
}

@Test
public void format4() {
String preparingLine = "13:17:00.087 [schedule-pool-8] DEBUG n.p.s.i.r.C.updateByPrimaryKeySelective - [debug,159] - ==> Preparing: update \"COLLABORATIVE_MANAGEMENT\" SET \"TASK_ID\" = ?, \"ISSUE_DATE\" = ?, \"ISSUE_PERSON\" = ?, \"ISSUE_PERSON_ID\" = ?, \"TYPE\" = ?, \"TASK_NAME\" = ?, \"COMPLETION_PERIOD\" = ?, \"COLLABORATIVE_RULE\" = ?, \"SYS_USER\" = ?, \"COLLABORATIVE_OBJECT\" = ?, \"EXECUTION_RULE\" = ?, \"EXECUTION_CONTENT\" = ?, \"TRIGGER_RULE\" = ?, \"LINKED_DATA_SOURCES\" = ?, \"PERIODICITY\" = ?, \"EXECUTIVE_UNIT\" = ?, \"MISSION_REQUIREMENT\" = ?, \"RELATED_ATTACHMENT\" = ?, \"TASK_STATUS\" = ?, \"SITE_SELECTION\" = ?, \"DIVISION\" = ?, \"EXECUTION_STATUS\" = ? where \"ID\" = ? ";
String parametersLine = "13:17:00.088 [schedule-pool-8] DEBUG n.p.s.i.r.C.updateByPrimaryKeySelective - [debug,159] - ==> Parameters: (String), 2020-11-07 00:00:00.0(Timestamp), 3(String), 74(Integer), 30(Integer), ddaaaaaaaaaaaaaaaaaaaaaaaa(String), 2020-11-04 00:00:00.0(Timestamp), 10(Integer), 2696(Integer), 刘明(String), 20(Integer), [{\"feedback\":\"0\",\"nowTask\":\"0\",\"time\":\"2020-11-02 00:11:01\",\"status\":\"10\"},{\"feedback\":\"0\",\"nowTask\":\"0\",\"time\":\"2020-12-09 00:12:03\",\"status\":\"0\"},{\"feedback\":\"0\",\"nowTask\":\"0\",\"time\":\"2020-12-22 00:12:03\",\"status\":\"0\"},{\"feedback\":\"0\",\"nowTask\":\"0\",\"time\":\"2020-12-06 00:12:03\",\"status\":\"0\"}](String), 10(Integer), ddd(String), (String), (String), 1616(String), (String), 10(Integer), (String), (String), (String), 1(Integer)";
final String[] strings = SqlProUtil.restoreSql(null, preparingLine, parametersLine);
System.out.println(strings[0]);
System.out.println(strings[1]);
}
}

0 comments on commit 132290c

Please sign in to comment.