This commit is contained in:
2024-03-21 18:28:51 +08:00
parent 68e5dda2b8
commit 59585e6369
7 changed files with 93 additions and 22 deletions

View File

@@ -1,5 +1,10 @@
package net.tccn.base;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.*;
import net.tccn.base.dbq.jdbc.api.DbSource;
import java.lang.reflect.Array;
@@ -211,4 +216,45 @@ public class Utils {
return _group;
}
public static String parserSql(String originalSql) {
try {
Select select = (Select) CCJSqlParserUtil.parse(originalSql);
// 如果未设置分页,设置默认分页 99
if (select.getLimit() == null) {
Limit limit = new Limit().withRowCount(new LongValue(10));
select.setLimit(limit);
originalSql = select.toString();
}
return originalSql;
} catch (JSQLParserException e) {
throw new RuntimeException(e);
}
}
public static String parserCountSql(String originalSql) {
String sqlCount = "SELECT COUNT(1)";
try {
Select select = (Select) CCJSqlParserUtil.parse(originalSql);
PlainSelect plainSelect = select.getPlainSelect();
FromItem fromItem = plainSelect.getFromItem();
List<Join> joins = plainSelect.getJoins();
Expression where = plainSelect.getWhere();
sqlCount += " FROM " + fromItem;
if (joins != null) {
for (Join join : joins) {
sqlCount += " " + join;
}
}
sqlCount += " WHERE " + where;
} catch (Exception e) {
throw new RuntimeException(e);
}
return sqlCount;
}
}