2022-09-27 21:35:30 +00:00
|
|
|
use crate::sql::common::val_u8;
|
|
|
|
use std::borrow::Cow;
|
2022-05-15 08:34:29 +00:00
|
|
|
|
2022-07-06 09:08:11 +00:00
|
|
|
const BRACKET_L: char = '⟨';
|
|
|
|
const BRACKET_R: char = '⟩';
|
|
|
|
|
2022-05-15 08:34:29 +00:00
|
|
|
const DOUBLE: char = '"';
|
|
|
|
const DOUBLE_ESC: &str = r#"\""#;
|
|
|
|
|
|
|
|
const BACKTICK: char = '`';
|
|
|
|
const BACKTICK_ESC: &str = r#"\`"#;
|
|
|
|
|
|
|
|
#[inline]
|
2022-10-16 16:19:36 +00:00
|
|
|
pub fn escape_str(s: &str) -> String {
|
2022-05-15 08:34:29 +00:00
|
|
|
format!("{}{}{}", DOUBLE, s, DOUBLE)
|
|
|
|
}
|
|
|
|
|
2022-07-06 09:08:11 +00:00
|
|
|
#[inline]
|
2022-09-27 21:35:30 +00:00
|
|
|
pub fn escape_id(s: &str) -> Cow<'_, str> {
|
|
|
|
for x in s.bytes() {
|
|
|
|
if !val_u8(x) {
|
|
|
|
return Cow::Owned(format!("{}{}{}", BRACKET_L, s, BRACKET_R));
|
2022-07-06 09:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-27 21:35:30 +00:00
|
|
|
Cow::Borrowed(s)
|
2022-07-06 09:08:11 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 08:34:29 +00:00
|
|
|
#[inline]
|
2022-09-27 21:35:30 +00:00
|
|
|
pub fn escape_key(s: &str) -> Cow<'_, str> {
|
|
|
|
for x in s.bytes() {
|
|
|
|
if !val_u8(x) {
|
|
|
|
return Cow::Owned(format!("{}{}{}", DOUBLE, s.replace(DOUBLE, DOUBLE_ESC), DOUBLE));
|
2022-05-15 08:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-27 21:35:30 +00:00
|
|
|
Cow::Borrowed(s)
|
2022-05-15 08:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-09-27 21:35:30 +00:00
|
|
|
pub fn escape_ident(s: &str) -> Cow<'_, str> {
|
|
|
|
for x in s.bytes() {
|
|
|
|
if !val_u8(x) {
|
|
|
|
return Cow::Owned(format!(
|
|
|
|
"{}{}{}",
|
|
|
|
BACKTICK,
|
|
|
|
s.replace(BACKTICK, BACKTICK_ESC),
|
|
|
|
BACKTICK
|
|
|
|
));
|
2022-05-15 08:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-27 21:35:30 +00:00
|
|
|
Cow::Borrowed(s)
|
2022-05-15 08:34:29 +00:00
|
|
|
}
|