diff --git a/lib/src/sql/escape.rs b/lib/src/sql/escape.rs index 70c5298e..36269064 100644 --- a/lib/src/sql/escape.rs +++ b/lib/src/sql/escape.rs @@ -1,5 +1,8 @@ use crate::sql::common::val_char; +const BRACKET_L: char = '⟨'; +const BRACKET_R: char = '⟩'; + const DOUBLE: char = '"'; const DOUBLE_ESC: &str = r#"\""#; @@ -11,6 +14,16 @@ pub fn escape_strand(s: &str) -> String { format!("{}{}{}", DOUBLE, s, DOUBLE) } +#[inline] +pub fn escape_id(s: &str) -> String { + for x in s.chars() { + if !val_char(x) { + return format!("{}{}{}", BRACKET_L, s, BRACKET_R); + } + } + s.to_owned() +} + #[inline] pub fn escape_key(s: &str) -> String { for x in s.chars() { diff --git a/lib/src/sql/id.rs b/lib/src/sql/id.rs index a2b3beb9..a5004f6d 100644 --- a/lib/src/sql/id.rs +++ b/lib/src/sql/id.rs @@ -1,6 +1,6 @@ use crate::cnf::ID_CHARS; use crate::sql::error::IResult; -use crate::sql::escape::escape_ident; +use crate::sql::escape::escape_id; use crate::sql::ident::ident_raw; use crate::sql::number::integer; use nanoid::nanoid; @@ -55,7 +55,7 @@ impl fmt::Display for Id { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Id::Number(v) => write!(f, "{}", v), - Id::String(v) => write!(f, "{}", escape_ident(v)), + Id::String(v) => write!(f, "{}", escape_id(v)), } } } diff --git a/lib/src/sql/thing.rs b/lib/src/sql/thing.rs index de724cfb..ac51ea81 100644 --- a/lib/src/sql/thing.rs +++ b/lib/src/sql/thing.rs @@ -1,5 +1,5 @@ use crate::sql::error::IResult; -use crate::sql::escape::escape_ident; +use crate::sql::escape::escape_id; use crate::sql::id::{id, Id}; use crate::sql::ident::ident_raw; use crate::sql::serde::is_internal_serialization; @@ -54,7 +54,7 @@ impl Thing { impl fmt::Display for Thing { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}:{}", escape_ident(&self.tb), self.id) + write!(f, "{}:{}", escape_id(&self.tb), self.id) } }