执行器库表信息查询修改,展示优化,增加快捷键执行

This commit is contained in:
暮光:城中城
2019-09-02 22:47:24 +08:00
parent 9c29f4c8fd
commit abc24b424f
15 changed files with 255 additions and 186 deletions

View File

@@ -27,6 +27,7 @@ import com.zyplayer.doc.db.framework.db.dto.*;
import com.zyplayer.doc.db.framework.db.mapper.base.BaseMapper;
import com.zyplayer.doc.db.framework.db.mapper.mysql.MysqlMapper;
import com.zyplayer.doc.db.framework.json.DocDbResponseJson;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.mybatis.spring.SqlSessionTemplate;
@@ -89,6 +90,7 @@ public class DatabaseDocController {
/**
* 获取编辑器所需的所有信息,用于自动补全
* 此接口会返回所有库表结构,介意的话请自己手动屏蔽调此接口
* @param sourceId
* @return
*/
@@ -109,19 +111,35 @@ public class DatabaseDocController {
List<DatabaseInfoDto> dbNameDtoList = baseMapper.getDatabaseList();
Map<String, List<TableInfoDto>> dbTableMap = new HashMap<>();
Map<String, List<TableColumnDescDto>> tableColumnsMap = new HashMap<>();
Map<String, List<TableInfoDto>> tableMapList = new HashMap<>();
// MYSQL可以一次性查询所有库表
if (databaseFactoryBean.getDatabaseProduct() == DatabaseProduct.MYSQL) {
List<TableInfoDto> dbTableList = baseMapper.getTableList(null);
tableMapList = dbTableList.stream().collect(Collectors.groupingBy(TableInfoDto::getDbName));
}
for (DatabaseInfoDto infoDto : dbNameDtoList) {
List<TableInfoDto> dbTableList = baseMapper.getTableList(infoDto.getDbName());
dbTableMap.put(infoDto.getDbName(), dbTableList);
for (TableInfoDto tableInfoDto : dbTableList) {
TableColumnVo tableColumnVo = this.getTableColumnVo(databaseFactoryBean, infoDto.getDbName(), tableInfoDto.getTableName());
// 重新组装一下,只返回两个字段,减少返回数据量
List<TableColumnDescDto> descDtoList = tableColumnVo.getColumnList().stream().map(val -> {
TableColumnDescDto dto = new TableColumnDescDto();
dto.setName(val.getName());
dto.setDescription(val.getDescription());
return dto;
}).collect(Collectors.toList());
tableColumnsMap.put(tableInfoDto.getTableName(), descDtoList);
List<TableInfoDto> tableInfoDtoList = tableMapList.get(infoDto.getDbName());
// SQLSERVER必须要库才能查
if (databaseFactoryBean.getDatabaseProduct() == DatabaseProduct.SQLSERVER) {
tableInfoDtoList = baseMapper.getTableList(infoDto.getDbName());
}
if (CollectionUtils.isEmpty(tableInfoDtoList)) {
continue;
}
dbTableMap.put(infoDto.getDbName(), tableInfoDtoList);
// 小于10个库查所有库否则只查询当前链接的库防止库表太多数据量太大
// 如果觉得没必要就自己改吧!
Map<String, List<TableColumnDescDto>> columnDescDtoMap = new HashMap<>();
if (dbNameDtoList.size() <= 10 || Objects.equals(databaseFactoryBean.getDbName(), infoDto.getDbName())) {
List<TableColumnDescDto> columnDescDto = baseMapper.getTableColumnList(infoDto.getDbName(), null);
columnDescDtoMap = columnDescDto.stream().collect(Collectors.groupingBy(TableColumnDescDto::getTableName));
}
for (TableInfoDto tableInfoDto : tableInfoDtoList) {
List<TableColumnDescDto> descDtoList = columnDescDtoMap.get(tableInfoDto.getTableName());
if (CollectionUtils.isNotEmpty(descDtoList)) {
tableColumnsMap.put(tableInfoDto.getTableName(), descDtoList);
}
}
}
Map<String, Object> dbResultMap = new HashMap<>();
@@ -177,9 +195,9 @@ public class DatabaseDocController {
}
@PostMapping(value = "/getTableDescList")
public ResponseJson getTableDescList(Long sourceId, String tableName) {
public ResponseJson getTableDescList(Long sourceId, String dbName, String tableName) {
BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
List<TableDescDto> columnDescDto = baseMapper.getTableDescList(tableName);
List<TableDescDto> columnDescDto = baseMapper.getTableDescList(dbName, tableName);
return DocDbResponseJson.ok(columnDescDto);
}
@@ -274,7 +292,7 @@ public class DatabaseDocController {
tableColumnVo.setColumnList(columnDescDto);
// 表注释
TableInfoVo tableInfoVo = new TableInfoVo();
List<TableDescDto> tableDescList = baseMapper.getTableDescList(tableName);
List<TableDescDto> tableDescList = baseMapper.getTableDescList(dbName, tableName);
String description = null;
if (tableDescList.size() > 0) {
TableDescDto descDto = tableDescList.get(0);

View File

@@ -58,6 +58,8 @@ public class DbSqlExecutorController {
if (!manageAuth && !select && !update) {
return DocDbResponseJson.warn("没有该数据源的执行权限");
}
// 保留历史记录
dbHistoryService.saveHistory(sql.trim(), sourceId);
List<String> resultList = new LinkedList<>();
// 支持;分割的多个sql执行
String[] sqlArr = sql.split(";");
@@ -99,9 +101,10 @@ public class DbSqlExecutorController {
}
@PostMapping(value = "/favorite/list")
public ResponseJson favoriteList() {
public ResponseJson favoriteList(Long sourceId) {
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
UpdateWrapper<DbFavorite> wrapper = new UpdateWrapper<>();
wrapper.eq(sourceId != null, "datasource_id", sourceId);
wrapper.eq("create_user_id", currentUser.getUserId());
wrapper.eq("yn", 1);
wrapper.orderByDesc("id");
@@ -112,8 +115,11 @@ public class DbSqlExecutorController {
@PostMapping(value = "/favorite/add")
public ResponseJson addFavorite(DbFavorite dbFavorite) {
Integer yn = Optional.ofNullable(dbFavorite.getYn()).orElse(1);
if (yn == 1 && StringUtils.isBlank(dbFavorite.getContent())) {
return DocDbResponseJson.warn("收藏的SQL不能为空");
if (yn == 1) {
if (StringUtils.isBlank(dbFavorite.getContent())) {
return DocDbResponseJson.warn("收藏的SQL不能为空");
}
dbFavorite.setContent(dbFavorite.getContent().trim());
}
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
if (dbFavorite.getId() != null && dbFavorite.getId() > 0) {

View File

@@ -1,68 +1,76 @@
package com.zyplayer.doc.db.framework.db.dto;
public class TableColumnDescDto {
private String name;
private String isidenity;
private String type;
private String nullable;
private String length;
private String ispramary;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsidenity() {
return isidenity;
}
public void setIsidenity(String isidenity) {
this.isidenity = isidenity;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNullable() {
return nullable;
}
public void setNullable(String nullable) {
this.nullable = nullable;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getIspramary() {
return ispramary;
}
public void setIspramary(String ispramary) {
this.ispramary = ispramary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.zyplayer.doc.db.framework.db.dto;
public class TableColumnDescDto {
private String tableName;
private String name;
private String isidenity;
private String type;
private String nullable;
private String length;
private String ispramary;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsidenity() {
return isidenity;
}
public void setIsidenity(String isidenity) {
this.isidenity = isidenity;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNullable() {
return nullable;
}
public void setNullable(String nullable) {
this.nullable = nullable;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getIspramary() {
return ispramary;
}
public void setIspramary(String ispramary) {
this.ispramary = ispramary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}

View File

@@ -1,32 +1,40 @@
package com.zyplayer.doc.db.framework.db.dto;
public class TableInfoDto {
private String tableName;
private String tableComment;
private String tableId;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getTableId() {
return tableId;
}
public void setTableId(String tableId) {
this.tableId = tableId;
}
public String getTableComment() {
return tableComment;
}
public void setTableComment(String tableComment) {
this.tableComment = tableComment;
}
}
package com.zyplayer.doc.db.framework.db.dto;
public class TableInfoDto {
private String dbName;
private String tableName;
private String tableComment;
private String tableId;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getTableId() {
return tableId;
}
public void setTableId(String tableId) {
this.tableId = tableId;
}
public String getTableComment() {
return tableComment;
}
public void setTableComment(String tableComment) {
this.tableComment = tableComment;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
}

View File

@@ -65,7 +65,7 @@ public interface BaseMapper {
* @param tableName 可不传,传了只查询指定表的注释
* @return 表注释
*/
List<TableDescDto> getTableDescList(@Param("tableName") String tableName);
List<TableDescDto> getTableDescList(@Param("dbName") String dbName, @Param("tableName") String tableName);
/**
* 增加表注释
@@ -78,7 +78,7 @@ public interface BaseMapper {
/**
* 增加字段注释
*
*
* @author 暮光:城中城
* @since 2018年8月8日
* @param dbName 数据库名
@@ -91,4 +91,4 @@ public interface BaseMapper {
@Param("columnName") String columnName, @Param("newDesc") String newDesc,
@Param("columnInfo") ColumnInfoDto columnInfo);
}
}

View File

@@ -18,7 +18,7 @@ public class ExecuteResult {
private List<Map<String, Object>> result;
public ExecuteResult() {
this.updateCount = 0;
this.updateCount = -1;
this.useTime = 0;
this.result = Collections.emptyList();
}

View File

@@ -5,11 +5,9 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.zyplayer.doc.data.service.manage.DbHistoryService;
import com.zyplayer.doc.db.framework.db.bean.DatabaseFactoryBean;
import com.zyplayer.doc.db.framework.db.bean.DatabaseRegistrationBean;
import com.zyplayer.doc.db.framework.db.interceptor.SqlLogUtil;
import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.parsing.GenericTokenParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -72,30 +70,28 @@ public class SqlExecutor {
* @author 暮光:城中城
* @since 2019年8月18日
*/
public ExecuteResult execute(DatabaseFactoryBean factoryBean, String executeId, ExecuteType executeType, String sql, Map<String, Object> paramMap, ResultHandler handler) {
public ExecuteResult execute(DatabaseFactoryBean factoryBean, String executeId, ExecuteType executeType, String sqlStr, Map<String, Object> paramMap, ResultHandler handler) {
if (factoryBean == null) {
return new ExecuteResult();
}
BoundSql boundSql = getBoundSql(sql, paramMap);
sql = boundSql.getSql();
String sqlStr = SqlLogUtil.getSqlString(paramMap, boundSql);
// BoundSql boundSql = getBoundSql(sql, paramMap);
// sql = boundSql.getSql();
// String sqlStr = SqlLogUtil.getSqlString(paramMap, boundSql);
logger.info("sql ==> {}", sqlStr);
// 保留历史记录
dbHistoryService.saveHistory(sqlStr, factoryBean.getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
// List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
PreparedStatement preparedStatement = null;
DruidPooledConnection connection = null;
// 执行查询
try {
long startTime = System.currentTimeMillis();
connection = factoryBean.getDataSource().getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement = connection.prepareStatement(sqlStr);
// 设置当前的PreparedStatement
statementMap.put(executeId, preparedStatement);
for (int i = 0; i < parameterMappings.size(); i++) {
preparedStatement.setObject(i + 1, paramMap.get(parameterMappings.get(i).getProperty()));
}
// for (int i = 0; i < parameterMappings.size(); i++) {
// preparedStatement.setObject(i + 1, paramMap.get(parameterMappings.get(i).getProperty()));
// }
// 限制下最大数量
preparedStatement.setMaxRows(1000);
if (ExecuteType.SELECT.equals(executeType)) {
@@ -120,11 +116,10 @@ public class SqlExecutor {
}
}
}
// 更新的数量
// 更新的数量小于0代表不是更新语句
int updateCount = preparedStatement.getUpdateCount();
updateCount = (updateCount < 0) ? 0 : updateCount;
long useTime = System.currentTimeMillis() - startTime;
return new ExecuteResult(updateCount, resultList, useTime, sql);
return new ExecuteResult(updateCount, resultList, useTime, sqlStr);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {

View File

@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zyplayer.doc.db.framework.db.mapper.base.BaseMapper">
<resultMap id="TableColumnDescDtoMap" type="com.zyplayer.doc.db.framework.db.dto.TableColumnDescDto" >
<result column="TABLE_NAME" property="tableName" jdbcType="VARCHAR" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
<result column="ISIDENITY" property="isidenity" jdbcType="VARCHAR" />
<result column="TYPE" property="type" jdbcType="VARCHAR" />
@@ -11,13 +12,13 @@
<result column="ISPRAMARY" property="ispramary" jdbcType="VARCHAR" />
<result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="QueryTableColumnDescDtoMap" type="com.zyplayer.doc.db.framework.db.dto.QueryTableColumnDescDto" >
<result column="TABLE_NAME" property="tableName" jdbcType="VARCHAR" />
<result column="COLUMN_NAME" property="columnName" jdbcType="VARCHAR" />
<result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
</resultMap>
<select id="getDatabaseList" resultType="com.zyplayer.doc.db.framework.db.dto.DatabaseInfoDto">
select TABLE_SCHEMA dbName
from information_schema.tables
@@ -25,44 +26,46 @@
</select>
<select id="getTableList" resultType="com.zyplayer.doc.db.framework.db.dto.TableInfoDto">
select table_name tableName, table_comment as tableComment
select table_schema dbName,table_name tableName, table_comment as tableComment
from information_schema.tables
where table_schema=#{dbName}
<if test="dbName != null">where table_schema=#{dbName}</if>
</select>
<select id="getTableColumnList" resultMap="TableColumnDescDtoMap">
SELECT COLUMN_NAME NAME,column_comment DESCRIPTION,column_type TYPE,if(is_nullable='YES',1,0) NULLABLE
SELECT table_name as TABLE_NAME,COLUMN_NAME NAME,column_comment DESCRIPTION,column_type TYPE,if(is_nullable='YES',1,0) NULLABLE
FROM INFORMATION_SCHEMA.Columns
WHERE table_schema=#{dbName} AND table_name=#{tableName}
WHERE table_schema=#{dbName}
<if test="tableName != null">and table_name=#{tableName}</if>
ORDER BY ordinal_position
</select>
<select id="getTableColumnDescList" resultMap="TableColumnDescDtoMap">
select 1
</select>
<select id="getTableAndColumnBySearch" resultMap="QueryTableColumnDescDtoMap">
SELECT TABLE_NAME, COLUMN_NAME, column_comment DESCRIPTION
FROM INFORMATION_SCHEMA.Columns
WHERE table_schema=#{dbName} AND (COLUMN_NAME like #{searchText} or column_comment like #{searchText})
</select>
<select id="getTableDescList" resultType="com.zyplayer.doc.db.framework.db.dto.TableDescDto">
select table_name tableName, table_comment as description
from information_schema.tables
<if test="tableName != null">
where table_name=#{tableName}
</if>
<where>
<if test="dbName != null">and table_schema=#{dbName}</if>
<if test="tableName != null">and table_name=#{tableName}</if>
</where>
</select>
<insert id="updateTableDesc">
alter table ${dbName}.${tableName} comment #{newDesc}
</insert>
<insert id="updateTableColumnDesc">
alter table ${dbName}.${tableName} modify column ${columnName}
${columnInfo.columnType} ${columnInfo.isNullable} ${columnInfo.columnDefault} ${columnInfo.extra}
comment #{newDesc}
</insert>
</mapper>

File diff suppressed because one or more lines are too long

View File

@@ -1,2 +1,2 @@
!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,u){for(var i,a,f,l=0,s=[];l<t.length;l++)a=t[l],o[a]&&s.push(o[a][0]),o[a]=0;for(i in c)Object.prototype.hasOwnProperty.call(c,i)&&(e[i]=c[i]);for(r&&r(t,c,u);s.length;)s.shift()();if(u)for(l=0;l<u.length;l++)f=n(n.s=u[l]);return f};var t={},o={2:0};n.e=function(e){function r(){i.onerror=i.onload=null,clearTimeout(a);var n=o[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var u=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.charset="utf-8",i.async=!0,i.timeout=12e4,n.nc&&i.setAttribute("nonce",n.nc),i.src=n.p+""+e+".js?"+{0:"d0fd867e51634f6a3bdb",1:"0a0403eb1820498dc9bc"}[e];var a=setTimeout(r,12e4);return i.onerror=i.onload=r,u.appendChild(i),c},n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=doc-db-manifest.js.map?2fb9e03777eaeb893be9
!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,c,u){for(var i,a,f,l=0,s=[];l<t.length;l++)a=t[l],o[a]&&s.push(o[a][0]),o[a]=0;for(i in c)Object.prototype.hasOwnProperty.call(c,i)&&(e[i]=c[i]);for(r&&r(t,c,u);s.length;)s.shift()();if(u)for(l=0;l<u.length;l++)f=n(n.s=u[l]);return f};var t={},o={2:0};n.e=function(e){function r(){i.onerror=i.onload=null,clearTimeout(a);var n=o[e];0!==n&&(n&&n[1](new Error("Loading chunk "+e+" failed.")),o[e]=void 0)}var t=o[e];if(0===t)return new Promise(function(e){e()});if(t)return t[2];var c=new Promise(function(n,r){t=o[e]=[n,r]});t[2]=c;var u=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.charset="utf-8",i.async=!0,i.timeout=12e4,n.nc&&i.setAttribute("nonce",n.nc),i.src=n.p+""+e+".js?"+{0:"80bc97c8e1f56e403c39",1:"0a0403eb1820498dc9bc"}[e];var a=setTimeout(r,12e4);return i.onerror=i.onload=r,u.appendChild(i),c},n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n.oe=function(e){throw console.error(e),e}}([]);
//# sourceMappingURL=doc-db-manifest.js.map?42eca50901b3d81e2837

View File

@@ -8,7 +8,7 @@
<body>
<div id="app"></div>
<script type="text/javascript" src="doc-db-manifest.js?2fb9e03777eaeb893be9"></script><script type="text/javascript" src="doc-db-vendor.js?0a0403eb1820498dc9bc"></script><script type="text/javascript" src="doc-db-index.js?d0fd867e51634f6a3bdb"></script></body>
<script type="text/javascript" src="doc-db-manifest.js?42eca50901b3d81e2837"></script><script type="text/javascript" src="doc-db-vendor.js?0a0403eb1820498dc9bc"></script><script type="text/javascript" src="doc-db-index.js?80bc97c8e1f56e403c39"></script></body>
</html>