fix: 修复数据库没表情况下报错

This commit is contained in:
meilin.huang
2022-03-24 17:50:44 +08:00
parent 60775bb6e3
commit a475818967
5 changed files with 59 additions and 14 deletions

View File

@@ -12,8 +12,8 @@
"countup.js": "^2.0.7",
"cropperjs": "^1.5.11",
"echarts": "^5.1.1",
"element-plus": "^2.1.3",
"@element-plus/icons-vue": "^1.1.1",
"element-plus": "^2.1.4",
"@element-plus/icons-vue": "^1.1.3",
"jsonlint": "^1.6.3",
"lodash": "^4.17.21",
"mitt": "^3.0.0",

View File

@@ -950,10 +950,5 @@ export default defineComponent({
#data-exec {
min-height: calc(100vh - 155px);
.el-table__empty-text {
width: 100%;
margin-left: 50px;
}
}
</style>

View File

@@ -48,7 +48,11 @@
<el-table v-loading="loading" :data="keys" stripe :highlight-current-row="true" style="cursor: pointer">
<el-table-column show-overflow-tooltip prop="key" label="key"></el-table-column>
<el-table-column prop="type" label="type" width="80"> </el-table-column>
<el-table-column prop="type" label="type" width="80">
<template #default="scope">
<el-tag :color="getTypeColor(scope.row.type)" size="small">{{ scope.row.type }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="ttl" label="ttl(过期时间)" width="130">
<template #default="scope">
{{ ttlConveter(scope.row.ttl) }}
@@ -313,6 +317,18 @@ export default defineComponent({
return result;
};
const getTypeColor = (type: string) => {
if (type == 'string') {
return '#E4F5EB';
}
if (type == 'hash') {
return '#F9E2AE';
}
if (type == 'set') {
return '#A8DEE0';
}
};
const onAddData = () => {
notNull(state.scanParam.id, '请先选择redis');
state.dataEdit.operationType = 1;
@@ -338,6 +354,7 @@ export default defineComponent({
getValue,
del,
ttlConveter,
getTypeColor,
onAddData,
onCancelDataEdit,
};

View File

@@ -169,11 +169,17 @@ func (d *Db) HintTables(rc *ctx.ReqCtx) {
for _, v := range tables {
tableNames = append(tableNames, v["tableName"])
}
// 获取所有表下的所有列信息
columnMds := dbi.GetColumnMetadatas(tableNames...)
// key = 表名value = 列名数组
res := make(map[string][]string)
// 表为空,则直接返回
if len(tableNames) == 0 {
rc.ResData = res
return
}
// 获取所有表下的所有列信息
columnMds := dbi.GetColumnMetadatas(tableNames...)
for _, v := range columnMds {
tName := v["tableName"]
if res[tName] == nil {

View File

@@ -11,6 +11,7 @@ import (
"mayfly-go/server/devops/domain/entity"
"mayfly-go/server/devops/domain/repository"
"mayfly-go/server/devops/infrastructure/persistence"
"strconv"
"strings"
"sync"
"time"
@@ -289,10 +290,17 @@ const (
FROM information_schema.STATISTICS
WHERE table_schema = (SELECT database()) AND table_name = '%s'`
// 默认每次查询列元信息数量
DEFAULT_COLUMN_SIZE = 2000
// mysql 列信息元数据
MYSQL_COLOUMN_MA = `SELECT table_name tableName, column_name columnName, column_type columnType,
column_comment columnComment, column_key columnKey, extra, is_nullable nullable from information_schema.columns
WHERE table_name in (%s) AND table_schema = (SELECT database()) ORDER BY ordinal_position limit 18000`
WHERE table_name in (%s) AND table_schema = (SELECT database()) ORDER BY tableName, ordinal_position limit %d, %d`
// mysql 列信息元数据总数
MYSQL_COLOUMN_MA_COUNT = `SELECT COUNT(*) maNum from information_schema.columns
WHERE table_name in (%s) AND table_schema = (SELECT database())`
)
func (d *DbInstance) GetTableMetedatas() []map[string]string {
@@ -312,12 +320,31 @@ func (d *DbInstance) GetColumnMetadatas(tableNames ...string) []map[string]strin
}
tableName = tableName + "'" + tableNames[i] + "'"
}
var countSqlTmp string
var sqlTmp string
if d.Type == "mysql" {
sql = fmt.Sprintf(MYSQL_COLOUMN_MA, tableName)
countSqlTmp = MYSQL_COLOUMN_MA_COUNT
sqlTmp = MYSQL_COLOUMN_MA
}
_, res, err := d.SelectData(sql)
biz.ErrIsNilAppendErr(err, "获取数据库列信息失败: %s")
countSql := fmt.Sprintf(countSqlTmp, tableName)
_, countRes, _ := d.SelectData(countSql)
// 查询出所有列信息总数,手动分页获取所有数据
maCount, _ := strconv.Atoi(countRes[0]["maNum"])
// 计算需要查询的页数
pageNum := maCount / DEFAULT_COLUMN_SIZE
if maCount%DEFAULT_COLUMN_SIZE > 0 {
pageNum++
}
res := make([]map[string]string, 0)
for index := 0; index < pageNum; index++ {
sql = fmt.Sprintf(sqlTmp, tableName, index*DEFAULT_COLUMN_SIZE, DEFAULT_COLUMN_SIZE)
_, result, err := d.SelectData(sql)
biz.ErrIsNilAppendErr(err, "获取数据库列信息失败: %s")
res = append(res, result...)
}
return res
}