Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,16 @@ impl fmt::Display for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.quote_style {
Some(q) if q == '"' || q == '\'' || q == '`' => {
let escaped = value::escape_quoted_string(&self.value, q);
write!(f, "{q}{escaped}{q}")
// The value is the decoded identifier, so every delimiter
// inside it is literal and must be doubled.
write!(f, "{q}")?;
for (i, part) in self.value.split(q).enumerate() {
if i > 0 {
write!(f, "{q}{q}")?;
}
f.write_str(part)?;
}
write!(f, "{q}")
}
Some('[') => write!(f, "[{}]", self.value),
None => f.write_str(&self.value),
Expand Down
43 changes: 43 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20060,3 +20060,46 @@ fn parse_insert_by_name() {
_ => unreachable!(),
}
}

#[test]
fn parse_quoted_identifier_with_consecutive_delimiters() {
let dialects = all_dialects_where(|d| d.is_delimited_identifier_start('"'));
let select = dialects.verified_only_select(r#"SELECT "a""""b""#);
assert_eq!(
&Expr::Identifier(Ident::with_quote('"', r#"a""b"#)),
expr_from_projection(&select.projection[0]),
);

let dialects = all_dialects_where(|d| d.is_delimited_identifier_start('`'));
let select = dialects.verified_only_select("SELECT `a````b`");
assert_eq!(
&Expr::Identifier(Ident::with_quote('`', "a``b")),
expr_from_projection(&select.projection[0]),
);
}

#[test]
fn quoted_identifier_display_round_trips() {
// Every value of length 1..=4 over the delimiter, a backslash, an ASCII
// and a multi-byte character, serialized and parsed back.
for quote in ['"', '`'] {
let dialects = all_dialects_where(move |d| d.is_delimited_identifier_start(quote));
let mut values = vec![String::new()];
for _ in 0..4 {
values = values
.iter()
.flat_map(|v| [quote, '\\', 'a', 'é'].map(|c| format!("{v}{c}")))
.collect();
for value in &values {
let ident = Ident::with_quote(quote, value.as_str());
let sql = format!("SELECT {ident}");
let select = dialects.verified_only_select(&sql);
assert_eq!(
&Expr::Identifier(ident),
expr_from_projection(&select.projection[0]),
"{sql} did not round trip",
);
}
}
}
}
8 changes: 6 additions & 2 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,9 @@ fn parse_escaped_quote_identifiers_with_no_escape() {
require_semicolon_stmt_delimiter: true,
}
)
.verified_stmt(sql),
.parse_sql_statements(sql)
.unwrap()
.remove(0),
Statement::Query(Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
Expand Down Expand Up @@ -1616,7 +1618,9 @@ fn parse_escaped_backticks_with_no_escape() {
vec![Box::new(MySqlDialect {})],
ParserOptions::new().with_unescape(false)
)
.verified_stmt(sql),
.parse_sql_statements(sql)
.unwrap()
.remove(0),
Statement::Query(Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
Expand Down