Skip to content

Commit e563bce

Browse files
author
banzhe
committed
feat: support RowBounds
1 parent b1400f9 commit e563bce

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,21 @@ public static ProcessResult<MybatisSqlConfiguration> generate(PsiElement psiElem
7474
configuration.setPsiElement(psiElement);
7575
configuration.setMethod(statementId);
7676
configuration.setParams("{}");
77+
configuration.setRowBounds(false);
7778

7879
return ProcessResult.success(configuration);
7980

8081
}
8182

8283
String params = generateMethodParam(psiMethod, parser);
84+
boolean isRowBounds = isRowBounds(psiMethod);
8385

8486
if (cache) {
8587
sqlConfig.setPsiElement(psiElement);
8688
sqlConfig.setMethod(statementId);
8789
sqlConfig.setParams(params);
8890
sqlConfig.setUpdateSql(true);
91+
sqlConfig.setRowBounds(isRowBounds);
8992
if (parser instanceof RandomPOJO2JSONParser) {
9093
sqlConfig.setDefaultParams(false);
9194
}
@@ -100,10 +103,19 @@ public static ProcessResult<MybatisSqlConfiguration> generate(PsiElement psiElem
100103
configuration.setPsiElement(psiElement);
101104
configuration.setMethod(statementId);
102105
configuration.setParams(params);
106+
configuration.setRowBounds(isRowBounds);
103107

104108
return ProcessResult.success(configuration);
105109
}
106110

111+
private static boolean isRowBounds(PsiMethod method) {
112+
List<ParamNameType> paramNameTypes = getMethodBodyParamList(method);
113+
return paramNameTypes.stream().anyMatch(paramNameType -> {
114+
String classQualifier = paramNameType.psiType.getCanonicalText();
115+
return StringUtils.equals(classQualifier, "org.apache.ibatis.session.RowBounds");
116+
});
117+
}
118+
107119
public static ProcessResult<String> generateSql(Project project, String methodQualifiedName, String params, boolean cache) {
108120

109121
try {
@@ -200,7 +212,14 @@ private static ProcessResult<String> getSqlFromXml(Project project, String quali
200212
return ProcessResult.fail(String.format("Oops! There is not %s in mapper file!!!", qualifiedMethod));
201213
}
202214

203-
return ProcessResult.success(sqlSourceMap.get(qualifiedMethod).getSql(params));
215+
String sql = sqlSourceMap.get(qualifiedMethod).getSql(params);
216+
217+
MybatisSqlConfiguration sqlConfig = project.getService(MybatisSqlStateComponent.class).getConfiguration();
218+
if (sqlConfig.isRowBounds()) {
219+
sql = String.format("%s\nLIMIT 0, 10", sql);
220+
}
221+
222+
return ProcessResult.success(sql);
204223
} catch (Throwable t) {
205224
StringWriter sw = new StringWriter();
206225
t.printStackTrace(new PrintWriter(sw));

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class MybatisSqlConfiguration {
1919
private String result;
2020
private boolean defaultParams;
2121
private boolean updateSql = true;
22+
private boolean rowBounds = false;
2223

2324
public String getMethod() {
2425
return method;
@@ -85,4 +86,12 @@ public boolean isUpdateSql() {
8586
public void setUpdateSql(boolean updateSql) {
8687
this.updateSql = updateSql;
8788
}
89+
90+
public boolean isRowBounds() {
91+
return rowBounds;
92+
}
93+
94+
public void setRowBounds(boolean rowBounds) {
95+
this.rowBounds = rowBounds;
96+
}
8897
}

src/main/java/io/github/linyimin/plugin/mybatis/scripting/tags/DynamicContext.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public int getUniqueNumber() {
9494
static class ContextMap extends HashMap<String, Object> {
9595

9696
private static final long serialVersionUID = 2977601501966151582L;
97-
private final JSONObject parameterJSONObject;
97+
private JSONObject parameterJSONObject;
9898
private final Object parameterObject;
9999

100100
public ContextMap(Object parameterObject) {
@@ -118,6 +118,7 @@ public ContextMap(Object parameterObject) {
118118

119119
if (JSONObject.isValidObject(parameterObjectStr)) {
120120
this.parameterJSONObject = JSONObject.parseObject(parameterObjectStr);
121+
this.processRowBounds();
121122
} else {
122123
this.parameterJSONObject = null;
123124
}
@@ -150,6 +151,30 @@ public Object get(Object key) {
150151

151152
return null;
152153
}
154+
155+
private void processRowBounds() {
156+
if (this.parameterJSONObject.size() != 2) {
157+
return;
158+
}
159+
for (Map.Entry<String, Object> entry : this.parameterJSONObject.entrySet()) {
160+
if (!(entry.getValue() instanceof JSONObject)) {
161+
return;
162+
}
163+
JSONObject json = (JSONObject) entry.getValue();
164+
if (json.size() == 2 && json.containsKey("limit") && json.containsKey("offset")) {
165+
this.parameterJSONObject.remove(entry.getKey());
166+
}
167+
}
168+
169+
if (this.parameterJSONObject.size() != 1) {
170+
return;
171+
}
172+
173+
Object obj = new ArrayList<>(this.parameterJSONObject.values()).get(0);
174+
if (obj instanceof JSONObject) {
175+
parameterJSONObject = (JSONObject) obj;
176+
}
177+
}
153178
}
154179

155180
static class ContextAccessor implements PropertyAccessor {

src/main/java/io/github/linyimin/plugin/mybatis/xml/XMLIncludeTransformer.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ public XMLIncludeTransformer(Map<String, XNode> sqlFragments) {
2323
}
2424

2525
public void applyIncludes(Node source) {
26-
// Properties variablesContext = new Properties();
27-
// Properties configurationVariables = configuration.getVariables();
28-
// Optional.ofNullable(configurationVariables).ifPresent(variablesContext::putAll);
2926
applyIncludes(source, new Properties(), false);
3027
}
3128

src/main/java/io/github/linyimin/plugin/ui/tree/TreeListener.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import io.github.linyimin.plugin.sql.result.SelectResult;
2525
import io.github.linyimin.plugin.ui.MouseCursorAdapter;
2626
import io.github.linyimin.plugin.ui.MybatisSqlScannerPanel;
27-
import org.apache.commons.lang3.StringUtils;
2827
import org.jetbrains.annotations.NotNull;
2928

3029
import javax.swing.*;
@@ -38,7 +37,6 @@
3837

3938
/**
4039
* @author banzhe
41-
* @date 2023/01/02 01:03
4240
**/
4341
public class TreeListener extends MouseAdapter {
4442

0 commit comments

Comments
 (0)