|
@@ -1,7 +1,5 @@
|
|
|
package com.gyee.table.service.impl;
|
|
|
|
|
|
-import com.alibaba.fastjson2.JSON;
|
|
|
-import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
@@ -25,7 +23,7 @@ import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
|
- * 服务实现类
|
|
|
+ * 服务实现类
|
|
|
* </p>
|
|
|
*
|
|
|
* @author gfhd
|
|
@@ -36,6 +34,7 @@ public class ObjectServiceImpl implements IObjectService {
|
|
|
|
|
|
private final static String regex = "#|/\\*|\\*/|--|\\buse\\b|\\binsert\\b|\\bdelete\\b|\\bupdate\\b|\\bcreate\\b|\\bdrop\\b|\\btruncate\\b|\\balter\\b|\\bgrant\\b|\\bexecute\\b|\\bexec\\b|\\bxp_cmdshell\\b|\\bcall\\b|\\bdeclare\\b|\\bsource\\b|\\bsql\\b|\\bchr\\b|\\bmid\\b|\\bmaster\\b|\\bchar\\b|\\bsitename\\b|\\bnet user\\b|;|-|\\+|\\btable\\b|\\bgroup_concat\\b|\\bcolumn_name\\b|\\binformation_schema.columns\\b|\\btable_schema\\b|//|/";
|
|
|
private final Pattern compile;
|
|
|
+
|
|
|
{
|
|
|
compile = Pattern.compile(regex);
|
|
|
}
|
|
@@ -49,6 +48,7 @@ public class ObjectServiceImpl implements IObjectService {
|
|
|
|
|
|
/**
|
|
|
* 根据表名查表头,根据sql查数据
|
|
|
+ *
|
|
|
* @param map tablenae,sql
|
|
|
* @return
|
|
|
*/
|
|
@@ -56,54 +56,54 @@ public class ObjectServiceImpl implements IObjectService {
|
|
|
public JSONObject selectBySql(Map map) {
|
|
|
|
|
|
String sql = (String) map.get("sql");
|
|
|
- if(StringUtils.isBlank(sql)) return Result.error(ResultCode.PARAM_IS_BLANK);
|
|
|
+ if (StringUtils.isBlank(sql)) return Result.error(ResultCode.PARAM_IS_BLANK);
|
|
|
|
|
|
Matcher matcher = compile.matcher(sql);
|
|
|
- if(matcher.find()){
|
|
|
+ if (matcher.find()) {
|
|
|
String group = matcher.group(0);
|
|
|
- return Result.error(ResultCode.ERROR_UNSUPPORTED_SQL,"查看字符:"+group);
|
|
|
+ return Result.error(ResultCode.ERROR_UNSUPPORTED_SQL, "查看字符:" + group);
|
|
|
}
|
|
|
|
|
|
List<LinkedHashMap<String, Object>> lms = objectMapper.selectAll(sql);
|
|
|
|
|
|
String tablename = (String) map.get("tablename");
|
|
|
- if(StringUtils.isBlank(tablename))
|
|
|
+ if (StringUtils.isBlank(tablename))
|
|
|
tablename = StringUtils.substringBetween("from ", " ");
|
|
|
- //return Result.successData(ResultCode.SUCCESS,lms);
|
|
|
+ //return Result.successData(ResultCode.SUCCESS,lms);
|
|
|
|
|
|
QueryWrapper<TableColumnHeader> wrapper = new QueryWrapper<>();
|
|
|
wrapper.eq("name", tablename);
|
|
|
boolean tableExist = tableColumnHeaderMapper.exists(wrapper);
|
|
|
|
|
|
List<TableColumnHeader> tableColumnHeader;
|
|
|
- if(!tableExist && lms.size()>0){
|
|
|
+ if (!tableExist && lms.size() > 0) {
|
|
|
tableColumnHeader = createTableColumnHeader(tablename, lms.get(0));
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
tableColumnHeader = tableColumnHeaderMapper.selectList(wrapper);
|
|
|
}
|
|
|
Table table = new Table();
|
|
|
table.setTableHeader(tableColumnHeader);
|
|
|
table.setTableData(lms);
|
|
|
//截取sql
|
|
|
- if (sql.contains("limit")||sql.contains("LIMIT")){
|
|
|
+ if (sql.contains("limit") || sql.contains("LIMIT")) {
|
|
|
|
|
|
String newSql = "";
|
|
|
- if(sql.contains("LIMIT"))
|
|
|
- newSql= sql.split("LIMIT")[0];
|
|
|
- else if (sql.contains("limit")){
|
|
|
- newSql= sql.split("limit")[0];
|
|
|
+ if (sql.contains("LIMIT"))
|
|
|
+ newSql = sql.split("LIMIT")[0];
|
|
|
+ else if (sql.contains("limit")) {
|
|
|
+ newSql = sql.split("limit")[0];
|
|
|
}
|
|
|
List<LinkedHashMap<String, Object>> linkedHashMaps = objectMapper.selectAll(newSql);
|
|
|
table.setCount(linkedHashMaps.size());
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
table.setCount(lms.size());
|
|
|
}
|
|
|
- return Result.successData(ResultCode.SUCCESS,table);
|
|
|
+ return Result.successData(ResultCode.SUCCESS, table);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public JSONObject selectBySqlToEchart(Map map) {
|
|
|
- String sql = (String)map.get("sql");
|
|
|
+ String sql = (String) map.get("sql");
|
|
|
if (StringUtils.isBlank(sql)) {
|
|
|
return Result.error(ResultCode.PARAM_IS_BLANK);
|
|
|
} else {
|
|
@@ -120,6 +120,7 @@ public class ObjectServiceImpl implements IObjectService {
|
|
|
|
|
|
/**
|
|
|
* 根据表头和数据生成默认表头
|
|
|
+ *
|
|
|
* @param tablename
|
|
|
* @param solhm
|
|
|
* @return
|
|
@@ -127,10 +128,10 @@ public class ObjectServiceImpl implements IObjectService {
|
|
|
private List<TableColumnHeader> createTableColumnHeader(String tablename, LinkedHashMap<String, Object> solhm) {
|
|
|
List<TableColumnHeader> tableHeaderColumns = new ArrayList<>();
|
|
|
long tableId = tableColumnHeaderMapper.selectCount(Wrappers.emptyWrapper()) + 1;
|
|
|
- int id=0;
|
|
|
+ int id = 0;
|
|
|
for (String s : solhm.keySet()) {
|
|
|
TableColumnHeader tch = new TableColumnHeader();
|
|
|
- id=id+1;
|
|
|
+ id = id + 1;
|
|
|
tch.setId(String.valueOf(id));
|
|
|
tch.setTid(tableId);
|
|
|
tch.setName(tablename);
|