Skip to content

feat: non-escape mode (draft for discussion) #862

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

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 12 additions & 7 deletions src/ast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,29 @@ impl fmt::Display for DateTimeField {

pub struct EscapeQuotedString<'a> {
string: &'a str,
quote: char,
_quote: char,
}

impl<'a> fmt::Display for EscapeQuotedString<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for c in self.string.chars() {
if c == self.quote {
write!(f, "{q}{q}", q = self.quote)?;
} else {
write!(f, "{c}")?;
}
// The below process is necessary iff the sql is escaped during parse.
// if c == self.quote {
// write!(f, "{q}{q}", q = self.quote)?;
// } else {
// write!(f, "{c}")?;
// }
write!(f, "{c}")?;
}
Ok(())
}
}

pub fn escape_quoted_string(string: &str, quote: char) -> EscapeQuotedString<'_> {
EscapeQuotedString { string, quote }
EscapeQuotedString {
string,
_quote: quote,
}
}

pub fn escape_single_quote_string(s: &str) -> EscapeQuotedString<'_> {
Expand Down
61 changes: 33 additions & 28 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use serde::{Deserialize, Serialize};
use sqlparser_derive::{Visit, VisitMut};

use crate::ast::DollarQuotedString;
use crate::dialect::Dialect;
use crate::dialect::{BigQueryDialect, GenericDialect, SnowflakeDialect};
use crate::dialect::{Dialect, MySqlDialect};
use crate::keywords::{Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX};

/// SQL Token enumeration
Expand Down Expand Up @@ -1113,34 +1113,38 @@ impl<'a> Tokenizer<'a> {
if chars.peek().map(|c| *c == quote_style).unwrap_or(false) {
s.push(ch);
chars.next();
s.push(ch); // added for non-escape mode
} else {
return Ok(s);
}
}
'\\' => {
// consume
chars.next();
// slash escaping is specific to MySQL dialect
if dialect_of!(self is MySqlDialect) {
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);
}
}
// The below escape-process should be skipped in non-escape mode.
// '\\' => {
// // consume
// chars.next();
// // slash escaping is specific to MySQL dialect
// if dialect_of!(self is MySqlDialect) {
// 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('\\');
// s.push(*next);
// //s.push(n);
// chars.next(); // consume next
// }
// } else {
// s.push(ch);
// }
// }
_ => {
chars.next(); // consume
s.push(ch);
Expand Down Expand Up @@ -1218,6 +1222,7 @@ fn parse_quoted_ident(chars: &mut State, quote_end: char) -> (String, Option<cha
if chars.peek() == Some(&quote_end) {
chars.next();
s.push(ch);
s.push(ch); // added for non-escape mode
} else {
last_char = Some(quote_end);
break;
Expand Down Expand Up @@ -1860,11 +1865,11 @@ mod tests {
let tokens = tokenizer.tokenize().unwrap();
let expected = vec![
Token::Whitespace(Whitespace::Space),
Token::make_word(r#"a " b"#, Some('"')),
Token::make_word(r#"a "" b"#, Some('"')),
Token::Whitespace(Whitespace::Space),
Token::make_word(r#"a ""#, Some('"')),
Token::make_word(r#"a """#, Some('"')),
Token::Whitespace(Whitespace::Space),
Token::make_word(r#"c """#, Some('"')),
Token::make_word(r#"c """""#, Some('"')),
Token::Whitespace(Whitespace::Space),
];
compare(expected, tokens);
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ fn parse_escaped_single_quote_string_predicate() {
left: Box::new(Expr::Identifier(Ident::new("salary"))),
op: NotEq,
right: Box::new(Expr::Value(Value::SingleQuotedString(
"Jim's salary".to_string()
"Jim''s salary".to_string()
))),
}),
ast.selection,
Expand Down
17 changes: 10 additions & 7 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ fn parse_quote_identifiers_2() {
distinct: None,
top: None,
projection: vec![SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "quoted ` identifier".into(),
value: "quoted `` identifier".into(),
quote_style: Some('`'),
}))],
into: None,
Expand Down Expand Up @@ -482,7 +482,7 @@ fn parse_quote_identifiers_3() {
distinct: None,
top: None,
projection: vec![SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "`quoted identifier`".into(),
value: "``quoted identifier``".into(),
quote_style: Some('`'),
}))],
into: None,
Expand Down Expand Up @@ -535,17 +535,20 @@ fn parse_escaped_string() {
_ => 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, r#"I\'m fine"#);

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

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

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 ");
assert_mysql_query_value(sql, r#"Testing: \0 \\ \% \_ \b \n \r \t \Z \a \ "#);

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

#[test]
Expand Down