Commit a9ec62ee by CaIon

🐛 fix(ability, channel): standardize boolean value handling across database queries

parent 4371717c
...@@ -42,15 +42,11 @@ func GetAllEnableAbilities() []Ability { ...@@ -42,15 +42,11 @@ func GetAllEnableAbilities() []Ability {
} }
func getPriority(group string, model string, retry int) (int, error) { func getPriority(group string, model string, retry int) (int, error) {
trueVal := "1"
if common.UsingPostgreSQL {
trueVal = "true"
}
var priorities []int var priorities []int
err := DB.Model(&Ability{}). err := DB.Model(&Ability{}).
Select("DISTINCT(priority)"). Select("DISTINCT(priority)").
Where(commonGroupCol+" = ? and model = ? and enabled = "+trueVal, group, model). Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, commonTrueVal).
Order("priority DESC"). // 按优先级降序排序 Order("priority DESC"). // 按优先级降序排序
Pluck("priority", &priorities).Error // Pluck用于将查询的结果直接扫描到一个切片中 Pluck("priority", &priorities).Error // Pluck用于将查询的结果直接扫描到一个切片中
...@@ -76,18 +72,14 @@ func getPriority(group string, model string, retry int) (int, error) { ...@@ -76,18 +72,14 @@ func getPriority(group string, model string, retry int) (int, error) {
} }
func getChannelQuery(group string, model string, retry int) *gorm.DB { func getChannelQuery(group string, model string, retry int) *gorm.DB {
trueVal := "1" maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, commonTrueVal)
if common.UsingPostgreSQL { channelQuery := DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = (?)", group, model, commonTrueVal, maxPrioritySubQuery)
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)
if retry != 0 { if retry != 0 {
priority, err := getPriority(group, model, retry) priority, err := getPriority(group, model, retry)
if err != nil { if err != nil {
common.SysError(fmt.Sprintf("Get priority failed: %s", err.Error())) common.SysError(fmt.Sprintf("Get priority failed: %s", err.Error()))
} else { } 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)
} }
} }
......
...@@ -145,7 +145,7 @@ func SearchChannels(keyword string, group string, model string, idSort bool) ([] ...@@ -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子句 // 构造WHERE子句
var whereClause string var whereClause string
...@@ -478,7 +478,7 @@ func SearchTags(keyword string, group string, model string, idSort bool) ([]*str ...@@ -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子句 // 构造WHERE子句
var whereClause string var whereClause string
......
...@@ -18,6 +18,8 @@ import ( ...@@ -18,6 +18,8 @@ import (
var commonGroupCol string var commonGroupCol string
var commonKeyCol string var commonKeyCol string
var commonTrueVal string
var commonFalseVal string
var logKeyCol string var logKeyCol string
var logGroupCol string var logGroupCol string
...@@ -27,11 +29,15 @@ func initCol() { ...@@ -27,11 +29,15 @@ func initCol() {
if common.UsingPostgreSQL { if common.UsingPostgreSQL {
commonGroupCol = `"group"` commonGroupCol = `"group"`
commonKeyCol = `"key"` commonKeyCol = `"key"`
commonTrueVal = "true"
commonFalseVal = "false"
} else { } else {
commonGroupCol = "`group`" commonGroupCol = "`group`"
commonKeyCol = "`key`" commonKeyCol = "`key`"
commonTrueVal = "1"
commonFalseVal = "0"
} }
if DB != LOG_DB { if os.Getenv("LOG_SQL_DSN") != "" {
switch common.LogSqlType { switch common.LogSqlType {
case common.DatabaseTypePostgreSQL: case common.DatabaseTypePostgreSQL:
logGroupCol = `"group"` logGroupCol = `"group"`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment