-
Notifications
You must be signed in to change notification settings - Fork 2
/
queryrules.go
621 lines (557 loc) · 14.2 KB
/
queryrules.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package proxysql
import (
"database/sql"
"fmt"
"strconv"
"strings"
"github.com/juju/errors"
)
type (
QueryRules struct {
Rule_id uint64 `db:"rule_id" json:"rule_id"`
Active uint64 `db:"active" json:"active"`
Username string `db:"username" json:"username"`
Schemaname string `db:"schemaname" json:"schemaname"`
FlagIN uint64 `db:"flagIN" json:"flagIN"`
Client_addr string `db:"client_addr" json:"client_addr"`
Proxy_addr string `db:"proxy_addr" json:"proxy_addr"`
Proxy_port string `db:"proxy_port" json:"proxy_port"`
Digest string `db:"digest" json:"digest"`
Match_digest string `db:"match_digest" json:"match_digest"`
Match_pattern string `db:"match_pattern" json:"match_pattern"`
Negate_match_pattern uint64 `db:"negate_match_pattern" json:"negate_match_pattern"`
FlagOUT string `db:"flagOUT" json:"flagOUT"`
Replace_pattern string `db:"replace_pattern" json:"replace_pattern"`
Destination_hostgroup string `db:"destination_hostgroup" json:"destination_hostgroup"`
Cache_ttl string `db:"cache_ttl" json:"cache_ttl"`
Reconnect string `db:"reconnect" json:"reconnect"`
Timeout string `db:"timeout" json:"timeout"`
Retries string `db:"retries" json:"retries"`
Delay string `db:"delay" json:"delay"`
Mirror_flagOUT string `db:"mirror_flagOUT" json:"mirror_flagOUT"`
Mirror_hostgroup string `db:"mirror_hostgroup" json:"mirror_hostgroup"`
Error_msg string `db:"error_msg" json:"error_msg"`
Log string `db:"log" json:"log"`
Apply uint64 `db:"apply" json:"apply"`
Comment string `db:"comment" json:"comment"`
}
)
const (
/*new query rules*/
StmtAddOneQr = `
INSERT
INTO
mysql_query_rules(rule_id,username)
VALUES(%d,%s)`
/*delete a query rules*/
StmtDeleteOneQr = `
DELETE
FROM
mysql_query_rules
WHERE rule_id = %d`
/*query all query rules.*/
StmtFindAllQr = `
SELECT
ifnull(rule_id,0) as rule_id,
ifnull(active,0) as active,
ifnull(username,"") as username,
ifnull(schemaname,"") as schemaname,
ifnull(flagIN,0) as flagIN,
ifnull(client_addr,"") as client_addr,
ifnull(proxy_addr,"") as proxy_addr,
ifnull(proxy_port,0) as proxy_port,
ifnull(digest,"") as digest,
ifnull(match_digest,"") as match_digest,
ifnull(match_pattern,"") as match_pattern,
ifnull(negate_match_pattern,0) as negate_match_pattern,
ifnull(flagOUT,0) as flagOUT,
ifnull(replace_pattern,"") as replace_pattern,
ifnull(destination_hostgroup,0) as destination_hostgroup,
ifnull(cache_ttl,0) as cache_ttl,
ifnull(reconnect,0) as reconnect,
ifnull(timeout,0) as timeout,
ifnull(retries,0) as retries,
ifnull(delay,0) as delay,
ifnull(mirror_flagOUT,0) as mirror_flagOUT,
ifnull(mirror_hostgroup,0) as mirror_hostgroup,
ifnull(error_msg,"") as error_msg,
ifnull(log,0) as log,
ifnull(apply,0) as apply,
ifnull(comment,"") as comment
FROM mysql_query_rules
LIMIT %d
OFFSET %d`
/*find last insert rule_id*/
StmtFindLastRuleId = `
SELECT
max(rule_id)
FROM mysql_query_rules
WHERE
username = %s`
/*update a query rules.*/
StmtUpdateOneQr = `
UPDATE
mysql_query_rules
SET
active=%d,
username=%s,
schemaname=%s,
flagIN=%d,
client_addr=%s,
proxy_addr=%s,
proxy_port=%s,
digest=%s,
match_digest=%s,
match_pattern=%s,
negate_match_pattern=%d,
flagOUT=%s,
replace_pattern=%s,
destination_hostgroup=%s,
cache_ttl=%s,
reconnect=%s,
timeout=%s,
retries=%s,
delay=%s,
mirror_flagOUT=%s,
mirror_hostgroup=%s,
error_msg=%s,
log=%s,
apply=%d,
comment=%s
WHERE
rule_id=%d`
)
// select * from mysql_query_rules limit n offset n
func FindAllQr(db *sql.DB, limit uint64, skip uint64) ([]QueryRules, error) {
var AllQr []QueryRules
Query := fmt.Sprintf(StmtFindAllQr, limit, skip)
// exec query statement
rows, err := db.Query(Query)
if err != nil {
return []QueryRules{}, errors.Trace(err)
}
defer rows.Close()
// scan results.
for rows.Next() {
var tmpqr QueryRules
err = rows.Scan(
&tmpqr.Rule_id,
&tmpqr.Active,
&tmpqr.Username,
&tmpqr.Schemaname,
&tmpqr.FlagIN,
&tmpqr.Client_addr,
&tmpqr.Proxy_addr,
&tmpqr.Proxy_port,
&tmpqr.Digest,
&tmpqr.Match_digest,
&tmpqr.Match_pattern,
&tmpqr.Negate_match_pattern,
&tmpqr.FlagOUT,
&tmpqr.Replace_pattern,
&tmpqr.Destination_hostgroup,
&tmpqr.Cache_ttl,
&tmpqr.Reconnect,
&tmpqr.Timeout,
&tmpqr.Retries,
&tmpqr.Delay,
&tmpqr.Mirror_flagOUT,
&tmpqr.Mirror_hostgroup,
&tmpqr.Error_msg,
&tmpqr.Log,
&tmpqr.Apply,
&tmpqr.Comment,
)
if err != nil {
continue
}
AllQr = append(AllQr, tmpqr)
}
return AllQr, nil
}
// new mysql query rules
func NewQr(username string) (*QueryRules, error) {
newqr := new(QueryRules)
if username == "" {
return nil, errors.BadRequestf(username)
}
if strings.Index(username, "\"") == -1 {
newqr.Username = fmt.Sprintf("\"%s\"", username)
} else {
newqr.Username = username
}
newqr.Destination_hostgroup = "NULL"
newqr.Schemaname = "NULL"
newqr.FlagIN = 0
newqr.Client_addr = "NULL"
newqr.Proxy_addr = "NULL"
newqr.Proxy_port = "NULL"
newqr.Digest = "NULL"
newqr.Match_digest = "NULL"
newqr.Match_pattern = "NULL"
newqr.Negate_match_pattern = 0
newqr.FlagOUT = "NULL"
newqr.Replace_pattern = "NULL"
newqr.Cache_ttl = "NULL"
newqr.Reconnect = "NULL"
newqr.Timeout = "NULL"
newqr.Retries = "NULL"
newqr.Delay = "NULL"
newqr.Mirror_flagOUT = "NULL"
newqr.Mirror_hostgroup = "NULL"
newqr.Error_msg = "NULL"
newqr.Log = "NULL"
newqr.Apply = 0
newqr.Active = 0
newqr.Comment = "NULL"
return newqr, nil
}
// set qr rule_id
func (qr *QueryRules) SetQrRuleid(rule_id uint64) {
qr.Rule_id = rule_id
}
// set qr active
func (qr *QueryRules) SetQrActive(active uint64) {
switch active {
case 0:
qr.Active = 0
case 1:
qr.Active = 1
default:
qr.Active = 1
}
}
// set qr apply
func (qr *QueryRules) SetQrApply(apply uint64) {
switch apply {
case 0:
qr.Apply = 0
case 1:
qr.Apply = 1
default:
qr.Apply = 1
}
}
// set qr schemaname
func (qr *QueryRules) SetQrSchemaname(schema_name string) {
if schema_name == "" || len(schema_name) == 0 {
qr.Schemaname = "NULL"
} else {
if strings.Index(schema_name, "\"") == -1 {
qr.Schemaname = fmt.Sprintf("\"%s\"", schema_name)
} else {
qr.Schemaname = schema_name
}
}
}
// set qr flagIN
func (qr *QueryRules) SetQrFlagIN(flag_in uint64) {
qr.FlagIN = flag_in
}
// set qr client_addr
func (qr *QueryRules) SetQrClientAddr(client_addr string) {
if client_addr == "" || len(client_addr) == 0 {
qr.Client_addr = "NULL"
} else {
if strings.Index(client_addr, "\"") == -1 {
qr.Client_addr = fmt.Sprintf("\"%s\"", client_addr)
} else {
qr.Client_addr = client_addr
}
}
}
// set qr proxy_addr
func (qr *QueryRules) SetQrProxyAddr(proxy_addr string) {
if proxy_addr == "" || len(proxy_addr) == 0 {
qr.Proxy_addr = "NULL"
} else {
if strings.Index(proxy_addr, "\"") == -1 {
qr.Proxy_addr = fmt.Sprintf("\"%s\"", proxy_addr)
} else {
qr.Proxy_addr = proxy_addr
}
}
}
// set qr proxy_port
func (qr *QueryRules) SetProxyPort(proxy_port string) {
if proxy_port == "" || len(proxy_port) == 0 {
qr.Proxy_port = "NULL"
} else {
if strings.Index(proxy_port, "\"") == -1 {
qr.Proxy_port = fmt.Sprintf("\"%s\"", proxy_port)
} else {
qr.Proxy_port = proxy_port
}
}
}
// set qr digest
func (qr *QueryRules) SetQrDigest(digest string) {
if digest == "" || len(digest) == 0 {
qr.Digest = "NULL"
} else {
if strings.Index(digest, "\"") == -1 {
qr.Digest = fmt.Sprintf("\"%s\"", digest)
} else {
qr.Digest = digest
}
}
}
// set qr match_digest
func (qr *QueryRules) SetQrMatchDigest(match_digest string) {
if match_digest == "" || len(match_digest) == 0 {
qr.Match_digest = "NULL"
} else {
if strings.Index(match_digest, "\"") == -1 {
qr.Match_digest = fmt.Sprintf("\"%s\"", match_digest)
} else {
qr.Match_digest = match_digest
}
}
}
// set qr match_pattern
func (qr *QueryRules) SetQrMatchPattern(match_pattern string) {
if match_pattern == "" || len(match_pattern) == 0 {
qr.Match_pattern = "NULL"
} else {
if strings.Index(match_pattern, "\"") == -1 {
qr.Match_pattern = fmt.Sprintf("\"%s\"", match_pattern)
} else {
qr.Match_pattern = match_pattern
}
}
}
// set qr mnegate_match_pattern
func (qr *QueryRules) SetQrNegateMatchPattern(negate_match_pattern uint64) {
switch negate_match_pattern {
case 0:
qr.Negate_match_pattern = 0
case 1:
qr.Negate_match_pattern = 1
default:
qr.Negate_match_pattern = 0
}
}
// set qr flagout
func (qr *QueryRules) SetQrFlagOut(flag_out string) {
if flag_out == "" || len(flag_out) == 0 {
qr.FlagOUT = "NULL"
} else {
if strings.Index(flag_out, "\"") == -1 {
qr.FlagOUT = fmt.Sprintf("\"%s\"", flag_out)
} else {
qr.FlagOUT = flag_out
}
}
}
// set qr replace_pattern
func (qr *QueryRules) SetQrReplacePattern(replace_pattern string) {
if replace_pattern == "" || len(replace_pattern) == 0 {
qr.Replace_pattern = "NULL"
} else {
if strings.Index(replace_pattern, "\"") == -1 {
qr.Replace_pattern = fmt.Sprintf("\"%s\"", replace_pattern)
} else {
qr.Replace_pattern = replace_pattern
}
}
}
// set qr destination_hostgroup
func (qr *QueryRules) SetQrDestHostGroup(destination_hostgroup string) {
if destination_hostgroup == "" || len(destination_hostgroup) == 0 {
qr.Destination_hostgroup = "NULL"
} else {
if strings.Index(destination_hostgroup, "\"") == -1 {
qr.Destination_hostgroup = fmt.Sprintf("\"%s\"", destination_hostgroup)
} else {
qr.Destination_hostgroup = destination_hostgroup
}
}
}
// set qr cache_ttl
func (qr *QueryRules) SetQrCacheTTL(cache_ttl string) {
if cache_ttl == "" || len(cache_ttl) == 0 {
qr.Cache_ttl = "NULL"
} else {
if strings.Index(cache_ttl, "\"") == -1 {
qr.Cache_ttl = fmt.Sprintf("\"%s\"", cache_ttl)
} else {
qr.Cache_ttl = cache_ttl
}
}
}
// set qr reconnect
func (qr *QueryRules) SetQrReconnect(reconnect string) {
if reconnect == "" || len(reconnect) == 0 {
qr.Reconnect = "NULL"
} else {
if strings.Index(reconnect, "\"") == -1 {
qr.Reconnect = fmt.Sprintf("\"%s\"", reconnect)
} else {
qr.Reconnect = reconnect
}
}
}
// set qr timeout
func (qr *QueryRules) SetQrTimeOut(timeout string) {
if timeout == "" || len(timeout) == 0 {
qr.Timeout = "NULL"
} else {
if strings.Index(timeout, "\"") == -1 {
qr.Timeout = fmt.Sprintf("\"%s\"", timeout)
} else {
qr.Timeout = timeout
}
}
}
// set qr retries
func (qr *QueryRules) SetQrRetries(retries string) {
if retries == "" || len(retries) == 0 {
qr.Retries = "NULL"
} else {
if strings.Index(retries, "\"") == -1 {
qr.Retries = fmt.Sprintf("\"%s\"", retries)
} else {
qr.Retries = retries
}
}
}
// set qr delay
func (qr *QueryRules) SetQrDelay(delay string) {
if delay == "" || len(delay) == 0 {
qr.Delay = "NULL"
} else {
if strings.Index(delay, "\"") == -1 {
qr.Delay = fmt.Sprintf("\"%s\"", delay)
} else {
qr.Delay = delay
}
}
}
// set qr mirror_flagout
func (qr *QueryRules) SetQrMirrorFlagOUT(mirror_flagout string) {
if mirror_flagout == "" || len(mirror_flagout) == 0 {
qr.Mirror_flagOUT = "NULL"
} else {
if strings.Index(mirror_flagout, "\"") == -1 {
qr.Mirror_flagOUT = fmt.Sprintf("\"%s\"", mirror_flagout)
} else {
qr.Mirror_flagOUT = mirror_flagout
}
}
}
// set qr mirror_hostgroup
func (qr *QueryRules) SetQrMirrorHostgroup(mirror_hostgroup string) {
if mirror_hostgroup == "" || len(mirror_hostgroup) == 0 {
qr.Mirror_hostgroup = "NULL"
} else {
if strings.Index(mirror_hostgroup, "\"") == -1 {
qr.Mirror_hostgroup = fmt.Sprintf("\"%s\"", mirror_hostgroup)
} else {
qr.Mirror_hostgroup = mirror_hostgroup
}
}
}
// set qr error_msg
func (qr *QueryRules) SetQrErrorMsg(error_msg string) {
if error_msg == "" || len(error_msg) == 0 {
qr.Error_msg = "NULL"
} else {
if strings.Index(error_msg, "\"") == -1 {
qr.Error_msg = fmt.Sprintf("\"%s\"", error_msg)
} else {
qr.Error_msg = error_msg
}
}
}
// set qr log
func (qr *QueryRules) SetQrLog(log string) {
if log == "" || len(log) == 0 {
qr.Log = "NULL"
} else {
if strings.Index(log, "\"") == -1 {
qr.Log = fmt.Sprintf("\"%s\"", log)
} else {
qr.Log = log
}
}
}
// add a new query rules.
func (qr *QueryRules) AddOneQr(db *sql.DB) error {
Query := fmt.Sprintf(StmtAddOneQr, qr.Rule_id, qr.Username)
_, err := db.Exec(Query)
if err != nil {
return errors.Trace(err) //add user failed
}
Query = fmt.Sprintf(StmtFindLastRuleId, qr.Username)
rows := db.QueryRow(Query)
/*
FIX:
It will always return 0 when you use sql.Result.LastInsertId() function to get last inserted row id.
the proxysql not support transaction.
So,I Query a max(id) after insert a row.
*/
err = rows.Scan(&qr.Rule_id)
if err != nil {
return errors.Trace(err)
}
LoadQueryRulesToRuntime(db)
SaveQueryRulesToDisk(db)
return nil
}
//delete a query rules.
func (qr *QueryRules) DeleteOneQr(db *sql.DB) error {
Query := fmt.Sprintf(StmtDeleteOneQr, qr.Rule_id)
result, err := db.Exec(Query)
if err != nil {
return errors.Trace(err)
}
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
return errors.NotFoundf(strconv.Itoa(int(qr.Rule_id)))
}
LoadQueryRulesToRuntime(db)
SaveQueryRulesToDisk(db)
return nil
}
//update a query rules.
func (qr *QueryRules) UpdateOneQrInfo(db *sql.DB) error {
var Query string
Query = fmt.Sprintf(StmtUpdateOneQr,
qr.Active,
qr.Username,
qr.Schemaname,
qr.FlagIN,
qr.Client_addr,
qr.Proxy_addr,
qr.Proxy_port,
qr.Digest,
qr.Match_digest,
qr.Match_pattern,
qr.Negate_match_pattern,
qr.FlagOUT,
qr.Replace_pattern,
qr.Destination_hostgroup,
qr.Cache_ttl,
qr.Reconnect,
qr.Timeout,
qr.Retries,
qr.Delay,
qr.Mirror_flagOUT,
qr.Mirror_hostgroup,
qr.Error_msg,
qr.Log,
qr.Apply,
qr.Comment,
qr.Rule_id)
result, err := db.Exec(Query)
if err != nil {
return errors.Trace(err)
}
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
return errors.NotFoundf(strconv.Itoa(int(qr.Rule_id)))
}
LoadQueryRulesToRuntime(db)
SaveQueryRulesToDisk(db)
return nil
}