-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_db.go
83 lines (68 loc) · 1.72 KB
/
node_db.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
78
79
80
81
82
83
package dlm
import (
"context"
"database/sql"
"errors"
"log/slog"
"github.com/yaitoo/sqle"
)
func (n *Node) createLease(l Lease) error {
cmd := sqle.New().
Insert("dlm_lease").
Set("topic", l.Topic).
Set("key", l.Key).
Set("lessee", l.Lessee).
Set("since", l.Since).
Set("ttl", l.TTL).End()
_, err := n.db.ExecBuilder(context.Background(), cmd)
if err != nil {
n.logger.Warn("dlm: create lease", slog.String("err", err.Error()))
return ErrBadDatabase
}
return nil
}
func (n *Node) updateLease(l Lease) error {
cmd := sqle.New().
Update("dlm_lease").
Set("topic", l.Topic).
Set("key", l.Key).
Set("lessee", l.Lessee).
Set("since", l.Since).
Set("ttl", l.TTL).
Where("topic = {topic} and key ={key}").End()
_, err := n.db.ExecBuilder(context.Background(), cmd)
if err != nil {
n.logger.Warn("dlm: update lease", slog.String("err", err.Error()))
return ErrBadDatabase
}
return nil
}
func (n *Node) getLease(topic, key string) (Lease, error) {
var l Lease
cmd := sqle.New().
Select("dlm_lease").
Where("topic = {topic} AND key = {key}").
Param("topic", topic).
Param("key", key)
err := n.db.QueryRowBuilder(context.Background(), cmd).Bind(&l)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return l, ErrNoLease
}
n.logger.Warn("dlm: get lease", slog.String("err", err.Error()))
return l, ErrBadDatabase
}
return l, nil
}
func (n *Node) deleteLease(topic, key string) error {
_, err := n.db.ExecBuilder(context.Background(), sqle.New().
Delete("dlm_lease").
Where("topic = {topic} AND key = {key}").
Param("topic", topic).
Param("key", key))
if err != nil {
n.logger.Warn("dlm: get lease", slog.String("err", err.Error()))
return ErrBadDatabase
}
return nil
}