type (
HookFuncSelect func(ctx context.Context, in *HookSelectInput) (result Result, err error)
HookFuncInsert func(ctx context.Context, in *HookInsertInput) (result sql.Result, err error)
HookFuncUpdate func(ctx context.Context, in *HookUpdateInput) (result sql.Result, err error)
HookFuncDelete func(ctx context.Context, in *HookDeleteInput) (result sql.Result, err error)
)
// HookHandler manages all supported hook functions for Model.
type HookHandler struct {
Select HookFuncSelect
Insert HookFuncInsert
Update HookFuncUpdate
Delete HookFuncDelete
}
3 Comments
秋云
没看明白,Hook方法怎么注册?
warning
糖水不加糖
hook切换分表操作的应用实现建议.updateHookHandler,insertHookHandler,deleteHookHandler三者直接对in *gdb.HookXXXInput中的in.Table进行覆写即可完成分表切换(理论).但selectHookHandler中替换in.Table是无效的,在h.Model.db.DoSelect(ctx, h.link, h.Sql, h.Args...)引用的是in.Sql的内容,因此需要对in.Sql中的table信息进行替换,例如执行in.Sql = gstr.Replace(in.Sql, in.Table, gstr.Replace(in.Table, "\"", "")+"_hash1")完成切换分表操作(已验证)