21 Star 48 Fork 14

siddontang / go-mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
rows.go 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
siddontang 提交于 2015-04-17 17:45 . update canal
package canal
import (
"fmt"
"github.com/siddontang/go-mysql/schema"
)
const (
UpdateAction = "update"
InsertAction = "insert"
DeleteAction = "delete"
)
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{}
}
func newRowsEvent(table *schema.Table, action string, rows [][]interface{}) *RowsEvent {
e := new(RowsEvent)
e.Table = table
e.Action = action
e.Rows = rows
return e
}
// Get primary keys in one row for a table, a table may use multi fields as the PK
func GetPKValues(table *schema.Table, row []interface{}) ([]interface{}, error) {
indexes := table.PKColumns
if len(indexes) == 0 {
return nil, fmt.Errorf("table %s has no PK", table)
} else if len(table.Columns) != len(row) {
return nil, fmt.Errorf("table %s has %d columns, but row data %v len is %d", table,
len(table.Columns), row, len(row))
}
values := make([]interface{}, 0, len(indexes))
for _, index := range indexes {
values = append(values, row[index])
}
return values, nil
}
Go
1
https://gitee.com/siddontang/go-mysql.git
git@gitee.com:siddontang/go-mysql.git
siddontang
go-mysql
go-mysql
04766b6d37d1

搜索帮助