mirror of
https://github.com/documize/community.git
synced 2025-08-07 22:45: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,31 +9,31 @@
|
|||
//
|
||||
// https://documize.com
|
||||
|
||||
package mysql
|
||||
package account
|
||||
|
||||
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/account"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Scope provides data access to MySQL.
|
||||
type Scope struct {
|
||||
Runtime *env.Runtime
|
||||
// Store provides data access to account information.
|
||||
type Store struct {
|
||||
store.Context
|
||||
domain.AccountStorer
|
||||
}
|
||||
|
||||
// Add inserts the given record into the datbase account table.
|
||||
func (s Scope) Add(ctx domain.RequestContext, account account.Account) (err error) {
|
||||
func (s Store) Add(ctx domain.RequestContext, account account.Account) (err error) {
|
||||
account.Created = time.Now().UTC()
|
||||
account.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec("INSERT INTO dmz_user_account (c_refid, c_orgid, c_userid, c_admin, c_editor, c_users, c_analytics, c_active, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_user_account (c_refid, c_orgid, c_userid, c_admin, c_editor, c_users, c_analytics, c_active, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"),
|
||||
account.RefID, account.OrgID, account.UserID, account.Admin, account.Editor, account.Users, account.Analytics, account.Active, account.Created, account.Revised)
|
||||
|
||||
if err != nil {
|
||||
|
@ -44,14 +44,14 @@ func (s Scope) Add(ctx domain.RequestContext, account account.Account) (err erro
|
|||
}
|
||||
|
||||
// GetUserAccount returns the database account record corresponding to the given userID, using the client's current organizaion.
|
||||
func (s Scope) GetUserAccount(ctx domain.RequestContext, userID string) (account account.Account, err error) {
|
||||
err = s.Runtime.Db.Get(&account, `
|
||||
func (s Store) GetUserAccount(ctx domain.RequestContext, userID string) (account account.Account, err error) {
|
||||
err = s.Runtime.Db.Get(&account, s.Bind(`
|
||||
SELECT a.id, a.c_refid AS refid, a.c_orgid AS orgid, a.c_userid AS userid,
|
||||
a.c_editor AS editor, a.c_admin AS admin, a.c_users AS users, a.c_analytics AS analytics,
|
||||
a.c_active AS active, a.c_created AS created, a.c_revised AS revised,
|
||||
b.c_company AS company, b.c_title AS title, b.c_message AS message, b.c_domain as domain
|
||||
FROM dmz_user_account a, dmz_org b
|
||||
WHERE b.c_refid=a.c_orgid AND a.c_orgid=? AND a.c_userid=?`,
|
||||
WHERE b.c_refid=a.c_orgid AND a.c_orgid=? AND a.c_userid=?`),
|
||||
ctx.OrgID, userID)
|
||||
|
||||
if err != sql.ErrNoRows && err != nil {
|
||||
|
@ -62,15 +62,15 @@ func (s Scope) GetUserAccount(ctx domain.RequestContext, userID string) (account
|
|||
}
|
||||
|
||||
// GetUserAccounts returns a slice of database account records, for all organizations that the userID is a member of, in organization title order.
|
||||
func (s Scope) GetUserAccounts(ctx domain.RequestContext, userID string) (t []account.Account, err error) {
|
||||
err = s.Runtime.Db.Select(&t, `
|
||||
func (s Store) GetUserAccounts(ctx domain.RequestContext, userID string) (t []account.Account, err error) {
|
||||
err = s.Runtime.Db.Select(&t, s.Bind(`
|
||||
SELECT a.id, a.c_refid AS refid, a.c_orgid AS orgid, a.c_userid AS userid,
|
||||
a.c_editor AS editor, a.c_admin AS admin, a.c_users AS users, a.c_analytics AS analytics,
|
||||
a.c_active AS active, a.c_created AS created, a.c_revised AS revised,
|
||||
b.c_company AS company, b.c_title AS title, b.c_message AS message, b.c_domain as domain
|
||||
FROM dmz_user_account a, dmz_org b
|
||||
WHERE a.c_userid=? AND a.c_orgid=b.c_refid AND a.c_active=1
|
||||
ORDER BY b.c_title`,
|
||||
ORDER BY b.c_title`),
|
||||
userID)
|
||||
|
||||
if err != sql.ErrNoRows && err != nil {
|
||||
|
@ -81,14 +81,14 @@ func (s Scope) GetUserAccounts(ctx domain.RequestContext, userID string) (t []ac
|
|||
}
|
||||
|
||||
// GetAccountsByOrg returns a slice of database account records, for all users in the client's organization.
|
||||
func (s Scope) GetAccountsByOrg(ctx domain.RequestContext) (t []account.Account, err error) {
|
||||
err = s.Runtime.Db.Select(&t, `
|
||||
func (s Store) GetAccountsByOrg(ctx domain.RequestContext) (t []account.Account, err error) {
|
||||
err = s.Runtime.Db.Select(&t, s.Bind(`
|
||||
SELECT a.id, a.c_refid AS refid, a.c_orgid AS orgid, a.c_userid AS userid,
|
||||
a.c_editor AS editor, a.c_admin AS admin, a.c_users AS users, a.c_analytics AS analytics,
|
||||
a.c_active AS active, a.c_created AS created, a.c_revised AS revised,
|
||||
b.c_company AS company, b.c_title AS title, b.c_message AS message, b.c_domain as domain
|
||||
FROM dmz_user_account a, dmz_org b
|
||||
WHERE a.c_orgid=b.c_refid AND a.c_orgid=? AND a.c_active=1`,
|
||||
WHERE a.c_orgid=b.c_refid AND a.c_orgid=? AND a.c_active=1`),
|
||||
ctx.OrgID)
|
||||
|
||||
if err != sql.ErrNoRows && err != nil {
|
||||
|
@ -99,8 +99,8 @@ func (s Scope) GetAccountsByOrg(ctx domain.RequestContext) (t []account.Account,
|
|||
}
|
||||
|
||||
// CountOrgAccounts returns the numnber of active user accounts for specified organization.
|
||||
func (s Scope) CountOrgAccounts(ctx domain.RequestContext) (c int) {
|
||||
row := s.Runtime.Db.QueryRow("SELECT count(*) FROM dmz_user_account WHERE c_orgid=? AND c_active=1", ctx.OrgID)
|
||||
func (s Store) CountOrgAccounts(ctx domain.RequestContext) (c int) {
|
||||
row := s.Runtime.Db.QueryRow(s.Bind("SELECT count(*) FROM dmz_user_account WHERE c_orgid=? AND c_active=1"), ctx.OrgID)
|
||||
err := row.Scan(&c)
|
||||
if err == sql.ErrNoRows {
|
||||
return 0
|
||||
|
@ -114,13 +114,13 @@ func (s Scope) CountOrgAccounts(ctx domain.RequestContext) (c int) {
|
|||
}
|
||||
|
||||
// UpdateAccount updates the database record for the given account to the given values.
|
||||
func (s Scope) UpdateAccount(ctx domain.RequestContext, account account.Account) (err error) {
|
||||
func (s Store) UpdateAccount(ctx domain.RequestContext, account account.Account) (err error) {
|
||||
account.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.NamedExec(`
|
||||
_, err = ctx.Transaction.NamedExec(s.Bind(`
|
||||
UPDATE dmz_user_account SET
|
||||
c_userid=:userid, c_admin=:admin, c_editor=:editor, c_users=:users, c_analytics=:analytics,
|
||||
c_active=:active, c_revised=:revised WHERE c_orgid=:orgid AND c_refid=:refid`, &account)
|
||||
c_active=:active, c_revised=:revised WHERE c_orgid=:orgid AND c_refid=:refid`), &account)
|
||||
|
||||
if err != sql.ErrNoRows && err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("execute update for account %s", account.RefID))
|
||||
|
@ -130,8 +130,8 @@ func (s Scope) UpdateAccount(ctx domain.RequestContext, account account.Account)
|
|||
}
|
||||
|
||||
// HasOrgAccount returns if the given orgID has valid userID.
|
||||
func (s Scope) HasOrgAccount(ctx domain.RequestContext, orgID, userID string) bool {
|
||||
row := s.Runtime.Db.QueryRow("SELECT count(*) FROM dmz_user_account WHERE c_orgid=? and c_userid=?", orgID, userID)
|
||||
func (s Store) HasOrgAccount(ctx domain.RequestContext, orgID, userID string) bool {
|
||||
row := s.Runtime.Db.QueryRow(s.Bind("SELECT count(*) FROM dmz_user_account WHERE c_orgid=? and c_userid=?"), orgID, userID)
|
||||
|
||||
var count int
|
||||
err := row.Scan(&count)
|
||||
|
@ -152,7 +152,6 @@ func (s Scope) HasOrgAccount(ctx domain.RequestContext, orgID, userID string) bo
|
|||
}
|
||||
|
||||
// DeleteAccount deletes the database record in the account table for user ID.
|
||||
func (s Scope) DeleteAccount(ctx domain.RequestContext, ID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
return b.DeleteConstrained(ctx.Transaction, "dmz_user_account", ctx.OrgID, ID)
|
||||
func (s Store) DeleteAccount(ctx domain.RequestContext, ID string) (rows int64, err error) {
|
||||
return s.DeleteConstrained(ctx.Transaction, "dmz_user_account", ctx.OrgID, ID)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue