Skip to content

Commit

Permalink
读取 dbadapter 以尽可能的兼容不同数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
gsw945 authored Nov 9, 2024
1 parent d254f72 commit e79d60e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions models/DocumentSearchResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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)

Check failure on line 45 in models/DocumentSearchResult.go

View workflow job for this annotation

GitHub Actions / Windows Latest MSVC

undefined: fmt

Check failure on line 45 in models/DocumentSearchResult.go

View workflow job for this annotation

GitHub Actions / Ubuntu Latest GCC

undefined: fmt
}

func NewDocumentSearchResult() *DocumentSearchResult {
return &DocumentSearchResult{}
}
Expand Down Expand Up @@ -294,7 +303,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"))

Check failure on line 306 in models/DocumentSearchResult.go

View workflow job for this annotation

GitHub Actions / Windows Latest MSVC

undefined: fmt

Check failure on line 306 in models/DocumentSearchResult.go

View workflow job for this annotation

GitHub Actions / Ubuntu Latest GCC

undefined: fmt
keyword = "%" + keyword + "%"

_need_escape := need_escape(keyword)
Expand All @@ -313,7 +322,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"))

Check failure on line 325 in models/DocumentSearchResult.go

View workflow job for this annotation

GitHub Actions / Windows Latest MSVC

undefined: fmt

Check failure on line 325 in models/DocumentSearchResult.go

View workflow job for this annotation

GitHub Actions / Ubuntu Latest GCC

undefined: fmt
keyword = "%" + keyword + "%"

_need_escape := need_escape(keyword)
Expand Down

0 comments on commit e79d60e

Please sign in to comment.