-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_find.go
190 lines (164 loc) · 4.16 KB
/
db_find.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package do
import (
"context"
"database/sql"
"fmt"
"reflect"
)
type Queryer interface {
// return query sql and args
Query() (query string, args []any)
}
type Finder[R any] interface {
Queryer
// new a result type object, not the same, to receive every row
// fields must be pointer type of result object's field, and it is match with query sql's select column one by one
NewScanObjAndFields(colTypes []*sql.ColumnType) (r *R, fields []any)
}
// FindFunc return query, args, and object generator; object generator should return new object and it's related fields when called; FindFunc is a Finder too.
type FindFunc[R any] func() (query string, args []any, genObj func(colTypes []*sql.ColumnType) (r *R, fields []any))
func (f FindFunc[R]) Query() (query string, args []any) {
query, args, _ = f()
return query, args
}
func (f FindFunc[R]) NewScanObjAndFields(colTypes []*sql.ColumnType) (r *R, fields []any) {
_, _, genObj := f()
return genObj(colTypes)
}
var (
_ Finder[int] = (FindFunc[int])(nil)
)
// FindFuncHelper return FindFunc with T
func FindFuncHelper[T any](
query string,
args []any,
fieldMappers ...func(string) string,
) FindFunc[T] {
return func() (
string,
[]any,
func(colTypes []*sql.ColumnType) (r *T, fields []any),
) {
return query, args, ObjectAndFieldsHelper[T](fieldMappers...)
}
}
type Storer interface {
*sql.DB | *sql.Tx | *sql.Conn
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
func FindList[S Storer, F Finder[R], R any](db S, finder F, res *[]R) (err error) {
query, args := finder.Query()
rows, err := db.QueryContext(context.TODO(), query, args...) // sql里select了n列
if err != nil {
return
}
defer rows.Close()
colTypes, err := rows.ColumnTypes()
if err != nil {
return
}
for rows.Next() {
obj, fields := finder.NewScanObjAndFields(colTypes) // fields也必须有n个元素
if err = rows.Scan(fields...); err != nil {
return
}
// PrintFields(fields)
*res = append(*res, *obj)
}
if err = rows.Err(); err != nil {
return
}
return
}
func FindFirst[S Storer, F Finder[R], R any](db S, finder F, res *R) (err error) {
var r []R
err = FindList(db, finder, &r)
if err != nil {
return
}
if len(r) > 0 {
*res = r[0]
}
return
}
func FindListByFunc[S Storer, F FindFunc[R], R any](db S, finder F, res *[]R) (err error) {
query, args, genObj := finder()
rows, err := db.QueryContext(context.TODO(), query, args...) // sql里select了n列
if err != nil {
return
}
defer rows.Close()
colTypes, err := rows.ColumnTypes()
if err != nil {
return
}
for rows.Next() {
obj, fields := genObj(colTypes) // fields也必须有n个元素
if err = rows.Scan(fields...); err != nil {
return
}
// PrintFields(fields)
*res = append(*res, *obj)
}
if err = rows.Err(); err != nil {
return
}
return
}
func FindFirstByFunc[S Storer, F FindFunc[R], R any](db S, finder F, res *R) (err error) {
var r []R
err = FindListByFunc(db, finder, &r)
if err != nil {
return
}
if len(r) > 0 {
*res = r[0]
}
return
}
func FindAll[S Storer, F Finder[R], R any](db S, finder F, initial R) (r []R, err error) {
query, args := finder.Query()
rows, err := db.QueryContext(context.TODO(), query, args...) // sql里select了n列
if err != nil {
return
}
defer rows.Close()
colTypes, err := rows.ColumnTypes()
if err != nil {
return
}
for rows.Next() {
obj, fields := finder.NewScanObjAndFields(colTypes) // fields也必须有n个元素
if err = rows.Scan(fields...); err != nil {
return
}
// PrintFields(fields)
r = append(r, *obj)
}
if err = rows.Err(); err != nil {
return
}
return
}
func FindOne[S Storer, F Finder[R], R any](db S, finder F, initial R) (r R, err error) {
res, err := FindAll(db, finder, initial)
if err != nil {
return r, err
}
if len(res) > 0 {
r = res[0]
}
return
}
func PrintFields(fields []any) {
fmt.Println("=== begin print fields")
for i := range fields {
vall := reflect.ValueOf(fields[i])
if vall.Kind() == reflect.Pointer {
vall = vall.Elem()
}
fmt.Printf("field: %v\n", vall.Interface())
}
fmt.Println("=== end print fields")
}