mirror of
https://github.com/documize/community.git
synced 2025-08-04 21:15:24 +02:00
PostgreSQL prep
Update of vendored SQL libs and refactoring of store provider layer.
This commit is contained in:
parent
d0e005f638
commit
b455e5eaf5
105 changed files with 10949 additions and 2376 deletions
|
@ -9,17 +9,17 @@
|
|||
//
|
||||
// https://documize.com
|
||||
|
||||
package mysql
|
||||
package search
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/core/stringutil"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/model/attachment"
|
||||
"github.com/documize/community/model/doc"
|
||||
"github.com/documize/community/model/page"
|
||||
|
@ -29,16 +29,17 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Scope provides data access to MySQL.
|
||||
type Scope struct {
|
||||
Runtime *env.Runtime
|
||||
// Store provides data access to space information.
|
||||
type Store struct {
|
||||
store.Context
|
||||
domain.SearchStorer
|
||||
}
|
||||
|
||||
// IndexDocument adds search index entries for document inserting title, tags and attachments as
|
||||
// searchable items. Any existing document entries are removed.
|
||||
func (s Scope) IndexDocument(ctx domain.RequestContext, doc doc.Document, a []attachment.Attachment) (err error) {
|
||||
func (s Store) IndexDocument(ctx domain.RequestContext, doc doc.Document, a []attachment.Attachment) (err error) {
|
||||
// remove previous search entries
|
||||
_, err = ctx.Transaction.Exec("DELETE FROM dmz_search WHERE c_orgid=? AND c_docid=? AND (c_itemtype='doc' OR c_itemtype='file' OR c_itemtype='tag')",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("DELETE FROM dmz_search WHERE c_orgid=? AND c_docid=? AND (c_itemtype='doc' OR c_itemtype='file' OR c_itemtype='tag')"),
|
||||
ctx.OrgID, doc.RefID)
|
||||
|
||||
if err != nil {
|
||||
|
@ -46,7 +47,7 @@ func (s Scope) IndexDocument(ctx domain.RequestContext, doc doc.Document, a []at
|
|||
}
|
||||
|
||||
// insert doc title
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)"),
|
||||
ctx.OrgID, doc.RefID, "", "doc", doc.Name)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "execute insert document title entry")
|
||||
|
@ -59,7 +60,7 @@ func (s Scope) IndexDocument(ctx domain.RequestContext, doc doc.Document, a []at
|
|||
continue
|
||||
}
|
||||
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)"),
|
||||
ctx.OrgID, doc.RefID, "", "tag", t)
|
||||
|
||||
if err != nil {
|
||||
|
@ -69,7 +70,7 @@ func (s Scope) IndexDocument(ctx domain.RequestContext, doc doc.Document, a []at
|
|||
}
|
||||
|
||||
for _, file := range a {
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)"),
|
||||
ctx.OrgID, doc.RefID, file.RefID, "file", file.Filename)
|
||||
|
||||
if err != nil {
|
||||
|
@ -81,8 +82,9 @@ func (s Scope) IndexDocument(ctx domain.RequestContext, doc doc.Document, a []at
|
|||
}
|
||||
|
||||
// DeleteDocument removes all search entries for document.
|
||||
func (s Scope) DeleteDocument(ctx domain.RequestContext, ID string) (err error) {
|
||||
_, err = ctx.Transaction.Exec("DELETE FROM dmz_search WHERE c_orgid=? AND c_docid=?", ctx.OrgID, ID)
|
||||
func (s Store) DeleteDocument(ctx domain.RequestContext, ID string) (err error) {
|
||||
_, err = ctx.Transaction.Exec(s.Bind("DELETE FROM dmz_search WHERE c_orgid=? AND c_docid=?"),
|
||||
ctx.OrgID, ID)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "execute delete document entries")
|
||||
|
@ -93,14 +95,14 @@ func (s Scope) DeleteDocument(ctx domain.RequestContext, ID string) (err error)
|
|||
|
||||
// IndexContent adds search index entry for document context.
|
||||
// Any existing document entries are removed.
|
||||
func (s Scope) IndexContent(ctx domain.RequestContext, p page.Page) (err error) {
|
||||
func (s Store) IndexContent(ctx domain.RequestContext, p page.Page) (err error) {
|
||||
// we do not index pending pages
|
||||
if p.Status == workflow.ChangePending || p.Status == workflow.ChangePendingNew {
|
||||
return
|
||||
}
|
||||
|
||||
// remove previous search entries
|
||||
_, err = ctx.Transaction.Exec("DELETE FROM dmz_search WHERE c_orgid=? AND c_docid=? AND c_itemid=? AND c_itemtype='page'",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("DELETE FROM dmz_search WHERE c_orgid=? AND c_docid=? AND c_itemid=? AND c_itemtype='page'"),
|
||||
ctx.OrgID, p.DocumentID, p.RefID)
|
||||
|
||||
if err != nil {
|
||||
|
@ -115,13 +117,13 @@ func (s Scope) IndexContent(ctx domain.RequestContext, p page.Page) (err error)
|
|||
}
|
||||
content = strings.TrimSpace(content)
|
||||
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)"),
|
||||
ctx.OrgID, p.DocumentID, p.RefID, "page", content)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "execute insert document content entry")
|
||||
}
|
||||
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_search (c_orgid, c_docid, c_itemid, c_itemtype, c_content) VALUES (?, ?, ?, ?, ?)"),
|
||||
ctx.OrgID, p.DocumentID, p.RefID, "page", p.Name)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "execute insert document page title entry")
|
||||
|
@ -131,10 +133,10 @@ func (s Scope) IndexContent(ctx domain.RequestContext, p page.Page) (err error)
|
|||
}
|
||||
|
||||
// DeleteContent removes all search entries for specific document content.
|
||||
func (s Scope) DeleteContent(ctx domain.RequestContext, pageID string) (err error) {
|
||||
func (s Store) DeleteContent(ctx domain.RequestContext, pageID string) (err error) {
|
||||
// remove all search entries
|
||||
var stmt1 *sqlx.Stmt
|
||||
stmt1, err = ctx.Transaction.Preparex("DELETE FROM dmz_search WHERE c_orgid=? AND c_itemid=? AND c_itemtype=?")
|
||||
stmt1, err = ctx.Transaction.Preparex(s.Bind("DELETE FROM dmz_search WHERE c_orgid=? AND c_itemid=? AND c_itemtype=?"))
|
||||
defer streamutil.Close(stmt1)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "prepare delete document content entry")
|
||||
|
@ -152,7 +154,7 @@ func (s Scope) DeleteContent(ctx domain.RequestContext, pageID string) (err erro
|
|||
|
||||
// Documents searches the documents that the client is allowed to see, using the keywords search string, then audits that search.
|
||||
// Visible documents include both those in the client's own organization and those that are public, or whose visibility includes the client.
|
||||
func (s Scope) Documents(ctx domain.RequestContext, q search.QueryOptions) (results []search.QueryResult, err error) {
|
||||
func (s Store) Documents(ctx domain.RequestContext, q search.QueryOptions) (results []search.QueryResult, err error) {
|
||||
q.Keywords = strings.TrimSpace(q.Keywords)
|
||||
if len(q.Keywords) == 0 {
|
||||
return
|
||||
|
@ -211,7 +213,7 @@ func (s Scope) Documents(ctx domain.RequestContext, q search.QueryOptions) (resu
|
|||
return
|
||||
}
|
||||
|
||||
func (s Scope) matchFullText(ctx domain.RequestContext, keywords, itemType string) (r []search.QueryResult, err error) {
|
||||
func (s Store) matchFullText(ctx domain.RequestContext, keywords, itemType string) (r []search.QueryResult, err error) {
|
||||
sql1 := `
|
||||
SELECT
|
||||
s.id, s.c_orgid AS orgid, s.c_docid AS documentid, s.c_itemid AS itemid, s.c_itemtype AS itemtype,
|
||||
|
@ -261,7 +263,7 @@ func (s Scope) matchFullText(ctx domain.RequestContext, keywords, itemType strin
|
|||
return
|
||||
}
|
||||
|
||||
func (s Scope) matchLike(ctx domain.RequestContext, keywords, itemType string) (r []search.QueryResult, err error) {
|
||||
func (s Store) matchLike(ctx domain.RequestContext, keywords, itemType string) (r []search.QueryResult, err error) {
|
||||
// LIKE clause does not like quotes!
|
||||
keywords = strings.Replace(keywords, "'", "", -1)
|
||||
keywords = strings.Replace(keywords, "\"", "", -1)
|
Loading…
Add table
Add a link
Reference in a new issue