mongreldb_kit_core/
external.rs1use 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}
49
50#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
56pub struct ViewSpec {
57 pub name: String,
58 pub sql: String,
61}
62
63impl ViewSpec {
64 pub fn new(name: impl Into<String>, sql: impl Into<String>) -> Self {
65 Self {
66 name: name.into(),
67 sql: sql.into(),
68 }
69 }
70
71 pub fn create_sql(&self) -> String {
75 format!("CREATE VIEW {} AS {}", quote_ident(&self.name), self.sql)
76 }
77
78 pub fn drop_sql(&self) -> String {
79 format!("DROP VIEW IF EXISTS {}", quote_ident(&self.name))
80 }
81}