MCPcopy
hub / github.com/sqlc-dev/sqlc / createFunction

Method createFunction

internal/sql/catalog/func.go:53–94  ·  view source on GitHub ↗
(stmt *ast.CreateFunctionStmt)

Source from the content-addressed store, hash-verified

51}
52
53func (c *Catalog) createFunction(stmt *ast.CreateFunctionStmt) error {
54 ns := stmt.Func.Schema
55 if ns == "" {
56 ns = c.DefaultSchema
57 }
58 s, err := c.getSchema(ns)
59 if err != nil {
60 return err
61 }
62 fn := &Function{
63 Name: stmt.Func.Name,
64 Args: make([]*Argument, len(stmt.Params.Items)),
65 ReturnType: stmt.ReturnType,
66 }
67 types := make([]*ast.TypeName, len(stmt.Params.Items))
68 for i, item := range stmt.Params.Items {
69 arg := item.(*ast.FuncParam)
70 var name string
71 if arg.Name != nil {
72 name = *arg.Name
73 }
74 fn.Args[i] = &Argument{
75 Name: name,
76 Type: arg.Type,
77 Mode: arg.Mode,
78 HasDefault: arg.DefExpr != nil,
79 }
80 types[i] = arg.Type
81 }
82
83 _, idx, err := s.getFunc(stmt.Func, types)
84 if err == nil && !stmt.Replace {
85 return sqlerr.RelationExists(stmt.Func.Name)
86 }
87
88 if idx >= 0 {
89 s.Funcs[idx] = fn
90 } else {
91 s.Funcs = append(s.Funcs, fn)
92 }
93 return nil
94}
95
96func (c *Catalog) dropFunction(stmt *ast.DropFunctionStmt) error {
97 for _, spec := range stmt.Funcs {

Callers 1

UpdateMethod · 0.95

Calls 3

getSchemaMethod · 0.95
RelationExistsFunction · 0.92
getFuncMethod · 0.80

Tested by

no test coverage detected