Skip to content

Commit 80751ed

Browse files
Generic: Fix quote escaping when displaying hex string literals (#2542)
1 parent d398304 commit 80751ed

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/ast/value.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl fmt::Display for Value {
276276
Value::NationalStringLiteral(v) => write!(f, "N'{}'", escape_single_quote_string(v)),
277277
Value::QuoteDelimitedStringLiteral(v) => v.fmt(f),
278278
Value::NationalQuoteDelimitedStringLiteral(v) => write!(f, "N{v}"),
279-
Value::HexStringLiteral(v) => write!(f, "X'{v}'"),
279+
Value::HexStringLiteral(v) => write!(f, "X'{}'", escape_single_quote_string(v)),
280280
Value::Boolean(v) => write!(f, "{v}"),
281281
Value::SingleQuotedByteStringLiteral(v) => write!(f, "B'{v}'"),
282282
Value::DoubleQuotedByteStringLiteral(v) => write!(f, "B\"{v}\""),
@@ -717,3 +717,15 @@ fn test_escape_quoted_string_with_multibyte_quote_char() {
717717
"a🦀🦀b🦀🦀c"
718718
);
719719
}
720+
#[cfg(test)]
721+
#[test]
722+
fn test_hex_string_literal_display_escaping() {
723+
assert_eq!(
724+
Value::HexStringLiteral("'".to_string()).to_string(),
725+
"X''''"
726+
);
727+
assert_eq!(
728+
Value::HexStringLiteral("it's".to_string()).to_string(),
729+
"X'it''s'"
730+
);
731+
}

‎tests/sqlparser_common.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20121,3 +20121,10 @@ fn parse_table_preserves_quotes_and_trailing_tokens() {
2012120121
err
2012220122
);
2012320123
}
20124+
20125+
#[test]
20126+
fn parse_hex_string_literal_display_escaping() {
20127+
all_dialects().verified_stmt("SELECT X''''");
20128+
all_dialects().verified_stmt("SELECT X'ab''cd'");
20129+
all_dialects().one_statement_parses_to("SELECT x'''' N", "SELECT X'''' AS N");
20130+
}

0 commit comments

Comments
 (0)