-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage.go
62 lines (50 loc) · 1.48 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package export
import (
"database/sql"
"github.com/cloudtrust/common-service/database/sqltypes"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
"github.com/pkg/errors"
)
const (
upsertConfigStmt = `UPSERT INTO config (
component_name,
version,
config)
VALUES ($1, $2, $3)`
selectConfigStmt = `SELECT * FROM config WHERE (component_name = $1 AND version = $2)`
)
// StorageModule is the module that saves configs in Storage DB.
type StorageModule struct {
db DB
}
// DB interface
type DB interface {
Exec(query string, args ...interface{}) (sql.Result, error)
QueryRow(query string, args ...interface{}) sqltypes.SQLRow
}
// NewConfigStorageModule returns the storage module.
func NewConfigStorageModule(db DB) *StorageModule {
return &StorageModule{
db: db,
}
}
// Save is used to save config in database
func (c *StorageModule) Save(componentName, version string, config []byte) error {
var _, err = c.db.Exec(upsertConfigStmt, componentName, version, config)
if err != nil {
return errors.Wrapf(err, msg.MsgErrCannotUpdate+"."+msg.Config+"."+componentName+"."+version)
}
return nil
}
func (c *StorageModule) Read(componentName, version string) ([]byte, error) {
var row = c.db.QueryRow(selectConfigStmt, componentName, version)
var (
cName, v string
config []byte
)
var err = row.Scan(&cName, &v, &config)
if err != nil {
return nil, errors.Wrapf(err, msg.MsgErrCannotUpdate+"."+msg.Config+"."+componentName+"."+version)
}
return config, nil
}