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

Function try_parse

nodedb-sql/src/ddl_ast/parse/trigger.rs:9–64  ·  view source on GitHub ↗
(
    upper: &str,
    parts: &[&str],
    trimmed: &str,
)

Source from the content-addressed store, hash-verified

7use crate::error::SqlError;
8
9pub(super) fn try_parse(
10 upper: &str,
11 parts: &[&str],
12 trimmed: &str,
13) -> Option<Result<NodedbStatement, SqlError>> {
14 (|| -> Result<Option<NodedbStatement>, SqlError> {
15 if upper.starts_with("CREATE ") && upper.contains("TRIGGER ") {
16 return Ok(Some(parse_create_trigger(upper, trimmed)));
17 }
18 if upper.starts_with("DROP TRIGGER ") {
19 let if_exists = upper.contains("IF EXISTS");
20 let name = match extract_name_after_if_exists(parts, "TRIGGER") {
21 None => return Ok(None),
22 Some(r) => r?,
23 };
24 let collection = extract_after_keyword(parts, "ON").unwrap_or_default();
25 return Ok(Some(NodedbStatement::Automation(
26 AutomationStmt::DropTrigger {
27 name,
28 collection,
29 if_exists,
30 },
31 )));
32 }
33 if upper.starts_with("ALTER TRIGGER ") {
34 // ALTER TRIGGER <name> ENABLE|DISABLE|OWNER TO <new_owner>
35 let name = parts.get(2).map(|s| s.to_lowercase()).unwrap_or_default();
36 let action = parts.get(3).map(|s| s.to_uppercase()).unwrap_or_default();
37 // When action is "OWNER", "TO" is at index 4 and new_owner at index 5.
38 let new_owner = if action == "OWNER" {
39 parts.get(5).map(|s| s.trim_end_matches(';').to_string())
40 } else {
41 None
42 };
43 return Ok(Some(NodedbStatement::Automation(
44 AutomationStmt::AlterTrigger {
45 name,
46 action,
47 new_owner,
48 },
49 )));
50 }
51 if upper.starts_with("SHOW TRIGGERS") {
52 let collection = if upper.starts_with("SHOW TRIGGERS ON ") {
53 parts.get(3).map(|s| s.to_string())
54 } else {
55 None
56 };
57 return Ok(Some(NodedbStatement::Automation(
58 AutomationStmt::ShowTriggers { collection },
59 )));
60 }
61 Ok(None)
62 })()
63 .transpose()
64}
65
66/// Structural extraction for `CREATE [OR REPLACE] [SYNC|DEFERRED] TRIGGER`.

Callers

nothing calls this directly

Calls 6

extract_after_keywordFunction · 0.85
to_stringMethod · 0.80
parse_create_triggerFunction · 0.70
containsMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected