1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-02 20:15:26 +02:00

PostgreSQL prep

Update of vendored SQL libs and refactoring of store provider layer.
This commit is contained in:
HarveyKandola 2018-09-26 17:59:56 +01:00
parent d0e005f638
commit b455e5eaf5
105 changed files with 10949 additions and 2376 deletions

View file

@ -80,16 +80,16 @@ func (h *Handler) SetSMTP(w http.ResponseWriter, r *http.Request) {
var config string
config = string(body)
ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
// ctx.Transaction, err = h.Runtime.Db.Beginx()
// if err != nil {
// response.WriteServerError(w, method, err)
// h.Runtime.Log.Error(method, err)
// return
// }
h.Store.Setting.Set("SMTP", config)
ctx.Transaction.Commit()
// ctx.Transaction.Commit()
h.Store.Audit.Record(ctx, audit.EventTypeSystemSMTP)

View file

@ -9,33 +9,30 @@
//
// https://documize.com
package mysql
package setting
import (
"bytes"
"database/sql"
"fmt"
"github.com/documize/community/core/env"
"github.com/documize/community/domain"
"github.com/documize/community/domain/store"
"github.com/pkg/errors"
)
// Scope provides data access to MySQL.
type Scope struct {
Runtime *env.Runtime
// Store provides data access to user permission information.
type Store struct {
store.Context
domain.SettingStorer
}
// Get fetches a configuration JSON element from the config table.
func (s Scope) Get(area, path string) (value string, err error) {
if path != "" {
path = "." + path
}
sql := "SELECT JSON_EXTRACT(c_config,'$" + path + "') FROM dmz_config WHERE c_key = '" + area + "';"
func (s Store) Get(area, path string) (value string, err error) {
qry := fmt.Sprintf("SELECT %s FROM dmz_config WHERE c_key = '%s';", s.GetJSONValue("c_config", path), area)
var item = make([]uint8, 0)
err = s.Runtime.Db.Get(&item, sql)
err = s.Runtime.Db.Get(&item, qry)
if err != nil {
return "", err
}
@ -49,16 +46,33 @@ func (s Scope) Get(area, path string) (value string, err error) {
}
// Set writes a configuration JSON element to the config table.
func (s Scope) Set(area, json string) (err error) {
func (s Store) Set(area, json string) (err error) {
if area == "" {
return errors.New("no area")
}
sql := "INSERT INTO dmz_config (c_key,c_config) " +
"VALUES ('" + area + "','" + json +
"') ON DUPLICATE KEY UPDATE c_config='" + json + "';"
tx, err := s.Runtime.Db.Beginx()
if err != nil {
s.Runtime.Log.Error(fmt.Sprintf("setting.Set %s", area), err)
return
}
_, err = s.Runtime.Db.Exec(sql)
_, err = tx.Exec(fmt.Sprintf("DELETE FROM dmz_config WHERE c_key = '%s'", area))
if err != nil && err != sql.ErrNoRows {
tx.Rollback()
s.Runtime.Log.Error(fmt.Sprintf("setting.Set %s", area), err)
return err
}
_, err = tx.Exec(fmt.Sprintf("INSERT INTO dmz_config (c_key,c_config) VALUES ('%s','%s')",
area, json))
if err != nil && err != sql.ErrNoRows {
tx.Rollback()
s.Runtime.Log.Error(fmt.Sprintf("setting.Set %s", area), err)
return err
}
tx.Commit()
return err
}
@ -66,15 +80,11 @@ func (s Scope) Set(area, json string) (err error) {
// GetUser fetches a configuration JSON element from the userconfig table for a given orgid/userid combination.
// Errors return the empty string. A blank path returns the whole JSON object, as JSON.
// You can store org level settings by providing an empty user ID.
func (s Scope) GetUser(orgID, userID, key, path string) (value string, err error) {
func (s Store) GetUser(orgID, userID, key, path string) (value string, err error) {
var item = make([]uint8, 0)
if path != "" {
path = "." + path
}
qry := "SELECT JSON_EXTRACT(c_config,'$" + path + "') FROM dmz_user_config WHERE c_key = '" + key +
"' AND c_orgid = '" + orgID + "' AND c_userid = '" + userID + "';"
qry := fmt.Sprintf("SELECT %s FROM dmz_config WHERE c_key = '%s' AND c_orgid='%s' AND c_userid='%s';",
s.GetJSONValue("c_config", path), key, orgID, userID)
err = s.Runtime.Db.Get(&item, qry)
if err != nil && err != sql.ErrNoRows {
@ -91,7 +101,7 @@ func (s Scope) GetUser(orgID, userID, key, path string) (value string, err error
// SetUser writes a configuration JSON element to the userconfig table for the specified user.
// You can store org level settings by providing an empty user ID.
func (s Scope) SetUser(orgID, userID, key, json string) (err error) {
func (s Store) SetUser(orgID, userID, key, json string) (err error) {
if key == "" {
return errors.New("no key")
}
@ -101,16 +111,16 @@ func (s Scope) SetUser(orgID, userID, key, json string) (err error) {
return err
}
_, err = tx.Exec("DELETE FROM dmz_user_config WHERE c_orgid=? AND c_userid=? AND c_key=?", orgID, userID, key)
_, err = tx.Exec(s.Bind("DELETE FROM dmz_user_config WHERE c_orgid=? AND c_userid=? AND c_key=?"),
orgID, userID, key)
if err != nil {
fmt.Println(err)
fmt.Println("ccc")
}
_, err = tx.Exec("INSERT INTO dmz_user_config (c_orgid, c_userid, c_key, c_config) VALUES (?, ?, ?, ?)", orgID, userID, key, json)
_, err = tx.Exec(s.Bind("INSERT INTO dmz_user_config (c_orgid, c_userid, c_key, c_config) VALUES (?, ?, ?, ?)"),
orgID, userID, key, json)
if err != nil {
fmt.Println(err)
fmt.Println("ddd")
}
if err == nil {