From d57e6425e58949cd8133aac182a53ea99adc459f Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Sat, 14 Jun 2025 18:15:45 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(ability,=20channel):=20stand?= =?UTF-8?q?ardize=20boolean=20value=20handling=20across=20database=20queri?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/ability.go | 16 ++++------------ model/channel.go | 4 ++-- model/main.go | 8 +++++++- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/model/ability.go b/model/ability.go index c4df5d07..96a9ef6a 100644 --- a/model/ability.go +++ b/model/ability.go @@ -42,15 +42,11 @@ func GetAllEnableAbilities() []Ability { } func getPriority(group string, model string, retry int) (int, error) { - trueVal := "1" - if common.UsingPostgreSQL { - trueVal = "true" - } var priorities []int err := DB.Model(&Ability{}). Select("DISTINCT(priority)"). - Where(commonGroupCol+" = ? and model = ? and enabled = "+trueVal, group, model). + Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, commonTrueVal). Order("priority DESC"). // 按优先级降序排序 Pluck("priority", &priorities).Error // Pluck用于将查询的结果直接扫描到一个切片中 @@ -76,18 +72,14 @@ func getPriority(group string, model string, retry int) (int, error) { } func getChannelQuery(group string, model string, retry int) *gorm.DB { - trueVal := "1" - if common.UsingPostgreSQL { - trueVal = "true" - } - maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(commonGroupCol+" = ? and model = ? and enabled = "+trueVal, group, model) - channelQuery := DB.Where(commonGroupCol+" = ? and model = ? and enabled = "+trueVal+" and priority = (?)", group, model, maxPrioritySubQuery) + maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, commonTrueVal) + channelQuery := DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = (?)", group, model, commonTrueVal, maxPrioritySubQuery) if retry != 0 { priority, err := getPriority(group, model, retry) if err != nil { common.SysError(fmt.Sprintf("Get priority failed: %s", err.Error())) } else { - channelQuery = DB.Where(commonGroupCol+" = ? and model = ? and enabled = "+trueVal+" and priority = ?", group, model, priority) + channelQuery = DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = ?", group, model, commonTrueVal, priority) } } diff --git a/model/channel.go b/model/channel.go index 598e1b6e..b5503eee 100644 --- a/model/channel.go +++ b/model/channel.go @@ -145,7 +145,7 @@ func SearchChannels(keyword string, group string, model string, idSort bool) ([] } // 构造基础查询 - baseQuery := DB.Model(&Channel{}).Omit(commonKeyCol) + baseQuery := DB.Model(&Channel{}).Omit("key") // 构造WHERE子句 var whereClause string @@ -478,7 +478,7 @@ func SearchTags(keyword string, group string, model string, idSort bool) ([]*str } // 构造基础查询 - baseQuery := DB.Model(&Channel{}).Omit(commonKeyCol) + baseQuery := DB.Model(&Channel{}).Omit("key") // 构造WHERE子句 var whereClause string diff --git a/model/main.go b/model/main.go index f3929b5d..289baa2f 100644 --- a/model/main.go +++ b/model/main.go @@ -18,6 +18,8 @@ import ( var commonGroupCol string var commonKeyCol string +var commonTrueVal string +var commonFalseVal string var logKeyCol string var logGroupCol string @@ -27,11 +29,15 @@ func initCol() { if common.UsingPostgreSQL { commonGroupCol = `"group"` commonKeyCol = `"key"` + commonTrueVal = "true" + commonFalseVal = "false" } else { commonGroupCol = "`group`" commonKeyCol = "`key`" + commonTrueVal = "1" + commonFalseVal = "0" } - if DB != LOG_DB { + if os.Getenv("LOG_SQL_DSN") != "" { switch common.LogSqlType { case common.DatabaseTypePostgreSQL: logGroupCol = `"group"`