MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / try_parse

Function try_parse

nodedb-sql/src/ddl_ast/parse/copy_to.rs:17–52  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

15/// Returns `None` if the SQL is not a COPY statement. Returns `Some(Err)` on
16/// parse errors. Returns `Some(Ok(CopyToFile {...}))` on success.
17pub(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
54fn parse_copy_to(trimmed: &str, after_copy: &str) -> Result<NodedbStatement, SqlError> {
55 // Determine if this is the query form or the table form.

Callers

nothing calls this directly

Calls 5

parse_copy_toFunction · 0.85
find_matching_parenFunction · 0.70
lenMethod · 0.45
containsMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected