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

Function parse_drop_database

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

Source from the content-addressed store, hash-verified

6use crate::error::SqlError;
7
8pub(super) fn parse_drop_database(parts: &[&str]) -> Result<NodedbStatement, SqlError> {
9 // FORCE is accepted as a synonym for CASCADE: both set `cascade = true`.
10 // Any token after the name that is not CASCADE/FORCE is a parse error —
11 // silently ignoring trailing garbage masks typos in user SQL.
12 let mut idx = 2usize;
13 let if_exists = if parts.get(idx).map(|w| w.to_uppercase()).as_deref() == Some("IF")
14 && parts.get(idx + 1).map(|w| w.to_uppercase()).as_deref() == Some("EXISTS")
15 {
16 idx += 2;
17 true
18 } else {
19 false
20 };
21
22 let name = parts
23 .get(idx)
24 .copied()
25 .ok_or_else(|| SqlError::Parse {
26 detail: "DROP DATABASE requires a name".into(),
27 })?
28 .to_string();
29 let name = name.trim_matches('"').to_string();
30 idx += 1;
31
32 let mut cascade = false;
33 for w in &parts[idx..] {
34 match w.to_uppercase().as_str() {
35 "CASCADE" | "FORCE" => cascade = true,
36 other => {
37 return Err(SqlError::Parse {
38 detail: format!("DROP DATABASE: unexpected token '{other}'"),
39 });
40 }
41 }
42 }
43
44 Ok(NodedbStatement::Database(DatabaseStmt::DropDatabase {
45 name,
46 if_exists,
47 cascade,
48 }))
49}
50
51#[cfg(test)]
52mod tests {

Callers 1

try_parseFunction · 0.70

Calls 3

to_stringMethod · 0.80
getMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected