improve hook feature for package gdb

This commit is contained in:
John Guo
2022-03-23 16:23:33 +08:00
parent 045c3e132f
commit 0e3f4f45e0
2 changed files with 15 additions and 10 deletions

View File

@ -39,7 +39,8 @@ type internalParamHook struct {
type internalParamHookSelect struct {
internalParamHook
handler HookFuncSelect
handler HookFuncSelect
queryType int
}
type internalParamHookInsert struct {
@ -62,10 +63,9 @@ type internalParamHookDelete struct {
// which is usually not be interesting for upper business hook handler.
type HookSelectInput struct {
internalParamHookSelect
Table string
Sql string
Args []interface{}
IsCountStatement bool // IsCountStatement marks this SELECT statement is COUNT statement.
Table string
Sql string
Args []interface{}
}
// HookInsertInput holds the parameters for insert hook operation.
@ -111,6 +111,11 @@ func (h *HookSelectInput) Next(ctx context.Context) (result Result, err error) {
return h.model.db.DoSelect(ctx, h.link, h.Sql, h.Args...)
}
// IsCountStatement checks and returns whether current SELECT statement is COUNT statement.
func (h *HookSelectInput) IsCountStatement() bool {
return h.queryType == queryTypeCount
}
// Next calls the next hook handler.
func (h *HookInsertInput) Next(ctx context.Context) (result sql.Result, err error) {
if h.handler != nil && !h.handlerCalled {

View File

@ -539,12 +539,12 @@ func (m *Model) doGetAllBySql(queryType int, sql string, args ...interface{}) (r
link: m.getLink(false),
model: m,
},
handler: m.hookHandler.Select,
handler: m.hookHandler.Select,
queryType: queryType,
},
Table: m.tables,
Sql: sql,
Args: m.mergeArguments(args),
IsCountStatement: queryType == queryTypeCount,
Table: m.tables,
Sql: sql,
Args: m.mergeArguments(args),
}
result, err = in.Next(m.GetCtx())