mirror of
https://github.com/documize/community.git
synced 2025-08-05 05:25:27 +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,67 +9,66 @@
|
|||
//
|
||||
// https://documize.com
|
||||
|
||||
// Package mysql handles data persistence for spaces.
|
||||
package mysql
|
||||
package space
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/store/mysql"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/model/space"
|
||||
"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.SpaceStorer
|
||||
}
|
||||
|
||||
// Add adds new folder into the store.
|
||||
func (s Scope) Add(ctx domain.RequestContext, sp space.Space) (err error) {
|
||||
func (s Store) Add(ctx domain.RequestContext, sp space.Space) (err error) {
|
||||
sp.UserID = ctx.UserID
|
||||
sp.Created = time.Now().UTC()
|
||||
sp.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_space (c_refid, c_name, c_orgid, c_userid, c_type, c_lifecycle, c_likes, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_space (c_refid, c_name, c_orgid, c_userid, c_type, c_lifecycle, c_likes, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"),
|
||||
sp.RefID, sp.Name, sp.OrgID, sp.UserID, sp.Type, sp.Lifecycle, sp.Likes, sp.Created, sp.Revised)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "unable to execute insert for label")
|
||||
err = errors.Wrap(err, "unable to execute insert for space")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns a space from the store.
|
||||
func (s Scope) Get(ctx domain.RequestContext, id string) (sp space.Space, err error) {
|
||||
err = s.Runtime.Db.Get(&sp, `SELECT id, c_refid AS refid,
|
||||
func (s Store) Get(ctx domain.RequestContext, id string) (sp space.Space, err error) {
|
||||
err = s.Runtime.Db.Get(&sp, s.Bind(`SELECT id, c_refid AS refid,
|
||||
c_name AS name, c_orgid AS orgid, c_userid AS userid,
|
||||
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
|
||||
c_created AS created, c_revised AS revised
|
||||
FROM dmz_space
|
||||
WHERE c_orgid=? and c_refid=?`,
|
||||
WHERE c_orgid=? and c_refid=?`),
|
||||
ctx.OrgID, id)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("unable to execute select for label %s", id))
|
||||
err = errors.Wrap(err, fmt.Sprintf("unable to execute select for space %s", id))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// PublicSpaces returns spaces that anyone can see.
|
||||
func (s Scope) PublicSpaces(ctx domain.RequestContext, orgID string) (sp []space.Space, err error) {
|
||||
qry := `SELECT id, c_refid AS refid,
|
||||
func (s Store) PublicSpaces(ctx domain.RequestContext, orgID string) (sp []space.Space, err error) {
|
||||
qry := s.Bind(`SELECT id, c_refid AS refid,
|
||||
c_name AS name, c_orgid AS orgid, c_userid AS userid,
|
||||
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
|
||||
c_created AS created, c_revised AS revised
|
||||
FROM dmz_space
|
||||
WHERE c_orgid=? AND c_type=1`
|
||||
WHERE c_orgid=? AND c_type=1`)
|
||||
|
||||
err = s.Runtime.Db.Select(&sp, qry, orgID)
|
||||
|
||||
|
@ -86,9 +85,8 @@ func (s Scope) PublicSpaces(ctx domain.RequestContext, orgID string) (sp []space
|
|||
|
||||
// GetViewable returns spaces that the user can see.
|
||||
// Also handles which spaces can be seen by anonymous users.
|
||||
func (s Scope) GetViewable(ctx domain.RequestContext) (sp []space.Space, err error) {
|
||||
q := `
|
||||
SELECT id, c_refid AS refid,
|
||||
func (s Store) GetViewable(ctx domain.RequestContext) (sp []space.Space, err error) {
|
||||
q := s.Bind(`SELECT id, c_refid AS refid,
|
||||
c_name AS name, c_orgid AS orgid, c_userid AS userid,
|
||||
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
|
||||
c_created AS created, c_revised AS revised
|
||||
|
@ -101,7 +99,7 @@ func (s Scope) GetViewable(ctx domain.RequestContext) (sp []space.Space, err err
|
|||
AND p.c_location='space' AND p.c_action='view' AND (r.c_userid=? OR r.c_userid='0')
|
||||
)
|
||||
)
|
||||
ORDER BY c_name`
|
||||
ORDER BY c_name`)
|
||||
|
||||
err = s.Runtime.Db.Select(&sp, q,
|
||||
ctx.OrgID,
|
||||
|
@ -123,15 +121,14 @@ func (s Scope) GetViewable(ctx domain.RequestContext) (sp []space.Space, err err
|
|||
}
|
||||
|
||||
// GetAll for admin users!
|
||||
func (s Scope) GetAll(ctx domain.RequestContext) (sp []space.Space, err error) {
|
||||
qry := `
|
||||
SELECT id, c_refid AS refid,
|
||||
func (s Store) GetAll(ctx domain.RequestContext) (sp []space.Space, err error) {
|
||||
qry := s.Bind(`SELECT id, c_refid AS refid,
|
||||
c_name AS name, c_orgid AS orgid, c_userid AS userid,
|
||||
c_type AS type, c_lifecycle AS lifecycle, c_likes AS likes,
|
||||
c_created AS created, c_revised AS revised
|
||||
FROM dmz_space
|
||||
WHERE c_orgid=?
|
||||
ORDER BY c_name`
|
||||
ORDER BY c_name`)
|
||||
|
||||
err = s.Runtime.Db.Select(&sp, qry, ctx.OrgID)
|
||||
|
||||
|
@ -147,19 +144,18 @@ func (s Scope) GetAll(ctx domain.RequestContext) (sp []space.Space, err error) {
|
|||
}
|
||||
|
||||
// Update saves space changes.
|
||||
func (s Scope) Update(ctx domain.RequestContext, sp space.Space) (err error) {
|
||||
func (s Store) Update(ctx domain.RequestContext, sp space.Space) (err error) {
|
||||
sp.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.NamedExec("UPDATE dmz_space SET c_name=:name, c_type=:type, c_lifecycle=:lifecycle, c_userid=:userid, c_likes=:likes, c_revised=:revised WHERE c_orgid=:orgid AND c_refid=:refid", &sp)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("unable to execute update for label %s", sp.RefID))
|
||||
err = errors.Wrap(err, fmt.Sprintf("unable to execute update for space %s", sp.RefID))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Delete removes space from the store.
|
||||
func (s Scope) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
return b.DeleteConstrained(ctx.Transaction, "dmz_space", ctx.OrgID, id)
|
||||
func (s Store) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
|
||||
return s.DeleteConstrained(ctx.Transaction, "dmz_space", ctx.OrgID, id)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue