forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.go
69 lines (55 loc) · 1.76 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package boil
import (
"context"
"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
}
// ContextExecutor can perform SQL queries with context
type ContextExecutor interface {
Executor
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, 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
}
// Beginner begins transactions.
type Beginner interface {
Begin() (*sql.Tx, error)
}
// Begin a transaction with the current global database handle.
func Begin() (Transactor, error) {
creator, ok := currentDB.(Beginner)
if !ok {
panic("database does not support transactions")
}
return creator.Begin()
}
// ContextTransactor can commit and rollback, on top of being able to execute
// context-aware queries.
type ContextTransactor interface {
Commit() error
Rollback() error
ContextExecutor
}
// ContextBeginner allows creation of context aware transactions with options.
type ContextBeginner interface {
BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error)
}
// BeginTx begins a transaction with the current global database handle.
func BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
creator, ok := currentDB.(ContextBeginner)
if !ok {
panic("database does not support context-aware transactions")
}
return creator.BeginTx(ctx, opts)
}