Files

125 lines
2.8 KiB
Go
Raw Permalink Normal View History

2024-07-19 19:04:11 +08:00
package repo
import (
"fmt"
2026-05-27 14:09:50 +08:00
"time"
"github.com/1Panel-dev/1Panel/core/constant"
2024-11-30 19:42:41 +08:00
"github.com/1Panel-dev/1Panel/core/global"
"github.com/1Panel-dev/1Panel/core/utils/re"
2024-07-19 19:04:11 +08:00
"gorm.io/gorm"
)
func WithByID(id uint) global.DBOption {
2024-07-25 14:43:41 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("id = ?", id)
}
}
func WithByGroupID(id uint) global.DBOption {
2024-11-30 19:42:41 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("group_id = ?", id)
}
}
func WithByIDs(ids []uint) global.DBOption {
2024-08-21 18:04:51 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("id in (?)", ids)
}
}
func WithByStringIDs(ids []string) global.DBOption {
var idItems []uint
for _, id := range ids {
var idItem uint
if _, err := fmt.Sscanf(id, "%d", &idItem); err == nil && idItem != 0 {
idItems = append(idItems, idItem)
}
}
return func(g *gorm.DB) *gorm.DB {
return g.Where("id IN (?)", idItems)
}
}
func WithByName(name string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`name` = ?", name)
}
}
2026-07-02 15:07:48 +08:00
func WithByLikeName(name string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(name) == 0 {
return g
}
return g.Where("name like ?", "%"+name+"%")
}
}
2026-04-09 14:34:28 +08:00
func WithByUserID(userID string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("user_id = ?", userID)
}
}
func WithoutByName(name string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`name` != ?", name)
}
}
2024-11-30 19:42:41 +08:00
func WithByType(ty string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`type` = ?", ty)
}
}
func WithByAddr(addr string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("addr = ?", addr)
}
}
func WithByKey(key string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("key = ?", key)
}
}
func WithByStatus(status string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("status = ?", status)
}
}
2026-05-27 14:09:50 +08:00
func WithByCreatedAt(startTime, endTime time.Time) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("created_at > ? AND created_at < ?", startTime, endTime)
}
}
func WithByNode(node string) global.DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("node = ?", node)
}
}
2024-11-30 19:42:41 +08:00
func WithOrderDesc(orderBy string) global.DBOption {
return WithOrderRuleBy(orderBy, constant.OrderDesc)
}
func WithOrderAsc(orderBy string) global.DBOption {
return WithOrderRuleBy(orderBy, constant.OrderAsc)
2024-07-19 19:04:11 +08:00
}
func WithOrderRuleBy(orderBy, order string) global.DBOption {
2024-12-17 15:59:21 +08:00
if orderBy == "createdAt" {
orderBy = "created_at"
}
if !re.GetRegex(re.OrderByValidationPattern).MatchString(orderBy) {
orderBy = "created_at"
}
switch order {
case constant.OrderDesc:
order = "desc"
case constant.OrderAsc:
order = "asc"
default:
orderBy = "created_at"
order = "desc"
}
return func(g *gorm.DB) *gorm.DB {
return g.Order(fmt.Sprintf("%s %s", orderBy, order))
}
}