Skip to main content

ubiquisync_sql/
util.rs

1//! Small SQL string helpers shared across backends.
2
3/// Quote a SQL identifier by wrapping it in double quotes and doubling any
4/// internal quote, so names that collide with keywords or contain specials are
5/// safe to splice into SQL. `quote_ident("select")` → `"select"`; a name
6/// containing a quote has it doubled: `quote_ident("a\"b")` → `"a""b"`.
7pub fn quote_ident(name: &str) -> String {
8    format!("\"{}\"", name.replace('"', "\"\""))
9}
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14    #[test]
15    fn quote_ident_wraps_in_double_quotes() {
16        assert_eq!(quote_ident("name"), "\"name\"");
17    }
18    #[test]
19    fn quote_ident_escapes_internal_double_quotes() {
20        assert_eq!(quote_ident("a\"b"), "\"a\"\"b\"");
21    }
22}