yintai2/app/common/dao/base.go

31 lines
485 B
Go

package dao
import (
"regexp"
"strings"
"yintai-api/base/global"
"gorm.io/gorm"
)
func getDB(db ...*gorm.DB) *gorm.DB {
if len(db) > 0 {
return db[0]
}
return global.GetDB()
}
func ezSort(a string) string {
var sortCol = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
s := strings.Fields(a)
if len(s) != 2 || !sortCol.MatchString(s[0]) {
return ""
}
dir := strings.ToUpper(s[1])
if dir != "ASC" && dir != "DESC" {
return ""
}
return "`" + s[0] + "` " + dir
}