-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
117 lines (94 loc) · 3.28 KB
/
store.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package postgres
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/commitment"
)
type store struct {
db *sqlx.DB
}
// New returns a new in memory commitment.Store
func New(db *sql.DB) commitment.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Save implements commitment.Store.Save
func (s *store) Save(ctx context.Context, record *commitment.Record) error {
model, err := toModel(record)
if err != nil {
return err
}
if err := model.dbSave(ctx, s.db); err != nil {
return err
}
res := fromModel(model)
res.CopyTo(record)
return nil
}
// GetByAddress implements commitment.Store.GetByAddress
func (s *store) GetByAddress(ctx context.Context, address string) (*commitment.Record, error) {
model, err := dbGetByAddress(ctx, s.db, address)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetByVault implements commitment.Store.GetByVault
func (s *store) GetByVault(ctx context.Context, vault string) (*commitment.Record, error) {
model, err := dbGetByVault(ctx, s.db, vault)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetByAction implements commitment.Store.GetByAction
func (s *store) GetByAction(ctx context.Context, intentId string, actionId uint32) (*commitment.Record, error) {
model, err := dbGetByAction(ctx, s.db, intentId, actionId)
if err != nil {
return nil, err
}
return fromModel(model), nil
}
// GetAllByState implements commitment.Store.GetAllByState
func (s *store) GetAllByState(ctx context.Context, state commitment.State, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*commitment.Record, error) {
models, err := dbGetAllByState(ctx, s.db, state, cursor, limit, direction)
if err != nil {
return nil, err
}
res := make([]*commitment.Record, len(models))
for i, model := range models {
res[i] = fromModel(model)
}
return res, nil
}
// GetUpgradeableByOwner implements commitment.Store.GetUpgradeableByOwner
func (s *store) GetUpgradeableByOwner(ctx context.Context, owner string, limit uint64) ([]*commitment.Record, error) {
models, err := dbGetUpgradeableByOwner(ctx, s.db, owner, limit)
if err != nil {
return nil, err
}
res := make([]*commitment.Record, len(models))
for i, model := range models {
res[i] = fromModel(model)
}
return res, nil
}
// GetUsedTreasuryPoolDeficit implements commitment.Store.GetUsedTreasuryPoolDeficit
func (s *store) GetUsedTreasuryPoolDeficit(ctx context.Context, pool string) (uint64, error) {
return dbGetUsedTreasuryPoolDeficit(ctx, s.db, pool)
}
// GetTotalTreasuryPoolDeficit implements commitment.Store.GetTotalTreasuryPoolDeficit
func (s *store) GetTotalTreasuryPoolDeficit(ctx context.Context, pool string) (uint64, error) {
return dbGetTotalTreasuryPoolDeficit(ctx, s.db, pool)
}
// CountByState implements commitment.Store.CountByState
func (s *store) CountByState(ctx context.Context, state commitment.State) (uint64, error) {
return dbCountByState(ctx, s.db, state)
}
// CountRepaymentsDivertedToVault implements commitment.Store.CountRepaymentsDivertedToVault
func (s *store) CountRepaymentsDivertedToVault(ctx context.Context, vault string) (uint64, error) {
return dbCountRepaymentsDivertedToVault(ctx, s.db, vault)
}