Skip to content

Commit

Permalink
Detach the results of the eased queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsivkov committed Mar 21, 2024
1 parent 8087bf2 commit 35ed30b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 11 additions & 2 deletions caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 35ed30b

Please sign in to comment.