forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.go
More file actions
79 lines (73 loc) · 1.65 KB
/
func.go
File metadata and controls
79 lines (73 loc) · 1.65 KB
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
package catalog
import (
"errors"
"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
)
func (c *Catalog) createFunction(stmt *ast.CreateFunctionStmt) error {
ns := stmt.Func.Schema
if ns == "" {
ns = c.DefaultSchema
}
s, err := c.getSchema(ns)
if err != nil {
return err
}
fn := &Function{
Name: stmt.Func.Name,
Args: make([]*Argument, len(stmt.Params.Items)),
ReturnType: stmt.ReturnType,
}
types := make([]*ast.TypeName, len(stmt.Params.Items))
for i, item := range stmt.Params.Items {
arg := item.(*ast.FuncParam)
var name string
if arg.Name != nil {
name = *arg.Name
}
fn.Args[i] = &Argument{
Name: name,
Type: arg.Type,
Mode: arg.Mode,
HasDefault: arg.DefExpr != nil,
}
types[i] = arg.Type
}
_, idx, err := s.getFunc(stmt.Func, types)
if err == nil && !stmt.Replace {
return sqlerr.RelationExists(stmt.Func.Name)
}
if idx >= 0 {
s.Funcs[idx] = fn
} else {
s.Funcs = append(s.Funcs, fn)
}
return nil
}
func (c *Catalog) dropFunction(stmt *ast.DropFunctionStmt) error {
for _, spec := range stmt.Funcs {
ns := spec.Name.Schema
if ns == "" {
ns = c.DefaultSchema
}
s, err := c.getSchema(ns)
if errors.Is(err, sqlerr.NotFound) && stmt.MissingOk {
continue
} else if err != nil {
return err
}
var idx int
if spec.HasArgs {
_, idx, err = s.getFunc(spec.Name, spec.Args)
} else {
_, idx, err = s.getFuncByName(spec.Name)
}
if errors.Is(err, sqlerr.NotFound) && stmt.MissingOk {
continue
} else if err != nil {
return err
}
s.Funcs = append(s.Funcs[:idx], s.Funcs[idx+1:]...)
}
return nil
}