Skip to content

Commit bbd6491

Browse files
committed
feat: 实现本地远程数据库切换
1 parent cd742b0 commit bbd6491

File tree

15 files changed

+1071
-27
lines changed

15 files changed

+1071
-27
lines changed

backend/app/api/v1/database_mysql.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ func (b *BaseApi) UpdateMysqlConfByFile(c *gin.Context) {
188188
// @Summary Page mysql databases
189189
// @Description 获取 mysql 数据库列表分页
190190
// @Accept json
191-
// @Param request body dto.SearchWithPage true "request"
191+
// @Param request body dto.MysqlDBSearch true "request"
192192
// @Success 200 {object} dto.PageResult
193193
// @Security ApiKeyAuth
194194
// @Router /databases/search [post]
195195
func (b *BaseApi) SearchMysql(c *gin.Context) {
196-
var req dto.SearchWithPage
196+
var req dto.MysqlDBSearch
197197
if err := c.ShouldBindJSON(&req); err != nil {
198198
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
199199
return

backend/app/api/v1/remote_db.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// @Summary Create remote database
1313
// @Description 创建远程数据库
1414
// @Accept json
15-
// @Param request body dto.DatabaseCreate true "request"
15+
// @Param request body dto.RemoteDBCreate true "request"
1616
// @Success 200
1717
// @Security ApiKeyAuth
1818
// @Router /databases/remote [post]
@@ -38,7 +38,7 @@ func (b *BaseApi) CreateRemoteDB(c *gin.Context) {
3838
// @Summary Page remote databases
3939
// @Description 获取远程数据库列表分页
4040
// @Accept json
41-
// @Param request body dto.SearchWithPage true "request"
41+
// @Param request body dto.RemoteDBSearch true "request"
4242
// @Success 200 {object} dto.PageResult
4343
// @Security ApiKeyAuth
4444
// @Router /databases/remote/search [post]
@@ -113,7 +113,7 @@ func (b *BaseApi) DeleteRemoteDB(c *gin.Context) {
113113
// @Summary Update remote database
114114
// @Description 更新远程数据库
115115
// @Accept json
116-
// @Param request body dto.DatabaseUpdate true "request"
116+
// @Param request body dto.RemoteDBUpdate true "request"
117117
// @Success 200
118118
// @Security ApiKeyAuth
119119
// @Router /databases/remote/update [post]

backend/app/dto/database.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ package dto
22

33
import "time"
44

5+
type MysqlDBSearch struct {
6+
PageInfo
7+
Info string `json:"info"`
8+
From string `json:"from"`
9+
OrderBy string `json:"orderBy"`
10+
Order string `json:"order"`
11+
}
12+
513
type MysqlDBInfo struct {
614
ID uint `json:"id"`
715
CreatedAt time.Time `json:"createdAt"`

backend/app/repo/databse_mysql.go

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type MysqlRepo struct{}
1313
type IMysqlRepo interface {
1414
Get(opts ...DBOption) (model.DatabaseMysql, error)
1515
WithByMysqlName(mysqlName string) DBOption
16+
WithByFrom(from string) DBOption
1617
List(opts ...DBOption) ([]model.DatabaseMysql, error)
1718
Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error)
1819
Create(ctx context.Context, mysql *model.DatabaseMysql) error
@@ -86,3 +87,12 @@ func (u *MysqlRepo) WithByMysqlName(mysqlName string) DBOption {
8687
return g.Where("mysql_name = ?", mysqlName)
8788
}
8889
}
90+
91+
func (u *MysqlRepo) WithByFrom(from string) DBOption {
92+
return func(g *gorm.DB) *gorm.DB {
93+
if len(from) != 0 {
94+
return g.Where("`from` = ?", from)
95+
}
96+
return g
97+
}
98+
}

backend/app/service/database_mysql.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
type MysqlService struct{}
3131

3232
type IMysqlService interface {
33-
SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error)
33+
SearchWithPage(search dto.MysqlDBSearch) (int64, interface{}, error)
3434
ListDBName() ([]string, error)
3535
Create(ctx context.Context, req dto.MysqlDBCreate) (*model.DatabaseMysql, error)
3636
ChangeAccess(info dto.ChangeDBInfo) error
@@ -50,8 +50,12 @@ func NewIMysqlService() IMysqlService {
5050
return &MysqlService{}
5151
}
5252

53-
func (u *MysqlService) SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error) {
54-
total, mysqls, err := mysqlRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info), commonRepo.WithOrderRuleBy(search.OrderBy, search.Order))
53+
func (u *MysqlService) SearchWithPage(search dto.MysqlDBSearch) (int64, interface{}, error) {
54+
total, mysqls, err := mysqlRepo.Page(search.Page, search.PageSize,
55+
mysqlRepo.WithByFrom(search.From),
56+
commonRepo.WithLikeName(search.Info),
57+
commonRepo.WithOrderRuleBy(search.OrderBy, search.Order),
58+
)
5559
var dtoMysqls []dto.MysqlDBInfo
5660
for _, mysql := range mysqls {
5761
var item dto.MysqlDBInfo

0 commit comments

Comments
 (0)