-
Notifications
You must be signed in to change notification settings - Fork 0
/
destination.go
76 lines (69 loc) · 1.96 KB
/
destination.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
package store
//go:generate counterfeiter -o fakes/destination_repo.go --fake-name DestinationRepo . DestinationRepo
type DestinationRepo interface {
Create(Transaction, int, int, int, int, string) (int, error)
Delete(Transaction, int) error
GetID(Transaction, int, int, int, int, string) (int, error)
CountWhereGroupID(Transaction, int) (int, error)
}
type DestinationTable struct {
}
func (d *DestinationTable) Create(tx Transaction, destination_group_id, port, startPort, endPort int, protocol string) (int, error) {
dualStatement := ""
if tx.DriverName() == "mysql" {
dualStatement = " FROM DUAL "
}
_, err := tx.Exec(tx.Rebind(`
INSERT INTO destinations (group_id, port, start_port, end_port, protocol)
SELECT ?, ?, ?, ?, ? `+dualStatement+`
WHERE
NOT EXISTS (
SELECT *
FROM destinations
WHERE group_id = ? AND port = ? AND start_port = ? AND end_port = ? AND protocol = ?
)`),
destination_group_id,
port,
startPort,
endPort,
protocol,
destination_group_id,
port,
startPort,
endPort,
protocol,
)
if err != nil {
return -1, err
}
id, err := d.GetID(tx, destination_group_id, port, startPort, endPort, protocol)
return id, err
}
func (d *DestinationTable) Delete(tx Transaction, id int) error {
_, err := tx.Exec(
tx.Rebind(`DELETE FROM destinations WHERE id = ?`),
id,
)
return err
}
func (d *DestinationTable) GetID(tx Transaction, destination_group_id, port, startPort, endPort int, protocol string) (int, error) {
var id int
err := tx.QueryRow(tx.Rebind(`
SELECT id FROM destinations
WHERE group_id = ? AND port = ? AND start_port = ? AND end_port = ? AND protocol = ? FOR UPDATE`),
destination_group_id,
port,
startPort,
endPort,
protocol,
).Scan(&id)
return id, err
}
func (d *DestinationTable) CountWhereGroupID(tx Transaction, group_id int) (int, error) {
var count int
err := tx.QueryRow(
tx.Rebind(`SELECT COUNT(*) FROM destinations WHERE group_id = ?`),
group_id,
).Scan(&count)
return count, err
}