pub(crate) fn quote_string_literal(s: &str) -> String {
let escaped = s.replace('\'', "''");
format!("'{escaped}'")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn escapes_single_quotes() {
assert_eq!(quote_string_literal("plain"), "'plain'");
assert_eq!(quote_string_literal("O'Reilly"), "'O''Reilly'");
assert_eq!(
quote_string_literal("'; DROP TABLE x; --"),
"'''; DROP TABLE x; --'"
);
}
#[test]
fn passes_through_json() {
let json = r#"{"name":"O'Reilly","ok":true}"#;
let quoted = quote_string_literal(json);
assert_eq!(quoted, "'{\"name\":\"O''Reilly\",\"ok\":true}'");
}
}