-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_sql.go
72 lines (61 loc) · 1.41 KB
/
db_sql.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
package do
import "fmt"
type (
SQLQuery string
SQLArgs []any
SQLQueryFunc func() (query SQLQuery, args SQLArgs)
)
// Append add sql to query, if the q is a format string of fmt, fmt.Sprintf will be used to concat the sql; the q will be the format, the left sql will be arguments.
func (q SQLQuery) Append(ss ...SQLQuery) (r SQLQuery) {
if isFormatString(q.Raw()) {
args := make([]any, 0, len(ss))
for _, s := range ss {
args = append(args, s)
}
return SQLQuery(fmt.Sprintf(q.Raw(), args...))
}
r += q
for _, s := range ss {
r += s
}
return
}
func (q SQLQuery) Raw() string {
return string(q)
}
func isFormatString(format string) bool {
ok, _ := checkPrintf(format)
return ok
}
func (a SQLArgs) Append(s SQLArgs) SQLArgs {
return append(a, s...)
}
func (a SQLArgs) Raw() []any {
return a
}
// SQLProcess process sql and args one by one, add more parts based on first sql
func SQLProcess(funcs ...SQLQueryFunc) (query SQLQuery, args SQLArgs) {
if len(funcs) == 0 {
return
}
query, args = funcs[0]()
qs := make([]SQLQuery, 0, len(funcs))
as := make(SQLArgs, 0, len(funcs))
for i, f := range funcs {
if i == 0 {
continue
}
q, a := f()
qs = append(qs, q)
as = append(as, a...)
}
query = query.Append(qs...)
args = args.Append(as)
return
}
func SQLProcessRaw(funcs ...SQLQueryFunc) (query string, args []any) {
q, a := SQLProcess(funcs...)
query = q.Raw()
args = a.Raw()
return
}