Try to parse a database-level DDL statement. Returns `None` for SQL that does not match any database-DDL prefix. Returns `Some(Err(...))` for structurally valid database DDL that contains a parse error (e.g. missing required name token).
(
_upper: &str,
parts: &[&str],
original: &str,
)
| 23 | /// Returns `Some(Err(...))` for structurally valid database DDL that contains a |
| 24 | /// parse error (e.g. missing required name token). |
| 25 | pub fn try_parse( |
| 26 | _upper: &str, |
| 27 | parts: &[&str], |
| 28 | original: &str, |
| 29 | ) -> Option<Result<NodedbStatement, SqlError>> { |
| 30 | let first = parts.first().copied().unwrap_or(""); |
| 31 | let second = parts.get(1).copied().unwrap_or("").to_uppercase(); |
| 32 | |
| 33 | match first.to_uppercase().as_str() { |
| 34 | "CREATE" if second == "DATABASE" => Some(parse_create_database(parts, original)), |
| 35 | "DROP" if second == "DATABASE" => Some(parse_drop_database(parts)), |
| 36 | "ALTER" if second == "DATABASE" => Some(parse_alter_database(parts, original)), |
| 37 | "USE" if second == "DATABASE" => Some(parse_use_database(parts)), |
| 38 | "CLONE" if second == "DATABASE" => Some(parse_clone_database(parts, original)), |
| 39 | "MIRROR" if second == "DATABASE" => Some(parse_mirror_database(parts)), |
| 40 | "MOVE" if second == "TENANT" => Some(parse_move_tenant(parts)), |
| 41 | "BACKUP" if second == "DATABASE" => Some(parse_backup_database(parts)), |
| 42 | "RESTORE" if second == "DATABASE" => Some(parse_restore_database(parts)), |
| 43 | "SHOW" if second == "DATABASES" && parts.len() == 2 => { |
| 44 | Some(Ok(NodedbStatement::Database(DatabaseStmt::ShowDatabases))) |
| 45 | } |
| 46 | // SHOW DATABASE QUOTA FOR <name> |
| 47 | "SHOW" |
| 48 | if second == "DATABASE" |
| 49 | && parts.get(2).map(|w| w.to_uppercase()).as_deref() == Some("QUOTA") => |
| 50 | { |
| 51 | Some(parse_show_database_quota_or_usage(parts, false)) |
| 52 | } |
| 53 | // SHOW DATABASE USAGE FOR <name> |
| 54 | "SHOW" |
| 55 | if second == "DATABASE" |
| 56 | && parts.get(2).map(|w| w.to_uppercase()).as_deref() == Some("USAGE") => |
| 57 | { |
| 58 | Some(parse_show_database_quota_or_usage(parts, true)) |
| 59 | } |
| 60 | // SHOW DATABASE LINEAGE FOR <name> |
| 61 | "SHOW" |
| 62 | if second == "DATABASE" |
| 63 | && parts.get(2).map(|w| w.to_uppercase()).as_deref() == Some("LINEAGE") => |
| 64 | { |
| 65 | Some(parse_show_database_lineage(parts)) |
| 66 | } |
| 67 | // SHOW DATABASE MIRROR STATUS [FOR <name>] |
| 68 | "SHOW" |
| 69 | if second == "DATABASE" |
| 70 | && parts.get(2).map(|w| w.to_uppercase()).as_deref() == Some("MIRROR") |
| 71 | && parts.get(3).map(|w| w.to_uppercase()).as_deref() == Some("STATUS") => |
| 72 | { |
| 73 | Some(parse_show_database_mirror_status(parts)) |
| 74 | } |
| 75 | _ => None, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[cfg(test)] |
| 80 | mod tests { |