Skip to content

Commit

Permalink
修复PostgreSQL数据库下,添加项目成员和全文搜索时发生的SQL语法兼容性错误 (#986)
Browse files Browse the repository at this point in the history
* FixedBUG: pq: 不支持 LIMIT #,# 语法

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

* 读取 dbadapter 以尽可能的兼容不同数据库
  • Loading branch information
iotames authored Nov 9, 2024
1 parent 0a93dbe commit 0000931
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions models/DocumentSearchResult.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"fmt"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -36,6 +37,15 @@ func need_escape(keyword string) bool {
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 {
return &DocumentSearchResult{}
}
Expand Down Expand Up @@ -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) {
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 + "%"

_need_escape := need_escape(keyword)
Expand All @@ -304,7 +314,6 @@ func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int) (docs
}
return sql
}

_, err = o.Raw(escape_sql(sql), bookId, keyword, keyword).QueryRows(&docs)

return
Expand All @@ -314,7 +323,7 @@ func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int) (docs
func (m *DocumentSearchResult) SearchAllDocument(keyword string) (docs []*DocumentSearchResult, err error) {
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 + "%"

_need_escape := need_escape(keyword)
Expand Down
2 changes: 1 addition & 1 deletion models/MemberResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (m *MemberRelationshipResult) FindNotJoinUsersByAccount(bookId, limit int,
func (m *MemberRelationshipResult) FindNotJoinUsersByAccountOrRealName(bookId, limit int, keyWord string) ([]*Member, error) {
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

Expand Down

0 comments on commit 0000931

Please sign in to comment.