From a7e5f1e509a2b35e11105e76462229b2e87c0e97 Mon Sep 17 00:00:00 2001 From: CalciumIon <1808837298@qq.com> Date: Tue, 31 Dec 2024 02:10:19 +0800 Subject: [PATCH] fix: try to fix pgsql #682 --- model/ability.go | 8 -------- model/channel.go | 8 -------- model/log.go | 2 +- model/token.go | 2 +- 4 files changed, 2 insertions(+), 18 deletions(-) diff --git a/model/ability.go b/model/ability.go index e21f928ff..bb8ae8064 100644 --- a/model/ability.go +++ b/model/ability.go @@ -23,10 +23,6 @@ type Ability struct { func GetGroupModels(group string) []string { var models []string // Find distinct models - groupCol := "`group`" - if common.UsingPostgreSQL { - groupCol = `"group"` - } DB.Table("abilities").Where(groupCol+" = ? and enabled = ?", group, true).Distinct("model").Pluck("model", &models) return models } @@ -45,10 +41,8 @@ func GetAllEnableAbilities() []Ability { } func getPriority(group string, model string, retry int) (int, error) { - groupCol := "`group`" trueVal := "1" if common.UsingPostgreSQL { - groupCol = `"group"` trueVal = "true" } @@ -81,10 +75,8 @@ func getPriority(group string, model string, retry int) (int, error) { } func getChannelQuery(group string, model string, retry int) *gorm.DB { - groupCol := "`group`" trueVal := "1" if common.UsingPostgreSQL { - groupCol = `"group"` trueVal = "true" } maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(groupCol+" = ? and model = ? and enabled = "+trueVal, group, model) diff --git a/model/channel.go b/model/channel.go index 2024bafd3..9b8634c2c 100644 --- a/model/channel.go +++ b/model/channel.go @@ -114,15 +114,11 @@ func GetChannelsByTag(tag string, idSort bool) ([]*Channel, error) { func SearchChannels(keyword string, group string, model string, idSort bool) ([]*Channel, error) { var channels []*Channel - keyCol := "`key`" - groupCol := "`group`" modelsCol := "`models`" // 如果是 PostgreSQL,使用双引号 if common.UsingPostgreSQL { keyCol = `"key"` - groupCol = `"group"` - modelsCol = `"models"` } order := "priority desc" @@ -437,14 +433,10 @@ func GetPaginatedTags(offset int, limit int) ([]*string, error) { func SearchTags(keyword string, group string, model string, idSort bool) ([]*string, error) { var tags []*string - keyCol := "`key`" - groupCol := "`group`" modelsCol := "`models`" // 如果是 PostgreSQL,使用双引号 if common.UsingPostgreSQL { - keyCol = `"key"` - groupCol = `"group"` modelsCol = `"models"` } diff --git a/model/log.go b/model/log.go index d172f057c..4a633a6a1 100644 --- a/model/log.go +++ b/model/log.go @@ -56,7 +56,7 @@ func formatUserLogs(logs []*Log) { func GetLogByKey(key string) (logs []*Log, err error) { if os.Getenv("LOG_SQL_DSN") != "" { var tk Token - if err = DB.Model(&Token{}).Where("`key`=?", strings.TrimPrefix(key, "sk-")).First(&tk).Error; err != nil { + if err = DB.Model(&Token{}).Where(keyCol+"=?", strings.TrimPrefix(key, "sk-")).First(&tk).Error; err != nil { return nil, err } err = LOG_DB.Model(&Log{}).Where("token_id=?", tk.Id).Find(&logs).Error diff --git a/model/token.go b/model/token.go index 39bea5364..3abd22cf6 100644 --- a/model/token.go +++ b/model/token.go @@ -68,7 +68,7 @@ func SearchUserTokens(userId int, keyword string, token string) (tokens []*Token if token != "" { token = strings.Trim(token, "sk-") } - err = DB.Where("user_id = ?", userId).Where("name LIKE ?", "%"+keyword+"%").Where("`key` LIKE ?", "%"+token+"%").Find(&tokens).Error + err = DB.Where("user_id = ?", userId).Where("name LIKE ?", "%"+keyword+"%").Where(keyCol+" LIKE ?", "%"+token+"%").Find(&tokens).Error return tokens, err }