rorm_sql/db_specific/
sqlite.rs

1use std::ffi::{c_char, c_void, CStr, CString};
2
3/**
4This function formats a string into a SQLite quoted string.
5*/
6pub(crate) fn fmt(input: &str) -> String {
7    let c_str = CString::new("%Q").expect("This should not fail");
8    let input_str = CString::new(input).expect("Found \\0 in format string.");
9
10    let formatted;
11    unsafe {
12        let formatted_str: *mut c_char =
13            libsqlite3_sys::sqlite3_mprintf(c_str.as_ptr(), input_str.as_ptr());
14
15        formatted = String::from(
16            CStr::from_ptr(formatted_str)
17                .to_str()
18                .expect("Return string must be UTF-8"),
19        );
20
21        libsqlite3_sys::sqlite3_free(formatted_str as *mut c_void);
22    }
23
24    formatted
25}