Try to parse a COPY TO statement. Returns `None` if the SQL is not a COPY statement. Returns `Some(Err)` on parse errors. Returns `Some(Ok(CopyToFile {...}))` on success.
(upper: &str, trimmed: &str)
| 15 | /// Returns `None` if the SQL is not a COPY statement. Returns `Some(Err)` on |
| 16 | /// parse errors. Returns `Some(Ok(CopyToFile {...}))` on success. |
| 17 | pub(super) fn try_parse(upper: &str, trimmed: &str) -> Option<Result<NodedbStatement, SqlError>> { |
| 18 | if !upper.starts_with("COPY ") { |
| 19 | return None; |
| 20 | } |
| 21 | |
| 22 | let after_copy = trimmed["COPY ".len()..].trim_start(); |
| 23 | let upper_after = upper["COPY ".len()..].trim_start(); |
| 24 | |
| 25 | // Query form: `COPY (SELECT ...) TO '<path>'` — the `(` starts immediately. |
| 26 | // Skip FROM/TO prefix checks; parse_copy_to will handle it. |
| 27 | if after_copy.starts_with('(') { |
| 28 | // Only claim it if there is a `) TO ` somewhere after the closing paren. |
| 29 | // We do a quick scan: find the closing paren, then check for TO. |
| 30 | if find_matching_paren(after_copy).is_some_and(|close| { |
| 31 | let rest = after_copy[close + 1..].trim_start().to_uppercase(); |
| 32 | rest.starts_with("TO ") |
| 33 | }) { |
| 34 | return Some(parse_copy_to(trimmed, after_copy)); |
| 35 | } |
| 36 | return None; |
| 37 | } |
| 38 | |
| 39 | // Table form: `COPY <collection> TO '<path>'`. |
| 40 | // Only handle the TO form; FROM form is handled in copy_from. |
| 41 | // For the table form, look at the upper-case tokens before the first quoted string. |
| 42 | let has_to = upper_after.contains(" TO "); |
| 43 | let has_from = upper_after.contains(" FROM "); |
| 44 | if has_from && (!has_to || upper_after.find(" FROM ") < upper_after.find(" TO ")) { |
| 45 | return None; |
| 46 | } |
| 47 | if !has_to { |
| 48 | return None; |
| 49 | } |
| 50 | |
| 51 | Some(parse_copy_to(trimmed, after_copy)) |
| 52 | } |
| 53 | |
| 54 | fn parse_copy_to(trimmed: &str, after_copy: &str) -> Result<NodedbStatement, SqlError> { |
| 55 | // Determine if this is the query form or the table form. |
nothing calls this directly
no test coverage detected