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

Function StripComments

internal/source/code.go:99–136  ·  view source on GitHub ↗
(sql string)

Source from the content-addressed store, hash-verified

97}
98
99func StripComments(sql string) (string, []string, error) {
100 s := bufio.NewScanner(strings.NewReader(strings.TrimSpace(sql)))
101 var lines, comments []string
102 for s.Scan() {
103 t := s.Text()
104 if strings.HasPrefix(t, "-- name:") {
105 continue
106 }
107 if strings.HasPrefix(t, "/* name:") && strings.HasSuffix(t, "*/") {
108 continue
109 }
110 if strings.HasPrefix(t, "# name:") {
111 continue
112 }
113 if strings.HasPrefix(t, "--") {
114 comments = append(comments, strings.TrimPrefix(t, "--"))
115 continue
116 }
117 if strings.HasPrefix(t, "/*") && strings.HasSuffix(t, "*/") {
118 // Preserve MySQL optimizer hints, which share block-comment
119 // syntax but are semantically part of the query.
120 if strings.HasPrefix(t, "/*+") {
121 lines = append(lines, t)
122 continue
123 }
124 t = strings.TrimPrefix(t, "/*")
125 t = strings.TrimSuffix(t, "*/")
126 comments = append(comments, t)
127 continue
128 }
129 if strings.HasPrefix(t, "#") {
130 comments = append(comments, strings.TrimPrefix(t, "#"))
131 continue
132 }
133 lines = append(lines, t)
134 }
135 return strings.Join(lines, "\n"), comments, s.Err()
136}
137
138func CleanedComments(rawSQL string, cs CommentSyntax) ([]string, error) {
139 s := bufio.NewScanner(strings.NewReader(strings.TrimSpace(rawSQL)))

Callers 1

parseQueryMethod · 0.92

Calls 3

ScanMethod · 0.45
JoinMethod · 0.45
ErrMethod · 0.45

Tested by

no test coverage detected