entity.go 506 B

12345678910111213141516171819202122232425
  1. package entity
  2. import (
  3. "context"
  4. "gorm.io/gorm"
  5. iContext "gxt-file-server/app/context"
  6. )
  7. func getDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
  8. trans, ok := iContext.FromTrans(ctx)
  9. if ok {
  10. db, ok := trans.(*gorm.DB)
  11. if ok {
  12. if iContext.FromTransLock(ctx) {
  13. db = db.Set("gorm:query_option", "FOR UPDATE")
  14. }
  15. return db
  16. }
  17. }
  18. return defDB
  19. }
  20. func getDBWithModel(ctx context.Context, defDB *gorm.DB, m interface{}) *gorm.DB {
  21. return getDB(ctx, defDB).Model(m).WithContext(ctx)
  22. }