数据查看优化,执行器支持复制为insert、update、json格式

This commit is contained in:
暮光:城中城
2021-05-24 22:42:01 +08:00
parent 9992ca9183
commit 206f410573
18 changed files with 214 additions and 68 deletions

View File

@@ -103,6 +103,7 @@ public class DatabaseDocController {
}
DbBaseService dbBaseService = dbBaseFactory.getDbBaseService(sourceId);
Map<String, Object> dbResultMap = dbBaseService.getEditorData(sourceId);
dbResultMap.put("product", dbBaseService.getDatabaseProduct().name().toLowerCase());
// 缓存10分钟如果10分钟内库里面增删改了表或字段则提示不出来
CacheUtil.put(cacheKey, dbResultMap, 6000);
return DocDbResponseJson.ok(dbResultMap);

View File

@@ -21,6 +21,14 @@ public class TableDdlVo {
return null;
}
public String getHive() {
return hive;
}
public void setHive(String hive) {
this.hive = hive;
}
public String getCurrent() {
return current;
}

View File

@@ -23,7 +23,7 @@ public interface BaseMapper {
* @author 暮光:城中城
* @since 2018年8月8日
*/
Map<String, String> getTableDdl(@Param("dbName") String dbName, @Param("tableName") String tableName);
List<Map<String, String>> getTableDdl(@Param("dbName") String dbName, @Param("tableName") String tableName);
/**
* 获取库列表

View File

@@ -19,11 +19,10 @@ public class DbBaseFactory {
DatabaseRegistrationBean databaseRegistrationBean;
@Resource
private List<DbBaseService> dbBaseServiceList;
private Map<DatabaseProductEnum, DbBaseService> dbBaseServiceMap;
private final Map<DatabaseProductEnum, DbBaseService> dbBaseServiceMap = new HashMap<>();
@PostConstruct
private void init() {
dbBaseServiceMap = new HashMap<>();
dbBaseServiceList.forEach(item -> dbBaseServiceMap.put(item.getDatabaseProduct(), item));
}

View File

@@ -67,7 +67,7 @@ public abstract class DbBaseService {
*
* @return 服务类型
*/
abstract DatabaseProductEnum getDatabaseProduct();
public abstract DatabaseProductEnum getDatabaseProduct();
/**
* 获取库列表
@@ -345,8 +345,16 @@ public abstract class DbBaseService {
* @since 2020年4月24日
*/
public String getQueryPageSql(DataViewParam dataViewParam) {
// 需要各数据服务自己实现,各数据库产品的实现都不一样
throw new ConfirmException("暂未支持的数据库类型");
StringBuilder sqlSb = new StringBuilder();
sqlSb.append(String.format("select * from %s.%s", dataViewParam.getDbName(), dataViewParam.getTableName()));
if (StringUtils.isNotBlank(dataViewParam.getCondition())) {
sqlSb.append(String.format(" where %s", dataViewParam.getCondition()));
}
if (StringUtils.isNotBlank(dataViewParam.getOrderColumn()) && StringUtils.isNotBlank(dataViewParam.getOrderType())) {
sqlSb.append(String.format(" order by %s %s", dataViewParam.getOrderColumn(), dataViewParam.getOrderType()));
}
sqlSb.append(String.format(" limit %s offset %s", dataViewParam.getPageSize(), dataViewParam.getOffset()));
return sqlSb.toString();
}
/**
@@ -357,7 +365,11 @@ public abstract class DbBaseService {
* @since 2020年4月24日
*/
public String getQueryCountSql(DataViewParam dataViewParam) {
// 需要各数据服务自己实现,各数据库产品的实现都不一样
throw new ConfirmException("暂未支持的数据库类型");
StringBuilder sqlSb = new StringBuilder();
sqlSb.append(String.format("select count(1) as counts from %s.%s", dataViewParam.getDbName(), dataViewParam.getTableName()));
if (StringUtils.isNotBlank(dataViewParam.getCondition())) {
sqlSb.append(String.format(" where %s", dataViewParam.getCondition()));
}
return sqlSb.toString();
}
}

View File

