|
@@ -1,13 +1,20 @@
|
|
|
package com.gyee.table.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.gyee.table.entity.Table;
|
|
|
+import com.gyee.table.entity.TableColumnHeader;
|
|
|
import com.gyee.table.mapper.ObjectAllMapper;
|
|
|
+import com.gyee.table.mapper.TableColumnHeaderMapper;
|
|
|
import com.gyee.table.result.Result;
|
|
|
import com.gyee.table.result.ResultCode;
|
|
|
import com.gyee.table.service.IObjectService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -26,24 +33,89 @@ import java.util.regex.Pattern;
|
|
|
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 Pattern compile = null;
|
|
|
+ private final Pattern compile;
|
|
|
{
|
|
|
compile = Pattern.compile(regex);
|
|
|
}
|
|
|
|
|
|
+ private final String[] emptyArray = {};
|
|
|
+
|
|
|
@Resource
|
|
|
private ObjectAllMapper objectMapper;
|
|
|
+ @Resource
|
|
|
+ private TableColumnHeaderMapper tableColumnHeaderMapper;
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据表名查表头,根据sql查数据
|
|
|
+ * @param map tablenae,sql
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@Override
|
|
|
public JSONObject selectBySql(Map map) {
|
|
|
|
|
|
String sql = (String) map.get("sql");
|
|
|
+ if(StringUtils.isBlank(sql)) return Result.error(ResultCode.PARAM_IS_BLANK);
|
|
|
+
|
|
|
Matcher matcher = compile.matcher(sql);
|
|
|
if(matcher.find()){
|
|
|
String group = matcher.group(0);
|
|
|
return Result.error(ResultCode.ERROR_UNSUPPORTED_SQL,"查看字符:"+group);
|
|
|
}
|
|
|
+
|
|
|
List<LinkedHashMap<String, Object>> lms = objectMapper.selectAll(sql);
|
|
|
- return Result.successData(ResultCode.SUCCESS,lms);
|
|
|
+
|
|
|
+ String tablename = (String) map.get("tablename");
|
|
|
+ if(StringUtils.isBlank(tablename))
|
|
|
+ //tablename = StringUtils.substringBetween("from ", " ");
|
|
|
+ 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){
|
|
|
+ tableColumnHeader = createTableColumnHeader(tablename, lms.get(0));
|
|
|
+ }else {
|
|
|
+ tableColumnHeader = tableColumnHeaderMapper.selectList(wrapper);
|
|
|
+ }
|
|
|
+ Table table = new Table();
|
|
|
+ table.setTableHeader(tableColumnHeader);
|
|
|
+ table.setTableData(lms);
|
|
|
+ return Result.successData(ResultCode.SUCCESS,table);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据表头和数据生成默认表头
|
|
|
+ * @param tablename
|
|
|
+ * @param solhm
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<TableColumnHeader> createTableColumnHeader(String tablename, LinkedHashMap<String, Object> solhm) {
|
|
|
+ List<TableColumnHeader> tableHeaderColumns = new ArrayList<>();
|
|
|
+ long tableId = tableColumnHeaderMapper.selectCount(Wrappers.emptyWrapper()) + 1;
|
|
|
+ for (String s : solhm.keySet()) {
|
|
|
+ TableColumnHeader tch = new TableColumnHeader();
|
|
|
+ tch.setTid(tableId);
|
|
|
+ tch.setName(tablename);
|
|
|
+ tch.setType("");
|
|
|
+ tch.setLabel(s);
|
|
|
+ tch.setWidth("");
|
|
|
+ tch.setMinwidth("");
|
|
|
+ tch.setProp(s);
|
|
|
+ tch.setAlign("center");
|
|
|
+ tch.setShowoverflowtooltip(true);
|
|
|
+ tch.setSortable(false);
|
|
|
+ tch.setOperations(emptyArray);
|
|
|
+ tch.setChildren(emptyArray);
|
|
|
+ tch.setShowfilters(false);
|
|
|
+ tch.setFilterstype("basic");
|
|
|
+ tch.setFilters(emptyArray);
|
|
|
+ tch.setFormattype("");
|
|
|
+ tch.setFormat(emptyArray);
|
|
|
+
|
|
|
+ tableHeaderColumns.add(tch);
|
|
|
+ }
|
|
|
+ return tableHeaderColumns;
|
|
|
}
|
|
|
}
|