MCPcopy Create free account
hub / github.com/PostHog/duckgres / ParseCopyToOptions

Function ParseCopyToOptions

server/conn_copy.go:132–158  ·  view source on GitHub ↗

ParseCopyToOptions extracts options from a COPY TO STDOUT command

(query string)

Source from the content-addressed store, hash-verified

130 Source string // Table name or (SELECT query)
131 Delimiter string
132 HasHeader bool
133 IsQuery bool // True if Source is a query in parentheses
134}
135
136// ParseCopyToOptions extracts options from a COPY TO STDOUT command
137func ParseCopyToOptions(query string) (*CopyToOptions, error) {
138 upperQuery := strings.ToUpper(query)
139
140 matches := copyToStdoutRegex.FindStringSubmatch(query)
141 if len(matches) < 2 {
142 return nil, fmt.Errorf("invalid COPY TO STDOUT syntax")
143 }
144
145 source := strings.TrimSpace(matches[1])
146 opts := &CopyToOptions{
147 Source: source,
148 Delimiter: "\t", // Default PostgreSQL text format delimiter
149 IsQuery: strings.HasPrefix(source, "(") && strings.HasSuffix(source, ")"),
150 }
151
152 // Parse delimiter
153 if m := copyDelimiterRegex.FindStringSubmatch(query); len(m) > 1 {
154 opts.Delimiter = m[1]
155 } else if copyWithCSVRegex.MatchString(upperQuery) {
156 opts.Delimiter = ","
157 }
158
159 // Parse header option (only valid with CSV)
160 opts.HasHeader = copyWithCSVRegex.MatchString(upperQuery) && copyWithHeaderRegex.MatchString(upperQuery)
161

Callers 1

TestParseCopyToOptionsFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseCopyToOptionsFunction · 0.68