Skip to main content

escape_sql_string_literal

Function escape_sql_string_literal 

Source
pub fn escape_sql_string_literal(s: &str) -> Cow<'_, str>
Expand description

Escape SQL single quotes by doubling them — the canonical, portable way to embed a string inside a '...' literal across Postgres / MySQL / SQLite. Returns a borrowed slice when no escape is required (zero allocation), otherwise an owned String.

use std::borrow::Cow;
use vespertide_core::escape_sql_string_literal;

assert!(matches!(escape_sql_string_literal("hello"), Cow::Borrowed(_)));
assert_eq!(escape_sql_string_literal("O'Brien"), "O''Brien");
assert_eq!(escape_sql_string_literal("'leading"), "''leading");
assert_eq!(escape_sql_string_literal(""), "");