-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.go
More file actions
70 lines (58 loc) · 1.61 KB
/
Copy pathhelper.go
File metadata and controls
70 lines (58 loc) · 1.61 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
61
62
63
64
65
66
67
68
69
70
package pgxadapter
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
)
func (a *Adapter) genRow(ptype string, rule []string) []interface{} {
row := make([]interface{}, 7)
row[0] = ptype
for i := 0; i < len(rule) && i < 6; i++ {
row[i+1] = rule[i]
}
// Verify defaults for remaining columns (already nil/interface{} nil which maps to NULL)
// If we want empty strings instead of NULLs:
for i := len(rule) + 1; i < 7; i++ {
row[i] = ""
}
return row
}
func (a *Adapter) removePolicy(ctx context.Context, tx pgx.Tx, ptype string, rule []string) error {
query := fmt.Sprintf("DELETE FROM %s WHERE ptype = $1", a.tableName)
args := []interface{}{ptype}
for i, v := range rule {
query += fmt.Sprintf(" AND v%d = $%d", i, i+2)
args = append(args, v)
}
_, err := tx.Exec(ctx, query, args...)
return err
}
func (a *Adapter) updatePolicy(ctx context.Context, tx pgx.Tx, ptype string, oldRule, newRule []string) error {
query := fmt.Sprintf("UPDATE %s SET ptype = $1", a.tableName)
args := []interface{}{ptype}
argIndex := 2
// Construct UPDATE clause
// We need to update v0..v5 based on newRule
// and WHERE based on oldRule
// Set clause
for i := 0; i < 6; i++ {
val := ""
if i < len(newRule) {
val = newRule[i]
}
query += fmt.Sprintf(", v%d = $%d", i, argIndex)
args = append(args, val)
argIndex++
}
// Where clause
query += fmt.Sprintf(" WHERE ptype = $%d", argIndex)
args = append(args, ptype)
argIndex++
for i, v := range oldRule {
query += fmt.Sprintf(" AND v%d = $%d", i, argIndex)
args = append(args, v)
argIndex++
}
_, err := tx.Exec(ctx, query, args...)
return err
}