-
Notifications
You must be signed in to change notification settings - Fork 1
/
statement.go
103 lines (89 loc) · 2.32 KB
/
statement.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package dbx
import (
"context"
"database/sql"
"time"
)
// Stmt is a preparer statement.
// it is like sql.Stmt.
type Stmt struct {
rawQuery string
stmt *sql.Stmt
option *Options
}
func newStmt(preparer preparer, query string, option *Options) (stmt *Stmt, err error) {
return newStmtContext(context.Background(), preparer, query, option)
}
func newStmtContext(ctx context.Context, preparer preparer, query string, option *Options) (stmt *Stmt, err error) {
s, err := preparer.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return &Stmt{
stmt: s,
rawQuery: query,
option: option,
}, nil
}
func timeFormat(fn func(t *time.Time) string, args ...interface{}) []interface{} {
var _args = make([]interface{}, 0)
for _, arg := range args {
if t, ok := arg.(*time.Time); ok {
_args = append(_args, fn(t))
continue
}
if t, ok := arg.(time.Time); ok {
_args = append(_args, fn(&t))
continue
}
_args = append(_args, arg)
}
return _args
}
func (s *Stmt) format(ctx context.Context, args ...interface{}) []interface{} {
if s.option.TimeFormat != nil {
args = timeFormat(s.option.TimeFormat, args...)
}
printSQL(s.rawQuery, args, s.option)
return args
}
func (s *Stmt) GetContext(ctx context.Context, dest interface{}, args ...interface{}) error {
args = s.format(ctx, args...)
rows, err := s.stmt.QueryContext(ctx, args...)
if err != nil {
return err
}
err = mapping(rows, dest, single)
if err != nil {
return err
}
return err
}
func (s *Stmt) Get(dest interface{}, args ...interface{}) error {
return s.GetContext(context.Background(), dest, args...)
}
func (s *Stmt) QueryContext(ctx context.Context, dest interface{}, args ...interface{}) error {
args = s.format(ctx, args...)
rows, err := s.stmt.QueryContext(ctx, args...)
if err != nil {
return err
}
err = mapping(rows, dest, slice)
if err != nil {
return err
}
return err
}
func (s *Stmt) Query(dest interface{}, args ...interface{}) error {
return s.QueryContext(context.Background(), dest, args...)
}
func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) {
args = s.format(ctx, args...)
return s.stmt.ExecContext(ctx, args...)
}
func (s *Stmt) Exec() (sql.Result, error) {
return s.ExecContext(context.Background())
}
func (s *Stmt) Close() error {
return s.stmt.Close()
}