-
Notifications
You must be signed in to change notification settings - Fork 23
/
functions.go
329 lines (309 loc) · 10.4 KB
/
functions.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"fmt"
"go/ast"
"io"
"strings"
)
const (
triggerData = "TriggerData"
triggerRow = "TriggerRow"
)
var datumTypes = map[string]string{
"error": "text",
"string": "text",
"[]byte": "bytea",
"int16": "smallint",
"uint16": "smallint",
"int32": "integer",
"uint32": "integer",
"int64": "bigint",
"int": "bigint",
"uint": "bigint",
"float32": "real",
"float64": "double precision",
"time.Time": "timestamp with timezone",
"bool": "boolean",
"[]string": "text[]",
"[]int16": "smallint[]",
"[]uint16": "smallint[]",
"[]int32": "integer[]",
"[]uint32": "integer[]",
"[]int64": "bigint[]",
"[]int": "bigint[]",
"[]uint": "bigint[]",
"[]float32": "real[]",
"[]float64": "double precision[]",
"[]bool": "boolean[]",
"[]time.Time": "timestamp with timezone[]",
"TriggerRow": "trigger",
}
//CodeWriter is an interface of an object that can print its code
type CodeWriter interface {
FuncDec() string
Code(w io.Writer)
SQL(packageName string, w io.Writer)
}
//NewCode parses the ast.FuncDecl and returns a new Function or An TriggerFunction
func NewCode(function *ast.FuncDecl) (CodeWriter, error) {
params, err := getParamList(function)
if err != nil {
return nil, err
}
returnType, err := getReturnType(function.Name.Name, function.Type.Results)
if err != nil {
return nil, err
}
if returnType == triggerRow {
if len(params) == 0 || params[0].Type != triggerData {
return nil, fmt.Errorf("Function %s can return *plgo.TriggerRow when the first parameter will be *plgo.TriggerData", function.Name.Name)
}
return &TriggerFunction{VoidFunction: VoidFunction{Name: function.Name.Name, Params: params[1:], Doc: function.Doc.Text()}}, nil
}
if returnType == "" {
return &VoidFunction{Name: function.Name.Name, Params: params, Doc: function.Doc.Text()}, nil
}
return &Function{VoidFunction: VoidFunction{Name: function.Name.Name, Params: params, Doc: function.Doc.Text()}, ReturnType: returnType}, nil
}
func getParamList(function *ast.FuncDecl) (Params []Param, err error) {
for i, param := range function.Type.Params.List {
for _, paramName := range param.Names {
switch paramType := param.Type.(type) {
case *ast.Ident:
//built in type
if _, ok := datumTypes[paramType.Name]; !ok {
return nil, fmt.Errorf("Function %s, parameter %s: type %s not supported", function.Name.Name, paramName.Name, paramType.Name)
}
Params = append(Params, Param{Name: paramName.Name, Type: paramType.Name})
case *ast.ArrayType:
//built in array type
arrayType, ok := paramType.Elt.(*ast.Ident)
if !ok {
return nil, fmt.Errorf("Function %s, parameter %s: array type not supported", function.Name.Name, paramName.Name)
}
if _, ok := datumTypes[arrayType.Name]; !ok {
return nil, fmt.Errorf("Function %s, parameter %s: array type not supported", function.Name.Name, paramName.Name)
}
Params = append(Params, Param{Name: paramName.Name, Type: "[]" + arrayType.Name})
case *ast.StarExpr:
//*plgo.TriggerData
selector, ok := paramType.X.(*ast.SelectorExpr)
if !ok {
return nil, fmt.Errorf("Function %s, parameter %s: type not supported", function.Name.Name, paramName.Name)
}
var pkg *ast.Ident
pkg, ok = selector.X.(*ast.Ident)
if !ok {
return nil, fmt.Errorf("Function %s, parameter %s: type not supported", function.Name.Name, paramName.Name)
}
if pkg.Name != plgo || selector.Sel.Name != triggerData {
return nil, fmt.Errorf("Function %s, parameter %s: type not supported", function.Name.Name, paramName.Name)
}
if i != 0 {
return nil, fmt.Errorf("Function %s, parameter %s: *plgo.TriggerData type must be the first parameter", function.Name.Name, paramName.Name)
}
if len(param.Names) > 1 {
return nil, fmt.Errorf("Function %s, parameter %s: *plgo.TriggerData must be just one parameter", function.Name.Name, paramName.Name)
}
Params = append(Params, Param{Name: param.Names[0].Name, Type: "TriggerData"})
default:
return nil, fmt.Errorf("Function %s, parameter %s: type not supported", function.Name.Name, paramName.Name)
}
}
}
return
}
func getReturnType(functionName string, results *ast.FieldList) (string, error) {
//Result is void
if results == nil {
return "", nil
}
if len(results.List) > 1 {
return "", fmt.Errorf("Function %s has multiple return types", functionName)
}
switch res := results.List[0].Type.(type) {
case *ast.StarExpr:
var selector *ast.SelectorExpr
selector, ok := res.X.(*ast.SelectorExpr)
if !ok {
return "", fmt.Errorf("Function %s has not supported return type", functionName)
}
var pkg *ast.Ident
pkg, ok = selector.X.(*ast.Ident)
if !ok {
return "", fmt.Errorf("Function %s has not supported return type", functionName)
}
if pkg.Name != plgo || selector.Sel.Name != triggerRow {
return "", fmt.Errorf("Function %s has not supported return type", functionName)
}
return "TriggerRow", nil
case *ast.Ident:
if _, ok := datumTypes[res.Name]; !ok {
return "", fmt.Errorf("Function %s has not suported return type", functionName)
}
return res.Name, nil
case *ast.ArrayType:
ident, ok := res.Elt.(*ast.Ident)
if !ok {
return "", fmt.Errorf("Function %s has not supported return type", functionName)
}
return "[]" + ident.Name, nil
default:
return "", fmt.Errorf("Function %s has not suported return type", functionName)
}
}
//Param the parameters of the functions
type Param struct {
Name, Type string
}
//VoidFunction is an function with no return type
type VoidFunction struct {
Name string
Params []Param
Doc string
}
//FuncDec returns the PG INFO_V1 macro
func (f *VoidFunction) FuncDec() string {
return "PG_FUNCTION_INFO_V1(" + f.Name + ");"
}
//Code writes the wrapper function
func (f *VoidFunction) Code(w io.Writer) {
w.Write([]byte("//export " + f.Name + "\nfunc " + f.Name + "(fcinfo *funcInfo) Datum {\n"))
if len(f.Params) > 0 {
for _, p := range f.Params {
w.Write([]byte("var " + p.Name + " " + p.Type + "\n"))
}
w.Write([]byte("fcinfo.Scan(\n"))
for _, p := range f.Params {
w.Write([]byte("&" + p.Name + ",\n"))
}
w.Write([]byte(")\n"))
}
w.Write([]byte(ToUnexported(f.Name) + "(\n"))
for _, p := range f.Params {
w.Write([]byte(p.Name + ",\n"))
}
w.Write([]byte(")\n"))
w.Write([]byte("return toDatum(nil)\n"))
w.Write([]byte("}\n"))
}
//SQL writes the SQL command that creates the function in DB
func (f *VoidFunction) SQL(packageName string, w io.Writer) {
w.Write([]byte("CREATE OR REPLACE FUNCTION " + f.Name + "("))
var paramStrings []string
for _, p := range f.Params {
paramStrings = append(paramStrings, p.Name+" "+datumTypes[p.Type])
}
w.Write([]byte(strings.Join(paramStrings, ",")))
w.Write([]byte(")\n"))
w.Write([]byte("RETURNS VOID AS\n"))
w.Write([]byte("'$libdir/" + packageName + "', '" + f.Name + "'\n"))
w.Write([]byte("LANGUAGE c VOLATILE STRICT;\n"))
if f.Doc == "" {
w.Write([]byte("\n"))
return
}
f.Comment(w)
}
//Comment writes the Doc comment of the golang function as an DB comment for that function
func (f *VoidFunction) Comment(w io.Writer) {
var paramTypes []string
for _, p := range f.Params {
paramTypes = append(paramTypes, datumTypes[p.Type])
}
w.Write([]byte("COMMENT ON FUNCTION " + f.Name + "(" + strings.Join(paramTypes, ",") + ") IS '" + f.Doc + "';\n\n"))
}
//Function is a list of parameters and the return type
type Function struct {
VoidFunction
ReturnType string
}
//Code writes the wrapper function
func (f *Function) Code(w io.Writer) {
w.Write([]byte("//export " + f.Name + "\nfunc " + f.Name + "(fcinfo *funcInfo) Datum {\n"))
if len(f.Params) > 0 {
for _, p := range f.Params {
w.Write([]byte("var " + p.Name + " " + p.Type + "\n"))
}
w.Write([]byte("fcinfo.Scan(\n"))
for _, p := range f.Params {
w.Write([]byte("&" + p.Name + ",\n"))
}
w.Write([]byte(")\n"))
}
w.Write([]byte("ret := "))
w.Write([]byte(ToUnexported(f.Name) + "(\n"))
for _, p := range f.Params {
w.Write([]byte(p.Name + ",\n"))
}
w.Write([]byte(")\n"))
w.Write([]byte("return toDatum(ret)\n"))
w.Write([]byte("}\n"))
}
//SQL writes the SQL command that creates the function in DB
func (f *Function) SQL(packageName string, w io.Writer) {
w.Write([]byte("CREATE OR REPLACE FUNCTION " + f.Name + "("))
var paramsString []string
for _, p := range f.Params {
paramsString = append(paramsString, p.Name+" "+datumTypes[p.Type])
}
w.Write([]byte(strings.Join(paramsString, ",")))
w.Write([]byte(")\n"))
if f.ReturnType[:2] == "[]" {
w.Write([]byte("RETURNS " + datumTypes[f.ReturnType[2:len(f.ReturnType)]] + "[] AS\n"))
} else {
w.Write([]byte("RETURNS " + datumTypes[f.ReturnType] + " AS\n"))
}
w.Write([]byte("'$libdir/" + packageName + "', '" + f.Name + "'\n"))
w.Write([]byte("LANGUAGE c VOLATILE STRICT;\n"))
if f.Doc == "" {
w.Write([]byte("\n"))
return
}
f.Comment(w)
}
//TriggerFunction a special type of function, it takes TriggerData as the first argument and TriggerRow as return type
type TriggerFunction struct {
VoidFunction
}
//Code writes the wrapper function
func (f *TriggerFunction) Code(w io.Writer) {
w.Write([]byte("//export " + f.Name + "\nfunc " + f.Name + "(fcinfo *funcInfo) Datum {\n"))
if len(f.Params) > 0 {
//TODO scan from fcinfo may not work, TEST IT!
for _, p := range f.Params {
w.Write([]byte("var " + p.Name + " " + p.Type + "\n"))
}
w.Write([]byte("fcinfo.Scan(\n"))
for _, p := range f.Params {
w.Write([]byte("&" + p.Name + ",\n"))
}
w.Write([]byte(")\n"))
}
w.Write([]byte("ret := "))
w.Write([]byte(ToUnexported(f.Name) + "(\nfcinfo.TriggerData(),\n"))
for _, p := range f.Params {
w.Write([]byte(p.Name + ",\n"))
}
w.Write([]byte(")\n"))
w.Write([]byte("return toDatum(ret)\n"))
w.Write([]byte("}\n"))
}
//SQL writes the SQL command that creates the function in DB
func (f *TriggerFunction) SQL(packageName string, w io.Writer) {
w.Write([]byte("CREATE OR REPLACE FUNCTION " + f.Name + "("))
var paramsString []string
for _, p := range f.Params {
paramsString = append(paramsString, p.Name+" "+datumTypes[p.Type])
}
w.Write([]byte(strings.Join(paramsString, ",")))
w.Write([]byte(")\n"))
w.Write([]byte("RETURNS TRIGGER AS\n"))
w.Write([]byte("'$libdir/" + packageName + "', '" + f.Name + "'\n"))
w.Write([]byte("LANGUAGE c VOLATILE STRICT;\n"))
if f.Doc == "" {
w.Write([]byte("\n"))
return
}
f.Comment(w)
}