-
Notifications
You must be signed in to change notification settings - Fork 37
/
frame.go
73 lines (57 loc) · 1.24 KB
/
frame.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
package clause
import (
"io"
"github.com/stephenafamo/bob"
)
type Frame struct {
Defined bool // whether any of the parts was defined
Mode string
Start any
End any // can be nil
Exclusion string // can be empty
}
func (f *Frame) SetMode(mode string) {
f.Defined = true
f.Mode = mode
}
func (f *Frame) SetStart(start any) {
f.Defined = true
f.Start = start
}
func (f *Frame) SetEnd(end any) {
f.Defined = true
f.End = end
}
func (f *Frame) SetExclusion(excl string) {
f.Defined = true
f.Exclusion = excl
}
func (f Frame) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
if f.Mode == "" {
f.Mode = "RANGE"
}
if f.Start == nil {
f.Start = "UNBOUNDED PRECEDING"
}
var args []any
w.Write([]byte(f.Mode))
w.Write([]byte(" "))
if f.End != nil {
w.Write([]byte("BETWEEN "))
}
startArgs, err := bob.Express(w, d, start, f.Start)
if err != nil {
return nil, err
}
args = append(args, startArgs...)
endArgs, err := bob.ExpressIf(w, d, start, f.End, f.End != nil, " AND ", "")
if err != nil {
return nil, err
}
args = append(args, endArgs...)
_, err = bob.ExpressIf(w, d, start, f.Exclusion, f.Exclusion != "", " EXCLUDE ", "")
if err != nil {
return nil, err
}
return args, nil
}