pub fn escape_name(name: &str) -> Result<String>Expand description
Escapes a SQL identifier for safe use in queries.
This function properly quotes and escapes a name to prevent SQL injection. The result is wrapped in double quotes with internal quotes escaped.
§Errors
Returns an error if the name exceeds the identifier length limit.
This behavior is consistent with Name::try_new().
§Example
use hyperdb_api::{escape_name, Result};
fn demo() -> Result<()> {
let escaped = escape_name("my_table")?;
assert_eq!(escaped, "\"my_table\"");
let special = escape_name("table\"with\"quotes")?;
assert_eq!(special, "\"table\"\"with\"\"quotes\"");
Ok(())
}
// A name Hyper accepts but PostgreSQL's 63-character NAMEDATALEN would not
assert!(escape_name(&"a".repeat(93)).is_ok());
// Absurdly long names are still rejected
assert!(escape_name(&"a".repeat(2000)).is_err());