use interface to enable custom Tx types

This commit is contained in:
Alex Grintsvayg 2017-08-23 16:59:29 -04:00
parent 86c580f537
commit e3c6bfd745

View file

@ -19,6 +19,11 @@ type Transactor interface {
// Beginner begins transactions.
type Beginner interface {
Begin() (Transactor, error)
}
// SQLBeginner begins transactions (non-interface return type)
type SQLBeginner interface {
Begin() (*sql.Tx, error)
}
@ -26,8 +31,12 @@ type Beginner interface {
func Begin() (Transactor, error) {
creator, ok := currentDB.(Beginner)
if !ok {
creator2, ok2 := currentDB.(SQLBeginner)
if !ok2 {
panic("database does not support transactions")
}
return creator2.Begin()
}
return creator.Begin()
}