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,31 +9,31 @@
|
|||
//
|
||||
// https://documize.com
|
||||
|
||||
package mysql
|
||||
package group
|
||||
|
||||
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/group"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Scope provides data access to MySQL.
|
||||
type Scope struct {
|
||||
Runtime *env.Runtime
|
||||
// Store provides data access to space category information.
|
||||
type Store struct {
|
||||
store.Context
|
||||
domain.DocumentStorer
|
||||
}
|
||||
|
||||
// Add inserts new user group into store.
|
||||
func (s Scope) Add(ctx domain.RequestContext, g group.Group) (err error) {
|
||||
func (s Store) Add(ctx domain.RequestContext, g group.Group) (err error) {
|
||||
g.Created = time.Now().UTC()
|
||||
g.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_group (c_refid, c_orgid, c_name, c_desc, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_group (c_refid, c_orgid, c_name, c_desc, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?)"),
|
||||
g.RefID, g.OrgID, g.Name, g.Purpose, g.Created, g.Revised)
|
||||
|
||||
if err != nil {
|
||||
|
@ -44,11 +44,11 @@ func (s Scope) Add(ctx domain.RequestContext, g group.Group) (err error) {
|
|||
}
|
||||
|
||||
// Get returns requested group.
|
||||
func (s Scope) Get(ctx domain.RequestContext, refID string) (g group.Group, err error) {
|
||||
err = s.Runtime.Db.Get(&g, `
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_name AS name, c_desc AS purpose, c_created AS created, c_revised AS revised
|
||||
func (s Store) Get(ctx domain.RequestContext, refID string) (g group.Group, err error) {
|
||||
err = s.Runtime.Db.Get(&g, s.Bind(`
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_name AS name, c_desc AS purpose, c_created AS created, c_revised AS revised
|
||||
FROM dmz_group
|
||||
WHERE c_orgid=? AND c_refid=?`,
|
||||
WHERE c_orgid=? AND c_refid=?`),
|
||||
ctx.OrgID, refID)
|
||||
|
||||
if err != nil {
|
||||
|
@ -59,17 +59,17 @@ func (s Scope) Get(ctx domain.RequestContext, refID string) (g group.Group, err
|
|||
}
|
||||
|
||||
// GetAll returns all user groups for current orgID.
|
||||
func (s Scope) GetAll(ctx domain.RequestContext) (groups []group.Group, err error) {
|
||||
func (s Store) GetAll(ctx domain.RequestContext) (groups []group.Group, err error) {
|
||||
groups = []group.Group{}
|
||||
|
||||
err = s.Runtime.Db.Select(&groups, `
|
||||
err = s.Runtime.Db.Select(&groups, s.Bind(`
|
||||
SELECT a.id, a.c_refid AS refid, a.c_orgid AS orgid, a.c_name AS name, a.c_desc AS purpose, a.c_created AS created, a.c_revised AS revised,
|
||||
COUNT(b.c_groupid) AS members
|
||||
FROM dmz_group a
|
||||
LEFT JOIN dmz_group_member b ON a.c_refid=b.c_groupid
|
||||
WHERE a.c_orgid=?
|
||||
GROUP BY a.id, a.c_refid, a.c_orgid, a.c_name, a.c_desc, a.c_created, a.c_revised
|
||||
ORDER BY a.c_name`,
|
||||
ORDER BY a.c_name`),
|
||||
ctx.OrgID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
|
@ -83,12 +83,12 @@ func (s Scope) GetAll(ctx domain.RequestContext) (groups []group.Group, err erro
|
|||
}
|
||||
|
||||
// Update group name and description.
|
||||
func (s Scope) Update(ctx domain.RequestContext, g group.Group) (err error) {
|
||||
func (s Store) Update(ctx domain.RequestContext, g group.Group) (err error) {
|
||||
g.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec(`UPDATE dmz_group SET
|
||||
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_group SET
|
||||
c_name=?, c_desc=?, c_revised=?
|
||||
WHERE c_orgid=? AND c_refid=?`,
|
||||
WHERE c_orgid=? AND c_refid=?`),
|
||||
g.Name, g.Purpose, g.Revised, ctx.OrgID, g.RefID)
|
||||
|
||||
if err != nil {
|
||||
|
@ -99,26 +99,25 @@ func (s Scope) Update(ctx domain.RequestContext, g group.Group) (err error) {
|
|||
}
|
||||
|
||||
// Delete removes group from store.
|
||||
func (s Scope) Delete(ctx domain.RequestContext, refID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
_, err = b.DeleteConstrained(ctx.Transaction, "dmz_group", ctx.OrgID, refID)
|
||||
func (s Store) Delete(ctx domain.RequestContext, refID string) (rows int64, err error) {
|
||||
_, err = s.DeleteConstrained(ctx.Transaction, "dmz_group", ctx.OrgID, refID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return b.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM dmz_group_member WHERE c_orgid=\"%s\" AND c_groupid=\"%s\"", ctx.OrgID, refID))
|
||||
return s.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM dmz_group_member WHERE c_orgid=\"%s\" AND c_groupid=\"%s\"", ctx.OrgID, refID))
|
||||
}
|
||||
|
||||
// GetGroupMembers returns all user associated with given group.
|
||||
func (s Scope) GetGroupMembers(ctx domain.RequestContext, groupID string) (members []group.Member, err error) {
|
||||
func (s Store) GetGroupMembers(ctx domain.RequestContext, groupID string) (members []group.Member, err error) {
|
||||
members = []group.Member{}
|
||||
|
||||
err = s.Runtime.Db.Select(&members, `
|
||||
err = s.Runtime.Db.Select(&members, s.Bind(`
|
||||
SELECT a.id, a.c_orgid AS orgid, a.c_groupid AS groupid, a.c_userid AS userid,
|
||||
IFNULL(b.c_firstname, '') as firstname, IFNULL(b.c_lastname, '') as lastname
|
||||
COALESCE(b.c_firstname, '') as firstname, COALESCE(b.c_lastname, '') as lastname
|
||||
FROM dmz_group_member a
|
||||
LEFT JOIN dmz_user b ON b.c_refid=a.c_userid
|
||||
WHERE a.c_orgid=? AND a.c_groupid=?
|
||||
ORDER BY b.c_firstname, b.c_lastname`,
|
||||
ORDER BY b.c_firstname, b.c_lastname`),
|
||||
ctx.OrgID, groupID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
|
@ -132,8 +131,9 @@ func (s Scope) GetGroupMembers(ctx domain.RequestContext, groupID string) (membe
|
|||
}
|
||||
|
||||
// JoinGroup adds user to group.
|
||||
func (s Scope) JoinGroup(ctx domain.RequestContext, groupID, userID string) (err error) {
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_group_member (c_orgid, c_groupid, c_userid) VALUES (?, ?, ?)", ctx.OrgID, groupID, userID)
|
||||
func (s Store) JoinGroup(ctx domain.RequestContext, groupID, userID string) (err error) {
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_group_member (c_orgid, c_groupid, c_userid) VALUES (?, ?, ?)"),
|
||||
ctx.OrgID, groupID, userID)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "insert group member")
|
||||
}
|
||||
|
@ -142,9 +142,9 @@ func (s Scope) JoinGroup(ctx domain.RequestContext, groupID, userID string) (err
|
|||
}
|
||||
|
||||
// LeaveGroup removes user from group.
|
||||
func (s Scope) LeaveGroup(ctx domain.RequestContext, groupID, userID string) (err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
_, err = b.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM dmz_group_member WHERE c_orgid=\"%s\" AND c_groupid=\"%s\" AND c_userid=\"%s\"", ctx.OrgID, groupID, userID))
|
||||
func (s Store) LeaveGroup(ctx domain.RequestContext, groupID, userID string) (err error) {
|
||||
_, err = s.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM dmz_group_member WHERE c_orgid=\"%s\" AND c_groupid=\"%s\" AND c_userid=\"%s\"",
|
||||
ctx.OrgID, groupID, userID))
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "clear group member")
|
||||
}
|
||||
|
@ -155,15 +155,15 @@ func (s Scope) LeaveGroup(ctx domain.RequestContext, groupID, userID string) (er
|
|||
// GetMembers returns members for every group.
|
||||
// Useful when you need to bulk fetch membership records
|
||||
// for subsequent processing.
|
||||
func (s Scope) GetMembers(ctx domain.RequestContext) (r []group.Record, err error) {
|
||||
func (s Store) GetMembers(ctx domain.RequestContext) (r []group.Record, err error) {
|
||||
r = []group.Record{}
|
||||
|
||||
err = s.Runtime.Db.Select(&r, `
|
||||
err = s.Runtime.Db.Select(&r, s.Bind(`
|
||||
SELECT a.id, a.c_orgid AS orgid, a.c_groupid AS groupid, a.c_userid AS userid,
|
||||
b.c_name As name, b.c_desc AS purpose
|
||||
FROM dmz_group_member a, dmz_group b
|
||||
WHERE a.c_orgid=? AND a.c_groupid=b.c_refid
|
||||
ORDER BY a.c_userid`,
|
||||
ORDER BY a.c_userid`),
|
||||
ctx.OrgID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
Loading…
Add table
Add a link
Reference in a new issue