-
Notifications
You must be signed in to change notification settings - Fork 322
/
mysql_normal_sync.go
190 lines (185 loc) · 5.21 KB
/
mysql_normal_sync.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
/*
普通模式同步
update 转成 insert on update
insert 转成 replace into
delete 转成 delete
只要是同一条数据,只要有遍历过,后面遍历出来的数据,则不再进行操作
*/
package src
import (
dbDriver "database/sql/driver"
"fmt"
pluginDriver "github.com/brokercap/Bifrost/plugin/driver"
"log"
"strings"
)
func (This *Conn) CommitNormal(list []*pluginDriver.PluginDataType,n int) (e error) {
deleteDataMap := make(map[interface{}]pluginDriver.PluginDataType,0)
insertDataMap := make(map[interface{}]pluginDriver.PluginDataType,0)
var ok bool
var normalFun = func(v *pluginDriver.PluginDataType) {
switch v.EventType {
case "insert":
for _,row := range v.Rows{
key := row[This.p.mysqlPriKey]
if _,ok=deleteDataMap[key];!ok {
if _, ok = insertDataMap[key]; !ok {
insertDataMap[key] = pluginDriver.PluginDataType{
Timestamp: v.Timestamp,
EventType: v.EventType,
Rows: []map[string]interface{}{row},
Query: v.Query,
SchemaName: v.SchemaName,
TableName: v.TableName,
BinlogFileNum: v.BinlogFileNum,
BinlogPosition: v.BinlogPosition,
Pri: v.Pri,
}
}
}
}
break
case "update":
for k := len(v.Rows)-1; k >= 0;k--{
row := v.Rows[k]
key := row[This.p.mysqlPriKey]
if k%2 == 0{
if _,ok:=deleteDataMap[key];!ok{
deleteDataMap[key] = pluginDriver.PluginDataType{
Timestamp: v.Timestamp,
EventType: v.EventType,
Rows: []map[string]interface{}{row},
Query: v.Query,
SchemaName: v.SchemaName,
TableName: v.TableName,
BinlogFileNum: v.BinlogFileNum,
BinlogPosition: v.BinlogPosition,
Pri: v.Pri,
}
}
}else{
if _,ok=deleteDataMap[key];!ok {
if _, ok = insertDataMap[key]; !ok {
insertDataMap[key] = pluginDriver.PluginDataType{
Timestamp: v.Timestamp,
EventType: v.EventType,
Rows: []map[string]interface{}{row},
Query: v.Query,
SchemaName: v.SchemaName,
TableName: v.TableName,
BinlogFileNum: v.BinlogFileNum,
BinlogPosition: v.BinlogPosition,
Pri: v.Pri,
}
}
}
}
}
break
case "delete":
if This.p.SyncType == SYNCMODE_LOG_UPDATE{
for _,row := range v.Rows{
key := row[This.p.mysqlPriKey]
if _,ok=deleteDataMap[key];!ok {
if _, ok = insertDataMap[key]; !ok {
insertDataMap[key] = pluginDriver.PluginDataType{
Timestamp: v.Timestamp,
EventType: v.EventType,
Rows: []map[string]interface{}{row},
Query: v.Query,
SchemaName: v.SchemaName,
TableName: v.TableName,
BinlogFileNum: v.BinlogFileNum,
BinlogPosition: v.BinlogPosition,
Pri: v.Pri,
}
}
}
}
}else{
for _,row := range v.Rows{
key := row[This.p.mysqlPriKey]
if _,ok:=deleteDataMap[key];!ok{
deleteDataMap[key] = pluginDriver.PluginDataType{
Timestamp: v.Timestamp,
EventType: v.EventType,
Rows: []map[string]interface{}{row},
Query: v.Query,
SchemaName: v.SchemaName,
TableName: v.TableName,
BinlogFileNum: v.BinlogFileNum,
BinlogPosition: v.BinlogPosition,
Pri: v.Pri,
}
}
}
}
break
default:
break
}
}
for i := n - 1; i >= 0; i-- {
v := list[i]
normalFun(v)
}
var stmt dbDriver.Stmt
// delete 的话,将多条数据,where id in (1,2) 方式合并
if len(deleteDataMap) > 0 {
keys := make([]dbDriver.Value,0)
for _, v := range deleteDataMap {
keys = append(keys,v.Rows[0][This.p.mysqlPriKey])
}
if len(keys) > 0{
var where string
//假如字段是int的话,就 in ()
if This.p.ckPriKeyFieldIsInt {
where = strings.Replace(strings.Trim(fmt.Sprint(keys), "[]"), " ", ",", -1)
}else{
where = "'"+strings.Replace(strings.Trim(fmt.Sprint(keys), "[]"), " ", "','", -1)+"'"
}
sql := "ALTER TABLE "+This.p.ckDatakey+" DELETE WHERE "+This.p.ckPriKey+ " in ( " +where+" )"
if This.p.bifrostDataVersionField != ""{
sql += " AND " + This.p.bifrostDataVersionField + " < "+ fmt.Sprint(This.p.nowBifrostDataVersion)
}
stmt = This.getStmt(sql)
if stmt == nil {
goto errLoop
}
_, This.err = stmt.Exec([]dbDriver.Value{where})
if This.err != nil {
log.Println("plugin clickhouse delete exec err:",This.err," sql:",sql," where:",sql)
stmt.Close()
goto errLoop
}
stmt.Close()
}
}
if len(insertDataMap) > 0 {
stmt = This.getStmt("insert")
if stmt == nil {
goto errLoop
}
var toV interface{}
for _, data := range insertDataMap {
val := make([]dbDriver.Value, 0)
for _, v := range This.p.Field {
toV, This.err = CkDataTypeTransfer(This.getMySQLData(&data,0,v.MySQL), v.CK, v.CkType)
if This.err != nil {
stmt.Close()
goto errLoop
}
val = append(val, toV)
}
_, This.err = stmt.Exec(val)
if This.err != nil {
log.Println("plugin clickhouse insert exec err:",This.err," data:",val)
stmt.Close()
goto errLoop
}
}
stmt.Close()
}
errLoop:
return
}