pub fn quote_ident(name: &str) -> String {
format!("\"{}\"", name.replace('"', "\"\""))
}
pub fn quote_relation(rel: &str) -> String {
rel.split('.')
.map(quote_ident)
.collect::<Vec<_>>()
.join(".")
}
pub fn source_relation(source_id: &str, table_name: &str) -> String {
format!(
"{}.public.{}",
quote_ident(source_id),
quote_ident(table_name)
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quotes_plain_identifier() {
assert_eq!(quote_ident("papers"), r#""papers""#);
}
#[test]
fn quotes_hyphenated_identifier() {
assert_eq!(quote_ident("patents-2024"), r#""patents-2024""#);
}
#[test]
fn doubles_embedded_quote() {
assert_eq!(quote_ident(r#"a"b"#), r#""a""b""#);
}
#[test]
fn quote_relation_quotes_each_multipart_segment() {
assert_eq!(
quote_relation("catalog.schema.table"),
r#""catalog"."schema"."table""#
);
}
#[test]
fn quote_relation_single_part_is_one_identifier() {
assert_eq!(quote_relation("papers"), r#""papers""#);
}
#[test]
fn quote_relation_quotes_hyphenated_parts() {
assert_eq!(
quote_relation("my-cat.papers-2024"),
r#""my-cat"."papers-2024""#
);
}
#[test]
fn quote_relation_doubles_embedded_quote_per_part() {
assert_eq!(quote_relation(r#"cat.a"b"#), r#""cat"."a""b""#);
}
#[test]
fn source_relation_quotes_both_parts() {
assert_eq!(
source_relation("my-source-2024", "papers-v2"),
r#""my-source-2024".public."papers-v2""#
);
}
}