nodeCreateFunction handles *tree.CreateFunction nodes.
(ctx *Context, node *tree.CreateFunction)
| 34 | |
| 35 | // nodeCreateFunction handles *tree.CreateFunction nodes. |
| 36 | func nodeCreateFunction(ctx *Context, node *tree.CreateFunction) (vitess.Statement, error) { |
| 37 | options, err := validateRoutineOptions(ctx, node.Options) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | // Grab the general information that we'll need to create the function |
| 42 | tableName := node.Name.ToTableName() |
| 43 | var retType *pgtypes.DoltgresType |
| 44 | if len(node.RetType) == 0 { |
| 45 | retType = pgtypes.Void |
| 46 | } else if !node.ReturnsTable { |
| 47 | // Return types may specify "trigger", but this doesn't apply elsewhere |
| 48 | _, retType, err = nodeResolvableTypeReference(ctx, node.RetType[0].Type, true) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | } else { |
| 53 | retType = createAnonymousCompositeType(node.RetType) |
| 54 | } |
| 55 | |
| 56 | params := make([]pgnodes.RoutineParam, len(node.Args)) |
| 57 | var defaults []vitess.Expr |
| 58 | for i, arg := range node.Args { |
| 59 | // parameter name |
| 60 | params[i].Name = arg.Name.String() |
| 61 | // parameter type |
| 62 | _, params[i].Type, err = nodeResolvableTypeReference(ctx, arg.Type, false) |
| 63 | if err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | // parameter default |
| 67 | if arg.Default != nil { |
| 68 | params[i].HasDefault = true |
| 69 | d, err := nodeExpr(ctx, arg.Default) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | defaults = append(defaults, d) |
| 74 | } |
| 75 | } |
| 76 | var strict bool |
| 77 | if nullInputOption, ok := options[tree.OptionNullInput]; ok { |
| 78 | if nullInputOption.NullInput == tree.ReturnsNullOnNullInput || nullInputOption.NullInput == tree.StrictNullInput { |
| 79 | strict = true |
| 80 | } |
| 81 | } |
| 82 | // We only support PL/pgSQL, SQL and C for now, so we verify that here |
| 83 | var parsedBody []plpgsql.InterpreterOperation |
| 84 | var sqlDef string |
| 85 | var sqlDefParsedStmts []vitess.Statement |
| 86 | var extensionName, extensionSymbol string |
| 87 | if languageOption, ok := options[tree.OptionLanguage]; ok { |
| 88 | switch strings.ToLower(languageOption.Language) { |
| 89 | case "plpgsql": |
| 90 | // PL/pgSQL is different from standard Postgres SQL, so we have to use a special parser to handle it. |
| 91 | // This parser also requires the full `CREATE FUNCTION` string, so we'll pass that. |
| 92 | parsedBody, err = plpgsql.Parse(ctx.originalQuery) |
| 93 | if err != nil { |
no test coverage detected