4dd5a93ed7
- Properly creating multiple BinlogEntry-ies when reading multi statement binlog event (Still todo: transactions)
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
/*
|
|
Copyright 2016 GitHub Inc.
|
|
See https://github.com/github/gh-osc/blob/master/LICENSE
|
|
*/
|
|
|
|
package binlog
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/github/gh-osc/go/sql"
|
|
"strings"
|
|
)
|
|
|
|
type EventDML string
|
|
|
|
const (
|
|
NotDML EventDML = "NoDML"
|
|
InsertDML = "Insert"
|
|
UpdateDML = "Update"
|
|
DeleteDML = "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)
|
|
}
|