修复PostgreSQL数据库下,添加项目成员和全文搜索时发生的SQL语法兼容性错误 (#986)

* FixedBUG: pq: 不支持 LIMIT #,# 语法

* FixedBug: 修复全文搜索时,因不兼容PostgreSQL发生的语法错误

* 读取 dbadapter 以尽可能的兼容不同数据库
master
Hankin 2024-11-09 12:04:43 +08:00 committed by GitHub
parent 0a93dbe44c
commit 0000931a88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package models package models
import ( import (
"fmt"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -36,6 +37,15 @@ func need_escape(keyword string) bool {
return false return false
} }
func escape_name(name string) string {
dbadapter, _ := web.AppConfig.String("db_adapter")
ch := "`"
if strings.EqualFold(dbadapter, "postgres") {
ch = `"`
}
return fmt.Sprintf("%s%s%s", ch, name, ch)
}
func NewDocumentSearchResult() *DocumentSearchResult { func NewDocumentSearchResult() *DocumentSearchResult {
return &DocumentSearchResult{} return &DocumentSearchResult{}
} }
@ -294,7 +304,7 @@ WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0 or team.team_member_
func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int) (docs []*DocumentSearchResult, err error) { func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int) (docs []*DocumentSearchResult, err error) {
o := orm.NewOrm() o := orm.NewOrm()
sql := "SELECT * FROM md_documents WHERE book_id = ? AND (document_name LIKE ? OR `release` LIKE ?) " sql := fmt.Sprintf("SELECT * FROM md_documents WHERE book_id = ? AND (document_name LIKE ? OR %s LIKE ?) ", escape_name("release"))
keyword = "%" + keyword + "%" keyword = "%" + keyword + "%"
_need_escape := need_escape(keyword) _need_escape := need_escape(keyword)
@ -304,7 +314,6 @@ func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int) (docs
} }
return sql return sql
} }
_, err = o.Raw(escape_sql(sql), bookId, keyword, keyword).QueryRows(&docs) _, err = o.Raw(escape_sql(sql), bookId, keyword, keyword).QueryRows(&docs)
return return
@ -314,7 +323,7 @@ func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int) (docs
func (m *DocumentSearchResult) SearchAllDocument(keyword string) (docs []*DocumentSearchResult, err error) { func (m *DocumentSearchResult) SearchAllDocument(keyword string) (docs []*DocumentSearchResult, err error) {
o := orm.NewOrm() o := orm.NewOrm()
sql := "SELECT * FROM md_documents WHERE (document_name LIKE ? OR `release` LIKE ?) " sql := fmt.Sprintf("SELECT * FROM md_documents WHERE (document_name LIKE ? OR %s LIKE ?) ", escape_name("release"))
keyword = "%" + keyword + "%" keyword = "%" + keyword + "%"
_need_escape := need_escape(keyword) _need_escape := need_escape(keyword)

View File

@ -115,7 +115,7 @@ func (m *MemberRelationshipResult) FindNotJoinUsersByAccount(bookId, limit int,
func (m *MemberRelationshipResult) FindNotJoinUsersByAccountOrRealName(bookId, limit int, keyWord string) ([]*Member, error) { func (m *MemberRelationshipResult) FindNotJoinUsersByAccountOrRealName(bookId, limit int, keyWord string) ([]*Member, error) {
o := orm.NewOrm() o := orm.NewOrm()
sql := "SELECT m.* FROM md_members as m LEFT JOIN md_relationship as rel ON rel.member_id = m.member_id AND rel.book_id = ? WHERE rel.relationship_id IS NULL AND (m.real_name LIKE ? OR m.account LIKE ?) LIMIT 0,?;" sql := "SELECT m.* FROM md_members as m LEFT JOIN md_relationship as rel ON rel.member_id = m.member_id AND rel.book_id = ? WHERE rel.relationship_id IS NULL AND (m.real_name LIKE ? OR m.account LIKE ?) LIMIT ? OFFSET 0;"
var members []*Member var members []*Member