@@ -1,6 +1,7 @@
package com.zyplayer.doc.db.service;
import com.zyplayer.doc.core.exception.ConfirmException;
import com.zyplayer.doc.db.controller.vo.TableDdlVo;
import com.zyplayer.doc.db.controller.vo.TableStatusVo;
import com.zyplayer.doc.db.framework.db.dto.QueryTableColumnDescDto;
import com.zyplayer.doc.db.framework.db.dto.TableColumnDescDto;
@@ -8,9 +9,12 @@ import com.zyplayer.doc.db.framework.db.dto.TableDescDto;
import com.zyplayer.doc.db.framework.db.dto.TableInfoDto;
import com.zyplayer.doc.db.framework.db.enums.DatabaseProductEnum;
import com.zyplayer.doc.db.framework.db.mapper.base.BaseMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class HiveServiceImpl extends DbBaseService {
@@ -30,6 +34,24 @@ public class HiveServiceImpl extends DbBaseService {
return tableList;
}
@Override
public TableDdlVo getTableDdl(Long sourceId, String dbName, String tableName) {
BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
List<Map<String, String>> tableDdlList = baseMapper.getTableDdl(dbName, tableName);
TableDdlVo tableDdlVo = new TableDdlVo();
tableDdlVo.setCurrent(DatabaseProductEnum.HIVE.name().toLowerCase());
if (CollectionUtils.isNotEmpty(tableDdlList)) {
// hive和impala结果集不一样
if (tableDdlList.size() == 1) {
tableDdlVo.setHive(tableDdlList.get(0).get("result"));
} else {
String createTabStmt = tableDdlList.stream().map(map -> map.get("createtab_stmt")).collect(Collectors.joining("\n"));
tableDdlVo.setHive(createTabStmt);
}
}
return tableDdlVo;
}
@Override
public List<TableColumnDescDto> getTableColumnDescList(Long sourceId, String tableName) {
throw new ConfirmException("不支持的操作");

View File

@@ -11,9 +11,11 @@ import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteParam;
import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteResult;
import com.zyplayer.doc.db.framework.db.mapper.base.ExecuteType;
import com.zyplayer.doc.db.framework.db.mapper.mysql.MysqlMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@@ -29,12 +31,15 @@ public class MysqlServiceImpl extends DbBaseService {
@Override
public TableDdlVo getTableDdl(Long sourceId, String dbName, String tableName) {
BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
Map<String, String> tableDdl = baseMapper.getTableDdl(dbName, tableName);
List<Map<String, String>> tableDdlList = baseMapper.getTableDdl(dbName, tableName);
TableDdlVo tableDdlVo = new TableDdlVo();
tableDdlVo.setCurrent(DatabaseProductEnum.MYSQL.name().toLowerCase());
tableDdlVo.setMysql(tableDdl.get("Create Table"));
tableDdlVo.setMysql("// 生成失败");
tableDdlVo.setOracle("// TODO 等待大佬来实现转换");
// TODO 将建表语句转换为其他数据库的,还不知道怎么做,先这样留着,看有没大佬来实现
if (CollectionUtils.isNotEmpty(tableDdlList)) {
tableDdlVo.setMysql(tableDdlList.get(0).get("Create Table"));
}
return tableDdlVo;
}
@@ -126,28 +131,4 @@ public class MysqlServiceImpl extends DbBaseService {
return ExecuteResult.error(e.getMessage(), procSql);
}
}
@Override
public String getQueryPageSql(DataViewParam dataViewParam) {
StringBuilder sqlSb = new StringBuilder();
sqlSb.append(String.format("select * from %s.%s", dataViewParam.getDbName(), dataViewParam.getTableName()));
if (StringUtils.isNotBlank(dataViewParam.getCondition())) {
sqlSb.append(String.format(" where %s", dataViewParam.getCondition()));
}
if (StringUtils.isNotBlank(dataViewParam.getOrderColumn()) && StringUtils.isNotBlank(dataViewParam.getOrderType())) {
sqlSb.append(String.format(" order by %s %s", dataViewParam.getOrderColumn(), dataViewParam.getOrderType()));
}
sqlSb.append(String.format(" limit %s offset %s", dataViewParam.getPageSize(), dataViewParam.getOffset()));
return sqlSb.toString();
}
@Override
public String getQueryCountSql(DataViewParam dataViewParam) {
StringBuilder sqlSb = new StringBuilder();
sqlSb.append(String.format("select count(1) as counts from %s.%s", dataViewParam.getDbName(), dataViewParam.getTableName()));
if (StringUtils.isNotBlank(dataViewParam.getCondition())) {
sqlSb.append(String.format(" where %s", dataViewParam.getCondition()));
}
return sqlSb.toString();
}
}

View File

@@ -7,7 +7,7 @@ import org.springframework.stereotype.Service;
public class OracleServiceImpl extends DbBaseService {
@Override
DatabaseProductEnum getDatabaseProduct() {
public DatabaseProductEnum getDatabaseProduct() {
return DatabaseProductEnum.ORACLE;
}
}

View File

@@ -7,7 +7,7 @@ import org.springframework.stereotype.Service;
public class PostgresqlServiceImpl extends DbBaseService {
@Override
DatabaseProductEnum getDatabaseProduct() {
public DatabaseProductEnum getDatabaseProduct() {
return DatabaseProductEnum.POSTGRESQL;
}
}

View File

@@ -14,7 +14,7 @@ import java.util.stream.Collectors;
public class SqlserverServiceImpl extends DbBaseService {
@Override
DatabaseProductEnum getDatabaseProduct() {
public DatabaseProductEnum getDatabaseProduct() {
return DatabaseProductEnum.SQLSERVER;
}

View File

@@ -1 +1 @@
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=favicon-db.png><title>数据库文档管理</title><link href=css/app.b6b9fe42.css rel=preload as=style><link href=css/chunk-vendors.8924efc6.css rel=preload as=style><link href=js/app.bfcb0d3d.js rel=preload as=script><link href=js/chunk-vendors.d40f789d.js rel=preload as=script><link href=css/chunk-vendors.8924efc6.css rel=stylesheet><link href=css/app.b6b9fe42.css rel=stylesheet></head><body><noscript><strong>We're sorry but zyplayer-db-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=js/chunk-vendors.d40f789d.js></script><script src=js/app.bfcb0d3d.js></script></body></html>
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=favicon-db.png><title>数据库文档管理</title><link href=css/app.b6b9fe42.css rel=preload as=style><link href=css/chunk-vendors.8924efc6.css rel=preload as=style><link href=js/app.c876342e.js rel=preload as=script><link href=js/chunk-vendors.d40f789d.js rel=preload as=script><link href=css/chunk-vendors.8924efc6.css rel=stylesheet><link href=css/app.b6b9fe42.css rel=stylesheet></head><body><noscript><strong>We're sorry but zyplayer-db-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=js/chunk-vendors.d40f789d.js></script><script src=js/app.c876342e.js></script></body></html>

View File

@@ -3,7 +3,7 @@ import vue from '../../main'
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url process.env.APP_BASE_API
timeout: 10000,
timeout: 60000,
headers: {'Content-type': 'application/x-www-form-urlencoded'},
withCredentials: true
});

View File

@@ -95,7 +95,7 @@
});
// 延迟设置展开的目录edit比app先初始化
setTimeout(() => {
this.doExecutorSql();
this.doExecutorSqlCommon();
this.$emit('initLoadDataList', {
sourceId: this.vueQueryParam.sourceId,
host: this.vueQueryParam.host,
@@ -114,21 +114,22 @@
},
handleCurrentChange(to) {
this.currentPage = to;
this.doExecutorSql();
this.doExecutorSqlCommon();
},
handlePageSizeChange(to) {
this.pageSize = to;
this.doExecutorSql();
this.doExecutorSqlCommon();
},
tableSortChange(sort) {
if (this.tableSort.prop === sort.prop && this.tableSort.order === sort.order) return;
this.tableSort = {prop: sort.prop, order: sort.order};
this.doExecutorSql();
this.tableSort = {orderColumn: sort.prop, orderType: (sort.order === 'ascending' ? 'asc' : 'desc')};
this.doExecutorSqlCommon();
},
refreshData() {
this.tableSort = {};
this.currentPage = 1;
this.doExecutorSql();
this.sqlExecutorEditor.setValue('', 1);
this.doExecutorSqlCommon();
},
cancelExecutorSql() {
datasourceApi.executeSqlCancel({executeId: this.nowExecutorId}).then(() => {
@@ -136,19 +137,21 @@
});
},
doExecutorClick() {
let conditionSql = this.sqlExecutorEditor.getSelectedText();
conditionSql = conditionSql || this.sqlExecutorEditor.getValue();
conditionSql = conditionSql || "";
this.doExecutorSqlCommon(conditionSql);
},
doExecutorSql() {
this.tableSort = {};
this.currentPage = 1;
this.doExecutorSqlCommon();
},
doExecutorSqlCommon(conditionSql) {
doExecutorSqlCommon() {
if (!this.vueQueryParam.sourceId) {
this.$message.error("请先选择数据源");
return;
}
if (!this.tableSort.orderColumn) {
this.tableSort = {orderColumn: this.vueQueryParam.orderColumn, orderType: 'asc'};
}
let conditionSql = this.sqlExecutorEditor.getSelectedText();
conditionSql = conditionSql || this.sqlExecutorEditor.getValue();
conditionSql = conditionSql || "";
this.executeError = "";
this.executeUseTime = "";
this.executeResultList = [];
@@ -163,8 +166,8 @@
condition: conditionSql,
pageNum: this.currentPage,
pageSize: this.pageSize,
orderColumn: this.tableSort.prop,
orderType: (this.tableSort.order === 'ascending' ? 'asc' : 'desc'),
orderColumn: this.tableSort.orderColumn,
orderType: this.tableSort.orderType,
params: '',
};
datasourceApi.dataViewQuery(param).then(json => {

View File

@@ -25,17 +25,30 @@
<el-card>
<div v-if="!!executeError" style="color: #f00;">{{executeError}}</div>
<div v-else-if="executeResultList.length <= 0" v-loading="sqlExecuting">暂无数据</div>
<div v-else>
<el-tabs :value="executeShowTable">
<div v-else style="position: relative;">
<div style="position: absolute;right: 0;z-index: 1;">
<!-- 复制选中行 -->
<el-dropdown @command="handleCopyCheckLineCommand" v-show="this.choiceResultObj[this.executeShowTable] && this.choiceResultObj[this.executeShowTable].length > 0">
<el-button type="primary" size="small">
复制选中行<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="insert">SQL Inserts</el-dropdown-item>
<el-dropdown-item command="update">SQL Updates</el-dropdown-item>
<el-dropdown-item command="json">JSON</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div>
<el-tabs v-model="executeShowTable">
<el-tab-pane label="信息" name="table0">
<pre>{{executeResultInfo}}</pre>
</el-tab-pane>
<el-tab-pane :label="'结果'+resultItem.index" :name="'table'+resultItem.index" v-for="resultItem in executeResultList" v-if="!!resultItem.index">
<el-tab-pane :label="'结果'+resultItem.index" :name="resultItem.name" v-for="resultItem in executeResultList" v-if="!!resultItem.index">
<div v-if="!!resultItem.errMsg" style="color: #f00;">{{resultItem.errMsg}}</div>
<el-table v-else :data="resultItem.dataList" stripe border style="width: 100%; margin-bottom: 5px;" class="execute-result-table" max-height="600">
<el-table-column width="60px" v-if="resultItem.dataCols.length > 0">
<template slot-scope="scope">{{scope.row._index}}</template>
</el-table-column>
<el-table v-else :data="resultItem.dataList" stripe border style="width: 100%; margin-bottom: 5px;" class="execute-result-table" max-height="600"
@selection-change="handleSelectionChange">
<el-table-column type="selection" width="55"></el-table-column>
<el-table-column type="index" width="50"></el-table-column>
<el-table-column v-for="item in resultItem.dataCols" :prop="item.prop" :label="item.prop" :width="item.width">
<template slot-scope="scope">
<textarea readonly :value="scope.row[item.prop]" class="el-textarea__inner" rows="1"></textarea>
@@ -93,6 +106,7 @@
import '../../common/lib/ace/mode-sql'
import '../../common/lib/ace/ext-language_tools'
import '../../common/lib/ace/snippets/sql'
import copyFormatter from './copy/index'
import sqlFormatter from "sql-formatter"
import datasourceApi from '../../common/api/datasource'
@@ -107,6 +121,7 @@
databaseList: [],
choiceDatabase: "",
editorDbProduct: "",
editorDbInfo: [],
editorDbTableInfo: {},
editorColumnInfo: {},
@@ -122,6 +137,8 @@
historyDrawerVisible: false,
myFavoriteList: [],
myHistoryListList: [],
// 选择复制
choiceResultObj: {},
}
},
mounted: function () {
@@ -245,6 +262,7 @@
var resultItem = this.dealExecuteResult(objItem);
if (resultItem.updateCount < 0) {
resultItem.index = itemIndex;
resultItem.name = 'table' + itemIndex;
itemIndex++;
}
executeResultList.push(resultItem);
@@ -271,6 +289,7 @@
datasourceApi.getEditorData({sourceId: this.choiceDatasourceId}).then(json => {
let data = json.data || {};
this.editorDbInfo = data.db || [];
this.editorDbProduct = data.product || '';
this.editorDbTableInfo = data.table || {};
this.editorColumnInfo = data.column || {};
});
@@ -317,9 +336,6 @@
width = (width > 200) ? 200 : width;
executeResultCols.push({prop: key, width: width + 25});
}
for (var i = 0; i < dataList.length; i++) {
dataList[i]._index = i + 1;
}
}
var resultObj = {};
resultObj.dataList = dataList;
@@ -422,9 +438,23 @@
}
}
});
},
}
}
},
handleSelectionChange(val) {
this.$set(this.choiceResultObj, this.executeShowTable, val);
},
handleCopyCheckLineCommand(type) {
let choiceData = this.choiceResultObj[this.executeShowTable] || [];
if (choiceData.length > 0) {
let dataCols = this.executeResultList.find(item => item.name === this.executeShowTable).dataCols;
let copyData = copyFormatter.format(type, this.editorDbProduct, dataCols, choiceData);
this.$copyText(copyData).then(
res => this.$message.success("内容已复制到剪切板!"),
err => this.$message.error("抱歉,复制失败!")
);
}
}
}
}
</script>
<style>

View File

@@ -0,0 +1,56 @@
/**
* 通用的转换器,如果通用的不能满足需要添加转换器实现转换
* @author 暮光:城中城
* @since 2021年5月23日
*/
export default {
insert(dataCols, choiceData) {
// 复制为insert语句
let copyData = '';
let names = '';
dataCols.forEach(col => {
if (names.length > 0) names += ', '
names += col.prop;
});
choiceData.forEach(item => {
let values = '';
dataCols.forEach(col => {
if (values.length > 0) values += ', ';
let val = item[col.prop];
if (typeof val === 'number' && !isNaN(val)) {
values += val;
} else {
val = val.replaceAll('\'', '\'\'');
values += "'" + val + "'";
}
});
copyData += 'insert into `table` (' + names + ') values (' + values + ');\n';
});
return copyData;
},
update(dataCols, choiceData) {
// 复制为update语句
let copyData = '';
choiceData.forEach(item => {
let values = '', where = '';
dataCols.forEach(col => {
if (values.length > 0) values += ', ';
values += col.prop + '=';
let val = item[col.prop];
if (typeof val === 'number' && !isNaN(val)) {
values += val;
if (col.prop === 'id') where = ' where id = ' + val;
} else {
val = val.replaceAll('\'', '\'\'');
values += "'" + val + "'";
}
});
copyData += 'update `table` set ' + values + where + ';\n';
});
return copyData;
},
json(dataCols, choiceData) {
// 复制为json
return JSON.stringify(choiceData);
},
}

