Commit 33ece2e3 by wqc

连接库查看表、表结构

parent 2296a378
......@@ -117,6 +117,12 @@
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.zq</groupId>
<artifactId>sys-server</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
......
package com.zq.dataoperation.controller;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.pool.DruidConnectionHolder;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.zq.common.exception.BusinessException;
import com.zq.common.utils.AssertUtils;
import com.zq.common.vo.ResultVo;
import com.zq.dataoperation.entity.CommonQuerySetting;
import com.zq.dataoperation.entity.QueryDb;
import com.zq.dataoperation.service.CommonQueryService;
import com.zq.dataoperation.service.QueryDbService;
import com.zq.dataoperation.utils.TableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Api(tags = "通用查询")
@RestController
@RequestMapping(value = "/data/query")
public class CommonQueryController{
public class CommonQueryController {
@Resource
private CommonQueryService commonQueryService;
......@@ -33,32 +29,31 @@ public class CommonQueryController{
@ApiOperation("获取全部数据源")
@GetMapping("/datasource")
public ResultVo getDatasource(){
public ResultVo getDatasource() {
return ResultVo.success(commonQueryService.getDatasource());
}
@ApiOperation("添加数据源")
@PostMapping("/addDatasource")
public ResultVo addDatasource(@RequestBody QueryDb queryDb){
commonQueryService.addDatasource(queryDb);
return ResultVo.success();
public ResultVo addDatasource(@RequestBody QueryDb queryDb) {
return ResultVo.success(commonQueryService.addDatasource(queryDb));
}
@ApiOperation("测试连接数据源")
@PostMapping("/checkConnect")
public ResultVo checkConnect(@RequestBody QueryDb queryDb){
public ResultVo checkConnect(@RequestBody QueryDb queryDb) {
return ResultVo.success(commonQueryService.checkConnect(queryDb));
}
@ApiOperation("修改数据源")
@PostMapping("/updateDatasource")
public ResultVo updateDatasource(@RequestBody QueryDb queryDb){
public ResultVo updateDatasource(@RequestBody QueryDb queryDb) {
AssertUtils.notNull(queryDb.getDbName(), "缺少数据库名");
AssertUtils.hasText(queryDb.getDbIp(), "缺少IP");
AssertUtils.notNull(queryDb.getDbPort(), "缺少端口");
AssertUtils.notNull(queryDb.getDbType(), "缺少数据库类型");
AssertUtils.hasText(queryDb.getUsername(), "缺少连接用户名");
AssertUtils.hasText(queryDb.getPassword(),"缺少连接密码");
AssertUtils.hasText(queryDb.getPassword(), "缺少连接密码");
queryDb.setUpdateTime(DateUtil.date());
boolean b = queryDbService.updateById(queryDb);
return b ? ResultVo.success("操作成功!") : ResultVo.fail("操作失败");
......@@ -66,6 +61,7 @@ public class CommonQueryController{
/**
* 根据ID删除数据源
*
* @param id
* @return
*/
......@@ -80,13 +76,13 @@ public class CommonQueryController{
@ApiOperation("获取全部查询")
@GetMapping
public ResultVo get(){
public ResultVo get() {
return ResultVo.success(commonQueryService.get());
}
@ApiOperation("添加查询")
@PostMapping("/add")
public ResultVo add(@RequestBody CommonQuerySetting commonQuerySetting){
public ResultVo add(@RequestBody CommonQuerySetting commonQuerySetting) {
commonQueryService.add(commonQuerySetting);
return ResultVo.success();
}
......@@ -95,7 +91,7 @@ public class CommonQueryController{
@PostMapping("/run")
public ResultVo run(@RequestBody Map<String, Object> body) throws Exception {
System.out.println(body);
return ResultVo.success(commonQueryService.run( body));
return ResultVo.success(commonQueryService.run(body));
}
@ApiOperation("自定义sql查询")
......@@ -103,4 +99,21 @@ public class CommonQueryController{
public ResultVo runSelect(@RequestBody Map<String, Object> body) throws Exception {
return ResultVo.success(commonQueryService.runSelect(body));
}
@ApiOperation("获取库下的所有表")
@PostMapping("/showTables")
public ResultVo showTables(@RequestBody Map<String, Object> body) throws Exception {
return ResultVo.success(commonQueryService.showTables(body));
}
@ApiOperation("获取表结构")
@PostMapping("/tablesStructure")
public ResultVo tablesStructure(@RequestBody Map<String, Object> body) throws Exception {
Object id = body.get("id");
AssertUtils.notNull(id, "缺失ID");
Object tableName = body.get("tableName");
AssertUtils.notNull(tableName, "缺失表名");
return ResultVo.success(TableUtil.getColumns(Convert.toLong(id), Convert.toStr(tableName)));
}
}
......@@ -2,14 +2,11 @@ package com.zq.dataoperation.service;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zq.common.exception.BusinessException;
import com.zq.common.vo.PageVo;
import com.zq.common.vo.ResultVo;
import com.zq.dataoperation.dao.CommonQuerySettingDao;
import com.zq.dataoperation.dao.QueryDbDao;
......@@ -18,17 +15,14 @@ import com.zq.dataoperation.entity.QueryDb;
import com.zq.dataoperation.utils.ConnectionUtil;
import com.zq.dataoperation.utils.SqlUtils;
import com.zq.spiderflow.dao.MetaDataMappingDao;
import com.zq.spiderflow.entity.MetaData;
import com.zq.spiderflow.entity.MetaDataMapping;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import java.sql.*;
import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -40,6 +34,8 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
@Resource
private MetaDataMappingDao metaDataMappingDao;
private Map<Long, DruidPooledConnection> connectionMap = new HashMap<>();
public List<QueryDb> getDatasource() {
......@@ -77,36 +73,15 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
sql = sql.replace("{" + group + "}", jsonObject.getStr(group));
}
}
ResultSet resultSet = null;
try {
Connection connection = ConnectionUtil.get(queryDb.getId());
resultSet = connection.prepareStatement(sql).executeQuery();
int columnCount = resultSet.getMetaData().getColumnCount();
List<Map> results = new ArrayList<>();
while (resultSet.next()) {
Map<String, String> map = new HashMap<>(columnCount);
for (int i = 1; i <= columnCount; i++) {
map.put(resultSet.getMetaData().getColumnName(i), resultSet.getString(i));
}
results.add(map);
}
return results;
} catch (Exception e) {
throw new Exception(e);
} finally {
if (resultSet != null) {
resultSet.close();
}
}
return commonQuery(sql, queryDb.getId());
}
public Object checkConnect(QueryDb queryDb) {
QueryDb queryDb1 = queryDbDao.selectById(queryDb.getId());
//jdbc:mysql://47.107.148.253:3306/nnjcy_data_model
String jdbcUrl=queryDb1.getDbIp();
String jdbcUrl = queryDb1.getDbIp();
try {
return SqlUtils.testConnection(jdbcUrl,queryDb1.getUsername(),queryDb1.getPassword());
return SqlUtils.testConnection(jdbcUrl, queryDb1.getUsername(), queryDb1.getPassword());
} catch (Exception e) {
log.error(e.getMessage());
return false;
......@@ -118,7 +93,6 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
String querySql = jsonObject.get("querySql").toString();
Long id = Long.valueOf(jsonObject.get("id").toString());
List<MetaDataMapping> list = metaDataMappingDao.selectByCateId(id);
ResultSet resultSet = null;
String tableSqlname = null;
Long queryDbId = null;
for (MetaDataMapping mapping : list) {
......@@ -126,8 +100,45 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
queryDbId = mapping.getQueryDbId();
}
String sql = "SELECT * FROM " + tableSqlname + " WHERE " + querySql;
return commonQuery(sql, queryDbId);
}
public Object showTables(@RequestBody Map<String, Object> body) throws Exception {
JSONObject jsonObject = JSONUtil.parseObj(body);
int page = (int) jsonObject.get("page");
int size = (int) jsonObject.get("size");
String likeName = jsonObject.get("likeName").toString();
int start = 0;
PageVo<Map> pageVo = new PageVo<>();
if (page - 1 == 0) {
pageVo.setStart(0);
start = 0;
} else {
pageVo.setStart((page - 1) * size);
start = (page - 1) * size;
}
Long queryId = Long.valueOf(jsonObject.get("queryId").toString());
QueryDb queryDb = queryDbDao.selectById(queryId);
List<Map> results = null;
List<Map> list = null;
if (queryDb != null) {
String sql = "SELECT TABLE_NAME,TABLE_COMMENT FROM INFORMATION_SCHEMA.`TABLES` WHERE TABLE_SCHEMA='" + queryDb.getDbName() + "' " + "AND TABLE_NAME LIKE CONCAT('" + likeName + "' ,'%') ORDER BY CREATE_TIME DESC LIMIT " + start + "," + size;
results = commonQuery(sql, queryId);
String sqlTotal = "SELECT TABLE_NAME,TABLE_COMMENT FROM INFORMATION_SCHEMA.`TABLES` WHERE TABLE_SCHEMA='" + queryDb.getDbName() + "' " + "AND TABLE_NAME LIKE CONCAT('" + likeName + "' ,'%')";
list = commonQuery(sqlTotal, queryId);
}
pageVo.setPage(page);
pageVo.setSize(size);
pageVo.setRows(results);
pageVo.setTotal(list.size());
return pageVo;
}
public List<Map> commonQuery(String sql, Long queryId) throws Exception {
ResultSet resultSet = null;
try {
Connection connection = ConnectionUtil.get(queryDbId);
Connection connection = ConnectionUtil.get(queryId);
resultSet = connection.prepareStatement(sql).executeQuery();
int columnCount = resultSet.getMetaData().getColumnCount();
List<Map> results = new ArrayList<>();
......
package com.zq.dataoperation.utils;
import com.zq.dataoperation.vo.Column;
import lombok.SneakyThrows;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
* @author Administrator
*/
public class TableUtil {
@SneakyThrows
public static Object getColumns(Long queryId, String tableName) {
String driver = "com.mysql.cj.jdbc.Driver";
Class.forName(driver);
Connection connection = ConnectionUtil.get(queryId);
DatabaseMetaData metaData = connection.getMetaData();
String primaryColumn = "";
ResultSet idxRs = metaData.getIndexInfo(connection.getCatalog(), connection.getCatalog(), tableName, false, false);
while (idxRs.next()) {
String indexName = idxRs.getString("INDEX_NAME");
if ("PRIMARY".equals(indexName)) {
primaryColumn = idxRs.getString("COLUMN_NAME");
break;
}
}
List<Column> columns = new ArrayList<>();
ResultSet columnRs = metaData.getColumns(connection.getCatalog(), connection.getCatalog(), tableName, "%");
while (columnRs.next()) {
Column column = new Column();
column.setName(columnRs.getString("COLUMN_NAME"));
column.setType(columnRs.getString("TYPE_NAME"));
column.setLength(columnRs.getString("COLUMN_SIZE"));
column.setDecimalDigits(columnRs.getString("DECIMAL_DIGITS"));
column.setNotNull(!columnRs.getBoolean("NULLABLE"));
column.setPrimary(column.getName().equals(primaryColumn));
column.setComment(columnRs.getString("REMARKS"));
columns.add(column);
}
return columns;
}
}
package com.zq.dataoperation.vo;
import lombok.Data;
@Data
public class Column {
String name;
String type;
String length;
String decimalDigits;
boolean notNull;
boolean primary;
String comment;
}
package com.zq.dataoperation.vo;
import com.zq.system.utils.StringUtils;
import lombok.Data;
@Data
public class Index {
String tableName;
String indexName;
boolean unique;
String params;
public void addParams(String param) {
if (StringUtils.isBlank(params)) {
params = param;
} else {
params = params + "," + param;
}
}
}
package com.zq.dataoperation.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Table {
String tableName;
String tableComment;
List<Column> columnList = new ArrayList<>();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment