RunShowSyntax analyzes the syntax and reports its structure as data for the client. Even an error is reported as data. Since errors won't propagate to the client as an error, but as a result, the usual code path to capture and record errors will not be triggered. Instead, the caller can pass a repo
( ctx context.Context, stmt string, report func(ctx context.Context, field, msg string), reportErr func(ctx context.Context, err error), )
| 24 | // be triggered. Instead, the caller can pass a reportErr closure to |
| 25 | // capture errors instead. May be nil. |
| 26 | func RunShowSyntax( |
| 27 | ctx context.Context, |
| 28 | stmt string, |
| 29 | report func(ctx context.Context, field, msg string), |
| 30 | reportErr func(ctx context.Context, err error), |
| 31 | ) { |
| 32 | if strings.HasSuffix(stmt, "??") { |
| 33 | // A statement (or, more likely, a prefix to a statement) followed |
| 34 | // by the help token (??). |
| 35 | // |
| 36 | // In that case, take a shortcut to avoid the complexity of |
| 37 | // instantiating a whole parser just to retrieve a help string. |
| 38 | // This also has the benefit of supporting retrieving help for |
| 39 | // the special non-terminals e.g. "<source> ??". |
| 40 | prefix := strings.ToUpper(strings.TrimSpace(stmt[:len(stmt)-2])) |
| 41 | if h, ok := HelpMessages[prefix]; ok { |
| 42 | msg := HelpMessage{Command: prefix, HelpMessageBody: h} |
| 43 | msgs := msg.String() |
| 44 | err := errors.WithHint(pgerror.WithCandidateCode(errors.New(specialHelpErrorPrefix), pgcode.Syntax), msgs) |
| 45 | doErr(ctx, report, reportErr, err) |
| 46 | return |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | stmts, err := Parse(stmt) |
| 51 | if err != nil { |
| 52 | doErr(ctx, report, reportErr, err) |
| 53 | } else { |
| 54 | for i := range stmts { |
| 55 | report(ctx, "sql", tree.AsStringWithFlags(stmts[i].AST, tree.FmtParsable)) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func doErr( |
| 61 | ctx context.Context, |
nothing calls this directly
no test coverage detected
searching dependent graphs…