Skip to content

Commit

Permalink
fix: try to fix pgsql #682
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion committed Dec 30, 2024
1 parent 87d5e28 commit a7e5f1e
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 18 deletions.
8 changes: 0 additions & 8 deletions model/ability.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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"
}

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions model/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
}

Expand Down
2 changes: 1 addition & 1 deletion model/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion model/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit a7e5f1e

Please sign in to comment.