-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.go
More file actions
105 lines (95 loc) · 2.92 KB
/
function.go
File metadata and controls
105 lines (95 loc) · 2.92 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
package sqlfunc
import (
"context"
"database/sql"
"iter"
"strings"
"github.com/deadblue/sqltmpl"
)
type (
// QueryFunc executes a retrieving query, returns the first result.
QueryFunc[P, R any] func(context.Context, P) (sql.Null[R], error)
// QueryFunc executes a retrieving query without parameters, returns the
// first result.
QueryFunc0[R any] func(context.Context) (sql.Null[R], error)
// QuerySeqFunc executes a retrieving query, returns an iterator for all
// matched results.
QuerySeqFunc[P, R any] func(context.Context, P) (iter.Seq[R], error)
// QuerySeqFunc0 executes a retrieving query without parameters, returns
// an iterator for all matched results.
QuerySeqFunc0[R any] func(context.Context) (iter.Seq[R], error)
// UpdateFunc executes an updating query, returns the number or rows
// affected by updating query.
UpdateFunc[P any] func(context.Context, P) (int64, error)
)
// MakeQueryFunc makes a QueryFunc from query template lines.
func MakeQueryFunc[P, R any](lines ...string) (f QueryFunc[P, R], err error) {
preprocessResult[R]()
tmpl, err := sqltmpl.Parse[P](lines...)
if err == nil {
f = func(ctx context.Context, params P) (result sql.Null[R], err error) {
query, args, err := tmpl.Render(params)
if err != nil {
return
}
return queryRow[R](ctx, query, args...)
}
}
return
}
// MakeQueryFunc0 makes a QueryFunc0 from query lines.
func MakeQueryFunc0[R any](lines ...string) (f QueryFunc0[R], err error) {
preprocessResult[R]()
query := strings.Join(lines, "\n")
f = func(ctx context.Context) (result sql.Null[R], err error) {
return queryRow[R](ctx, query)
}
return
}
// MakeQuerySeqFunc makes a QuerySeqFunc from query template lines.
func MakeQuerySeqFunc[P, R any](lines ...string) (f QuerySeqFunc[P, R], err error) {
preprocessResult[R]()
tmpl, err := sqltmpl.Parse[P](lines...)
if err == nil {
f = func(ctx context.Context, params P) (results iter.Seq[R], err error) {
query, args, err := tmpl.Render(params)
if err != nil {
return
}
return queryRows[R](ctx, query, args...)
}
}
return
}
// MakeQuerySeqFunc0 makes a QuerySeqFunc0 from query lines.
func MakeQuerySeqFunc0[R any](lines ...string) (f QuerySeqFunc0[R], err error) {
preprocessResult[R]()
query := strings.Join(lines, "\n")
f = func(ctx context.Context) (results iter.Seq[R], err error) {
return queryRows[R](ctx, query)
}
return
}
// MakeUpdateFunc makes an UpdateFunc from query template lines.
func MakeUpdateFunc[P any](lines ...string) (f UpdateFunc[P], err error) {
tmpl, err := sqltmpl.Parse[P](lines...)
if err != nil {
return
}
f = func(ctx context.Context, params P) (affectedRows int64, err error) {
executor, err := fromContext(ctx)
if err != nil {
return
}
query, args, err := tmpl.Render(params)
if err != nil {
return
}
ret, err := executor.ExecContext(ctx, query, args...)
if err == nil {
affectedRows, err = ret.RowsAffected()
}
return
}
return
}