-
Notifications
You must be signed in to change notification settings - Fork 36
/
function.go
147 lines (115 loc) · 2.85 KB
/
function.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
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
package dialect
import (
"io"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/expr"
)
func NewFunction(name string, args ...any) Function {
return Function{name: name, args: args}
}
type Function struct {
name string
args []any
// Used in value functions. Supported by Sqlite and Postgres
filter []any
alias string // used when there should be an alias before the columns
columns []columnDef
// For chain methods
expr.Chain[Expression, Expression]
}
func (f *Function) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
if f.name == "" {
return nil, nil
}
w.Write([]byte(f.name))
w.Write([]byte("("))
args, err := bob.ExpressSlice(w, d, start, f.args, "", ", ", "")
if err != nil {
return nil, err
}
w.Write([]byte(")"))
filterArgs, err := bob.ExpressSlice(w, d, start, f.filter, " FILTER (WHERE ", " AND ", ")")
if err != nil {
return nil, err
}
args = append(args, filterArgs...)
if len(f.columns) > 0 || len(f.alias) > 0 {
w.Write([]byte(" AS "))
}
if len(f.alias) > 0 {
w.Write([]byte(f.alias))
w.Write([]byte(" "))
}
colArgs, err := bob.ExpressSlice(w, d, start+len(args), f.columns, "(", ", ", ")")
if err != nil {
return nil, err
}
args = append(args, colArgs...)
return args, nil
}
func (f *Function) FilterWhere(e ...any) *functionOver {
f.filter = append(f.filter, e...)
fo := &functionOver{
function: f,
}
fo.WindowChain = &WindowChain[*functionOver]{Wrap: fo}
fo.Base = fo
return fo
}
func (f *Function) Over(window string) *functionOver {
fo := &functionOver{
function: f,
}
fo.WindowChain = &WindowChain[*functionOver]{Wrap: fo}
fo.Base = fo
return fo
}
func (f *Function) As(alias string) *Function {
f.alias = alias
return f
}
func (f *Function) Col(name, datatype string) *Function {
f.columns = append(f.columns, columnDef{
name: name,
dataType: datatype,
})
return f
}
type columnDef struct {
name string
dataType string
}
func (c columnDef) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
w.Write([]byte(c.name + " " + c.dataType))
return nil, nil
}
type functionOver struct {
function *Function
*WindowChain[*functionOver]
expr.Chain[Expression, Expression]
}
func (wr *functionOver) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
fargs, err := bob.Express(w, d, start, wr.function)
if err != nil {
return nil, err
}
winargs, err := bob.ExpressIf(w, d, start+len(fargs), wr.def, wr.def.Valid(), "OVER (", ")")
if err != nil {
return nil, err
}
return append(fargs, winargs...), nil
}
type Functions []*Function
func (f Functions) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
if len(f) > 1 {
w.Write([]byte("ROWS FROM ("))
}
args, err := bob.ExpressSlice(w, d, start, f, "", ", ", "")
if err != nil {
return nil, err
}
if len(f) > 1 {
w.Write([]byte(")"))
}
return args, nil
}