2017-02-12 11:13:54 +00:00
|
|
|
package canal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
"github.com/siddontang/go-mysql/replication"
|
2017-02-12 11:13:54 +00:00
|
|
|
"github.com/siddontang/go-mysql/schema"
|
|
|
|
)
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
// The action name for sync.
|
2017-02-12 11:13:54 +00:00
|
|
|
const (
|
|
|
|
UpdateAction = "update"
|
|
|
|
InsertAction = "insert"
|
|
|
|
DeleteAction = "delete"
|
|
|
|
)
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
// RowsEvent is the event for row replication.
|
2017-02-12 11:13:54 +00:00
|
|
|
type RowsEvent struct {
|
|
|
|
Table *schema.Table
|
|
|
|
Action string
|
|
|
|
// changed row list
|
|
|
|
// binlog has three update event version, v0, v1 and v2.
|
|
|
|
// for v1 and v2, the rows number must be even.
|
|
|
|
// Two rows for one event, format is [before update row, after update row]
|
|
|
|
// for update v0, only one row for a event, and we don't support this version.
|
|
|
|
Rows [][]interface{}
|
2019-01-01 08:57:46 +00:00
|
|
|
// Header can be used to inspect the event
|
|
|
|
Header *replication.EventHeader
|
2017-02-12 11:13:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
func newRowsEvent(table *schema.Table, action string, rows [][]interface{}, header *replication.EventHeader) *RowsEvent {
|
2017-02-12 11:13:54 +00:00
|
|
|
e := new(RowsEvent)
|
|
|
|
|
|
|
|
e.Table = table
|
|
|
|
e.Action = action
|
|
|
|
e.Rows = rows
|
2019-01-01 08:57:46 +00:00
|
|
|
e.Header = header
|
|
|
|
|
|
|
|
e.handleUnsigned()
|
2017-02-12 11:13:54 +00:00
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
func (r *RowsEvent) handleUnsigned() {
|
|
|
|
// Handle Unsigned Columns here, for binlog replication, we can't know the integer is unsigned or not,
|
|
|
|
// so we use int type but this may cause overflow outside sometimes, so we must convert to the really .
|
|
|
|
// unsigned type
|
|
|
|
if len(r.Table.UnsignedColumns) == 0 {
|
|
|
|
return
|
2017-02-12 11:13:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
for i := 0; i < len(r.Rows); i++ {
|
|
|
|
for _, index := range r.Table.UnsignedColumns {
|
|
|
|
switch t := r.Rows[i][index].(type) {
|
|
|
|
case int8:
|
|
|
|
r.Rows[i][index] = uint8(t)
|
|
|
|
case int16:
|
|
|
|
r.Rows[i][index] = uint16(t)
|
|
|
|
case int32:
|
|
|
|
r.Rows[i][index] = uint32(t)
|
|
|
|
case int64:
|
|
|
|
r.Rows[i][index] = uint64(t)
|
|
|
|
case int:
|
|
|
|
r.Rows[i][index] = uint(t)
|
|
|
|
default:
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
}
|
2017-02-12 11:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer interface.
|
|
|
|
func (r *RowsEvent) String() string {
|
|
|
|
return fmt.Sprintf("%s %s %v", r.Action, r.Table, r.Rows)
|
|
|
|
}
|