2016-04-13 15:51:58 +02:00
|
|
|
package boil
|
|
|
|
|
2016-08-28 16:12:37 +02:00
|
|
|
import "database/sql"
|
2016-06-12 00:09:46 +02:00
|
|
|
|
|
|
|
// Executor can perform SQL queries.
|
2016-04-13 15:51:58 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-06-12 00:09:46 +02:00
|
|
|
// Transactor can commit and rollback, on top of being able to execute queries.
|
2016-04-13 15:51:58 +02:00
|
|
|
type Transactor interface {
|
|
|
|
Commit() error
|
|
|
|
Rollback() error
|
|
|
|
|
|
|
|
Executor
|
|
|
|
}
|
|
|
|
|
2016-07-09 18:31:50 +02:00
|
|
|
// Beginner begins transactions.
|
|
|
|
type Beginner interface {
|
2017-08-23 22:59:29 +02:00
|
|
|
Begin() (Transactor, error)
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 00:09:46 +02:00
|
|
|
// Begin a transaction
|
2016-04-13 15:51:58 +02:00
|
|
|
func Begin() (Transactor, error) {
|
2016-07-09 18:31:50 +02:00
|
|
|
creator, ok := currentDB.(Beginner)
|
2016-04-13 15:51:58 +02:00
|
|
|
if !ok {
|
2016-07-09 18:31:50 +02:00
|
|
|
panic("database does not support transactions")
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return creator.Begin()
|
|
|
|
}
|