Skip to content

Added support for Mysql Backslash escapes #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2023
Merged
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
27 changes: 19 additions & 8 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,29 +1094,40 @@ impl<'a> Tokenizer<'a> {

chars.next(); // consume the opening quote

// slash escaping is specific to MySQL dialect
let mut is_escaped = false;
while let Some(&ch) = chars.peek() {
match ch {
char if char == quote_style => {
chars.next(); // consume
if is_escaped {
s.push(ch);
is_escaped = false;
} else if chars.peek().map(|c| *c == quote_style).unwrap_or(false) {
if chars.peek().map(|c| *c == quote_style).unwrap_or(false) {
s.push(ch);
chars.next();
} else {
return Ok(s);
}
}
'\\' => {
// consume
chars.next();
// slash escaping is specific to MySQL dialect
if dialect_of!(self is MySqlDialect) {
is_escaped = !is_escaped;
if let Some(next) = chars.peek() {
// See https://dev.mysql.com/doc/refman/8.0/en/string-literals.html#character-escape-sequences
let n = match next {
'\'' | '\"' | '\\' | '%' | '_' => *next,
'0' => '\0',
'b' => '\u{8}',
'n' => '\n',
'r' => '\r',
't' => '\t',
'Z' => '\u{1a}',
_ => *next,
};
s.push(n);
chars.next(); // consume next
}
} else {
s.push(ch);
}
chars.next();
}
_ => {
chars.next(); // consume
Expand Down
47 changes: 23 additions & 24 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,35 +518,34 @@ fn parse_unterminated_escape() {

#[test]
fn parse_escaped_string() {
let sql = r#"SELECT 'I\'m fine'"#;

let stmt = mysql().one_statement_parses_to(sql, "");

match stmt {
Statement::Query(query) => match *query.body {
SetExpr::Select(value) => {
let expr = expr_from_projection(only(&value.projection));
assert_eq!(
*expr,
Expr::Value(Value::SingleQuotedString("I'm fine".to_string()))
);
}
fn assert_mysql_query_value(sql: &str, quoted: &str) {
let stmt = mysql().one_statement_parses_to(sql, "");

match stmt {
Statement::Query(query) => match *query.body {
SetExpr::Select(value) => {
let expr = expr_from_projection(only(&value.projection));
assert_eq!(
*expr,
Expr::Value(Value::SingleQuotedString(quoted.to_string()))
);
}
_ => unreachable!(),
},
_ => unreachable!(),
},
_ => unreachable!(),
};
};
}
let sql = r#"SELECT 'I\'m fine'"#;
assert_mysql_query_value(sql, "I'm fine");

let sql = r#"SELECT 'I''m fine'"#;
assert_mysql_query_value(sql, "I'm fine");

let projection = mysql().verified_only_select(sql).projection;
let item = projection.get(0).unwrap();
let sql = r#"SELECT 'I\"m fine'"#;
assert_mysql_query_value(sql, "I\"m fine");

match &item {
SelectItem::UnnamedExpr(Expr::Value(value)) => {
assert_eq!(*value, Value::SingleQuotedString("I'm fine".to_string()));
}
_ => unreachable!(),
}
let sql = r#"SELECT 'Testing: \0 \\ \% \_ \b \n \r \t \Z \a \ '"#;
assert_mysql_query_value(sql, "Testing: \0 \\ % _ \u{8} \n \r \t \u{1a} a ");
}

#[test]
Expand Down