mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-02 13:12:16 +00:00
Support for postgres String Constants with Unicode Escapes (#1355)
This commit is contained in:
parent
c3ba2f33c6
commit
bc15f7b4ce
7 changed files with 180 additions and 0 deletions
|
@ -52,6 +52,10 @@ pub enum Value {
|
|||
/// See [Postgres docs](https://www.postgresql.org/docs/8.3/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS)
|
||||
/// for more details.
|
||||
EscapedStringLiteral(String),
|
||||
/// u&'string value' (postgres extension)
|
||||
/// See [Postgres docs](https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-UESCAPE)
|
||||
/// for more details.
|
||||
UnicodeStringLiteral(String),
|
||||
/// B'string value'
|
||||
SingleQuotedByteStringLiteral(String),
|
||||
/// B"string value"
|
||||
|
@ -102,6 +106,7 @@ impl fmt::Display for Value {
|
|||
}
|
||||
Value::DollarQuotedString(v) => write!(f, "{v}"),
|
||||
Value::EscapedStringLiteral(v) => write!(f, "E'{}'", escape_escaped_string(v)),
|
||||
Value::UnicodeStringLiteral(v) => write!(f, "U&'{}'", escape_unicode_string(v)),
|
||||
Value::NationalStringLiteral(v) => write!(f, "N'{v}'"),
|
||||
Value::HexStringLiteral(v) => write!(f, "X'{v}'"),
|
||||
Value::Boolean(v) => write!(f, "{v}"),
|
||||
|
@ -347,6 +352,41 @@ pub fn escape_escaped_string(s: &str) -> EscapeEscapedStringLiteral<'_> {
|
|||
EscapeEscapedStringLiteral(s)
|
||||
}
|
||||
|
||||
pub struct EscapeUnicodeStringLiteral<'a>(&'a str);
|
||||
|
||||
impl<'a> fmt::Display for EscapeUnicodeStringLiteral<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for c in self.0.chars() {
|
||||
match c {
|
||||
'\'' => {
|
||||
write!(f, "''")?;
|
||||
}
|
||||
'\\' => {
|
||||
write!(f, r#"\\"#)?;
|
||||
}
|
||||
x if x.is_ascii() => {
|
||||
write!(f, "{}", c)?;
|
||||
}
|
||||
_ => {
|
||||
let codepoint = c as u32;
|
||||
// if the character fits in 32 bits, we can use the \XXXX format
|
||||
// otherwise, we need to use the \+XXXXXX format
|
||||
if codepoint <= 0xFFFF {
|
||||
write!(f, "\\{:04X}", codepoint)?;
|
||||
} else {
|
||||
write!(f, "\\+{:06X}", codepoint)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn escape_unicode_string(s: &str) -> EscapeUnicodeStringLiteral<'_> {
|
||||
EscapeUnicodeStringLiteral(s)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue