mirror of
https://github.com/documize/community.git
synced 2025-08-07 06:25:23 +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
|
@ -20,23 +20,24 @@ import (
|
|||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/store/mysql"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/model/org"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Scope provides data access to MySQL.
|
||||
type Scope struct {
|
||||
Runtime *env.Runtime
|
||||
// Store provides data access to organization (tenant) information.
|
||||
type Store struct {
|
||||
store.Context
|
||||
domain.OrganizationStorer
|
||||
}
|
||||
|
||||
// AddOrganization inserts the passed organization record into the organization table.
|
||||
func (s Scope) AddOrganization(ctx domain.RequestContext, org org.Organization) (err error) {
|
||||
func (s Store) AddOrganization(ctx domain.RequestContext, org org.Organization) (err error) {
|
||||
org.Created = time.Now().UTC()
|
||||
org.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec(
|
||||
"INSERT INTO dmz_org (c_refid, c_company, c_title, c_message, c_domain, c_email, c_anonaccess, c_serial, c_maxtags, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
s.Bind("INSERT INTO dmz_org (c_refid, c_company, c_title, c_message, c_domain, c_email, c_anonaccess, c_serial, c_maxtags, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"),
|
||||
org.RefID, org.Company, org.Title, org.Message, strings.ToLower(org.Domain),
|
||||
strings.ToLower(org.Email), org.AllowAnonymousAccess, org.Serial, org.MaxTags, org.Created, org.Revised)
|
||||
|
||||
|
@ -48,15 +49,15 @@ func (s Scope) AddOrganization(ctx domain.RequestContext, org org.Organization)
|
|||
}
|
||||
|
||||
// GetOrganization returns the Organization reocrod from the organization database table with the given id.
|
||||
func (s Scope) GetOrganization(ctx domain.RequestContext, id string) (org org.Organization, err error) {
|
||||
stmt, err := s.Runtime.Db.Preparex(`SELECT id, c_refid AS refid,
|
||||
func (s Store) GetOrganization(ctx domain.RequestContext, id string) (org org.Organization, err error) {
|
||||
stmt, err := s.Runtime.Db.Preparex(s.Bind(`SELECT id, c_refid AS refid,
|
||||
c_title AS title, c_message AS message, c_domain AS domain,
|
||||
c_service AS conversionendpoint, c_email AS email, c_serial AS serial, c_active AS active,
|
||||
c_anonaccess AS allowanonymousaccess, c_authprovider AS authprovider,
|
||||
coalesce(c_authconfig,JSON_UNQUOTE('{}')) AS authconfig, c_maxtags AS maxtags,
|
||||
coalesce(c_authconfig,` + s.EmptyJSON() + `) AS authconfig, c_maxtags AS maxtags,
|
||||
c_created AS created, c_revised AS revised
|
||||
FROM dmz_org
|
||||
WHERE c_refid=?`)
|
||||
WHERE c_refid=?`))
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -75,7 +76,7 @@ func (s Scope) GetOrganization(ctx domain.RequestContext, id string) (org org.Or
|
|||
|
||||
// GetOrganizationByDomain returns the organization matching a given URL subdomain.
|
||||
// No context is required because user might not be authenticated yet.
|
||||
func (s Scope) GetOrganizationByDomain(subdomain string) (o org.Organization, err error) {
|
||||
func (s Store) GetOrganizationByDomain(subdomain string) (o org.Organization, err error) {
|
||||
err = nil
|
||||
subdomain = strings.TrimSpace(strings.ToLower(subdomain))
|
||||
|
||||
|
@ -86,14 +87,15 @@ func (s Scope) GetOrganizationByDomain(subdomain string) (o org.Organization, er
|
|||
}
|
||||
|
||||
// match on given domain name
|
||||
err = s.Runtime.Db.Get(&o, `SELECT id, c_refid AS refid,
|
||||
err = s.Runtime.Db.Get(&o, s.Bind(`SELECT id, c_refid AS refid,
|
||||
c_title AS title, c_message AS message, c_domain AS domain,
|
||||
c_service AS conversionendpoint, c_email AS email, c_serial AS serial, c_active AS active,
|
||||
c_anonaccess AS allowanonymousaccess, c_authprovider AS authprovider,
|
||||
coalesce(c_authconfig,JSON_UNQUOTE('{}')) AS authconfig, c_maxtags AS maxtags,
|
||||
coalesce(c_authconfig,`+s.EmptyJSON()+`) AS authconfig, c_maxtags AS maxtags,
|
||||
c_created AS created, c_revised AS revised
|
||||
FROM dmz_org
|
||||
WHERE c_domain=? AND c_active=1`, subdomain)
|
||||
WHERE c_domain=? AND c_active=1`),
|
||||
subdomain)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
@ -101,14 +103,15 @@ func (s Scope) GetOrganizationByDomain(subdomain string) (o org.Organization, er
|
|||
err = nil
|
||||
|
||||
// match on empty domain AS last resort
|
||||
err = s.Runtime.Db.Get(&o, `SELECT id, c_refid AS refid,
|
||||
err = s.Runtime.Db.Get(&o, s.Bind(`SELECT id, c_refid AS refid,
|
||||
c_title AS title, c_message AS message, c_domain AS domain,
|
||||
c_service AS conversionendpoint, c_email AS email, c_serial AS serial, c_active AS active,
|
||||
c_anonaccess AS allowanonymousaccess, c_authprovider AS authprovider,
|
||||
coalesce(c_authconfig,JSON_UNQUOTE('{}')) AS authconfig, c_maxtags AS maxtags,
|
||||
coalesce(c_authconfig,`+s.EmptyJSON()+`) AS authconfig, c_maxtags AS maxtags,
|
||||
c_created AS created, c_revised AS revised
|
||||
FROM dmz_org
|
||||
WHERE c_domain='' AND c_active=1`)
|
||||
WHERE c_domain='' AND c_active=1`))
|
||||
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
err = errors.Wrap(err, "unable to execute select for empty subdomain")
|
||||
}
|
||||
|
@ -117,7 +120,7 @@ func (s Scope) GetOrganizationByDomain(subdomain string) (o org.Organization, er
|
|||
}
|
||||
|
||||
// UpdateOrganization updates the given organization record in the database to the values supplied.
|
||||
func (s Scope) UpdateOrganization(ctx domain.RequestContext, org org.Organization) (err error) {
|
||||
func (s Store) UpdateOrganization(ctx domain.RequestContext, org org.Organization) (err error) {
|
||||
org.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.NamedExec(`UPDATE dmz_org SET
|
||||
|
@ -134,14 +137,13 @@ func (s Scope) UpdateOrganization(ctx domain.RequestContext, org org.Organizatio
|
|||
}
|
||||
|
||||
// DeleteOrganization deletes the orgID organization from the organization table.
|
||||
func (s Scope) DeleteOrganization(ctx domain.RequestContext, orgID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
return b.Delete(ctx.Transaction, "dmz_org", orgID)
|
||||
func (s Store) DeleteOrganization(ctx domain.RequestContext, orgID string) (rows int64, err error) {
|
||||
return s.Delete(ctx.Transaction, "dmz_org", orgID)
|
||||
}
|
||||
|
||||
// RemoveOrganization sets the orgID organization to be inactive, thus executing a "soft delete" operation.
|
||||
func (s Scope) RemoveOrganization(ctx domain.RequestContext, orgID string) (err error) {
|
||||
_, err = ctx.Transaction.Exec("UPDATE dmz_org SET c_active=0 WHERE c_refid=?", orgID)
|
||||
func (s Store) RemoveOrganization(ctx domain.RequestContext, orgID string) (err error) {
|
||||
_, err = ctx.Transaction.Exec(s.Bind("UPDATE dmz_org SET c_active=0 WHERE c_refid=?"), orgID)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("unable to execute soft delete for org %s", orgID))
|
||||
|
@ -151,7 +153,7 @@ func (s Scope) RemoveOrganization(ctx domain.RequestContext, orgID string) (err
|
|||
}
|
||||
|
||||
// UpdateAuthConfig updates the given organization record in the database with the auth config details.
|
||||
func (s Scope) UpdateAuthConfig(ctx domain.RequestContext, org org.Organization) (err error) {
|
||||
func (s Store) UpdateAuthConfig(ctx domain.RequestContext, org org.Organization) (err error) {
|
||||
org.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.NamedExec(`UPDATE dmz_org SET
|
||||
|
@ -168,8 +170,8 @@ func (s Scope) UpdateAuthConfig(ctx domain.RequestContext, org org.Organization)
|
|||
}
|
||||
|
||||
// CheckDomain makes sure there is an organisation with the correct domain
|
||||
func (s Scope) CheckDomain(ctx domain.RequestContext, domain string) string {
|
||||
row := s.Runtime.Db.QueryRow("SELECT COUNT(*) FROM dmz_org WHERE c_domain=? AND c_active=1", domain)
|
||||
func (s Store) CheckDomain(ctx domain.RequestContext, domain string) string {
|
||||
row := s.Runtime.Db.QueryRow(s.Bind("SELECT COUNT(*) FROM dmz_org WHERE c_domain=? AND c_active=1"), domain)
|
||||
|
||||
var count int
|
||||
err := row.Scan(&count)
|
||||
|
@ -177,7 +179,6 @@ func (s Scope) CheckDomain(ctx domain.RequestContext, domain string) string {
|
|||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if count == 1 {
|
||||
return domain
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue