mirror of
https://github.com/1f349/orchid.git
synced 2024-11-09 23:02:53 +00:00
24 lines
399 B
Go
24 lines
399 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
func (q *Queries) UseTx(ctx context.Context, cb func(tx *Queries) error) error {
|
|
sqlDB, ok := q.db.(*sql.DB)
|
|
if !ok {
|
|
panic("cannot open transaction without sql.DB")
|
|
}
|
|
tx, err := sqlDB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
err = cb(q.WithTx(tx))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|