-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
70 lines (49 loc) · 1.07 KB
/
db.go
File metadata and controls
70 lines (49 loc) · 1.07 KB
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
70
package dbtyp
import (
"context"
"database/sql"
"github.com/kanmu/dbtyp/iface"
)
type DB[T any] struct {
iface.DB
}
// Type converter
func (v *DB[T]) ExecQueryer() *ExecQueryer[T] {
return &ExecQueryer[T]{i: v}
}
func (v *DB[T]) Execer() *Execer[T] {
return &Execer[T]{i: v}
}
func (v *DB[T]) Queryer() *Queryer[T] {
return &Queryer[T]{i: v}
}
// Typed begin
func (v *DB[T]) BeginT() (*Tx[T], error) {
tx, err := v.Begin()
if err != nil {
return nil, err
}
return &Tx[T]{Tx: tx}, nil
}
func (v *DB[T]) BeginTxT(ctx context.Context, opts *sql.TxOptions) (*Tx[T], error) {
tx, err := v.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &Tx[T]{Tx: tx}, nil
}
// Typed prepare
func (v *DB[T]) PrepareT(query string) (*Stmt[T], error) {
stmt, err := v.Prepare(query)
if err != nil {
return nil, err
}
return &Stmt[T]{Stmt: stmt}, nil
}
func (v *DB[T]) PrepareContextT(ctx context.Context, query string) (*Stmt[T], error) {
stmt, err := v.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return &Stmt[T]{Stmt: stmt}, nil
}