Skip to main content

mongreldb_kit_core/
external.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4pub struct VirtualTableSpec {
5    pub name: String,
6    pub module: String,
7    #[serde(default, skip_serializing_if = "Vec::is_empty")]
8    pub args: Vec<String>,
9}
10
11impl VirtualTableSpec {
12    pub fn new(
13        name: impl Into<String>,
14        module: impl Into<String>,
15        args: impl IntoIterator<Item = impl Into<String>>,
16    ) -> Self {
17        Self {
18            name: name.into(),
19            module: module.into(),
20            args: args.into_iter().map(Into::into).collect(),
21        }
22    }
23
24    pub fn create_sql(&self) -> String {
25        let args = self.args.join(", ");
26        if args.is_empty() {
27            format!(
28                "CREATE VIRTUAL TABLE {} USING {}",
29                quote_ident(&self.name),
30                quote_ident(&self.module)
31            )
32        } else {
33            format!(
34                "CREATE VIRTUAL TABLE {} USING {}({args})",
35                quote_ident(&self.name),
36                quote_ident(&self.module)
37            )
38        }
39    }
40
41    pub fn drop_sql(&self) -> String {
42        format!("DROP TABLE {}", quote_ident(&self.name))
43    }
44}
45
46pub fn quote_ident(name: &str) -> String {
47    format!("\"{}\"", name.replace('"', "\"\""))
48}