1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00
documize/domain/setting/mysql/store.go

106 lines
2.6 KiB
Go
Raw Normal View History

2017-07-26 20:03:23 +01:00
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package mysql
import (
"bytes"
2017-08-08 11:24:21 +01:00
"database/sql"
2017-07-26 20:03:23 +01:00
"github.com/documize/community/core/env"
"github.com/pkg/errors"
)
// Scope provides data access to MySQL.
type Scope struct {
Runtime *env.Runtime
}
// Get fetches a configuration JSON element from the config table.
2017-08-27 16:39:09 +01:00
func (s Scope) Get(area, path string) (value string, err error) {
2017-07-26 20:03:23 +01:00
if path != "" {
path = "." + path
}
2017-09-25 14:37:11 +01:00
sql := "SELECT JSON_EXTRACT(`config`,'$" + path + "') FROM `config` WHERE `key` = '" + area + "';"
2017-07-26 20:03:23 +01:00
var item = make([]uint8, 0)
2017-09-25 14:37:11 +01:00
err = s.Runtime.Db.Get(&item, sql)
2017-07-26 20:03:23 +01:00
if err != nil {
2017-08-27 16:39:09 +01:00
return "", err
2017-07-26 20:03:23 +01:00
}
if len(item) > 1 {
q := []byte(`"`)
value = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
}
2017-08-27 16:39:09 +01:00
return value, nil
2017-07-26 20:03:23 +01:00
}
// Set writes a configuration JSON element to the config table.
2017-09-25 14:37:11 +01:00
func (s Scope) Set(area, json string) (err error) {
2017-07-26 20:03:23 +01:00
if area == "" {
return errors.New("no area")
}
sql := "INSERT INTO `config` (`key`,`config`) " +
"VALUES ('" + area + "','" + json +
"') ON DUPLICATE KEY UPDATE `config`='" + json + "';"
2017-09-25 14:37:11 +01:00
_, err = s.Runtime.Db.Exec(sql)
2017-07-26 20:03:23 +01:00
return err
}
// 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.
2017-08-27 16:39:09 +01:00
func (s Scope) GetUser(orgID, userID, area, path string) (value string, err error) {
2017-07-26 20:03:23 +01:00
if path != "" {
path = "." + path
}
2017-08-08 11:24:21 +01:00
qry := "SELECT JSON_EXTRACT(`config`,'$" + path + "') FROM `userconfig` WHERE `key` = '" + area +
2017-07-26 20:03:23 +01:00
"' AND `orgid` = '" + orgID + "' AND `userid` = '" + userID + "';"
var item = make([]uint8, 0)
2017-09-25 14:37:11 +01:00
err = s.Runtime.Db.Get(&item, qry)
2017-08-08 11:24:21 +01:00
if err != nil && err != sql.ErrNoRows {
2017-08-27 16:39:09 +01:00
return "", err
2017-07-26 20:03:23 +01:00
}
if len(item) > 1 {
q := []byte(`"`)
value = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
}
2017-08-27 16:39:09 +01:00
return value, nil
2017-07-26 20:03:23 +01:00
}
// SetUser writes a configuration JSON element to the userconfig table for the current user.
2017-09-25 14:37:11 +01:00
func (s Scope) SetUser(orgID, userID, area, json string) (err error) {
2017-07-26 20:03:23 +01:00
if area == "" {
return errors.New("no area")
}
sql := "INSERT INTO `userconfig` (`orgid`,`userid`,`key`,`config`) " +
"VALUES ('" + orgID + "','" + userID + "','" + area + "','" + json +
"') ON DUPLICATE KEY UPDATE `config`='" + json + "';"
2017-09-25 14:37:11 +01:00
_, err = s.Runtime.Db.Exec(sql)
2017-07-26 20:03:23 +01:00
return err
}