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,7 +1,6 @@
package net.tccn.base;
import org.redkale.convert.json.JsonConvert;
import org.redkale.net.http.RestMapping;
import org.redkale.service.Service;
import org.redkale.source.CacheMemorySource;
@@ -21,9 +20,9 @@ public class BaseService implements Service {
@Resource(name = "SERVER_ROOT")
protected File webroot;
public Logger logger = Logger.getLogger(this.getClass().getSimpleName());
protected Logger logger = Logger.getLogger(this.getClass().getSimpleName());
public static boolean isWinos = System.getProperty("os.name").contains("Window");
protected static boolean isWinos = System.getProperty("os.name").contains("Window");
@Resource(name = "cache")
protected static CacheMemorySource cacheSource = new CacheMemorySource("cache");
@@ -31,13 +30,12 @@ public class BaseService implements Service {
@Resource(name = "APP_HOME")
protected File APP_HOME;
public static Properties prop = new Properties();
protected static Properties prop = new Properties();
protected static TplKit tplKit = TplKit.use(true);
private static boolean tplInit = false;
@RestMapping(ignore = true)
public <T> T getT(String key, Class<T> clazz, Supplier<T> supplier) {
protected <T> T getT(String key, Class<T> clazz, Supplier<T> supplier) {
Object obj = cacheSource.getAndRefresh(key, 1000 * 60 * 3, clazz);
if (obj != null) {
return (T) obj;
@@ -50,12 +48,11 @@ public class BaseService implements Service {
return t;
}
@RestMapping(ignore = true)
public String platId(String token) {
protected String platId(String token) {
return MetaKit.getPlatId(token);
}
public boolean isEmpty(Object obj) {
protected boolean isEmpty(Object obj) {
return Utils.isEmpty(obj);
}
}

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;
}
}

View File

@@ -3,7 +3,9 @@ package net.tccn.qtask.impl;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import net.tccn.base.Kv;
import net.tccn.base.MetaKit;
import net.tccn.base.Utils;
import net.tccn.base.dbq.jdbc.api.DbKit;
import net.tccn.qtask.QTask;
import net.tccn.qtask.Task;
@@ -31,6 +33,11 @@ public class QTaskMysql extends QTaskAbs implements QTask {
Template tpl = engine.getTemplateByString(task.getContent());
String sql = tpl.renderToString(getTask().getPara()).replaceAll("[\\s]+", " ");
// 聚合统计返回统计结果 TODO 待完善
if ((sql.startsWith("SELECT COUNT") || sql.startsWith("select count") || sql.startsWith("SELECT count") || sql.startsWith("select COUNT"))) {
return dbKit.find(sql, Kv.class);
}
/*
// todo: 从sql分析支持多种sql处理类别
if (sql.startsWith("select count")) {
@@ -58,6 +65,12 @@ public class QTaskMysql extends QTaskAbs implements QTask {
dbKit.exetute(sql);
}*/
return dbKit.queryList(sql, Map.class);
// sql 解析-检查:未设置分页设置默认分页
sql = Utils.parserSql(sql);
Kv kv = Kv.of();
kv.set("count", dbKit.findLong(Utils.parserCountSql(sql)));
kv.set("list", dbKit.queryList(sql, Map.class));
return kv;
}
}