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

Function parse_clone_database

nodedb-sql/src/ddl_ast/parse/database/clone.rs:8–84  ·  view source on GitHub ↗
(
    parts: &[&str],
    upper: &str,
)

Source from the content-addressed store, hash-verified

6use crate::error::SqlError;
7
8pub(super) fn parse_clone_database(
9 parts: &[&str],
10 upper: &str,
11) -> Result<NodedbStatement, SqlError> {
12 let new_name = parts
13 .get(2)
14 .copied()
15 .ok_or_else(|| SqlError::Parse {
16 detail: "CLONE DATABASE requires a target name".into(),
17 })?
18 .trim_matches('"')
19 .to_string();
20 let from_idx = parts
21 .iter()
22 .position(|w| w.to_uppercase() == "FROM")
23 .ok_or_else(|| SqlError::Parse {
24 detail: "CLONE DATABASE requires FROM <source>".into(),
25 })?;
26 let source_name = parts
27 .get(from_idx + 1)
28 .copied()
29 .ok_or_else(|| SqlError::Parse {
30 detail: "CLONE DATABASE FROM requires a source name".into(),
31 })?
32 .trim_matches('"')
33 .to_string();
34
35 // Determine the AS OF clause.
36 // Accepted forms:
37 // (no AS OF) → Latest
38 // AS OF SYSTEM TIME LATEST → Latest
39 // AS OF SYSTEM TIME <ms> → SystemTimeMs(<ms>)
40 let as_of = if upper.contains("AS OF SYSTEM TIME") {
41 let ms_idx = parts
42 .iter()
43 .position(|w| w.to_uppercase() == "AS")
44 .and_then(|i| {
45 if parts.get(i + 1).map(|w| w.to_uppercase()).as_deref() == Some("OF")
46 && parts.get(i + 2).map(|w| w.to_uppercase()).as_deref() == Some("SYSTEM")
47 && parts.get(i + 3).map(|w| w.to_uppercase()).as_deref() == Some("TIME")
48 {
49 Some(i + 4)
50 } else {
51 None
52 }
53 });
54 match ms_idx {
55 Some(idx) => match parts.get(idx) {
56 Some(tok) if tok.to_uppercase() == "LATEST" => CloneAsOf::Latest,
57 Some(ms_str) => {
58 let ms = ms_str.parse::<i64>().map_err(|_| SqlError::Parse {
59 detail: format!(
60 "CLONE DATABASE AS OF SYSTEM TIME: expected integer milliseconds \
61 or LATEST, got '{ms_str}'"
62 ),
63 })?;
64 CloneAsOf::SystemTimeMs(ms)
65 }

Callers 1

try_parseFunction · 0.70

Calls 4

to_stringMethod · 0.80
getMethod · 0.45
iterMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected