.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user