mirror of
https://github.com/documize/community.git
synced 2025-07-25 08:09:43 +02:00
[WIP] new schema implementation
This commit is contained in:
parent
9c2594b1b4
commit
28342fcf5e
27 changed files with 1413 additions and 1158 deletions
|
@ -35,8 +35,8 @@ func (s Scope) Add(ctx domain.RequestContext, c category.Category) (err error) {
|
|||
c.Created = time.Now().UTC()
|
||||
c.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO category (refid, orgid, labelid, category, created, revised) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
c.RefID, c.OrgID, c.LabelID, c.Category, c.Created, c.Revised)
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_category (c_refid, c_orgid, c_spaceid, c_name, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
c.RefID, c.OrgID, c.SpaceID, c.Name, c.Created, c.Revised)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "unable to execute insert category")
|
||||
|
@ -49,14 +49,16 @@ func (s Scope) Add(ctx domain.RequestContext, c category.Category) (err error) {
|
|||
// Context is used to for user ID.
|
||||
func (s Scope) GetBySpace(ctx domain.RequestContext, spaceID string) (c []category.Category, err error) {
|
||||
err = s.Runtime.Db.Select(&c, `
|
||||
SELECT id, refid, orgid, labelid, category, created, revised FROM category
|
||||
WHERE orgid=? AND labelid=?
|
||||
AND refid IN (SELECT refid FROM permission WHERE orgid=? AND location='category' AND refid IN (
|
||||
SELECT refid from permission WHERE orgid=? AND who='user' AND (whoid=? OR whoid='0') AND location='category' UNION ALL
|
||||
SELECT p.refid from permission p LEFT JOIN rolemember r ON p.whoid=r.roleid
|
||||
WHERE p.orgid=? AND p.who='role' AND p.location='category' AND (r.userid=? OR r.userid='0')
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_spaceid=? AND c_refid IN
|
||||
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_location='category' AND c_refid IN
|
||||
(SELECT c_refid from dmz_permission WHERE c_orgid=? AND c_who='user' AND (c_whoid=? OR c_whoid='0') AND c_location='category'
|
||||
UNION ALL
|
||||
SELECT p.c_refid from dmz_permission p LEFT JOIN dmz_group_member r ON p.c_whoid=r.c_groupid
|
||||
WHERE p.c_orgid=? AND p.c_who='role' AND p.c_location='category' AND (r.c_userid=? OR r.c_userid='0')
|
||||
))
|
||||
ORDER BY category`, ctx.OrgID, spaceID, ctx.OrgID, ctx.OrgID, ctx.UserID, ctx.OrgID, ctx.UserID)
|
||||
ORDER BY name`, ctx.OrgID, spaceID, ctx.OrgID, ctx.OrgID, ctx.UserID, ctx.OrgID, ctx.UserID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
|
@ -73,14 +75,16 @@ func (s Scope) GetAllBySpace(ctx domain.RequestContext, spaceID string) (c []cat
|
|||
c = []category.Category{}
|
||||
|
||||
err = s.Runtime.Db.Select(&c, `
|
||||
SELECT id, refid, orgid, labelid, category, created, revised FROM category
|
||||
WHERE orgid=? AND labelid=?
|
||||
AND labelid IN (SELECT refid FROM permission WHERE orgid=? AND location='space' AND refid IN (
|
||||
SELECT refid from permission WHERE orgid=? AND who='user' AND (whoid=? OR whoid='0') AND location='space' AND action='view' UNION ALL
|
||||
SELECT p.refid from permission p LEFT JOIN rolemember r ON p.whoid=r.roleid
|
||||
WHERE p.orgid=? AND p.who='role' AND p.location='space' AND p.action='view' AND (r.userid=? OR r.userid='0')
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_spaceid=? AND spaceid IN
|
||||
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_location='space' AND c_refid IN
|
||||
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_who='user' AND (c_whoid=? OR c_whoid='0') AND c_location='space' AND c_action='view'
|
||||
UNION ALL
|
||||
SELECT p.c_refid FROM dmz_permission p LEFT JOIN dmz_group_member r ON p.c_whoid=r.c_groupid
|
||||
WHERE p.c_orgid=? AND p.c_who='role' AND p.c_location='space' AND p.c_action='view' AND (r.c_userid=? OR r.c_userid='0')
|
||||
))
|
||||
ORDER BY category`, ctx.OrgID, spaceID, ctx.OrgID, ctx.OrgID, ctx.UserID, ctx.OrgID, ctx.UserID)
|
||||
ORDER BY dmz_category`, ctx.OrgID, spaceID, ctx.OrgID, ctx.OrgID, ctx.UserID, ctx.OrgID, ctx.UserID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
|
@ -95,14 +99,16 @@ func (s Scope) GetAllBySpace(ctx domain.RequestContext, spaceID string) (c []cat
|
|||
// GetByOrg returns all categories accessible by user for their org.
|
||||
func (s Scope) GetByOrg(ctx domain.RequestContext, userID string) (c []category.Category, err error) {
|
||||
err = s.Runtime.Db.Select(&c, `
|
||||
SELECT id, refid, orgid, labelid, category, created, revised FROM category
|
||||
WHERE orgid=?
|
||||
AND refid IN (SELECT refid FROM permission WHERE orgid=? AND location='category' AND refid IN (
|
||||
SELECT refid from permission WHERE orgid=? AND who='user' AND (whoid=? OR whoid='0') AND location='category' UNION ALL
|
||||
SELECT p.refid from permission p LEFT JOIN rolemember r ON p.whoid=r.roleid
|
||||
WHERE p.orgid=? AND p.who='role' AND p.location='category' AND (r.userid=? OR r.userid='0')
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_refid IN
|
||||
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_location='category' AND c_refid IN (
|
||||
SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_who='user' AND (c_whoid=? OR c_whoid='0') AND c_location='category'
|
||||
UNION ALL
|
||||
SELECT p.c_refid FROM dmz_permission p LEFT JOIN dmz_group_member r ON p.c_whoid=r.c_groupid
|
||||
WHERE p.c_orgid=? AND p.c_who='role' AND p.c_location='category' AND (r.c_userid=? OR r.c_userid='0')
|
||||
))
|
||||
ORDER BY category`, ctx.OrgID, ctx.OrgID, ctx.OrgID, userID, ctx.OrgID, userID)
|
||||
ORDER BY dmz_category`, ctx.OrgID, ctx.OrgID, ctx.OrgID, userID, ctx.OrgID, userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
|
@ -118,7 +124,7 @@ func (s Scope) GetByOrg(ctx domain.RequestContext, userID string) (c []category.
|
|||
func (s Scope) Update(ctx domain.RequestContext, c category.Category) (err error) {
|
||||
c.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.NamedExec("UPDATE category SET category=:category, revised=:revised WHERE orgid=:orgid AND refid=:refid", c)
|
||||
_, err = ctx.Transaction.NamedExec("UPDATE dmz_category SET c_name=:name, c_revised=:revised WHERE c_orgid=:orgid AND c_refid=:refid", c)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("unable to execute update for category %s", c.RefID))
|
||||
|
@ -129,7 +135,10 @@ func (s Scope) Update(ctx domain.RequestContext, c category.Category) (err error
|
|||
|
||||
// Get returns specified category
|
||||
func (s Scope) Get(ctx domain.RequestContext, id string) (c category.Category, err error) {
|
||||
err = s.Runtime.Db.Get(&c, "SELECT id, refid, orgid, labelid, category, created, revised FROM category WHERE orgid=? AND refid=?",
|
||||
err = s.Runtime.Db.Get(&c, `
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_refid=?`,
|
||||
ctx.OrgID, id)
|
||||
|
||||
if err != nil {
|
||||
|
@ -142,7 +151,7 @@ func (s Scope) Get(ctx domain.RequestContext, id string) (c category.Category, e
|
|||
// Delete removes category from the store.
|
||||
func (s Scope) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
return b.DeleteConstrained(ctx.Transaction, "category", ctx.OrgID, id)
|
||||
return b.DeleteConstrained(ctx.Transaction, "dmz_category", ctx.OrgID, id)
|
||||
}
|
||||
|
||||
// AssociateDocument inserts category membership record into the category member table.
|
||||
|
@ -150,8 +159,8 @@ func (s Scope) AssociateDocument(ctx domain.RequestContext, m category.Member) (
|
|||
m.Created = time.Now().UTC()
|
||||
m.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO categorymember (refid, orgid, categoryid, labelid, documentid, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
m.RefID, m.OrgID, m.CategoryID, m.LabelID, m.DocumentID, m.Created, m.Revised)
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_category_member (c_refid, c_orgid, c_categoryid, c_spaceid, docid, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
m.RefID, m.OrgID, m.CategoryID, m.SpaceID, m.DocumentID, m.Created, m.Revised)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "unable to execute insert categorymember")
|
||||
|
@ -164,7 +173,7 @@ func (s Scope) AssociateDocument(ctx domain.RequestContext, m category.Member) (
|
|||
func (s Scope) DisassociateDocument(ctx domain.RequestContext, categoryID, documentID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
|
||||
sql := fmt.Sprintf("DELETE FROM categorymember WHERE orgid='%s' AND categoryid='%s' AND documentid='%s'",
|
||||
sql := fmt.Sprintf("DELETE FROM dmz_category_member WHERE c_orgid='%s' AND c_categoryid='%s' AND c_docid='%s'",
|
||||
ctx.OrgID, categoryID, documentID)
|
||||
|
||||
return b.DeleteWhere(ctx.Transaction, sql)
|
||||
|
@ -174,7 +183,7 @@ func (s Scope) DisassociateDocument(ctx domain.RequestContext, categoryID, docum
|
|||
func (s Scope) RemoveCategoryMembership(ctx domain.RequestContext, categoryID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
|
||||
sql := fmt.Sprintf("DELETE FROM categorymember WHERE orgid='%s' AND categoryid='%s'",
|
||||
sql := fmt.Sprintf("DELETE FROM dmz_category_member WHERE c_orgid='%s' AND c_categoryid='%s'",
|
||||
ctx.OrgID, categoryID)
|
||||
|
||||
return b.DeleteWhere(ctx.Transaction, sql)
|
||||
|
@ -184,7 +193,7 @@ func (s Scope) RemoveCategoryMembership(ctx domain.RequestContext, categoryID st
|
|||
func (s Scope) RemoveSpaceCategoryMemberships(ctx domain.RequestContext, spaceID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
|
||||
sql := fmt.Sprintf("DELETE FROM categorymember WHERE orgid='%s' AND labelid='%s'",
|
||||
sql := fmt.Sprintf("DELETE FROM dmz_category_member WHERE c_orgid='%s' AND c_spaceid='%s'",
|
||||
ctx.OrgID, spaceID)
|
||||
|
||||
return b.DeleteWhere(ctx.Transaction, sql)
|
||||
|
@ -194,7 +203,7 @@ func (s Scope) RemoveSpaceCategoryMemberships(ctx domain.RequestContext, spaceID
|
|||
func (s Scope) RemoveDocumentCategories(ctx domain.RequestContext, documentID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
|
||||
sql := fmt.Sprintf("DELETE FROM categorymember WHERE orgid='%s' AND documentid='%s'",
|
||||
sql := fmt.Sprintf("DELETE FROM dmz_category_member WHERE c_orgid='%s' AND c_docid='%s'",
|
||||
ctx.OrgID, documentID)
|
||||
|
||||
return b.DeleteWhere(ctx.Transaction, sql)
|
||||
|
@ -204,10 +213,10 @@ func (s Scope) RemoveDocumentCategories(ctx domain.RequestContext, documentID st
|
|||
func (s Scope) DeleteBySpace(ctx domain.RequestContext, spaceID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
|
||||
s1 := fmt.Sprintf("DELETE FROM categorymember WHERE orgid='%s' AND labelid='%s'", ctx.OrgID, spaceID)
|
||||
s1 := fmt.Sprintf("DELETE FROM categorymember WHERE c_orgid='%s' AND c_groupid='%s'", ctx.OrgID, spaceID)
|
||||
b.DeleteWhere(ctx.Transaction, s1)
|
||||
|
||||
s2 := fmt.Sprintf("DELETE FROM category WHERE orgid='%s' AND labelid='%s'", ctx.OrgID, spaceID)
|
||||
s2 := fmt.Sprintf("DELETE FROM dmz_category WHERE c_orgid='%s' AND c_spaceid='%s'", ctx.OrgID, spaceID)
|
||||
return b.DeleteWhere(ctx.Transaction, s2)
|
||||
}
|
||||
|
||||
|
@ -216,29 +225,29 @@ func (s Scope) GetSpaceCategorySummary(ctx domain.RequestContext, spaceID string
|
|||
c = []category.SummaryModel{}
|
||||
|
||||
err = s.Runtime.Db.Select(&c, `
|
||||
SELECT 'documents' as type, categoryid, COUNT(*) AS count
|
||||
FROM categorymember
|
||||
WHERE orgid=? AND labelid=?
|
||||
AND documentid IN (
|
||||
SELECT refid FROM document
|
||||
WHERE orgid=? AND labelid=?
|
||||
AND lifecycle!=2 AND template=0 AND groupid=''
|
||||
SELECT 'documents' AS type, c_categoryid, COUNT(*) AS count
|
||||
FROM dmz_category_member
|
||||
WHERE c_orgid=? AND c_spaceid=?
|
||||
AND c_docid IN (
|
||||
SELECT c_refid
|
||||
FROM dmz_doc
|
||||
WHERE c_orgid=? AND c_spaceid=? AND c_lifecycle!=2 AND c_template=0 AND c_groupid=''
|
||||
UNION ALL
|
||||
SELECT d.refid
|
||||
SELECT d.c_refid
|
||||
FROM (
|
||||
SELECT groupid, MIN(versionorder) AS latestversion
|
||||
FROM document
|
||||
WHERE orgid=? AND labelid=? AND lifecycle!=2 AND groupid!='' AND template=0
|
||||
GROUP BY groupid
|
||||
) AS x INNER JOIN document AS d ON d.groupid=x.groupid AND d.versionorder=x.latestversion
|
||||
SELECT c_groupid, MIN(c_versionorder) AS latestversion
|
||||
FROM dmz_doc
|
||||
WHERE c_orgid=? AND c_spaceid=? AND c_lifecycle!=2 AND c_groupid!='' AND c_template=0
|
||||
GROUP BY c_groupid
|
||||
) AS x INNER JOIN dmz_doc AS d ON d.c_groupid=x.c_groupid AND d.c_versionorder=x.latestversion
|
||||
)
|
||||
GROUP BY categoryid, type
|
||||
GROUP BY c_categoryid, c_type
|
||||
UNION ALL
|
||||
SELECT 'users' as type, refid AS categoryid, count(*) AS count
|
||||
FROM permission
|
||||
WHERE orgid=? AND location='category'
|
||||
AND refid IN (SELECT refid FROM category WHERE orgid=? AND labelid=?)
|
||||
GROUP BY refid, type`,
|
||||
SELECT 'users' AS type, c_refid AS categoryid, count(*) AS count
|
||||
FROM dmz_permission
|
||||
WHERE c_orgid=? AND c_location='category' AND c_refid IN
|
||||
(SELECT c_refid FROM dmz_category WHERE c_orgid=? AND c_spaceid=?)
|
||||
GROUP BY c_refid, c_type`,
|
||||
ctx.OrgID, spaceID,
|
||||
ctx.OrgID, spaceID, ctx.OrgID, spaceID,
|
||||
ctx.OrgID, ctx.OrgID, spaceID)
|
||||
|
@ -258,8 +267,9 @@ func (s Scope) GetDocumentCategoryMembership(ctx domain.RequestContext, document
|
|||
c = []category.Category{}
|
||||
|
||||
err = s.Runtime.Db.Select(&c, `
|
||||
SELECT id, refid, orgid, labelid, category, created, revised FROM category
|
||||
WHERE orgid=? AND refid IN (SELECT categoryid FROM categorymember WHERE orgid=? AND documentid=?)`, ctx.OrgID, ctx.OrgID, documentID)
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_refid IN (SELECT c_categoryid FROM dmz_category_member WHERE c_orgid=? AND c_docid=?)`, ctx.OrgID, ctx.OrgID, documentID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
|
@ -274,14 +284,17 @@ func (s Scope) GetDocumentCategoryMembership(ctx domain.RequestContext, document
|
|||
// GetSpaceCategoryMembership returns category/document associations within space.
|
||||
func (s Scope) GetSpaceCategoryMembership(ctx domain.RequestContext, spaceID string) (c []category.Member, err error) {
|
||||
err = s.Runtime.Db.Select(&c, `
|
||||
SELECT id, refid, orgid, labelid, categoryid, documentid, created, revised FROM categorymember
|
||||
WHERE orgid=? AND labelid=?
|
||||
AND labelid IN (SELECT refid FROM permission WHERE orgid=? AND location='space' AND refid IN (
|
||||
SELECT refid from permission WHERE orgid=? AND who='user' AND (whoid=? OR whoid='0') AND location='space' AND action='view' UNION ALL
|
||||
SELECT p.refid from permission p LEFT JOIN rolemember r ON p.whoid=r.roleid WHERE p.orgid=? AND p.who='role' AND p.location='space'
|
||||
AND p.action='view' AND (r.userid=? OR r.userid='0')
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_categoryid AS categoryid, c_docid AS documentid, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category_member
|
||||
WHERE c_orgid=? AND c_spaceid=? AND spaceid IN
|
||||
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_location='space' AND c_refid IN
|
||||
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_who='user' AND (c_whoid=? OR c_whoid='0') AND c_location='space' AND c_action='view'
|
||||
UNION ALL
|
||||
SELECT p.c_refid FROM dmz_permission p LEFT JOIN dmz_group_member r ON p.c_whoid=r.c_groupid
|
||||
WHERE p.c_orgid=? AND p.c_who='role' AND p.c_location='space'
|
||||
AND p.c_action='view' AND (r.c_userid=? OR r.c_userid='0')
|
||||
))
|
||||
ORDER BY documentid`, ctx.OrgID, spaceID, ctx.OrgID, ctx.OrgID, ctx.UserID, ctx.OrgID, ctx.UserID)
|
||||
ORDER BY documentid`, ctx.OrgID, spaceID, ctx.OrgID, ctx.OrgID, ctx.UserID, ctx.OrgID, ctx.UserID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
|
@ -296,14 +309,16 @@ func (s Scope) GetSpaceCategoryMembership(ctx domain.RequestContext, spaceID str
|
|||
// GetOrgCategoryMembership returns category/document associations within organization.
|
||||
func (s Scope) GetOrgCategoryMembership(ctx domain.RequestContext, userID string) (c []category.Member, err error) {
|
||||
err = s.Runtime.Db.Select(&c, `
|
||||
SELECT id, refid, orgid, labelid, categoryid, documentid, created, revised FROM categorymember
|
||||
WHERE orgid=?
|
||||
AND labelid IN (SELECT refid FROM permission WHERE orgid=? AND location='space' AND refid IN (
|
||||
SELECT refid from permission WHERE orgid=? AND who='user' AND (whoid=? OR whoid='0') AND location='space' AND action='view' UNION ALL
|
||||
SELECT p.refid from permission p LEFT JOIN rolemember r ON p.whoid=r.roleid WHERE p.orgid=? AND p.who='role' AND p.location='space'
|
||||
AND p.action='view' AND (r.userid=? OR r.userid='0')
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_categoryid AS categoryid, c_docid AS documentid, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category_member
|
||||
WHERE c_orgid=? AND c_spaceid IN
|
||||
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_location='space' AND c_refid IN
|
||||
(SELECT c_refid from dmz_permission WHERE c_orgid=? AND c_who='user' AND (c_whoid=? OR c_whoid='0') AND c_location='space' AND c_action='view'
|
||||
UNION ALL
|
||||
SELECT p.c_refid from dmz_permission p LEFT JOIN dmz_group_member r ON p.c_whoid=r.c_groupid WHERE p.c_orgid=? AND p.c_who='role' AND p.c_location='space'
|
||||
AND p.c_action='view' AND (r.c_userid=? OR r.c_userid='0')
|
||||
))
|
||||
ORDER BY documentid`, ctx.OrgID, ctx.OrgID, ctx.OrgID, userID, ctx.OrgID, userID)
|
||||
ORDER BY documentid`, ctx.OrgID, ctx.OrgID, ctx.OrgID, userID, ctx.OrgID, userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue