Files
mayfly-go/server/internal/db/infrastructure/persistence/db.go

75 lines
2.0 KiB
Go
Raw Normal View History

package persistence
import (
"fmt"
2022-09-09 18:26:08 +08:00
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
2022-10-26 20:49:29 +08:00
"mayfly-go/pkg/utils"
"strings"
)
2022-09-09 18:26:08 +08:00
type dbRepoImpl struct{}
2022-09-09 18:26:08 +08:00
func newDbRepo() repository.Db {
return new(dbRepoImpl)
}
// 分页获取数据库信息列表
2022-10-26 20:49:29 +08:00
func (d *dbRepoImpl) GetDbList(condition *entity.DbQuery, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
sql := "SELECT d.* FROM t_db d WHERE 1 = 1 "
if condition.Host != "" {
sql = sql + " AND d.host LIKE '%" + condition.Host + "%'"
}
2021-12-13 09:49:05 +08:00
if condition.Database != "" {
sql = sql + " AND d.database LIKE '%" + condition.Database + "%'"
}
2022-10-26 20:49:29 +08:00
if len(condition.TagIds) > 0 {
sql = sql + " AND d.tag_id IN " + fmt.Sprintf("(%s)", strings.Join(utils.NumberArr2StrArr(condition.TagIds), ","))
}
if condition.TagPathLike != "" {
sql = sql + " AND d.tag_path LIKE '" + condition.TagPathLike + "%'"
}
sql = sql + " ORDER BY d.create_time DESC"
return model.GetPageBySql(sql, pageParam, toEntity)
}
2022-10-26 20:49:29 +08:00
func (d *dbRepoImpl) Count(condition *entity.DbQuery) int64 {
where := make(map[string]interface{})
if len(condition.TagIds) > 0 {
where["tag_id"] = condition.TagIds
}
if condition.TagId != 0 {
where["tag_id"] = condition.TagId
}
return model.CountByMap(new(entity.Db), where)
2021-09-11 14:04:09 +08:00
}
// 根据条件获取账号信息
2022-09-09 18:26:08 +08:00
func (d *dbRepoImpl) GetDb(condition *entity.Db, cols ...string) error {
return model.GetBy(condition, cols...)
}
// 根据id获取
2022-09-09 18:26:08 +08:00
func (d *dbRepoImpl) GetById(id uint64, cols ...string) *entity.Db {
db := new(entity.Db)
if err := model.GetById(db, id, cols...); err != nil {
return nil
}
return db
}
2022-09-09 18:26:08 +08:00
func (d *dbRepoImpl) Insert(db *entity.Db) {
biz.ErrIsNil(model.Insert(db), "新增数据库信息失败")
}
2022-09-09 18:26:08 +08:00
func (d *dbRepoImpl) Update(db *entity.Db) {
biz.ErrIsNil(model.UpdateById(db), "更新数据库信息失败")
}
2022-09-09 18:26:08 +08:00
func (d *dbRepoImpl) Delete(id uint64) {
model.DeleteById(new(entity.Db), id)
}