-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
64 lines (49 loc) · 1.51 KB
/
query.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
package query
import (
"context"
"database/sql"
"github.com/gernest/sydent-go/models"
)
var _ models.SQL = (*SQL)(nil)
// SQL implements models.SQL interface based on *sql.DB adding instrumentation
// and logging.
type SQL struct {
db *sql.DB
}
func New(db *sql.DB) *SQL {
return &SQL{db: db}
}
func (s *SQL) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return s.db.ExecContext(ctx, query, args...)
}
func (s *SQL) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return s.db.QueryContext(ctx, query, args...)
}
func (s *SQL) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
return s.db.QueryRowContext(ctx, query, args...)
}
func (s *SQL) BeginTx(ctx context.Context, opts *sql.TxOptions) (models.Tx, error) {
tx, err := s.db.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &Tx{tx: tx}, nil
}
type Tx struct {
tx *sql.Tx
}
func (t *Tx) Commit() error {
return t.tx.Commit()
}
func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return t.tx.ExecContext(ctx, query, args...)
}
func (t *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return t.tx.QueryContext(ctx, query, args...)
}
func (t *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
return t.tx.QueryRowContext(ctx, query, args...)
}
func (t *Tx) Rollback() error {
return t.tx.Rollback()
}