View File

@@ -0,0 +1,23 @@
import base from './base'
export default {
format(type, product, dataCols, choiceData) {
let formatter = this.getProduct(product);
if (type === 'insert') {
// 复制为insert语句
return formatter.insert(dataCols, choiceData);
} else if (type === 'update') {
// 复制为update语句
return formatter.update(dataCols, choiceData);
} else if (type === 'json') {
// 复制为json
return formatter.json(dataCols, choiceData);
}
},
getProduct(product) {
if (product === 'mysql') {
// 不同数据源类型可以用不用的处理器类型,暂时只实现了一套基础的
}
return base;
},
}

View File

@@ -103,6 +103,11 @@
<pre><code v-html="tableDDLInfo.postgresql"></code></pre>
</div>
</el-tab-pane>
<el-tab-pane label="hive" name="hive" v-if="!!tableDDLInfo.hive">
<div v-highlight>
<pre><code v-html="tableDDLInfo.hive"></code></pre>
</div>
</el-tab-pane>
</el-tabs>
</el-dialog>
</div>
@@ -199,12 +204,18 @@
row.inEdit = 1;
},
previewTableData() {
if (!this.columnList || this.columnList.length <= 0) {
this.$message.error("字段信息尚未加载成功,请稍候...");
return;
}
let previewParam = {
sourceId: this.vueQueryParam.sourceId,
dbName: this.vueQueryParam.dbName,
tableName: this.vueQueryParam.tableName,
host: this.vueQueryParam.host,
dbType: this.tableStatusInfo.dbType,
// 默认排序字段先随便取一个impala等数据库必须排序后才能分页查
orderColumn: this.columnList[0].name,
};
this.$router.push({path: '/data/dataPreview', query: previewParam});
},