refactor: 实现 DbType 类型,集中处理部分差异化的数据库操作

This commit is contained in:
kanzihuang
2023-10-15 10:36:01 +08:00
parent f04b82c933
commit ba82b5b516
8 changed files with 127 additions and 61 deletions

View File

@@ -144,6 +144,7 @@ func (d *dbAppImpl) GetDbConnection(dbId uint64, dbName string) *DbConnection {
biz.IsTrue(strings.Contains(" "+db.Database+" ", " "+dbName+" "), "未配置数据库【%s】的操作权限", dbName)
instance := d.dbInstanceApp.GetById(db.InstanceId)
biz.NotNil(instance, "数据库实例不存在")
// 密码解密
instance.PwdDecrypt()
@@ -178,7 +179,7 @@ func (d *dbAppImpl) GetDbConnection(dbId uint64, dbName string) *DbConnection {
type DbInfo struct {
Id uint64
Name string
Type string // 类型mysql oracle等
Type entity.DbType // 类型mysql oracle等
Host string
Port int
Network string
@@ -242,14 +243,14 @@ func (d *DbConnection) Exec(sql string) (int64, error) {
// 获取数据库元信息实现接口
func (d *DbConnection) GetMeta() DbMetadata {
dbType := d.Info.Type
if dbType == entity.DbTypeMysql {
switch d.Info.Type {
case entity.DbTypeMysql:
return &MysqlMetadata{di: d}
}
if dbType == entity.DbTypePostgres {
case entity.DbTypePostgres:
return &PgsqlMetadata{di: d}
default:
panic(fmt.Sprintf("invalid database type: %s", d.Info.Type))
}
return nil
}
// 关闭连接