-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathproteus_function.go
More file actions
183 lines (152 loc) · 4.48 KB
/
Copy pathproteus_function.go
File metadata and controls
183 lines (152 loc) · 4.48 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
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
package proteus
import (
"context"
"database/sql"
"log/slog"
"reflect"
"strings"
"github.com/jonbodner/proteus/mapper"
)
type Builder struct {
adapter ParamAdapter
mappers []QueryMapper
}
func NewBuilder(adapter ParamAdapter, mappers ...QueryMapper) Builder {
return Builder{
adapter: adapter,
mappers: mappers,
}
}
func (fb Builder) BuildFunction(ctx context.Context, f any, query string, names []string) error {
// make sure that f is of the right type (pointer to function)
funcPointerType := reflect.TypeOf(f)
//must be a pointer to func
if funcPointerType.Kind() != reflect.Pointer {
return ValidationError{Kind: NotPointer}
}
funcType := funcPointerType.Elem()
//if not a func, error out
if funcType.Kind() != reflect.Func {
return ValidationError{Kind: NotPointerToFunc}
}
//validate to make sure that the function matches what we expect
hasCtx, err := validateFunction(funcType)
if err != nil {
return err
}
var nameOrderMap map[string]int
startPos := 1
if hasCtx {
startPos = 2
}
if len(names) == 0 {
nameOrderMap = buildDummyParameters(funcType.NumIn(), startPos)
} else {
nameOrderMap = buildFuncNameOrderMap(names, startPos)
}
//check to see if the query is in a QueryMapper
query, err = lookupQuery(query, fb.mappers)
if err != nil {
return err
}
implementation, err := makeImplementation(ctx, funcType, query, fb.adapter, nameOrderMap)
if err != nil {
return err
}
reflect.ValueOf(f).Elem().Set(reflect.MakeFunc(funcType, implementation))
return nil
}
func buildFuncNameOrderMap(names []string, startPos int) map[string]int {
out := map[string]int{}
for k, v := range names {
out[strings.TrimSpace(v)] = k + startPos
}
return out
}
type sliceTypes []reflect.Type
func (st sliceTypes) In(i int) reflect.Type {
return st[i]
}
func (fb Builder) Exec(ctx context.Context, e ContextExecutor, query string, params map[string]any) (int64, error) {
result, err := fb.ExecResult(ctx, e, query, params)
if err != nil {
return 0, err
}
count, err := result.RowsAffected()
return count, err
}
func (fb Builder) ExecResult(ctx context.Context, e ContextExecutor, query string, params map[string]any) (sql.Result, error) {
finalQuery, queryArgs, err := fb.setupDynamicQueries(ctx, query, params)
if err != nil {
return nil, err
}
slog.DebugContext(ctx, "calling query", "query", finalQuery, "params", queryArgs)
result, err := e.ExecContext(ctx, finalQuery, queryArgs...)
return result, err
}
func (fb Builder) Query(ctx context.Context, q ContextQuerier, query string, params map[string]any, output any) error {
// make sure that output is a pointer to something
outputPointerType := reflect.TypeOf(output)
if outputPointerType.Kind() != reflect.Pointer {
return ValidationError{Kind: NotPointer}
}
finalQuery, queryArgs, err := fb.setupDynamicQueries(ctx, query, params)
if err != nil {
return err
}
slog.DebugContext(ctx, "calling query", "query", finalQuery, "params", queryArgs)
rows, err := q.QueryContext(ctx, finalQuery, queryArgs...)
if err != nil {
return err
}
sType := outputPointerType.Elem()
qZero := reflect.Zero(sType)
builder, err := mapper.MakeBuilder(ctx, sType)
if err != nil {
return err
}
val, err := handleMapping(ctx, sType, rows, builder)
if err != nil {
return err
}
outputValue := reflect.ValueOf(output).Elem()
if val == nil {
outputValue.Set(qZero)
return nil
}
outputValue.Set(reflect.ValueOf(val).Convert(sType))
return nil
}
func (fb Builder) setupDynamicQueries(ctx context.Context, query string, paramsAndNames map[string]any) (string, []any, error) {
params := make([]any, 0, len(paramsAndNames))
names := make([]string, 0, len(paramsAndNames))
for k, v := range paramsAndNames {
params = append(params, v)
names = append(names, k)
}
nameOrderMap := buildFuncNameOrderMap(names, 0)
//check to see if the query is in a QueryMapper
query, err := lookupQuery(query, fb.mappers)
if err != nil {
return "", nil, err
}
st := make(sliceTypes, 0, len(params))
args := make([]reflect.Value, 0, len(params))
for _, v := range params {
st = append(st, reflect.TypeOf(v))
args = append(args, reflect.ValueOf(v))
}
fixedQuery, paramOrder, err := buildFixedQueryAndParamOrder(ctx, query, nameOrderMap, st, fb.adapter)
if err != nil {
return "", nil, err
}
finalQuery, err := fixedQuery.finalize(ctx, args)
if err != nil {
return "", nil, err
}
queryArgs, err := buildQueryArgs(ctx, args, paramOrder)
if err != nil {
return "", nil, err
}
return finalQuery, queryArgs, nil
}