-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
63 lines (54 loc) · 1.55 KB
/
policy.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
package store
//go:generate counterfeiter -o fakes/policy_repo.go --fake-name PolicyRepo . PolicyRepo
type PolicyRepo interface {
Create(Transaction, int, int) error
Delete(Transaction, int, int) error
CountWhereGroupID(Transaction, int) (int, error)
CountWhereDestinationID(Transaction, int) (int, error)
}
type PolicyTable struct {
}
func (p *PolicyTable) Create(tx Transaction, source_group_id int, destination_id int) error {
dualStatement := ""
if tx.DriverName() == "mysql" {
dualStatement = " FROM DUAL "
}
_, err := tx.Exec(tx.Rebind(`
INSERT INTO policies (group_id, destination_id)
SELECT ?, ? `+dualStatement+`
WHERE
NOT EXISTS (
SELECT *
FROM policies
WHERE group_id = ? AND destination_id = ?
)`),
source_group_id,
destination_id,
source_group_id,
destination_id,
)
return err
}
func (p *PolicyTable) Delete(tx Transaction, source_group_id int, destination_id int) error {
_, err := tx.Exec(tx.Rebind(`DELETE FROM policies WHERE group_id = ? AND destination_id = ?`),
source_group_id,
destination_id,
)
return err
}
func (p *PolicyTable) CountWhereGroupID(tx Transaction, source_group_id int) (int, error) {
var count int
err := tx.QueryRow(
tx.Rebind(`SELECT COUNT(*) FROM policies WHERE group_id = ?`),
source_group_id,
).Scan(&count)
return count, err
}
func (p *PolicyTable) CountWhereDestinationID(tx Transaction, destination_id int) (int, error) {
var count int
err := tx.QueryRow(
tx.Rebind(`SELECT COUNT(*) FROM policies WHERE destination_id = ?`),
destination_id,
).Scan(&count)
return count, err
}