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

Function ParseQueryNameAndType

internal/metadata/meta.go:61–119  ·  view source on GitHub ↗
(t string, commentStyle CommentSyntax)

Source from the content-addressed store, hash-verified

59}
60
61func ParseQueryNameAndType(t string, commentStyle CommentSyntax) (string, string, error) {
62 for _, line := range strings.Split(t, "\n") {
63 var prefix string
64 if strings.HasPrefix(line, "--") {
65 if !commentStyle.Dash {
66 continue
67 }
68 prefix = "--"
69 }
70 if strings.HasPrefix(line, "/*") {
71 if !commentStyle.SlashStar {
72 continue
73 }
74 prefix = "/*"
75 }
76 if strings.HasPrefix(line, "#") {
77 if !commentStyle.Hash {
78 continue
79 }
80 prefix = "#"
81 }
82 if prefix == "" {
83 continue
84 }
85 rest := line[len(prefix):]
86 if !strings.HasPrefix(strings.TrimSpace(rest), "name") {
87 continue
88 }
89 if !strings.Contains(rest, ":") {
90 continue
91 }
92 if !strings.HasPrefix(rest, " name: ") {
93 return "", "", fmt.Errorf("invalid metadata: %s", line)
94 }
95
96 part := strings.Split(strings.TrimSpace(line), " ")
97 if prefix == "/*" {
98 part = part[:len(part)-1] // removes the trailing "*/" element
99 }
100 if len(part) == 3 {
101 return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
102 }
103 if len(part) != 4 {
104 return "", "", fmt.Errorf("invalid query comment: %s", line)
105 }
106 queryName := part[2]
107 queryType := strings.TrimSpace(part[3])
108 switch queryType {
109 case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdExecLastId, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne:
110 default:
111 return "", "", fmt.Errorf("invalid query type: %s", queryType)
112 }
113 if err := validateQueryName(queryName); err != nil {
114 return "", "", err
115 }
116 return queryName, queryType, nil
117 }
118 return "", "", nil

Callers 2

parseQueryMethod · 0.92

Calls 1

validateQueryNameFunction · 0.85

Tested by 1