-
Notifications
You must be signed in to change notification settings - Fork 0
/
express_builder.go
83 lines (74 loc) · 1.79 KB
/
express_builder.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
package litsql
// ExpressBuilder builds arguments in a sequence of Express calls.
type ExpressBuilder interface {
Express(e Expression)
ExpressIf(e Expression, cond bool, prefix, suffix Expression)
ExpressSlice(expressions []Expression, prefix, sep, suffix Expression)
WriteQuery(e Query)
Result() ([]any, error)
Err() error
}
type expressBuilder struct {
w Writer
d Dialect
start int
args []any
err error
}
func NewExpressBuilder(w Writer, d Dialect, start int) ExpressBuilder {
return &expressBuilder{
w: w,
d: d,
start: start,
}
}
func (h *expressBuilder) Express(e Expression) {
if h.err != nil || e == nil {
return
}
var newArgs []any
newArgs, h.err = Express(h.w, h.d, h.start+len(h.args), e)
if h.err != nil {
return
}
h.args = append(h.args, newArgs...)
}
func (h *expressBuilder) ExpressIf(e Expression, cond bool, prefix, suffix Expression) {
if h.err != nil || e == nil {
return
}
var newArgs []any
newArgs, h.err = ExpressIf(h.w, h.d, h.start+len(h.args), e, cond, prefix, suffix)
if h.err != nil {
return
}
h.args = append(h.args, newArgs...)
}
func (h *expressBuilder) ExpressSlice(expressions []Expression, prefix, sep, suffix Expression) {
if h.err != nil || len(expressions) == 0 {
return
}
var newArgs []any
newArgs, h.err = ExpressSlice(h.w, h.d, h.start+len(h.args), expressions, prefix, sep, suffix)
if h.err != nil {
return
}
h.args = append(h.args, newArgs...)
}
func (h *expressBuilder) WriteQuery(e Query) {
if h.err != nil || e == nil {
return
}
var newArgs []any
newArgs, h.err = e.WriteQuery(h.w, h.start+len(h.args))
if h.err != nil {
return
}
h.args = append(h.args, newArgs...)
}
func (h *expressBuilder) Err() error {
return h.err
}
func (h *expressBuilder) Result() ([]any, error) {
return h.args, h.err
}