-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.go
More file actions
60 lines (49 loc) · 1.43 KB
/
Copy pathbatch.go
File metadata and controls
60 lines (49 loc) · 1.43 KB
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
package pgxadapter
import (
"context"
"github.com/jackc/pgx/v5"
)
// AddPolicies adds policy rules to the storage.
func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error {
return a.AddPoliciesCtx(context.Background(), sec, ptype, rules)
}
// AddPoliciesCtx adds policy rules to the storage with context.
func (a *Adapter) AddPoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error {
tx, err := a.pool.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
var rows [][]interface{}
for _, rule := range rules {
rows = append(rows, a.genRow(ptype, rule))
}
_, err = tx.CopyFrom(
ctx,
pgx.Identifier{a.tableName},
[]string{"ptype", "v0", "v1", "v2", "v3", "v4", "v5"},
pgx.CopyFromRows(rows),
)
if err != nil {
return err
}
return tx.Commit(ctx)
}
// RemovePolicies removes policy rules from the storage.
func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error {
return a.RemovePoliciesCtx(context.Background(), sec, ptype, rules)
}
// RemovePoliciesCtx removes policy rules from the storage with context.
func (a *Adapter) RemovePoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error {
tx, err := a.pool.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
for _, rule := range rules {
if err := a.removePolicy(ctx, tx, ptype, rule); err != nil {
return err
}
}
return tx.Commit(ctx)
}