forked from hanchuanchuan/gh-ost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binlog_dml_event.go
67 lines (58 loc) · 1.5 KB
/
binlog_dml_event.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
/*
Copyright 2016 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package binlog
import (
"fmt"
"strings"
"github.com/github/gh-ost/go/sql"
)
type EventDML string
const (
NotDML EventDML = "NoDML"
InsertDML EventDML = "Insert"
UpdateDML EventDML = "Update"
DeleteDML EventDML = "Delete"
)
func ToEventDML(description string) EventDML {
// description can be a statement (`UPDATE my_table ...`) or a RBR event name (`UpdateRowsEventV2`)
description = strings.TrimSpace(strings.Split(description, " ")[0])
switch strings.ToLower(description) {
case "insert":
return InsertDML
case "update":
return UpdateDML
case "delete":
return DeleteDML
}
if strings.HasPrefix(description, "WriteRows") {
return InsertDML
}
if strings.HasPrefix(description, "UpdateRows") {
return UpdateDML
}
if strings.HasPrefix(description, "DeleteRows") {
return DeleteDML
}
return NotDML
}
// BinlogDMLEvent is a binary log rows (DML) event entry, with data
type BinlogDMLEvent struct {
DatabaseName string
TableName string
DML EventDML
WhereColumnValues *sql.ColumnValues
NewColumnValues *sql.ColumnValues
}
func NewBinlogDMLEvent(databaseName, tableName string, dml EventDML) *BinlogDMLEvent {
event := &BinlogDMLEvent{
DatabaseName: databaseName,
TableName: tableName,
DML: dml,
}
return event
}
func (this *BinlogDMLEvent) String() string {
return fmt.Sprintf("[%+v on %s:%s]", this.DML, this.DatabaseName, this.TableName)
}