Skip to content

Commit e79d60e

Browse files
authored
读取 dbadapter 以尽可能的兼容不同数据库
1 parent d254f72 commit e79d60e

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

models/DocumentSearchResult.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ func need_escape(keyword string) bool {
3636
return false
3737
}
3838

39+
func escape_name(name string) string {
40+
dbadapter, _ := web.AppConfig.String("db_adapter")
41+
ch := "`"
42+
if strings.EqualFold(dbadapter, "postgres") {
43+
ch = `"`
44+
}
45+
return fmt.Sprintf("%s%s%s", ch, name, ch)
46+
}
47+
3948
func NewDocumentSearchResult() *DocumentSearchResult {
4049
return &DocumentSearchResult{}
4150
}
@@ -294,7 +303,7 @@ WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0 or team.team_member_
294303
func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int) (docs []*DocumentSearchResult, err error) {
295304
o := orm.NewOrm()
296305

297-
sql := `SELECT * FROM md_documents WHERE book_id = ? AND (document_name LIKE ? OR "release" LIKE ?) `
306+
sql := fmt.Sprintf("SELECT * FROM md_documents WHERE book_id = ? AND (document_name LIKE ? OR %s LIKE ?) ", escape_name("release"))
298307
keyword = "%" + keyword + "%"
299308

300309
_need_escape := need_escape(keyword)
@@ -313,7 +322,7 @@ func (m *DocumentSearchResult) SearchDocument(keyword string, bookId int) (docs
313322
func (m *DocumentSearchResult) SearchAllDocument(keyword string) (docs []*DocumentSearchResult, err error) {
314323
o := orm.NewOrm()
315324

316-
sql := `SELECT * FROM md_documents WHERE (document_name LIKE ? OR "release" LIKE ?) `
325+
sql := fmt.Sprintf("SELECT * FROM md_documents WHERE (document_name LIKE ? OR %s LIKE ?) ", escape_name("release"))
317326
keyword = "%" + keyword + "%"
318327

319328
_need_escape := need_escape(keyword)

0 commit comments

Comments
 (0)