sqlboiler/boil/db.go

42 lines
855 B
Go
Raw Permalink Normal View History

package boil
import "database/sql"
// Executor can perform SQL queries.
type Executor interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
}
// Transactor can commit and rollback, on top of being able to execute queries.
type Transactor interface {
Commit() error
Rollback() error
Executor
}
2016-07-09 18:31:50 +02:00
// Beginner begins transactions.
type Beginner interface {
Begin() (Transactor, error)
}
2017-09-02 17:52:31 +02:00
type SQLBeginner interface {
Begin() (*sql.Tx, error)
}
// Begin a transaction
func Begin() (Transactor, error) {
2016-07-09 18:31:50 +02:00
creator, ok := currentDB.(Beginner)
if !ok {
2017-09-02 17:52:31 +02:00
creator2, ok2 := currentDB.(SQLBeginner)
if !ok2 {
panic("database does not support transactions")
}
return creator2.Begin()
}
return creator.Begin()
}