-
Notifications
You must be signed in to change notification settings - Fork 0
/
curd.go
145 lines (118 loc) · 3.86 KB
/
curd.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package bsql
import (
"context"
"database/sql"
"fmt"
"go.uber.org/zap"
"reflect"
"strings"
"time"
)
func QueryRows(ctx context.Context, rst interface{}, query string, args ...interface{}) error {
if reflect.TypeOf(rst).Kind() != reflect.Pointer {
return fmt.Errorf("QueryRows rst must be a pointer")
}
obj := reflect.Indirect(reflect.ValueOf(rst))
objType := obj.Type()
if objType.Kind() != reflect.Slice {
return fmt.Errorf("QueryRows rst pointed must a slice")
}
var executed string
start := time.Now()
defer func() {
zap.L().Info("QueryRows", zap.String("sql", executed), zap.Any("args", args), zap.Duration("duration", time.Since(start)))
}()
db, err := defaultPool.getMaster()
if err != nil {
return err
}
_, columns := getColumnsByType(objType.Elem(), opsTypeQuery)
executed = strings.Replace(query, "*", columns, 1)
stmt, err := db.PrepareContext(ctx, executed)
if err != nil {
return fmt.Errorf("err on prepare statement: %w", unifyError(defaultPool.driverName, err))
}
defer stmt.Close()
rows, err := stmt.QueryContext(ctx, args...)
if err != nil {
return fmt.Errorf("err on stmt queryContext: %w", unifyError(defaultPool.driverName, err))
}
defer rows.Close()
for rows.Next() {
item := reflect.New(objType.Elem()).Elem()
err = rows.Scan(getBinding(item.Addr().Interface(), opsTypeQuery)...)
if err != nil {
return fmt.Errorf("err on scan: %w", unifyError(defaultPool.driverName, err))
}
obj.Set(reflect.Append(obj, item))
}
return nil
}
func QueryRow(ctx context.Context, rst interface{}, query string, args ...interface{}) error {
if reflect.TypeOf(rst).Kind() != reflect.Pointer {
return fmt.Errorf("rst must be a pointer")
}
obj := reflect.Indirect(reflect.ValueOf(rst))
objType := obj.Type()
if objType.Kind() != reflect.Struct {
return fmt.Errorf("rst pointed must a struct")
}
var executed string
start := time.Now()
defer func() {
zap.L().Info("QueryRow", zap.String("sql", query), zap.Any("args", args), zap.Duration("duration", time.Since(start)))
}()
db, err := defaultPool.getMaster()
if err != nil {
return err
}
_, columns := getColumnsByType(objType, opsTypeQuery)
executed = strings.Replace(query, "*", columns, 1)
stmt, err := db.PrepareContext(ctx, executed)
if err != nil {
return fmt.Errorf("err on prepare statement: %w", unifyError(defaultPool.driverName, err))
}
defer stmt.Close()
row := stmt.QueryRowContext(ctx, args...)
err = row.Scan(getBinding(rst, opsTypeQuery)...)
if err != nil {
return fmt.Errorf("err on scan: %w", unifyError(defaultPool.driverName, err))
}
return nil
}
func Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
start := time.Now()
defer func() {
zap.L().Info("Exec", zap.String("query", query), zap.Any("args", args), zap.Duration("duration", time.Since(start)))
}()
db, err := defaultPool.getMaster()
if err != nil {
return nil, err
}
stmt, err := db.PrepareContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("err on prepare statement: %w", unifyError(defaultPool.driverName, err))
}
defer stmt.Close()
return stmt.ExecContext(ctx, args...)
}
func Insert(ctx context.Context, query string, data interface{}) (sql.Result, error) {
var executed string
start := time.Now()
defer func() {
zap.L().Info("Insert", zap.String("sql", executed), zap.Any("data", data), zap.Duration("duration", time.Since(start)))
}()
db, err := defaultPool.getMaster()
if err != nil {
return nil, err
}
n, columns := getColumns(data, opsTypeInsert)
withColumn := strings.Replace(query, "*", columns, 1)
executed = strings.Replace(withColumn, "*", genInPlaceHolder(n), 1)
stmt, err := db.PrepareContext(ctx, executed)
if err != nil {
return nil, fmt.Errorf("err on prepare statement: %w", unifyError(defaultPool.driverName, err))
}
defer stmt.Close()
return stmt.ExecContext(ctx, getBinding(data, opsTypeInsert)...)
}