ParseCopyToOptions extracts options from a COPY TO STDOUT command
(query string)
| 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 |
| 137 | func 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 |
no outgoing calls