feature: 将数据库实例管理集成到数据库管理模块中

This commit is contained in:
kanzihuang
2023-08-30 22:41:42 +08:00
parent f7b685cfad
commit 86ad183c41
22 changed files with 446 additions and 490 deletions

View File

@@ -1,53 +1,22 @@
<template>
<div>
<el-dialog :title="title" v-model="dialogVisible" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
<el-dialog :title="title" v-model="dialogVisible" @open="open" :before-close="cancel" :close-on-click-modal="false" :destroy-on-close="true" width="38%">
<el-form :model="form" ref="dbForm" :rules="rules" label-width="auto">
<el-tabs v-model="tabActiveName">
<el-tab-pane label="基础信息" name="basic">
<el-form-item prop="tagId" label="标签:" required>
<tag-select v-model="form.tagId" v-model:tag-path="form.tagPath" style="width: 100%" />
</el-form-item>
<el-form-item prop="instanceId" label="数据库实例:" required>
<el-select :disabled="form.id !== undefined" @change="getAllDatabase" v-model="form.instanceId" placeholder="请选择实例" filterable clearable style="width: 200px">
<el-option v-for="item in state.instances" :key="item.id" :label="item.name" :value="item.id"> </el-option>
</el-select>
</el-form-item>
<el-form-item prop="name" label="别名:" required>
<el-input v-model.trim="form.name" placeholder="请输入数据库别名" auto-complete="off"></el-input>
</el-form-item>
<el-form-item prop="type" label="类型:" required>
<el-select style="width: 100%" v-model="form.type" placeholder="请选择数据库类型">
<el-option key="item.id" label="mysql" value="mysql"> </el-option>
<el-option key="item.id" label="postgres" value="postgres"> </el-option>
</el-select>
</el-form-item>
<el-form-item prop="host" label="host:" required>
<el-col :span="18">
<el-input :disabled="form.id !== undefined" v-model.trim="form.host" placeholder="请输入主机ip" auto-complete="off"></el-input>
</el-col>
<el-col style="text-align: center" :span="1">:</el-col>
<el-col :span="5">
<el-input type="number" v-model.number="form.port" placeholder="请输入端口"></el-input>
</el-col>
</el-form-item>
<el-form-item prop="username" label="用户名:" required>
<el-input v-model.trim="form.username" placeholder="请输入用户名"></el-input>
</el-form-item>
<el-form-item prop="password" label="密码:">
<el-input
type="password"
show-password
v-model.trim="form.password"
placeholder="请输入密码,修改操作可不填"
autocomplete="new-password"
>
<template v-if="form.id && form.id != 0" #suffix>
<el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
<template #reference>
<el-link @click="getDbPwd" :underline="false" type="primary" class="mr5">原密码 </el-link>
</template>
</el-popover>
</template>
</el-input>
</el-form-item>
<el-form-item prop="database" label="数据库名:" required>
<el-col :span="19">
<el-select
@change="changeDatabase"
v-model="databaseList"
@@ -62,41 +31,11 @@
>
<el-option v-for="db in allDatabases" :key="db" :label="db" :value="db" />
</el-select>
</el-col>
<el-col style="text-align: center" :span="1">
<el-divider direction="vertical" border-style="dashed" />
</el-col>
<el-col :span="4">
<el-link @click="getAllDatabase" :underline="false" type="success">获取库名</el-link>
</el-col>
</el-form-item>
<el-form-item prop="remark" label="备注:">
<el-input v-model.trim="form.remark" auto-complete="off" type="textarea"></el-input>
</el-form-item>
</el-tab-pane>
<el-tab-pane label="其他配置" name="other">
<el-form-item prop="params" label="连接参数:">
<el-input v-model.trim="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2">
<template #suffix>
<el-link
target="_blank"
href="https://github.com/go-sql-driver/mysql#parameters"
:underline="false"
type="primary"
class="mr5"
>参数参考</el-link
>
</template>
</el-input>
</el-form-item>
<el-form-item prop="sshTunnelMachineId" label="SSH隧道:">
<ssh-tunnel-select v-model="form.sshTunnelMachineId" />
</el-form-item>
</el-tab-pane>
</el-tabs>
</el-form>
<template #footer>
@@ -113,10 +52,7 @@
import { toRefs, reactive, watch, ref } from 'vue';
import { dbApi } from './api';
import { ElMessage } from 'element-plus';
import { notBlank } from '@/common/assert';
import { RsaEncrypt } from '@/common/rsa';
import TagSelect from '../component/TagSelect.vue';
import SshTunnelSelect from '../component/SshTunnelSelect.vue';
const props = defineProps({
visible: {
@@ -141,6 +77,15 @@ const rules = {
trigger: ['change', 'blur'],
},
],
instanceId: [
{
required: true,
message: '请选择数据库实例',
trigger: ['change', 'blur'],
},
],
name: [
{
required: true,
@@ -148,27 +93,6 @@ const rules = {
trigger: ['change', 'blur'],
},
],
type: [
{
required: true,
message: '请选择数据库类型',
trigger: ['change', 'blur'],
},
],
host: [
{
required: true,
message: '请输入主机ip和port',
trigger: ['change', 'blur'],
},
],
username: [
{
required: true,
message: '请输入用户名',
trigger: ['change', 'blur'],
},
],
database: [
{
required: true,
@@ -182,43 +106,34 @@ const dbForm: any = ref(null);
const state = reactive({
dialogVisible: false,
tabActiveName: 'basic',
allDatabases: [] as any,
databaseList: [] as any,
form: {
id: null,
tagId: null as any,
tagPath: null as any,
type: null,
name: null,
host: '',
port: 3306,
username: null,
password: null,
params: null,
database: '',
remark: '',
sshTunnelMachineId: null as any,
instanceId: null as any,
},
// 原密码
pwd: '',
btnLoading: false,
instances: [] as any,
});
const { dialogVisible, tabActiveName, allDatabases, databaseList, form, pwd, btnLoading } = toRefs(state);
const { dialogVisible, allDatabases, databaseList, form, btnLoading } = toRefs(state);
watch(props, (newValue: any) => {
state.dialogVisible = newValue.visible;
if (!state.dialogVisible) {
return;
}
state.tabActiveName = 'basic';
if (newValue.db) {
state.form = { ...newValue.db };
// 将数据库名使用空格切割,获取所有数据库列表
state.databaseList = newValue.db.database.split(' ');
} else {
state.form = { port: 3306 } as any;
state.form = {} as any;
state.databaseList = [];
}
});
@@ -232,26 +147,26 @@ const changeDatabase = () => {
const getAllDatabase = async () => {
const reqForm = { ...state.form };
reqForm.password = await RsaEncrypt(reqForm.password);
if (state.form.instanceId > 0) {
state.allDatabases = await dbApi.getAllDatabase.request(reqForm);
ElMessage.success('获取成功, 请选择需要管理操作的数据库');
}
};
const getDbPwd = async () => {
state.pwd = await dbApi.getDbPwd.request({ id: state.form.id });
const getAllInstances = async () => {
const data = await dbApi.instances.request(null);
if (data) {
state.instances = data.list;
}
}
const open = async () => {
await getAllInstances()
await getAllDatabase()
};
const btnOk = async () => {
if (!state.form.id) {
notBlank(state.form.password, '新增操作,密码不可为空');
}
dbForm.value.validate(async (valid: boolean) => {
if (valid) {
const reqForm = { ...state.form };
reqForm.password = await RsaEncrypt(reqForm.password);
if (!state.form.sshTunnelMachineId) {
reqForm.sshTunnelMachineId = -1;
}
dbApi.saveDb.request(reqForm).then(() => {
ElMessage.success('保存成功');
emit('val-change', state.form);
@@ -272,6 +187,7 @@ const btnOk = async () => {
const resetInputDb = () => {
state.databaseList = [];
state.allDatabases = [];
state.instances = [];
};
const cancel = () => {

View File

@@ -14,11 +14,17 @@
@pageChange="search()"
>
<template #tagPathSelect>
<el-select @focus="getTags" v-model="query.tagPath" placeholder="请选择标签" @clear="search" filterable clearable style="width: 200px">
<el-select @focus="getTags" v-model="query.tagPath" placeholder="请选择标签" filterable clearable style="width: 200px">
<el-option v-for="item in tags" :key="item" :label="item" :value="item"> </el-option>
</el-select>
</template>
<template #instanceSelect>
<el-select @focus="getInstances" v-model="query.instanceId" placeholder="请选择实例" filterable clearable style="width: 200px">
<el-option v-for="item in instances" :key="item.id" :label="item.name" :value="item.id"> </el-option>
</el-select>
</template>
<template #queryRight>
<el-button v-auth="perms.saveDb" type="primary" icon="plus" @click="editDb(false)">添加</el-button>
<el-button v-auth="perms.delDb" :disabled="selectionData.length < 1" @click="deleteDb()" type="danger" icon="delete">删除</el-button>
@@ -226,30 +232,23 @@
<el-input disabled type="textarea" :autosize="{ minRows: 15, maxRows: 30 }" v-model="ddlDialog.ddl" size="small"> </el-input>
</el-dialog>
<el-dialog v-model="infoDialog.visible">
<el-dialog v-model="infoDialog.visible" :before-close="onBeforeCloseInfoDialog" :close-on-click-modal="false">
<el-descriptions title="详情" :column="3" border>
<el-descriptions-item :span="1.5" label="id">{{ infoDialog.data.id }}</el-descriptions-item>
<el-descriptions-item :span="1.5" label="名称">{{ infoDialog.data.name }}</el-descriptions-item>
<el-descriptions-item :span="3" label="标签路径">{{ infoDialog.data.tagPath }}</el-descriptions-item>
<el-descriptions-item :span="2" label="主机">{{ infoDialog.data.host }}</el-descriptions-item>
<el-descriptions-item :span="1" label="端口">{{ infoDialog.data.port }}</el-descriptions-item>
<el-descriptions-item :span="2" label="用户名">{{ infoDialog.data.username }}</el-descriptions-item>
<el-descriptions-item :span="1" label="类型">{{ infoDialog.data.type }}</el-descriptions-item>
<el-descriptions-item :span="3" label="连接参数">{{ infoDialog.data.params }}</el-descriptions-item>
<el-descriptions-item :span="3" label="备注">{{ infoDialog.data.remark }}</el-descriptions-item>
<el-descriptions-item :span="2" label="名称">{{ infoDialog.data.name }}</el-descriptions-item>
<el-descriptions-item :span="1" label="id">{{ infoDialog.data.id }}</el-descriptions-item>
<el-descriptions-item :span="3" label="数据库">{{ infoDialog.data.database }}</el-descriptions-item>
<el-descriptions-item :span="3" label="SSH隧道">{{ infoDialog.data.sshTunnelMachineId > 0 ? '是' : '否' }} </el-descriptions-item>
<el-descriptions-item :span="3" label="备注">{{ infoDialog.data.remark }}</el-descriptions-item>
<el-descriptions-item :span="2" label="创建时间">{{ dateFormat(infoDialog.data.createTime) }} </el-descriptions-item>
<el-descriptions-item :span="1" label="创建者">{{ infoDialog.data.creator }}</el-descriptions-item>
<el-descriptions-item :span="2" label="更新时间">{{ dateFormat(infoDialog.data.updateTime) }} </el-descriptions-item>
<el-descriptions-item :span="1" label="修改者">{{ infoDialog.data.modifier }}</el-descriptions-item>
<el-descriptions-item :span="3" label="数据库实例名称">{{ infoDialog.instance.name }}</el-descriptions-item>
<el-descriptions-item :span="2" label="主机">{{ infoDialog.instance.host }}</el-descriptions-item>
<el-descriptions-item :span="1" label="端口">{{ infoDialog.instance.port }}</el-descriptions-item>
<el-descriptions-item :span="2" label="用户名">{{ infoDialog.instance.username }}</el-descriptions-item>
<el-descriptions-item :span="1" label="类型">{{ infoDialog.instance.type }}</el-descriptions-item>
</el-descriptions>
</el-dialog>
@@ -292,15 +291,15 @@ const perms = {
delDb: 'db:del',
};
const queryConfig = [TableQuery.slot('tagPath', '标签', 'tagPathSelect')];
const queryConfig = [
TableQuery.slot('tagPath', '标签', 'tagPathSelect'),
TableQuery.slot('instanceId', '实例', 'instanceSelect'),
];
const columns = ref([
TableColumn.new('tagPath', '标签路径').isSlot().setAddWidth(20),
TableColumn.new('name', '名称'),
TableColumn.new('host', 'host:port').setFormatFunc((data: any, _prop: string) => `${data.host}:${data.port}`),
TableColumn.new('type', '类型'),
TableColumn.new('database', '数据库').isSlot().setMinWidth(70),
TableColumn.new('username', '用户名'),
TableColumn.new('remark', '备注'),
TableColumn.new('more', '更多').isSlot().setMinWidth(165).fixedRight(),
]);
@@ -316,6 +315,7 @@ const state = reactive({
dbId: 0,
db: '',
tags: [],
instances: [],
/**
* 选中的数据
*/
@@ -325,6 +325,7 @@ const state = reactive({
*/
query: {
tagPath: null,
instanceId: null,
pageNum: 1,
pageSize: 10,
},
@@ -333,6 +334,10 @@ const state = reactive({
infoDialog: {
visible: false,
data: null as any,
instance: null as any,
query: {
instanceId: 0,
}
},
showDumpInfo: false,
dumpInfo: {
@@ -427,6 +432,7 @@ const {
dbId,
db,
tags,
instances,
selectionData,
query,
datas,
@@ -489,15 +495,31 @@ const search = async () => {
}
};
const showInfo = (info: any) => {
const showInfo = async (info: any) => {
state.infoDialog.data = info;
state.infoDialog.query.instanceId = info.instanceId;
const res = await dbApi.getInstance.request(state.infoDialog.query);
state.infoDialog.instance = res;
state.infoDialog.visible = true;
};
const onBeforeCloseInfoDialog = () => {
state.infoDialog.visible = false;
state.infoDialog.data = null;
state.infoDialog.instance = null;
};
const getTags = async () => {
state.tags = await dbApi.dbTags.request(null);
};
const getInstances = async () => {
const data = await dbApi.instances.request(null);
if (data) {
state.instances = data.list;
}
};
const editDb = async (data: any) => {
if (!data) {
state.dbEditDialog.data = null;
@@ -527,7 +549,7 @@ const deleteDb = async () => {
};
const onShowSqlExec = async (row: any) => {
state.sqlExecLogDialog.title = `${row.name}[${row.host}:${row.port}]`;
state.sqlExecLogDialog.title = `${row.name}`;
state.sqlExecLogDialog.query.dbId = row.id;
state.sqlExecLogDialog.dbs = row.database.split(' ');
searchSqlExecLog();

View File

@@ -30,17 +30,9 @@
type="password"
show-password
v-model.trim="form.password"
placeholder="请输入密码,修改操作可不填"
placeholder="请输入密码"
autocomplete="new-password"
>
<template v-if="form.id && form.id != 0" #suffix>
<el-popover @hide="pwd = ''" placement="right" title="原密码" :width="200" trigger="click" :content="pwd">
<template #reference>
<el-link @click="getDbPwd" :underline="false" type="primary" class="mr5">原密码 </el-link>
</template>
</el-popover>
</template>
</el-input>
/>
</el-form-item>
<el-form-item prop="remark" label="备注:">
@@ -94,7 +86,7 @@ const props = defineProps({
visible: {
type: Boolean,
},
db: {
data: {
type: [Boolean, Object],
},
title: {
@@ -155,6 +147,8 @@ const state = reactive({
},
// 原密码
pwd: '',
// 原用户名
oldUserName: null,
btnLoading: false,
});
@@ -166,10 +160,12 @@ watch(props, (newValue: any) => {
return;
}
state.tabActiveName = 'basic';
if (newValue.db) {
state.form = { ...newValue.db };
if (newValue.data) {
state.form = { ...newValue.data};
state.oldUserName = state.form.username
} else {
state.form = { port: 3306 } as any;
state.oldUserName = null
}
});
@@ -180,7 +176,10 @@ const getDbPwd = async () => {
const btnOk = async () => {
if (!state.form.id) {
notBlank(state.form.password, '新增操作,密码不可为空');
} else if (state.form.username != state.oldUserName) {
notBlank(state.form.password, '已修改用户名,请输入密码');
}
dbForm.value.validate(async (valid: boolean) => {
if (valid) {
const reqForm = { ...state.form };

View File

@@ -50,7 +50,7 @@
</el-descriptions>
</el-dialog>
<instance-edit @val-change="valChange" :title="instanceEditDialog.title" v-model:visible="instanceEditDialog.visible" v-model:db="instanceEditDialog.data"></instance-edit>
<instance-edit @val-change="valChange" :title="instanceEditDialog.title" v-model:visible="instanceEditDialog.visible" v-model:data="instanceEditDialog.data"></instance-edit>
</div>
</template>
@@ -167,7 +167,7 @@ const valChange = () => {
const deleteInstance = async () => {
try {
await ElMessageBox.confirm(`确定删除【${state.selectionData.map((x: any) => x.name).join(', ')}实例?`, '提示', {
await ElMessageBox.confirm(`确定删除数据库实例${state.selectionData.map((x: any) => x.name).join(', ')}】?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',

View File

@@ -5,8 +5,6 @@ export const dbApi = {
dbs: Api.newGet('/dbs'),
dbTags: Api.newGet('/dbs/tags'),
saveDb: Api.newPost('/dbs'),
getAllDatabase: Api.newPost('/dbs/databases'),
getDbPwd: Api.newGet('/dbs/{id}/pwd'),
deleteDb: Api.newDelete('/dbs/{id}'),
dumpDb: Api.newPost('/dbs/{id}/dump'),
tableInfos: Api.newGet('/dbs/{id}/t-infos'),
@@ -29,6 +27,8 @@ export const dbApi = {
// 获取权限列表
instances: Api.newGet('/instances'),
getInstance: Api.newGet("/instances/{instanceId}"),
getAllDatabase: Api.newGet('/instances/{instanceId}/databases'),
saveInstance: Api.newPost('/instances'),
getInstancePwd: Api.newGet('/instances/{id}/pwd'),
deleteInstance: Api.newDelete('/instances/{id}'),

View File

@@ -14,9 +14,7 @@ import (
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/cryptox"
"mayfly-go/pkg/utils/stringx"
"mayfly-go/pkg/utils/structx"
"mayfly-go/pkg/ws"
"strconv"
"strings"
@@ -27,6 +25,7 @@ import (
)
type Db struct {
InstanceApp application.Instance
DbApp application.Db
DbSqlExecApp application.DbSqlExec
MsgApp msgapp.Msg
@@ -58,46 +57,22 @@ func (d *Db) Save(rc *req.Ctx) {
form := &form.DbForm{}
db := ginx.BindJsonAndCopyTo[*entity.Db](rc.GinCtx, form, new(entity.Db))
// 密码解密,并使用解密后的赋值
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
db.Password = originPwd
// 密码脱敏记录日志
form.Password = "****"
rc.ReqParam = form
db.SetBaseInfo(rc.LoginAccount)
d.DbApp.Save(db)
}
// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
func (d *Db) GetDbPwd(rc *req.Ctx) {
dbId := GetDbId(rc.GinCtx)
dbEntity := d.DbApp.GetById(dbId, "Password")
dbEntity.PwdDecrypt()
rc.ResData = dbEntity.Password
}
// 获取数据库实例的所有数据库名
func (d *Db) GetDatabaseNames(rc *req.Ctx) {
form := &form.DbForm{}
ginx.BindJsonAndValid(rc.GinCtx, form)
db := new(entity.Db)
structx.Copy(db, form)
// 密码解密,并使用解密后的赋值
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")
db.Password = originPwd
// 如果id不为空并且密码为空则从数据库查询
if form.Id != 0 && db.Password == "" {
db = d.DbApp.GetById(form.Id)
db.PwdDecrypt()
}
rc.ResData = d.DbApp.GetDatabases(db)
instance := d.InstanceApp.GetById(form.InstanceId, "Password")
biz.NotNil(instance, "获取数据库实例错误")
instance.PwdDecrypt()
rc.ResData = d.InstanceApp.GetDatabases(instance)
rc.ResData = d.InstanceApp.GetDatabases(instance)
}
func (d *Db) DeleteDb(rc *req.Ctx) {
@@ -115,20 +90,29 @@ func (d *Db) DeleteDb(rc *req.Ctx) {
}
}
func (d *Db) getDbInstance(g *gin.Context) *application.DbInstance {
dbName := g.Query("db")
biz.NotEmpty(dbName, "db不能为空")
dbId := getDbId(g)
db := d.DbApp.GetById(dbId)
instance := d.InstanceApp.GetById(db.InstanceId)
return d.DbApp.GetDbInstance(db, instance, dbName)
}
func (d *Db) TableInfos(rc *req.Ctx) {
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableInfos()
rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetTableInfos()
}
func (d *Db) TableIndex(rc *req.Ctx) {
tn := rc.GinCtx.Query("tableName")
biz.NotEmpty(tn, "tableName不能为空")
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableIndex(tn)
rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetTableIndex(tn)
}
func (d *Db) GetCreateTableDdl(rc *req.Ctx) {
tn := rc.GinCtx.Query("tableName")
biz.NotEmpty(tn, "tableName不能为空")
rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetCreateTableDdl(tn)
rc.ResData = d.getDbInstance(rc.GinCtx).GetMeta().GetCreateTableDdl(tn)
}
func (d *Db) ExecSql(rc *req.Ctx) {
@@ -136,9 +120,10 @@ func (d *Db) ExecSql(rc *req.Ctx) {
form := &form.DbSqlExecForm{}
ginx.BindJsonAndValid(g, form)
id := GetDbId(g)
db := form.Db
dbInstance := d.DbApp.GetDbInstance(id, db)
dbId := getDbId(g)
db := d.DbApp.GetById(dbId)
instance := d.InstanceApp.GetById(db.InstanceId)
dbInstance := d.DbApp.GetDbInstance(db, instance, form.Db)
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
rc.ReqParam = fmt.Sprintf("%s\n-> %s", dbInstance.Info.GetLogDesc(), form.Sql)
@@ -148,8 +133,8 @@ func (d *Db) ExecSql(rc *req.Ctx) {
sql := stringx.TrimSpaceAndBr(form.Sql)
execReq := &application.DbSqlExecReq{
DbId: id,
Db: db,
DbId: dbId,
Db: form.Db,
Remark: form.Remark,
DbInstance: dbInstance,
LoginAccount: rc.LoginAccount,
@@ -192,9 +177,10 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
file, _ := fileheader.Open()
filename := fileheader.Filename
dbId, db := GetIdAndDb(g)
dbId := getDbId(g)
dbName := getDbName(g)
dbInstance := d.DbApp.GetDbInstance(dbId, db)
dbInstance := d.getDbInstance(rc.GinCtx)
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
rc.ReqParam = fmt.Sprintf("%s -> filename: %s", dbInstance.Info.GetLogDesc(), filename)
@@ -216,7 +202,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
execReq := &application.DbSqlExecReq{
DbId: dbId,
Db: db,
Db: dbName,
Remark: fileheader.Filename,
DbInstance: dbInstance,
LoginAccount: rc.LoginAccount,
@@ -249,7 +235,7 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
// 数据库dump
func (d *Db) DumpSql(rc *req.Ctx) {
g := rc.GinCtx
dbId, db := GetIdAndDb(g)
db := getDbName(g)
dumpType := g.Query("type")
tablesStr := g.Query("tables")
biz.NotEmpty(tablesStr, "请选择要导出的表")
@@ -260,7 +246,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
// 是否需要导出数据
needData := dumpType == "2" || dumpType == "3"
dbInstance := d.DbApp.GetDbInstance(dbId, db)
dbInstance := d.getDbInstance(rc.GinCtx)
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.LoginAccount.Id, dbInstance.Info.TagPath), "%s")
now := time.Now()
@@ -275,7 +261,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
writer.WriteString(fmt.Sprintf("\n-- 导出数据库: %s ", db))
writer.WriteString("\n-- ----------------------------\n")
dbmeta := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta()
dbmeta := d.getDbInstance(rc.GinCtx).GetMeta()
for _, table := range tables {
if needStruct {
writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表结构: %s \n-- ----------------------------\n", table))
@@ -329,7 +315,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
// @router /api/db/:dbId/t-metadata [get]
func (d *Db) TableMA(rc *req.Ctx) {
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
dbi := d.getDbInstance(rc.GinCtx)
rc.ResData = dbi.GetMeta().GetTables()
}
@@ -339,13 +325,13 @@ func (d *Db) ColumnMA(rc *req.Ctx) {
tn := g.Query("tableName")
biz.NotEmpty(tn, "tableName不能为空")
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
dbi := d.getDbInstance(rc.GinCtx)
rc.ResData = dbi.GetMeta().GetColumns(tn)
}
// @router /api/db/:dbId/hint-tables [get]
func (d *Db) HintTables(rc *req.Ctx) {
dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx))
dbi := d.getDbInstance(rc.GinCtx)
dm := dbi.GetMeta()
// 获取所有表
@@ -391,7 +377,7 @@ func (d *Db) SaveSql(rc *req.Ctx) {
ginx.BindJsonAndValid(g, dbSqlForm)
rc.ReqParam = dbSqlForm
dbId := GetDbId(g)
dbId := getDbId(g)
// 判断dbId是否存在
err := gormx.GetById(new(entity.Db), dbId)
biz.ErrIsNil(err, "该数据库信息不存在")
@@ -413,9 +399,10 @@ func (d *Db) SaveSql(rc *req.Ctx) {
// 获取所有保存的sql names
func (d *Db) GetSqlNames(rc *req.Ctx) {
id, db := GetIdAndDb(rc.GinCtx)
dbId := getDbId(rc.GinCtx)
dbName := getDbName(rc.GinCtx)
// 获取用于是否有该dbsql的保存记录有则更改否则新增
dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
dbSql.CreatorId = rc.LoginAccount.Id
var sqls []entity.DbSql
gormx.ListBy(dbSql, &sqls, "id", "name")
@@ -425,7 +412,7 @@ func (d *Db) GetSqlNames(rc *req.Ctx) {
// 删除保存的sql
func (d *Db) DeleteSql(rc *req.Ctx) {
dbSql := &entity.DbSql{Type: 1, DbId: GetDbId(rc.GinCtx)}
dbSql := &entity.DbSql{Type: 1, DbId: getDbId(rc.GinCtx)}
dbSql.CreatorId = rc.LoginAccount.Id
dbSql.Name = rc.GinCtx.Query("name")
dbSql.Db = rc.GinCtx.Query("db")
@@ -436,9 +423,10 @@ func (d *Db) DeleteSql(rc *req.Ctx) {
// @router /api/db/:dbId/sql [get]
func (d *Db) GetSql(rc *req.Ctx) {
id, db := GetIdAndDb(rc.GinCtx)
dbId := getDbId(rc.GinCtx)
dbName := getDbName(rc.GinCtx)
// 根据创建者id 数据库id以及sql模板名称查询保存的sql信息
dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db}
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
dbSql.CreatorId = rc.LoginAccount.Id
dbSql.Name = rc.GinCtx.Query("name")
@@ -449,14 +437,14 @@ func (d *Db) GetSql(rc *req.Ctx) {
rc.ResData = dbSql
}
func GetDbId(g *gin.Context) uint64 {
func getDbId(g *gin.Context) uint64 {
dbId, _ := strconv.Atoi(g.Param("dbId"))
biz.IsTrue(dbId > 0, "dbId错误")
return uint64(dbId)
}
func GetIdAndDb(g *gin.Context) (uint64, string) {
func getDbName(g *gin.Context) string {
db := g.Query("db")
biz.NotEmpty(db, "db不能为空")
return GetDbId(g), db
return db
}

View File

@@ -3,17 +3,11 @@ package form
type DbForm struct {
Id uint64 `json:"id"`
Name string `binding:"required" json:"name"`
Type string `binding:"required" json:"type"` // 类型mysql oracle等
Host string `binding:"required" json:"host"`
Port int `binding:"required" json:"port"`
Username string `binding:"required" json:"username"`
Password string `json:"password"`
Params string `json:"params"`
Database string `json:"database"`
Remark string `json:"remark"`
TagId uint64 `binding:"required" json:"tagId"`
TagPath string `binding:"required" json:"tagPath"`
SshTunnelMachineId int `json:"sshTunnelMachineId"`
InstanceId uint64 `binding:"required" json:"instanceId"`
}
type DbSqlSaveForm struct {

View File

@@ -17,15 +17,19 @@ import (
type Instance struct {
InstanceApp application.Instance
DbApp application.Db
MsgApp msgapp.Msg
}
// Instances 获取数据库实例信息
// @router /api/instances [get]
func (d *Instance) Instances(rc *req.Ctx) {
queryCond, page := ginx.BindQueryAndPage[*entity.InstanceQuery](rc.GinCtx, new(entity.InstanceQuery))
rc.ResData = d.InstanceApp.GetPageList(queryCond, page, new([]vo.SelectDataInstanceVO))
}
// SaveInstance 保存数据库实例信息
// @router /api/instances [post]
func (d *Instance) SaveInstance(rc *req.Ctx) {
form := &form.InstanceForm{}
instance := ginx.BindJsonAndCopyTo[*entity.Instance](rc.GinCtx, form, new(entity.Instance))
@@ -43,29 +47,57 @@ func (d *Instance) SaveInstance(rc *req.Ctx) {
d.InstanceApp.Save(instance)
}
// 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
func (d *Instance) GetInstancePwd(rc *req.Ctx) {
dbId := GetInstanceId(rc.GinCtx)
dbEntity := d.InstanceApp.GetById(dbId, "Password")
dbEntity.PwdDecrypt()
rc.ResData = dbEntity.Password
// GetInstance 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
// @router /api/instances/:instance [GET]
func (d *Instance) GetInstance(rc *req.Ctx) {
dbId := getInstanceId(rc.GinCtx)
dbEntity := d.InstanceApp.GetById(dbId)
biz.IsTrue(dbEntity != nil, "获取数据库实例错误")
dbEntity.Password = ""
rc.ResData = dbEntity
}
// GetInstancePwd 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
// @router /api/instances/:instance/pwd [GET]
func (d *Instance) GetInstancePwd(rc *req.Ctx) {
instanceId := getInstanceId(rc.GinCtx)
instanceEntity := d.InstanceApp.GetById(instanceId, "Password")
biz.IsTrue(instanceEntity != nil, "获取数据库实例错误")
instanceEntity.PwdDecrypt()
rc.ResData = instanceEntity.Password
}
// DeleteInstance 删除数据库实例信息
// @router /api/instances/:instance [DELETE]
func (d *Instance) DeleteInstance(rc *req.Ctx) {
idsStr := ginx.PathParam(rc.GinCtx, "dbId")
idsStr := ginx.PathParam(rc.GinCtx, "instanceId")
rc.ReqParam = idsStr
ids := strings.Split(idsStr, ",")
for _, v := range ids {
value, err := strconv.Atoi(v)
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
dbId := uint64(value)
d.InstanceApp.Delete(dbId)
instanceId := uint64(value)
if d.DbApp.Count(&entity.DbQuery{InstanceId: instanceId}) != 0 {
instance := d.InstanceApp.GetById(instanceId, "name")
biz.NotNil(instance, "获取数据库实例错误数据库实例ID为%d", instance.Id)
biz.IsTrue(false, "不能删除数据库实例【%s】请先删除其关联的数据库资源。", instance.Name)
}
d.InstanceApp.Delete(instanceId)
}
}
func GetInstanceId(g *gin.Context) uint64 {
dbId, _ := strconv.Atoi(g.Param("dbId"))
biz.IsTrue(dbId > 0, "dbId错误")
return uint64(dbId)
func getInstanceId(g *gin.Context) uint64 {
instanceId, _ := strconv.Atoi(g.Param("instanceId"))
biz.IsTrue(instanceId > 0, "instanceId 错误")
return uint64(instanceId)
}
// 获取数据库实例的所有数据库名
func (d *Instance) GetDatabaseNames(rc *req.Ctx) {
instanceId := getInstanceId(rc.GinCtx)
instance := d.InstanceApp.GetById(instanceId, "Password")
biz.IsTrue(instance != nil, "获取数据库实例错误")
instance.PwdDecrypt()
rc.ResData = d.InstanceApp.GetDatabases(instance)
}

View File

@@ -6,22 +6,19 @@ type SelectDataDbVO struct {
//models.BaseModel
Id *int64 `json:"id"`
Name *string `json:"name"`
Host *string `json:"host"`
Port *int `json:"port"`
Type *string `json:"type"`
Params *string `json:"params"`
Database *string `json:"database"`
Username *string `json:"username"`
Remark *string `json:"remark"`
TagId *int64 `json:"tagId"`
TagPath *string `json:"tagPath"`
InstanceId *int64 `json:"instanceId"`
InstanceName *string `json:"instanceName"`
InstanceType *string `json:"type"`
CreateTime *time.Time `json:"createTime"`
Creator *string `json:"creator"`
CreatorId *int64 `json:"creatorId"`
UpdateTime *time.Time `json:"updateTime"`
Modifier *string `json:"modifier"`
ModifierId *int64 `json:"modifierId"`
SshTunnelMachineId int `json:"sshTunnelMachineId"`
}

View File

@@ -12,7 +12,6 @@ import (
"mayfly-go/pkg/logx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/structx"
"reflect"
"strconv"
"strings"
@@ -40,10 +39,7 @@ type Db interface {
// 获取数据库连接实例
// @param id 数据库实例id
// @param db 数据库
GetDbInstance(id uint64, db string) *DbInstance
// 获取数据库实例的所有数据库列表
GetDatabases(entity *entity.Db) []string
GetDbInstance(db *entity.Db, instance *entity.Instance, dbName string) *DbInstance
}
func newDbApp(dbRepo repository.Db, dbSqlRepo repository.DbSql) Db {
@@ -78,32 +74,19 @@ func (d *dbAppImpl) GetById(id uint64, cols ...string) *entity.Db {
}
func (d *dbAppImpl) Save(dbEntity *entity.Db) {
// 默认tcp连接
dbEntity.Network = dbEntity.GetNetwork()
// 测试连接
if dbEntity.Password != "" {
TestConnection(dbEntity)
}
// 查找是否存在该库
oldDb := &entity.Db{Host: dbEntity.Host, Port: dbEntity.Port, Username: dbEntity.Username}
if dbEntity.SshTunnelMachineId > 0 {
oldDb.SshTunnelMachineId = dbEntity.SshTunnelMachineId
}
oldDb := &entity.Db{Name: dbEntity.Name}
err := d.GetDbBy(oldDb)
if dbEntity.Id == 0 {
biz.NotEmpty(dbEntity.Password, "密码不能为空")
biz.IsTrue(err != nil, "该数据库实例已存在")
dbEntity.PwdEncrypt()
biz.IsTrue(err != nil, "该数据库资源已存在")
d.dbRepo.Insert(dbEntity)
return
}
// 如果存在该库,则校验修改的库是否为该库
if err == nil {
biz.IsTrue(oldDb.Id == dbEntity.Id, "该数据库实例已存在")
biz.IsTrue(oldDb.Id == dbEntity.Id, "该数据库资源已存在")
}
dbId := dbEntity.Id
@@ -129,7 +112,6 @@ func (d *dbAppImpl) Save(dbEntity *entity.Db) {
d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: dbId, Db: v.(string)})
}
dbEntity.PwdEncrypt()
d.dbRepo.Update(dbEntity)
}
@@ -145,39 +127,13 @@ func (d *dbAppImpl) Delete(id uint64) {
d.dbSqlRepo.DeleteBy(&entity.DbSql{DbId: id})
}
func (d *dbAppImpl) GetDatabases(ed *entity.Db) []string {
ed.Network = ed.GetNetwork()
databases := make([]string, 0)
var dbConn *sql.DB
var metaDb string
var getDatabasesSql string
if ed.Type == entity.DbTypeMysql {
metaDb = "information_schema"
getDatabasesSql = "SELECT SCHEMA_NAME AS dbname FROM SCHEMATA"
} else {
metaDb = "postgres"
getDatabasesSql = "SELECT datname AS dbname FROM pg_database"
}
dbConn, err := GetDbConn(ed, metaDb)
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
defer dbConn.Close()
_, res, err := SelectDataByDb(dbConn, getDatabasesSql)
biz.ErrIsNilAppendErr(err, "获取数据库列表失败")
for _, re := range res {
databases = append(databases, re["dbname"].(string))
}
return databases
}
var mutex sync.Mutex
func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
cacheKey := GetDbCacheKey(id, db)
func (d *dbAppImpl) GetDbInstance(db *entity.Db, instance *entity.Instance, dbName string) *DbInstance {
cacheKey := GetDbCacheKey(db.Id, dbName)
// Id不为0则为需要缓存
needCache := id != 0
needCache := db.Id != 0
if needCache {
load, ok := dbCache.Get(cacheKey)
if ok {
@@ -187,33 +143,32 @@ func (da *dbAppImpl) GetDbInstance(id uint64, db string) *DbInstance {
mutex.Lock()
defer mutex.Unlock()
d := da.GetById(id)
biz.NotNil(d, "数据库信息不存在")
biz.IsTrue(strings.Contains(d.Database, db), "未配置该库的操作权限")
// 密码解密
d.PwdDecrypt()
biz.NotNil(db, "数据库信息不存在")
biz.IsTrue(strings.Contains(" "+db.Database+" ", " "+dbName+" "), "未配置该库的操作权限")
dbInfo := new(DbInfo)
structx.Copy(dbInfo, d)
dbInfo.Database = db
// 密码解密
instance.PwdDecrypt()
dbInfo := NewDbInfo(db, instance)
dbInfo.Database = dbName
dbi := &DbInstance{Id: cacheKey, Info: dbInfo}
DB, err := GetDbConn(d, db)
conn, err := getInstanceConn(instance, dbName)
if err != nil {
dbi.Close()
logx.Errorf("连接db失败: %s:%d/%s", d.Host, d.Port, db)
logx.Errorf("连接db失败: %s:%d/%s", dbInfo.Host, dbInfo.Port, dbName)
panic(biz.NewBizErr(fmt.Sprintf("数据库连接失败: %s", err.Error())))
}
// 最大连接周期超过时间的连接就close
// DB.SetConnMaxLifetime(100 * time.Second)
// conn.SetConnMaxLifetime(100 * time.Second)
// 设置最大连接数
DB.SetMaxOpenConns(5)
conn.SetMaxOpenConns(5)
// 设置闲置连接数
DB.SetMaxIdleConns(1)
conn.SetMaxIdleConns(1)
dbi.db = DB
logx.Infof("连接db: %s:%d/%s", d.Host, d.Port, db)
dbi.db = conn
logx.Infof("连接db: %s:%d/%s", dbInfo.Host, dbInfo.Port, dbName)
if needCache {
dbCache.Put(cacheKey, dbi)
}
@@ -235,6 +190,19 @@ type DbInfo struct {
SshTunnelMachineId int
}
func NewDbInfo(db *entity.Db, instance *entity.Instance) *DbInfo {
return &DbInfo{
Id: db.Id,
Name: db.Name,
Type: instance.Type,
Host: instance.Host,
Port: instance.Port,
Username: instance.Username,
TagPath: db.TagPath,
SshTunnelMachineId: instance.SshTunnelMachineId,
}
}
// 获取记录日志的描述
func (d *DbInfo) GetLogDesc() string {
return fmt.Sprintf("DB[id=%d, tag=%s, name=%s, ip=%s:%d, database=%s]", d.Id, d.TagPath, d.Name, d.Host, d.Port, d.Database)
@@ -325,35 +293,6 @@ func GetDbInstanceByCache(id string) *DbInstance {
return nil
}
func TestConnection(d *entity.Db) {
// 验证第一个库是否可以连接即可
DB, err := GetDbConn(d, strings.Split(d.Database, " ")[0])
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
defer DB.Close()
}
// 获取数据库连接
func GetDbConn(d *entity.Db, db string) (*sql.DB, error) {
var DB *sql.DB
var err error
if d.Type == entity.DbTypeMysql {
DB, err = getMysqlDB(d, db)
} else if d.Type == entity.DbTypePostgres {
DB, err = getPgsqlDB(d, db)
}
if err != nil {
return nil, err
}
err = DB.Ping()
if err != nil {
DB.Close()
return nil, err
}
return DB, nil
}
func SelectDataByDb(db *sql.DB, selectSql string) ([]string, []map[string]any, error) {
rows, err := db.Query(selectSql)
if err != nil {

View File

@@ -1,6 +1,7 @@
package application
import (
"database/sql"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/biz"
@@ -8,21 +9,24 @@ import (
)
type Instance interface {
// 分页获取
// GetPageList 分页获取数据库实例
GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
Count(condition *entity.InstanceQuery) int64
// 根据条件获取
// GetInstanceBy 根据条件获取数据库实例
GetInstanceBy(condition *entity.Instance, cols ...string) error
// 根据id获取
// GetById 根据id获取数据库实例
GetById(id uint64, cols ...string) *entity.Instance
Save(instanceEntity *entity.Instance)
// 删除数据库信息
// Delete 删除数据库信息
Delete(id uint64)
// GetDatabases 获取数据库实例的所有数据库列表
GetDatabases(entity *entity.Instance) []string
}
func newInstanceApp(InstanceRepo repository.Instance) Instance {
@@ -35,34 +39,33 @@ type instanceAppImpl struct {
instanceRepo repository.Instance
}
// 分页获取数据库信息列表
func (d *instanceAppImpl) GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return d.instanceRepo.GetInstanceList(condition, pageParam, toEntity, orderBy...)
// GetPageList 分页获取数据库实例
func (app *instanceAppImpl) GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return app.instanceRepo.GetInstanceList(condition, pageParam, toEntity, orderBy...)
}
func (d *instanceAppImpl) Count(condition *entity.InstanceQuery) int64 {
return d.instanceRepo.Count(condition)
func (app *instanceAppImpl) Count(condition *entity.InstanceQuery) int64 {
return app.instanceRepo.Count(condition)
}
// 根据条件获取
func (d *instanceAppImpl) GetInstanceBy(condition *entity.Instance, cols ...string) error {
return d.instanceRepo.GetInstance(condition, cols...)
// GetInstanceBy 根据条件获取数据库实例
func (app *instanceAppImpl) GetInstanceBy(condition *entity.Instance, cols ...string) error {
return app.instanceRepo.GetInstance(condition, cols...)
}
// 根据id获取
func (d *instanceAppImpl) GetById(id uint64, cols ...string) *entity.Instance {
return d.instanceRepo.GetById(id, cols...)
// GetById 根据id获取数据库实例
func (app *instanceAppImpl) GetById(id uint64, cols ...string) *entity.Instance {
return app.instanceRepo.GetById(id, cols...)
}
func (d *instanceAppImpl) Save(instanceEntity *entity.Instance) {
func (app *instanceAppImpl) Save(instanceEntity *entity.Instance) {
// 默认tcp连接
instanceEntity.Network = instanceEntity.GetNetwork()
// 测试连接
// todo 测试数据库连接
//if instanceEntity.Password != "" {
// TestConnection(instanceEntity)
//}
if instanceEntity.Password != "" {
testConnection(instanceEntity)
}
// 查找是否存在该库
oldInstance := &entity.Instance{Host: instanceEntity.Host, Port: instanceEntity.Port, Username: instanceEntity.Username}
@@ -70,23 +73,77 @@ func (d *instanceAppImpl) Save(instanceEntity *entity.Instance) {
oldInstance.SshTunnelMachineId = instanceEntity.SshTunnelMachineId
}
err := d.GetInstanceBy(oldInstance)
err := app.GetInstanceBy(oldInstance)
if instanceEntity.Id == 0 {
biz.NotEmpty(instanceEntity.Password, "密码不能为空")
biz.IsTrue(err != nil, "该数据库实例已存在")
instanceEntity.PwdEncrypt()
d.instanceRepo.Insert(instanceEntity)
app.instanceRepo.Insert(instanceEntity)
} else {
// 如果存在该库,则校验修改的库是否为该库
if err == nil {
biz.IsTrue(oldInstance.Id == instanceEntity.Id, "该数据库实例已存在")
}
instanceEntity.PwdEncrypt()
d.instanceRepo.Update(instanceEntity)
app.instanceRepo.Update(instanceEntity)
}
}
func (d *instanceAppImpl) Delete(id uint64) {
// todo 删除数据库库实例前必须删除关联数据库
d.instanceRepo.Delete(id)
func (app *instanceAppImpl) Delete(id uint64) {
app.instanceRepo.Delete(id)
}
// getInstanceConn 获取数据库连接数据库实例
func getInstanceConn(instance *entity.Instance, db string) (*sql.DB, error) {
var conn *sql.DB
var err error
if instance.Type == entity.DbTypeMysql {
conn, err = getMysqlDB(instance, db)
} else if instance.Type == entity.DbTypePostgres {
conn, err = getPgsqlDB(instance, db)
}
if err != nil {
return nil, err
}
err = conn.Ping()
if err != nil {
conn.Close()
return nil, err
}
return conn, nil
}
func testConnection(d *entity.Instance) {
// 不指定数据库名称
conn, err := getInstanceConn(d, "")
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
defer conn.Close()
}
func (app *instanceAppImpl) GetDatabases(ed *entity.Instance) []string {
ed.Network = ed.GetNetwork()
databases := make([]string, 0)
var dbConn *sql.DB
var metaDb string
var getDatabasesSql string
if ed.Type == entity.DbTypeMysql {
metaDb = "information_schema"
getDatabasesSql = "SELECT SCHEMA_NAME AS dbname FROM SCHEMATA"
} else {
metaDb = "postgres"
getDatabasesSql = "SELECT datname AS dbname FROM pg_database"
}
dbConn, err := getInstanceConn(ed, metaDb)
biz.ErrIsNilAppendErr(err, "数据库连接失败: %s")
defer dbConn.Close()
_, res, err := SelectDataByDb(dbConn, getDatabasesSql)
biz.ErrIsNilAppendErr(err, "获取数据库列表失败")
for _, re := range res {
databases = append(databases, re["dbname"].(string))
}
return databases
}

View File

@@ -13,7 +13,7 @@ import (
"github.com/go-sql-driver/mysql"
)
func getMysqlDB(d *entity.Db, db string) (*sql.DB, error) {
func getMysqlDB(d *entity.Instance, db string) (*sql.DB, error) {
// SSH Conect
if d.SshTunnelMachineId > 0 {
sshTunnelMachine := machineapp.GetMachineApp().GetSshTunnelMachine(d.SshTunnelMachineId)

View File

@@ -16,7 +16,7 @@ import (
"github.com/lib/pq"
)
func getPgsqlDB(d *entity.Db, db string) (*sql.DB, error) {
func getPgsqlDB(d *entity.Instance, db string) (*sql.DB, error) {
driverName := d.Type
// SSH Conect
if d.SshTunnelMachineId > 0 {
@@ -28,7 +28,11 @@ func getPgsqlDB(d *entity.Db, db string) (*sql.DB, error) {
sql.Drivers()
}
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", d.Host, d.Port, d.Username, d.Password, db)
var dbParam string
if db != "" {
dbParam = "dbname=" + db
}
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s %s sslmode=disable", d.Host, d.Port, d.Username, d.Password, dbParam)
if d.Params != "" {
dsn = fmt.Sprintf("%s %s", dsn, strings.Join(strings.Split(d.Params, "&"), " "))
}

View File

@@ -1,8 +1,6 @@
package entity
import (
"fmt"
"mayfly-go/internal/common/utils"
"mayfly-go/pkg/model"
)
@@ -10,39 +8,9 @@ type Db struct {
model.Model
Name string `orm:"column(name)" json:"name"`
Type string `orm:"column(type)" json:"type"` // 类型mysql oracle等
Host string `orm:"column(host)" json:"host"`
Port int `orm:"column(port)" json:"port"`
Network string `orm:"column(network)" json:"network"`
Username string `orm:"column(username)" json:"username"`
Password string `orm:"column(password)" json:"-"`
Database string `orm:"column(database)" json:"database"`
Params string `json:"params"`
Remark string `json:"remark"`
TagId uint64
TagPath string
SshTunnelMachineId int `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
}
// 获取数据库连接网络, 若没有使用ssh隧道则直接返回。否则返回拼接的网络需要注册至指定dial
func (d *Db) GetNetwork() string {
network := d.Network
if d.SshTunnelMachineId <= 0 {
if network == "" {
return "tcp"
} else {
return network
}
}
return fmt.Sprintf("%s+ssh:%d", d.Type, d.SshTunnelMachineId)
}
func (d *Db) PwdEncrypt() {
// 密码替换为加密后的密码
d.Password = utils.PwdAesEncrypt(d.Password)
}
func (d *Db) PwdDecrypt() {
// 密码替换为解密后的密码
d.Password = utils.PwdAesDecrypt(d.Password)
InstanceId uint64
}

View File

@@ -22,18 +22,13 @@ type DbQuery struct {
model.Model
Name string `orm:"column(name)" json:"name"`
Type string `orm:"column(type)" json:"type"` // 类型mysql oracle等
Host string `orm:"column(host)" json:"host"`
Port int `orm:"column(port)" json:"port"`
Network string `orm:"column(network)" json:"network"`
Username string `orm:"column(username)" json:"username"`
Password string `orm:"column(password)" json:"-"`
Database string `orm:"column(database)" json:"database"`
Params string `json:"params"`
Remark string `json:"remark"`
TagIds []uint64
TagIds []uint64 `orm:"column(tag_id)"`
TagPath string `form:"tagPath"`
InstanceId uint64 `form:"instanceId"`
}
type DbSqlExecQuery struct {

View File

@@ -6,12 +6,12 @@ import (
)
type Db interface {
// 分页获取机器信息列表
// 分页获取数据信息列表
GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
Count(condition *entity.DbQuery) int64
// 根据条件获取账号信息
// 根据条件获取数据库信息
GetDb(condition *entity.Db, cols ...string) error
// 根据id获取

View File

@@ -11,7 +11,7 @@ type Instance interface {
Count(condition *entity.InstanceQuery) int64
// 根据条件获取账号信息
// 根据条件获取实例信息
GetInstance(condition *entity.Instance, cols ...string) error
// 根据id获取

View File

@@ -16,12 +16,17 @@ func newDbRepo() repository.Db {
// 分页获取数据库信息列表
func (d *dbRepoImpl) GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
qd := gormx.NewQuery(new(entity.Db)).
Like("host", condition.Host).
Like("database", condition.Database).
In("tag_id", condition.TagIds).
RLike("tag_path", condition.TagPath).
OrderByAsc("tag_path")
qd := gormx.NewQueryWithTableName("t_db db").
Select("db.*, inst.name instance_name, inst.type instance_type").
Joins("JOIN t_instance inst ON db.instance_id = inst.id").
Eq("db.instance_id", condition.InstanceId).
Like("db.database", condition.Database).
In("db.tag_id", condition.TagIds).
RLike("db.tag_path", condition.TagPath).
Eq0("db."+model.DeletedColumn, model.ModelUndeleted).
Eq0("inst."+model.DeletedColumn, model.ModelUndeleted).
OrderByAsc("db.tag_path")
return gormx.PageQuery(qd, pageParam, toEntity)
}
@@ -30,6 +35,9 @@ func (d *dbRepoImpl) Count(condition *entity.DbQuery) int64 {
if len(condition.TagIds) > 0 {
where["tag_id"] = condition.TagIds
}
if condition.InstanceId > 0 {
where["instance_id"] = condition.InstanceId
}
return gormx.CountByCond(new(entity.Db), where)
}

View File

@@ -39,7 +39,6 @@ func (d *instanceRepoImpl) GetById(id uint64, cols ...string) *entity.Instance {
instance := new(entity.Instance)
if err := gormx.GetById(instance, id, cols...); err != nil {
return nil
}
return instance
}

View File

@@ -14,6 +14,7 @@ func InitDbRouter(router *gin.RouterGroup) {
db := router.Group("dbs")
d := &api.Db{
InstanceApp: application.GetInstanceApp(),
DbApp: application.GetDbApp(),
DbSqlExecApp: application.GetDbSqlExecApp(),
MsgApp: msgapp.GetMsgApp(),
@@ -31,8 +32,6 @@ func InitDbRouter(router *gin.RouterGroup) {
// 获取数据库实例的所有数据库名
req.NewPost("/databases", d.GetDatabaseNames),
req.NewGet(":dbId/pwd", d.GetDbPwd),
req.NewDelete(":dbId", d.DeleteDb).Log(req.NewLogSave("db-删除数据库信息")),
req.NewGet(":dbId/t-infos", d.TableInfos),

View File

@@ -14,6 +14,7 @@ func InitInstanceRouter(router *gin.RouterGroup) {
d := &api.Instance{
InstanceApp: application.GetInstanceApp(),
DbApp: application.GetDbApp(),
MsgApp: msgapp.GetMsgApp(),
}
@@ -23,9 +24,12 @@ func InitInstanceRouter(router *gin.RouterGroup) {
req.NewPost("", d.SaveInstance).Log(req.NewLogSave("db-保存数据库实例信息")),
req.NewGet(":dbId/pwd", d.GetInstancePwd),
req.NewGet(":instanceId", d.GetInstance),
req.NewGet(":instanceId/pwd", d.GetInstancePwd),
// 获取数据库实例的所有数据库名
req.NewGet(":instanceId/databases", d.GetDatabaseNames),
req.NewDelete(":dbId", d.DeleteInstance).Log(req.NewLogSave("db-删除数据库实例")),
req.NewDelete(":instanceId", d.DeleteInstance).Log(req.NewLogSave("db-删除数据库实例")),
}
req.BatchSetGroup(instances, reqs[:])

View File

@@ -1,36 +1,61 @@
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_instance
-- ----------------------------
DROP TABLE IF EXISTS `t_instance`;
CREATE TABLE `t_instance` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称',
`host` varchar(100) COLLATE utf8mb4_bin NOT NULL,
`port` int(8) NOT NULL,
`username` varchar(255) COLLATE utf8mb4_bin NOT NULL,
`password` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
`type` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '数据库实例类型(mysql...)',
`params` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '其他连接参数',
`network` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
`ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
`remark` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '备注,描述等',
`create_time` datetime DEFAULT NULL,
`creator_id` bigint(20) DEFAULT NULL,
`creator` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`modifier_id` bigint(20) DEFAULT NULL,
`modifier` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
`is_deleted` tinyint(8) NOT NULL DEFAULT '0',
`delete_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库实例信息表';
-- ----------------------------
-- Records of t_instance
-- ----------------------------
BEGIN;
COMMIT;
-- ----------------------------
-- Table structure for t_db
-- ----------------------------
DROP TABLE IF EXISTS `t_db`;
CREATE TABLE `t_db` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL COMMENT '数据库实例名称',
`host` varchar(100) NOT NULL,
`port` int(8) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) DEFAULT NULL,
`type` varchar(20) NOT NULL COMMENT '数据库实例类型(mysql...)',
`database` varchar(1000) DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
`params` varchar(125) DEFAULT NULL COMMENT '其他连接参数',
`network` varchar(20) DEFAULT NULL,
`ssh_tunnel_machine_id` bigint(20) DEFAULT NULL COMMENT 'ssh隧道的机器id',
`remark` varchar(125) DEFAULT NULL COMMENT '备注,描述等',
`name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库实例名称',
`database` varchar(1000) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '数据库,空格分割多个数据库',
`remark` varchar(125) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '备注,描述等',
`tag_id` bigint(20) DEFAULT NULL COMMENT '标签id',
`tag_path` varchar(255) DEFAULT NULL COMMENT '标签路径',
`tag_path` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '标签路径',
`instance_id` bigint(20) NOT NULL COMMENT '数据库实例 ID',
`create_time` datetime DEFAULT NULL,
`creator_id` bigint(20) DEFAULT NULL,
`creator` varchar(32) DEFAULT NULL,
`creator` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`modifier_id` bigint(20) DEFAULT NULL,
`modifier` varchar(32) DEFAULT NULL,
`is_deleted` tinyint(8) NOT NULL DEFAULT 0,
`modifier` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL,
`is_deleted` tinyint(8) NOT NULL DEFAULT '0',
`delete_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_path` (`tag_path`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库资源信息表';
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据库资源信息表';
-- ----------------------------
-- Records of t_db
@@ -609,6 +634,10 @@ INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(130, 2, '12sSjal1/W9XKiabq/', 1, 1, '计划任务', '/machine/cron-job', 1689646396, '{"component":"ops/machine/cronjob/CronJobList","icon":"AlarmClock","isKeepAlive":true,"routeName":"CronJobList"}', 1, 'admin', 1, 'admin', '2023-07-18 10:13:16', '2023-07-18 10:14:06', 0, NULL);
INSERT INTO t_sys_resource (id, pid, ui_path, type, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(134, 80, 'Mongo452/eggago31/3sblw1Wb/', 2, 1, '删除数据', 'mongo:data:del', 1692674964, 'null', 1, 'admin', 1, 'admin', '2023-08-22 11:29:24', '2023-08-22 11:29:24', 0, NULL);
INSERT INTO t_sys_resource (id, pid, ui_path, type, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES(133, 80, 'Mongo452/eggago31/xvpKk36u/', 2, 1, '保存数据', 'mongo:data:save', 1692674943, 'null', 1, 'admin', 1, 'admin', '2023-08-22 11:29:04', '2023-08-22 11:29:11', 0, NULL);
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (135, 36, 'dbms23ax/X0f4BxT0/', 1, 1, '数据库实例管理', 'instances', 1693040706, '{\"component\":\"ops/db/InstanceList\",\"icon\":\"Coin\",\"isKeepAlive\":true,\"routeName\":\"InstanceList\"}', 1, 'admin', 1, 'admin', '2023-08-26 09:05:07', '2023-08-29 22:35:11', 0, NULL);
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (136, 133, 'dbms23ax/X0f4BxT0/D23fUiBr/', 2, 1, '实例保存', 'instance:save', 1693041001, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:10:02', '2023-08-26 09:10:02', 0, NULL);
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (137, 133, 'dbms23ax/X0f4BxT0/mJlBeTCs/', 2, 1, '基本权限', 'instance', 1693041055, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:10:55', '2023-08-26 09:10:55', 0, NULL);
INSERT INTO t_sys_resource (id, pid, ui_path, `type`, status, name, code, weight, meta, creator_id, creator, modifier_id, modifier, create_time, update_time, is_deleted, delete_time) VALUES (138, 133, 'dbms23ax/X0f4BxT0/Sgg8uPwz/', 2, 1, '实例删除', 'instance:del', 1693041084, 'null', 1, 'admin', 1, 'admin', '2023-08-26 09:11:24', '2023-08-26 09:11:24', 0, NULL);
COMMIT;
-- ----------------------------
@@ -830,7 +859,13 @@ INSERT INTO `t_sys_role_resource` (role_id,resource_id,creator_id,creator,create
(1,128,1,'admin','2023-03-16 16:11:25', 0, NULL),
(1,130,1,'admin','2023-03-16 16:11:25', 0, NULL),
(1,131,1,'admin','2023-03-16 16:11:25', 0, NULL),
(1,132,1,'admin','2023-03-16 16:11:25', 0, NULL);
(1,132,1,'admin','2023-03-16 16:11:25', 0, NULL),
(1,133,1,'admin','2023-08-30 20:17:00', 0, NULL),
(1,134,1,'admin','2023-08-30 20:17:00', 0, NULL),
(1,135,1,'admin','2023-08-30 20:17:00', 0, NULL),
(1,136,1,'admin','2023-08-30 20:17:00', 0, NULL),
(1,137,1,'admin','2023-08-30 20:17:00', 0, NULL),
(1,138,1,'admin','2023-08-30 20:17:00', 0, NULL);
COMMIT;
-- ----------------------------