feat(postgres): add quote_ident/quote_literal for DDL

This commit is contained in:
charles-gauthereau
2026-07-23 09:20:15 +02:00
parent 9fbaab6feb
commit ca17bf9cb9
2 changed files with 25 additions and 0 deletions
+8
View File
@@ -122,6 +122,14 @@ pub(crate) fn pg_restore_binary_name() -> &'static str {
}
}
pub(crate) fn quote_ident(s: &str) -> String {
format!("\"{}\"", s.replace('"', "\"\""))
}
pub(crate) fn quote_literal(s: &str) -> String {
format!("'{}'", s.replace('\'', "''"))
}
pub(crate) fn pg_dump_exists_in(dir: &std::path::Path) -> bool {
dir.join(pg_dump_binary_name()).is_file()
}
+17
View File
@@ -383,3 +383,20 @@ mod select_pg_path_tests {
}
}
}
mod quoting_tests {
use crate::domain::postgres::connection::{quote_ident, quote_literal};
#[test]
fn quote_ident_escapes_double_quotes() {
assert_eq!(quote_ident("devdb"), "\"devdb\"");
assert_eq!(quote_ident("a\"b"), "\"a\"\"b\"");
assert_eq!(quote_ident("drop\"; --"), "\"drop\"\"; --\"");
}
#[test]
fn quote_literal_escapes_single_quotes() {
assert_eq!(quote_literal("UTF8"), "'UTF8'");
assert_eq!(quote_literal("O'Brien"), "'O''Brien'");
}
}