diff --git a/caches.go b/caches.go index a7ff54d..97c1ae7 100644 --- a/caches.go +++ b/caches.go @@ -119,11 +119,20 @@ func (c *Caches) ease(db *gorm.DB, identifier string) { return } - q := &Query[any]{ + detachedQuery := &Query[any]{ + Dest: db.Statement.Dest, + RowsAffected: db.Statement.RowsAffected, + } + + easedQuery := &Query[any]{ Dest: res.db.Statement.Dest, RowsAffected: res.db.Statement.RowsAffected, } - q.replaceOn(db) + if err := easedQuery.copyTo(detachedQuery); err != nil { + _ = db.AddError(err) + } + + detachedQuery.replaceOn(db) } func (c *Caches) checkCache(db *gorm.DB, identifier string) bool { diff --git a/query.go b/query.go index 078d46b..2cd8f07 100644 --- a/query.go +++ b/query.go @@ -19,6 +19,15 @@ func (q *Query[T]) Unmarshal(bytes []byte) error { return json.Unmarshal(bytes, q) } +func (q *Query[T]) copyTo(dst *Query[any]) error { + bytes, err := q.Marshal() + if err != nil { + return err + } + + return dst.Unmarshal(bytes) +} + func (q *Query[T]) replaceOn(db *gorm.DB) { SetPointedValue(db.Statement.Dest, q.Dest) SetPointedValue(&db.Statement.RowsAffected, &q.RowsAffected)