feat(utils): 为 CURD 操作添加 HookHandler 支持

- 在 Curd 结构体中添加 Hook 字段用于数据库操作钩子处理
- 为 DeletePri、DeleteWhere、Save、Update 和 UpdatePri 方法集成 Hook 支持
- 所有数据库操作方法现在通过 Hook 进行扩展处理
- 支持在数据库操作执行前后注入自定义逻辑
- 保持原有的数据库操作功能不变,仅增强扩展性
main v1.0.1002
black1552 2026-02-13 10:59:28 +08:00
parent 1eda606705
commit 964d311247
1 changed files with 7 additions and 6 deletions

View File

@ -25,6 +25,7 @@ type IDao interface {
type Curd[R any] struct { type Curd[R any] struct {
Dao IDao Dao IDao
Hook gdb.HookHandler
} }
var pageInfo = []string{ var pageInfo = []string{
@ -229,11 +230,11 @@ func (c Curd[R]) Value(ctx ctx, where any, field any) (*gvar.Var, error) {
return c.Dao.Ctx(ctx).Where(where).Fields(field).Value() return c.Dao.Ctx(ctx).Where(where).Fields(field).Value()
} }
func (c Curd[R]) DeletePri(ctx ctx, primaryKey any) error { func (c Curd[R]) DeletePri(ctx ctx, primaryKey any) error {
_, err := c.Dao.Ctx(ctx).WherePri(primaryKey).Delete() _, err := c.Dao.Ctx(ctx).Hook(c.Hook).WherePri(primaryKey).Delete()
return err return err
} }
func (c Curd[R]) DeleteWhere(ctx ctx, where any) error { func (c Curd[R]) DeleteWhere(ctx ctx, where any) error {
_, err := c.Dao.Ctx(ctx).Where(where).Delete() _, err := c.Dao.Ctx(ctx).Hook(c.Hook).Where(where).Delete()
return err return err
} }
@ -300,7 +301,7 @@ func (c Curd[R]) Count(ctx ctx, where any) (count int, err error) {
} }
func (c Curd[R]) Save(ctx ctx, data any) (id int64, err error) { func (c Curd[R]) Save(ctx ctx, data any) (id int64, err error) {
result, err := c.Dao.Ctx(ctx).Save(data) result, err := c.Dao.Ctx(ctx).Hook(c.Hook).Save(data)
if err != nil { if err != nil {
return return
} }
@ -309,7 +310,7 @@ func (c Curd[R]) Save(ctx ctx, data any) (id int64, err error) {
} }
func (c Curd[R]) Update(ctx ctx, where any, data any) (count int64, err error) { func (c Curd[R]) Update(ctx ctx, where any, data any) (count int64, err error) {
result, err := c.Dao.Ctx(ctx).Where(where).Data(data).Update() result, err := c.Dao.Ctx(ctx).Hook(c.Hook).Where(where).Data(data).Update()
if err != nil { if err != nil {
return return
} }
@ -318,7 +319,7 @@ func (c Curd[R]) Update(ctx ctx, where any, data any) (count int64, err error) {
} }
func (c Curd[R]) UpdatePri(ctx ctx, primaryKey any, data any) (count int64, err error) { func (c Curd[R]) UpdatePri(ctx ctx, primaryKey any, data any) (count int64, err error) {
result, err := c.Dao.Ctx(ctx).WherePri(primaryKey).Data(data).Update() result, err := c.Dao.Ctx(ctx).Hook(c.Hook).WherePri(primaryKey).Data(data).Update()
if err != nil { if err != nil {
return return
} }