forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
77 lines (59 loc) · 1.24 KB
/
dao.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package sql
import (
"github.com/pydio/cells/common/dao"
"github.com/pydio/cells/common/sql"
"github.com/pydio/cells/x/configx"
)
func NewDAO(o dao.DAO) dao.DAO {
switch v := o.(type) {
case sql.DAO:
return &sqlimpl{DAO: v}
}
return nil
}
type DAO interface {
dao.DAO
Get() ([]byte, error)
Set([]byte) error
}
var queries = map[string]interface{}{
"get": "select data from %%PREFIX%%_config where id = 1",
"set": "insert into %%PREFIX%%_config(id, data) values (1, ?) on duplicate key update data = ?",
}
type sqlimpl struct {
sql.DAO
}
// Init handler for the SQL DAO
func (s *sqlimpl) Init(options configx.Values) error {
// super
s.DAO.Init(options)
// Preparing the db statements
if options.Val("prepare").Default(true).Bool() {
for key, query := range queries {
if err := s.Prepare(key, query); err != nil {
return err
}
}
}
return nil
}
func (s *sqlimpl) Get() ([]byte, error) {
stmt, err := s.DAO.GetStmt("get")
if err != nil {
return nil, err
}
var b []byte
row := stmt.QueryRow()
row.Scan(&b)
return b, nil
}
func (s *sqlimpl) Set(data []byte) error {
stmt, err := s.DAO.GetStmt("set")
if err != nil {
return err
}
if _, err := stmt.Exec(data, data); err != nil {
return err
}
return nil
}