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. // Beginner begins transactions.
type Beginner interface { type Beginner interface {
Begin() (Transactor, error)
}
// SQLBeginner begins transactions (non-interface return type)
type SQLBeginner interface {
Begin() (*sql.Tx, error) Begin() (*sql.Tx, error)
} }
@ -26,7 +31,11 @@ type Beginner interface {
func Begin() (Transactor, error) { func Begin() (Transactor, error) {
creator, ok := currentDB.(Beginner) creator, ok := currentDB.(Beginner)
if !ok { if !ok {
panic("database does not support transactions") creator2, ok2 := currentDB.(SQLBeginner)
if !ok2 {
panic("database does not support transactions")
}
return creator2.Begin()
} }
return creator.Begin() return creator.Begin()