Skip to content

Commit 01e4d7d

Browse files
author
banzhe
committed
feat: support scan mybatis sql
1 parent 99b6140 commit 01e4d7d

17 files changed

+879
-36
lines changed

src/main/java/io/github/linyimin/plugin/cache/MybatisXmlContentCache.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ public class MybatisXmlContentCache {
2727

2828
private static final Map<Project, Map<String /* method qualified name */, Set<XmlTag>>> projectMapperMethodMap = new HashMap<>();
2929

30+
private static final Map<Project, Map<String /* namespace */, Set<XmlTag> /* method xmlTag */>> projectNamespaceMethodMap = new HashMap<>();
3031

31-
public static List<String> acquireByNamespace(Project project) {
32+
public static List<String> acquireByNamespace(Project project, boolean forceUpdate) {
3233

3334
Set<String> namespaces = projectMybatisMapperMap.getOrDefault(project, new HashMap<>()).keySet();
3435

36+
if (forceUpdate) {
37+
addXmlCache(project);
38+
return new ArrayList<>(projectMybatisMapperMap.getOrDefault(project, new HashMap<>()).keySet());
39+
}
40+
3541
if (!namespaces.isEmpty()) {
3642
return new ArrayList<>(namespaces);
3743
}
@@ -67,6 +73,19 @@ public static Set<XmlTag> acquireByMethodName(Project project, String methodQual
6773
return projectMapperMethodMap.getOrDefault(project, new HashMap<>()).getOrDefault(methodQualifiedName, new HashSet<>());
6874
}
6975

76+
public static Set<XmlTag> acquireMethodsByNamespace(Project project, String namespace) {
77+
78+
Map<String /* namespace */, Set<XmlTag>> cache = projectNamespaceMethodMap.getOrDefault(project, new HashMap<>());
79+
80+
if (cache.containsKey(namespace)) {
81+
return cache.get(namespace);
82+
}
83+
84+
addXmlCache(project);
85+
86+
return projectNamespaceMethodMap.getOrDefault(project, new HashMap<>()).getOrDefault(namespace, new HashSet<>());
87+
}
88+
7089
private static void addXmlCache(Project project) {
7190

7291
ProjectRootManager.getInstance(project).getFileIndex().iterateContent(fileOrDir -> {
@@ -125,10 +144,25 @@ private static void addMapperCache(Project project, PsiFile psiFile) {
125144

126145
addNamespaceCache(project, namespace, id);
127146

147+
addNamespaceMethodCache(project, namespace, subTag);
148+
128149
}
129150

130151
}
131152

153+
private static void addNamespaceMethodCache(Project project, String namespace, XmlTag subTag) {
154+
155+
Map<String, Set<XmlTag>> map = projectNamespaceMethodMap.getOrDefault(project, new HashMap<>());
156+
157+
Set<XmlTag> xmlTags = map.getOrDefault(namespace, new HashSet<>());
158+
159+
xmlTags.add(subTag);
160+
map.put(namespace, xmlTags);
161+
162+
projectNamespaceMethodMap.put(project, map);
163+
164+
}
165+
132166
private static void addNamespaceXmlTagCache(Project project, String namespace, XmlTag xmlTag) {
133167

134168
Map<String, Set<XmlTag>> namespaceCacheMap = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());

src/main/java/io/github/linyimin/plugin/component/SqlParamGenerateComponent.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
**/
3939
public class SqlParamGenerateComponent {
4040

41-
public static void generate(PsiElement psiElement, POJO2JSONParser parser) {
41+
public static ProcessResult<MybatisSqlConfiguration> generate(PsiElement psiElement, POJO2JSONParser parser, boolean cache) {
4242

4343
PsiMethod psiMethod = null;
4444

@@ -59,36 +59,43 @@ public static void generate(PsiElement psiElement, POJO2JSONParser parser) {
5959

6060
MybatisSqlConfiguration sqlConfig = psiElement.getProject().getService(MybatisSqlStateComponent.class).getConfiguration();
6161

62-
sqlConfig.setPsiElement(psiElement);
62+
if (psiMethod == null) {
6363

64-
// 设置缓存, method qualified name and params
65-
if (Objects.nonNull(psiMethod)) {
66-
67-
sqlConfig.setMethod(acquireMethodName(psiMethod));
64+
if (cache) {
65+
sqlConfig.setPsiElement(psiElement);
66+
// 找不到对应的接口方法
67+
sqlConfig.setMethod(statementId);
68+
sqlConfig.setParams("");
69+
}
70+
return ProcessResult.fail(String.format("method of %s is not exist.", statementId));
71+
}
6872

69-
sqlConfig.setParams(generateMethodParam(psiMethod, parser));
73+
String method = acquireMethodName(psiMethod);
74+
String params = generateMethodParam(psiMethod, parser);
7075

76+
if (cache) {
77+
sqlConfig.setMethod(method);
78+
sqlConfig.setParams(params);
7179
sqlConfig.setUpdateSql(true);
72-
7380
if (parser instanceof RandomPOJO2JSONParser) {
7481
sqlConfig.setDefaultParams(false);
7582
}
76-
7783
if (parser instanceof DefaultPOJO2JSONParser) {
7884
sqlConfig.setDefaultParams(true);
7985
}
8086

81-
} else if (statementId != null) {
82-
// 找不到对应的接口方法
83-
sqlConfig.setMethod(statementId);
84-
sqlConfig.setParams("");
87+
return ProcessResult.success(sqlConfig);
8588
}
8689

87-
}
90+
MybatisSqlConfiguration configuration = new MybatisSqlConfiguration();
91+
configuration.setPsiElement(psiElement);
92+
configuration.setMethod(method);
93+
configuration.setParams(params);
8894

89-
public static ProcessResult<String> generateSql(Project project, String methodQualifiedName, String params) {
95+
return ProcessResult.success(configuration);
96+
}
9097

91-
MybatisSqlConfiguration sqlConfig = project.getService(MybatisSqlStateComponent.class).getConfiguration();
98+
public static ProcessResult<String> generateSql(Project project, String methodQualifiedName, String params, boolean cache) {
9299

93100
try {
94101
ProcessResult<String> processResult = getSqlFromAnnotation(project, methodQualifiedName, params);
@@ -99,7 +106,9 @@ public static ProcessResult<String> generateSql(Project project, String methodQu
99106

100107
processResult = getSqlFromXml(project, methodQualifiedName, params);
101108

102-
if (processResult.isSuccess()) {
109+
if (processResult.isSuccess() && cache) {
110+
MybatisSqlConfiguration sqlConfig = project.getService(MybatisSqlStateComponent.class).getConfiguration();
111+
103112
sqlConfig.setSql(processResult.getData());
104113
sqlConfig.setUpdateSql(false);
105114
}
@@ -119,7 +128,7 @@ public static ProcessResult<String> generateSql(Project project) {
119128
if (StringUtils.isBlank(sqlConfig.getMethod())) {
120129
return ProcessResult.fail("Please select a mybatis method");
121130
}
122-
return generateSql(project, sqlConfig.getMethod(), sqlConfig.getParams());
131+
return generateSql(project, sqlConfig.getMethod(), sqlConfig.getParams(), true);
123132
}
124133

125134
private static ProcessResult<String> getSqlFromAnnotation(Project project, String qualifiedMethod, String params) {
@@ -236,7 +245,7 @@ public static List<ParamNameType> getMethodBodyParamList(PsiMethod psiMethod) {
236245
String paramAnnotationValue = getParamAnnotationValue(param);
237246
String name = StringUtils.isBlank(paramAnnotationValue) ? param.getName() : paramAnnotationValue;
238247

239-
ParamNameType paramNameType = new ParamNameType(name, param.getType());
248+
ParamNameType paramNameType = ApplicationManager.getApplication().runReadAction((Computable<ParamNameType>) () -> new ParamNameType(name, param.getType()));
240249
result.add(paramNameType);
241250
}
242251
return result;

src/main/java/io/github/linyimin/plugin/configuration/model/MybatisSqlConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public void setSql(String sql) {
5252
}
5353
}
5454

55+
public void setRawSql(String sql) {
56+
this.sql = sql;
57+
}
58+
5559
public String getResult() {
5660
return result;
5761
}

src/main/java/io/github/linyimin/plugin/pojo2json/POJO2JSONParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.intellij.psi.javadoc.PsiDocComment;
66
import com.intellij.psi.javadoc.PsiDocTag;
77
import com.intellij.psi.util.PsiUtil;
8-
import io.github.linyimin.plugin.mybatis.parsing.ParseException;
98
import io.github.linyimin.plugin.pojo2json.type.*;
109
import org.apache.commons.lang3.StringUtils;
1110
import org.apache.commons.lang3.tuple.Pair;

src/main/java/io/github/linyimin/plugin/provider/generate/SqlGenerateNavigationHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void navigate(MouseEvent e, PsiElement elt) {
3636

3737
if (!mybatisSqlViewerToolWindow.isActive()) {
3838

39-
SqlParamGenerateComponent.generate(elt, POJO2JSONParserFactory.RANDOM_POJO_2_JSON_PARSER);
39+
SqlParamGenerateComponent.generate(elt, POJO2JSONParserFactory.RANDOM_POJO_2_JSON_PARSER, true);
4040

4141
activateWindow(mybatisSqlViewerToolWindow);
4242
notifyParamChange(elt.getProject());
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="io.github.linyimin.plugin.ui.MybatisSqlScannerPanel">
3+
<grid id="27dc6" binding="scannerResultPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="0" left="0" bottom="0" right="0"/>
5+
<constraints>
6+
<xy x="20" y="20" width="594" height="400"/>
7+
</constraints>
8+
<properties/>
9+
<border type="none"/>
10+
<children>
11+
<grid id="48f7a" binding="scannerResultContentPanel" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
12+
<margin top="0" left="0" bottom="0" right="0"/>
13+
<constraints>
14+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
15+
</constraints>
16+
<properties/>
17+
<border type="none"/>
18+
<children>
19+
<component id="e9e69" class="javax.swing.JRadioButton" binding="allRadioButton" default-binding="true">
20+
<constraints>
21+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
22+
</constraints>
23+
<properties>
24+
<selected value="true"/>
25+
<text value="all"/>
26+
</properties>
27+
</component>
28+
<hspacer id="89326">
29+
<constraints>
30+
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
31+
</constraints>
32+
</hspacer>
33+
<component id="6630c" class="javax.swing.JRadioButton" binding="complianceWithSpecRadioButton" default-binding="true">
34+
<constraints>
35+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
36+
</constraints>
37+
<properties>
38+
<text value="compliance with spec"/>
39+
</properties>
40+
</component>
41+
<component id="f2b3c" class="javax.swing.JRadioButton" binding="doesNotMeetSpecRadioButton" default-binding="true">
42+
<constraints>
43+
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
44+
</constraints>
45+
<properties>
46+
<text value="does not meet spec"/>
47+
</properties>
48+
</component>
49+
<component id="357e1" class="javax.swing.JRadioButton" binding="fullTableScanRadioButton" default-binding="true">
50+
<constraints>
51+
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
52+
</constraints>
53+
<properties>
54+
<text value="full table scan"/>
55+
</properties>
56+
</component>
57+
<grid id="1a50f" binding="sqlPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
58+
<margin top="0" left="0" bottom="0" right="0"/>
59+
<constraints>
60+
<grid row="1" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
61+
</constraints>
62+
<properties/>
63+
<border type="none"/>
64+
<children>
65+
<grid id="41759" binding="sqlContentPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="true" hgap="-1" vgap="-1">
66+
<margin top="0" left="0" bottom="0" right="0"/>
67+
<constraints>
68+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
69+
</constraints>
70+
<properties/>
71+
<border type="none"/>
72+
<children>
73+
<grid id="c9af6" binding="sqlAndExplainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
74+
<margin top="0" left="0" bottom="0" right="0"/>
75+
<constraints>
76+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
77+
</constraints>
78+
<properties/>
79+
<border type="none"/>
80+
<children>
81+
<grid id="e3aed" binding="statementPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
82+
<margin top="0" left="0" bottom="0" right="0"/>
83+
<constraints>
84+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
85+
</constraints>
86+
<properties/>
87+
<border type="none"/>
88+
<children/>
89+
</grid>
90+
<grid id="d7cef" binding="statementRulePanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
91+
<margin top="0" left="0" bottom="0" right="0"/>
92+
<constraints>
93+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
94+
</constraints>
95+
<properties/>
96+
<border type="none"/>
97+
<children/>
98+
</grid>
99+
</children>
100+
</grid>
101+
<grid id="e73c7" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
102+
<margin top="0" left="0" bottom="0" right="0"/>
103+
<constraints>
104+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
105+
</constraints>
106+
<properties/>
107+
<border type="none"/>
108+
<children>
109+
<grid id="422dc" binding="indexPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
110+
<margin top="0" left="0" bottom="0" right="0"/>
111+
<constraints>
112+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
113+
</constraints>
114+
<properties/>
115+
<border type="none"/>
116+
<children>
117+
<scrollpane id="7071d" binding="indexScrollPane">
118+
<constraints>
119+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
120+
</constraints>
121+
<properties/>
122+
<border type="none"/>
123+
<children>
124+
<component id="c8fca" class="javax.swing.JTable" binding="indexTable">
125+
<constraints/>
126+
<properties/>
127+
</component>
128+
</children>
129+
</scrollpane>
130+
</children>
131+
</grid>
132+
<component id="b51a7" class="javax.swing.JButton" binding="jumpButton" default-binding="true">
133+
<constraints>
134+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
135+
</constraints>
136+
<properties>
137+
<text value="jump"/>
138+
</properties>
139+
</component>
140+
</children>
141+
</grid>
142+
</children>
143+
</grid>
144+
<grid id="75394" binding="scanTreeResultPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
145+
<margin top="0" left="0" bottom="0" right="0"/>
146+
<constraints>
147+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
148+
</constraints>
149+
<properties/>
150+
<border type="none"/>
151+
<children/>
152+
</grid>
153+
</children>
154+
</grid>
155+
</children>
156+
</grid>
157+
</children>
158+
</grid>
159+
</form>

0 commit comments

Comments
 (0)