forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdml_events.go
462 lines (382 loc) · 11 KB
/
dml_events.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
package ghostferry
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/shopspring/decimal"
"github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go-mysql/replication"
"github.com/siddontang/go-mysql/schema"
)
type RowData []interface{}
// The mysql driver never actually gives you a uint64 from Scan, instead you
// get an int64 for values that fit in int64 or a byte slice decimal string
// with the uint64 value in it.
func (r RowData) GetUint64(colIdx int) (res uint64, err error) {
if valueByteSlice, ok := r[colIdx].([]byte); ok {
valueString := string(valueByteSlice)
res, err = strconv.ParseUint(valueString, 10, 64)
if err != nil {
return 0, err
}
} else {
signedInt := reflect.ValueOf(r[colIdx]).Int()
if signedInt < 0 {
return 0, fmt.Errorf("expected position %d in row to contain an unsigned number", colIdx)
}
res = uint64(signedInt)
}
return
}
type DMLEvent interface {
Database() string
Table() string
TableSchema() *TableSchema
AsSQLString(string, string) (string, error)
OldValues() RowData
NewValues() RowData
PaginationKey() (uint64, error)
BinlogPosition() mysql.Position
}
// The base of DMLEvent to provide the necessary methods.
type DMLEventBase struct {
table *TableSchema
pos mysql.Position
}
func (e *DMLEventBase) Database() string {
return e.table.Schema
}
func (e *DMLEventBase) Table() string {
return e.table.Name
}
func (e *DMLEventBase) TableSchema() *TableSchema {
return e.table
}
func (e *DMLEventBase) BinlogPosition() mysql.Position {
return e.pos
}
type BinlogInsertEvent struct {
newValues RowData
*DMLEventBase
}
func NewBinlogInsertEvents(table *TableSchema, rowsEvent *replication.RowsEvent, pos mysql.Position) ([]DMLEvent, error) {
insertEvents := make([]DMLEvent, len(rowsEvent.Rows))
for i, row := range rowsEvent.Rows {
insertEvents[i] = &BinlogInsertEvent{
newValues: row,
DMLEventBase: &DMLEventBase{table: table, pos: pos},
}
}
return insertEvents, nil
}
func (e *BinlogInsertEvent) OldValues() RowData {
return nil
}
func (e *BinlogInsertEvent) NewValues() RowData {
return e.newValues
}
func (e *BinlogInsertEvent) AsSQLString(schemaName, tableName string) (string, error) {
if err := verifyValuesHasTheSameLengthAsColumns(e.table, e.newValues); err != nil {
return "", err
}
query := "INSERT IGNORE INTO " +
QuotedTableNameFromString(schemaName, tableName) +
" (" + strings.Join(quotedColumnNames(e.table), ",") + ")" +
" VALUES (" + buildStringListForValues(e.table.Columns, e.newValues) + ")"
return query, nil
}
func (e *BinlogInsertEvent) PaginationKey() (uint64, error) {
return paginationKeyFromEventData(e.table, e.newValues)
}
type BinlogUpdateEvent struct {
oldValues RowData
newValues RowData
*DMLEventBase
}
func NewBinlogUpdateEvents(table *TableSchema, rowsEvent *replication.RowsEvent, pos mysql.Position) ([]DMLEvent, error) {
// UPDATE events have two rows in the RowsEvent. The first row is the
// entries of the old record (for WHERE) and the second row is the
// entries of the new record (for SET).
// There can be n db rows changed in one RowsEvent, resulting in
// 2*n binlog rows.
updateEvents := make([]DMLEvent, len(rowsEvent.Rows)/2)
for i, row := range rowsEvent.Rows {
if i%2 == 1 {
continue
}
updateEvents[i/2] = &BinlogUpdateEvent{
oldValues: row,
newValues: rowsEvent.Rows[i+1],
DMLEventBase: &DMLEventBase{table: table, pos: pos},
}
}
return updateEvents, nil
}
func (e *BinlogUpdateEvent) OldValues() RowData {
return e.oldValues
}
func (e *BinlogUpdateEvent) NewValues() RowData {
return e.newValues
}
func (e *BinlogUpdateEvent) AsSQLString(schemaName, tableName string) (string, error) {
if err := verifyValuesHasTheSameLengthAsColumns(e.table, e.oldValues, e.newValues); err != nil {
return "", err
}
query := "UPDATE " + QuotedTableNameFromString(schemaName, tableName) +
" SET " + buildStringMapForSet(e.table.Columns, e.newValues) +
" WHERE " + buildStringMapForWhere(e.table.Columns, e.oldValues)
return query, nil
}
func (e *BinlogUpdateEvent) PaginationKey() (uint64, error) {
return paginationKeyFromEventData(e.table, e.newValues)
}
type BinlogDeleteEvent struct {
oldValues RowData
*DMLEventBase
}
func (e *BinlogDeleteEvent) OldValues() RowData {
return e.oldValues
}
func (e *BinlogDeleteEvent) NewValues() RowData {
return nil
}
func NewBinlogDeleteEvents(table *TableSchema, rowsEvent *replication.RowsEvent, pos mysql.Position) ([]DMLEvent, error) {
deleteEvents := make([]DMLEvent, len(rowsEvent.Rows))
for i, row := range rowsEvent.Rows {
deleteEvents[i] = &BinlogDeleteEvent{
oldValues: row,
DMLEventBase: &DMLEventBase{table: table, pos: pos},
}
}
return deleteEvents, nil
}
func (e *BinlogDeleteEvent) AsSQLString(schemaName, tableName string) (string, error) {
if err := verifyValuesHasTheSameLengthAsColumns(e.table, e.oldValues); err != nil {
return "", err
}
query := "DELETE FROM " + QuotedTableNameFromString(schemaName, tableName) +
" WHERE " + buildStringMapForWhere(e.table.Columns, e.oldValues)
return query, nil
}
func (e *BinlogDeleteEvent) PaginationKey() (uint64, error) {
return paginationKeyFromEventData(e.table, e.oldValues)
}
func NewBinlogDMLEvents(table *TableSchema, ev *replication.BinlogEvent, pos mysql.Position) ([]DMLEvent, error) {
rowsEvent := ev.Event.(*replication.RowsEvent)
for _, row := range rowsEvent.Rows {
if len(row) != len(table.Columns) {
return nil, fmt.Errorf(
"table %s.%s has %d columns but event has %d columns instead",
table.Schema,
table.Name,
len(table.Columns),
len(row),
)
}
for i, col := range table.Columns {
if col.IsUnsigned {
switch v := row[i].(type) {
case int64:
row[i] = uint64(v)
case int32:
row[i] = uint32(v)
case int16:
row[i] = uint16(v)
case int8:
row[i] = uint8(v)
case int:
row[i] = uint(v)
}
}
}
}
switch ev.Header.EventType {
case replication.WRITE_ROWS_EVENTv1, replication.WRITE_ROWS_EVENTv2:
return NewBinlogInsertEvents(table, rowsEvent, pos)
case replication.DELETE_ROWS_EVENTv1, replication.DELETE_ROWS_EVENTv2:
return NewBinlogDeleteEvents(table, rowsEvent, pos)
case replication.UPDATE_ROWS_EVENTv1, replication.UPDATE_ROWS_EVENTv2:
return NewBinlogUpdateEvents(table, rowsEvent, pos)
default:
return nil, fmt.Errorf("unrecognized rows event: %s", ev.Header.EventType.String())
}
}
func quotedColumnNames(table *TableSchema) []string {
cols := make([]string, len(table.Columns))
for i, column := range table.Columns {
cols[i] = quoteField(column.Name)
}
return cols
}
func verifyValuesHasTheSameLengthAsColumns(table *TableSchema, values ...RowData) error {
for _, v := range values {
if len(table.Columns) != len(v) {
return fmt.Errorf(
"table %s.%s has %d columns but event has %d columns instead",
table.Schema,
table.Name,
len(table.Columns),
len(v),
)
}
}
return nil
}
func buildStringListForValues(columns []schema.TableColumn, values []interface{}) string {
var buffer []byte
for i, value := range values {
if i > 0 {
buffer = append(buffer, ',')
}
buffer = appendEscapedValue(buffer, value, columns[i])
}
return string(buffer)
}
func buildStringMapForWhere(columns []schema.TableColumn, values []interface{}) string {
var buffer []byte
for i, value := range values {
if i > 0 {
buffer = append(buffer, " AND "...)
}
buffer = append(buffer, quoteField(columns[i].Name)...)
if isNilValue(value) {
// "WHERE value = NULL" will never match rows.
buffer = append(buffer, " IS NULL"...)
} else {
buffer = append(buffer, '=')
buffer = appendEscapedValue(buffer, value, columns[i])
}
}
return string(buffer)
}
func buildStringMapForSet(columns []schema.TableColumn, values []interface{}) string {
var buffer []byte
for i, value := range values {
if i > 0 {
buffer = append(buffer, ',')
}
buffer = append(buffer, quoteField(columns[i].Name)...)
buffer = append(buffer, '=')
buffer = appendEscapedValue(buffer, value, columns[i])
}
return string(buffer)
}
func isNilValue(value interface{}) bool {
if value == nil {
return true
} else if vb, ok := value.([]byte); ok && vb == nil {
return true
}
return false
}
func appendEscapedValue(buffer []byte, value interface{}, column schema.TableColumn) []byte {
if isNilValue(value) {
return append(buffer, "NULL"...)
}
if uintv, ok := Uint64Value(value); ok {
return strconv.AppendUint(buffer, uintv, 10)
}
if intv, ok := Int64Value(value); ok {
return strconv.AppendInt(buffer, intv, 10)
}
switch v := value.(type) {
case string:
return appendEscapedString(buffer, v)
case []byte:
return appendEscapedBuffer(buffer, v, column.Type == schema.TYPE_JSON)
case bool:
if v {
return append(buffer, '1')
} else {
return append(buffer, '0')
}
case float64:
return strconv.AppendFloat(buffer, v, 'g', -1, 64)
case float32:
return strconv.AppendFloat(buffer, float64(v), 'g', -1, 64)
case decimal.Decimal:
return appendEscapedString(buffer, v.String())
default:
panic(fmt.Sprintf("unsupported type %t", value))
}
}
func Uint64Value(value interface{}) (uint64, bool) {
switch v := value.(type) {
case uint64:
return v, true
case uint32:
return uint64(v), true
case uint16:
return uint64(v), true
case uint8:
return uint64(v), true
case uint:
return uint64(v), true
}
return 0, false
}
func Int64Value(value interface{}) (int64, bool) {
switch v := value.(type) {
case int64:
return v, true
case int32:
return int64(v), true
case int16:
return int64(v), true
case int8:
return int64(v), true
case int:
return int64(v), true
}
return 0, false
}
// appendEscapedString replaces single quotes with quote-escaped single quotes.
// When the NO_BACKSLASH_ESCAPES mode is on, this is the extent of escaping
// necessary for strings.
//
// ref: https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L963-L1038
// ref: https://github.com/go-sql-driver/mysql/blob/9181e3a86a19bacd63e68d43ae8b7b36320d8092/utils.go#L717-L758
func appendEscapedString(buffer []byte, value string) []byte {
buffer = append(buffer, '\'')
for i := 0; i < len(value); i++ {
c := value[i]
if c == '\'' {
buffer = append(buffer, '\'', '\'')
} else {
buffer = append(buffer, c)
}
}
return append(buffer, '\'')
}
func appendEscapedBuffer(buffer, value []byte, isJSON bool) []byte {
if isJSON {
// See https://bugs.mysql.com/bug.php?id=98496
if len(value) == 0 {
value = []byte("null")
}
buffer = append(buffer, "CAST("...)
} else {
buffer = append(buffer, "_binary"...)
}
buffer = append(buffer, '\'')
for i := 0; i < len(value); i++ {
c := value[i]
if c == '\'' {
buffer = append(buffer, '\'', '\'')
} else {
buffer = append(buffer, c)
}
}
buffer = append(buffer, '\'')
if isJSON {
buffer = append(buffer, " AS JSON)"...)
}
return buffer
}
func paginationKeyFromEventData(table *TableSchema, rowData RowData) (uint64, error) {
if err := verifyValuesHasTheSameLengthAsColumns(table, rowData); err != nil {
return 0, err
}
return rowData.GetUint64(table.GetPaginationKeyIndex())
}