From 0000931a881cc4f8e1bee669bd2d2a3b9c9e0512 Mon Sep 17 00:00:00 2001 From: Hankin <11134879+iotames@users.noreply.github.com> Date: Sat, 9 Nov 2024 12:04:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPostgreSQL=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E4=B8=8B=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E6=88=90=E5=91=98=E5=92=8C=E5=85=A8=E6=96=87=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=97=B6=E5=8F=91=E7=94=9F=E7=9A=84SQL=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7=E9=94=99=E8=AF=AF=20(#986)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * FixedBUG: pq: 不支持 LIMIT #,# 语法 * FixedBug: 修复全文搜索时,因不兼容PostgreSQL发生的语法错误 * 读取 dbadapter 以尽可能的兼容不同数据库 --- models/DocumentSearchResult.go | 15 ++++++++++++--- models/MemberResult.go | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/models/DocumentSearchResult.go b/models/DocumentSearchResult.go index 8e5097f0..c93477b3 100644 --- a/models/DocumentSearchResult.go +++ b/models/DocumentSearchResult.go @@ -1,6 +1,7 @@ package models import ( + "fmt" "regexp" "strings" "time" @@ -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{} } @@ -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) @@ -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 @@ -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) diff --git a/models/MemberResult.go b/models/MemberResult.go index 8688e85b..4983c4ac 100644 --- a/models/MemberResult.go +++ b/models/MemberResult.go @@ -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