Skip to content

Instantly share code, notes, and snippets.

@fahmifan
Created March 16, 2024 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fahmifan/034b1bd1e1bdd2b04d68be07c756aab2 to your computer and use it in GitHub Desktop.
Save fahmifan/034b1bd1e1bdd2b04d68be07c756aab2 to your computer and use it in GitHub Desktop.
Get SQL Tx from Gorm
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func DBTxFromGorm(tx *gorm.DB) (DBTX, bool) {
dbtx, ok := tx.Statement.ConnPool.(*sql.Tx)
return dbtx, ok
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment