-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryer.go
42 lines (36 loc) · 882 Bytes
/
queryer.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
package sqlu
import (
"context"
"database/sql"
"errors"
)
// TxQueryer Query only (no transaction initiator)
type TxQueryer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
// Queryer *sql.DB and transaction initiator
type Queryer interface {
Begin() (*sql.Tx, error)
TxQueryer
}
// Get Queryer
func Q(db interface{}) Queryer {
switch r := db.(type) {
case *sql.DB:
return r
case *sql.Tx:
return txWrap{r}
default:
return nil
}
}
type txWrap struct {
TxQueryer
}
func (txWrap) Begin() (*sql.Tx, error) {
return nil, errors.New("not Tx")
}