-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.go
106 lines (90 loc) · 2.13 KB
/
rule.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
package proxysql
import (
"errors"
"fmt"
)
type Rule struct {
connection *Connection
ID int `yaml:"rule_id"`
Active int `yaml:"active"`
Apply int `yaml:"apply"`
HostgroupID int `yaml:"destination_hostgroup"`
MatchDigest string `yaml:"match_digest"`
Username string `yaml:"username"`
}
func (r *Rule) New(c *Connection) {
r.connection = c
}
func (r *Rule) Equal(in Rule) bool {
return r.ID == in.ID
}
func (r *Rule) Select() error {
if r.connection == nil {
return errors.New("Connection does not exist")
}
return r.connection.Instance.QueryRow(r.QuerySelect()).Scan(
&r.ID,
&r.Active,
&r.Apply,
&r.HostgroupID,
&r.Username,
&r.MatchDigest,
)
}
func (r *Rule) Insert() error {
if r.connection == nil {
return errors.New("Connection does not exist")
}
_, err := r.connection.Instance.Query(r.QueryInsert())
return err
}
func (r *Rule) Update() error {
if r.connection == nil {
return errors.New("Connection does not exist")
}
_, err := r.connection.Instance.Query(r.QueryUpdate())
return err
}
func (r *Rule) Delete() error {
if r.connection == nil {
return errors.New("Connection does not exist")
}
_, err := r.connection.Instance.Query(r.QueryDelete())
return err
}
func (r *Rule) QuerySelect() string {
return fmt.Sprintf(
"SELECT rule_id, active, apply, destination_hostgroup, username, match_digest FROM mysql_query_rules WHERE rule_id = %d;",
r.ID,
)
}
func (r *Rule) QueryInsert() string {
return fmt.Sprintf(
"INSERT INTO mysql_query_rules (rule_id, active, apply, destination_hostgroup, username, match_digest) VALUES (%d, %d, %d, %d, '%s', '%s');",
r.ID,
r.Active,
r.Apply,
r.HostgroupID,
r.Username,
r.MatchDigest,
)
}
func (r *Rule) QueryUpdate() string {
return fmt.Sprintf(
"UPDATE mysql_query_rules "+
"SET active = %d, apply = %d, destination_hostgroup = %d, username = '%s', match_digest = '%s' "+
"WHERE rule_id = %d;",
r.Active,
r.Apply,
r.HostgroupID,
r.Username,
r.MatchDigest,
r.ID,
)
}
func (r *Rule) QueryDelete() string {
return fmt.Sprintf(
"DELETE FROM mysql_query_rules WHERE rule_id = %d;",
r.ID,
)
}