Skip to content

Instantly share code, notes, and snippets.

View fahmifan's full-sized avatar

fahmi irfan fahmifan

View GitHub Profile
@fahmifan
fahmifan / sql_gorm.go
Created March 16, 2024 22:24
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