/
filter.go
168 lines (145 loc) · 3.18 KB
/
filter.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"regexp"
"strings"
"github.com/ngaut/log"
)
var (
defaultIgnoreDB = "mysql"
triggerRegex = regexp.MustCompile(`(^CREATE (DEFINER=\S+ )*TRIGGER)`)
)
// whiteFilter whitelist filtering
func (s *Syncer) whiteFilter(stbs []TableName) []TableName {
var tbs []TableName
if len(s.cfg.DoTables) == 0 && len(s.cfg.DoDBs) == 0 {
return stbs
}
for _, tb := range stbs {
if s.matchTable(s.cfg.DoTables, tb) {
tbs = append(tbs, tb)
}
if s.matchDB(s.cfg.DoDBs, tb.Schema) {
tbs = append(tbs, tb)
}
}
return tbs
}
// blackFilter blacklist filtering
func (s *Syncer) blackFilter(stbs []TableName) []TableName {
var tbs []TableName
for _, tb := range stbs {
if s.matchTable(s.cfg.IgnoreTables, tb) {
continue
}
if s.matchDB(s.cfg.IgnoreDBs, tb.Schema) {
continue
}
tbs = append(tbs, tb)
}
return tbs
}
func (s *Syncer) skipQueryEvent(sql string, schema string) bool {
sql = strings.ToUpper(sql)
// For mariadb, for query event, like `# Dumm`
// But i don't know what is the meaning of this event.
if strings.HasPrefix(sql, "#") {
return true
}
if strings.HasPrefix(sql, "GRANT") {
return true
}
if strings.HasPrefix(sql, "FLUSH PRIVILEGES") {
return true
}
if strings.HasPrefix(sql, "ALTER USER") {
return true
}
if strings.HasPrefix(sql, "DROP USER") {
return true
}
if strings.HasPrefix(sql, "SAVEPOINT") {
return true
}
if strings.HasPrefix(sql, "OPTIMIZE TABLE") {
return true
}
if strings.HasPrefix(sql, "DROP TRIGGER") {
return true
}
if triggerRegex.FindStringIndex(sql) != nil {
return true
}
if schema == defaultIgnoreDB {
return true
}
return false
}
// skipRowEvent first whitelist filtering and then blacklist filtering
func (s *Syncer) skipRowEvent(schema string, table string) bool {
if schema == defaultIgnoreDB {
return true
}
tbs := []TableName{
{
Schema: strings.ToLower(schema),
Name: strings.ToLower(table),
},
}
tbs = s.whiteFilter(tbs)
tbs = s.blackFilter(tbs)
if len(tbs) == 0 {
return true
}
return false
}
// skipQueryDDL first whitelist filtering and then blacklist filtering
func (s *Syncer) skipQueryDDL(sql string, schema string) bool {
tb, err := parserDDLTableName(sql)
if err != nil {
log.Warnf("[get table failure]:%s %s", sql, err)
}
if tb.Schema == "" {
tb.Schema = schema
}
if tb.Schema == defaultIgnoreDB {
return true
}
if err == nil {
tbs := []TableName{tb}
tbs = s.whiteFilter(tbs)
tbs = s.blackFilter(tbs)
if len(tbs) == 0 {
return true
}
return false
}
return false
}
func (s *Syncer) matchString(pattern string, t string) bool {
if re, ok := s.reMap[pattern]; ok {
return re.MatchString(t)
}
return pattern == t
}
func (s *Syncer) matchDB(patternDBS []string, a string) bool {
for _, b := range patternDBS {
if s.matchString(b, a) {
return true
}
}
return false
}
func (s *Syncer) matchTable(patternTBS []TableName, tb TableName) bool {
for _, ptb := range patternTBS {
if s.matchString(ptb.Name, tb.Name) && s.matchString(ptb.Schema, tb.Schema) {
return true
}
//create database or drop database
if tb.Name == "" {
if s.matchString(tb.Schema, ptb.Schema) {
return true
}
}
}
return false
}