Skip to content

feat: mysql no-escape mode #870

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 21 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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
35 changes: 29 additions & 6 deletions src/ast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::Number(v, l) => write!(f, "{}{long}", v, long = if *l { "L" } else { "" }),
Value::DoubleQuotedString(v) => write!(f, "\"{v}\""),
Value::DoubleQuotedString(v) => write!(f, "\"{}\"", escape_double_quote_string(v)),
Value::SingleQuotedString(v) => write!(f, "'{}'", escape_single_quote_string(v)),
Value::DollarQuotedString(v) => write!(f, "{v}"),
Value::EscapedStringLiteral(v) => write!(f, "E'{}'", escape_escaped_string(v)),
Expand Down Expand Up @@ -184,12 +184,31 @@ pub struct EscapeQuotedString<'a> {

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}")?;
let quote = self.quote;
let mut previous_char = char::default();
let mut peekable_chars = self.string.chars().peekable();
while let Some(&ch) = peekable_chars.peek() {
match ch {
char if char == quote => {
if previous_char == '\\' {
write!(f, "{char}")?;
peekable_chars.next();
continue;
}
peekable_chars.next();
if peekable_chars.peek().map(|c| *c == quote).unwrap_or(false) {
write!(f, "{char}{char}")?;
peekable_chars.next();
} else {
write!(f, "{char}{char}")?;
}
}
_ => {
write!(f, "{ch}")?;
peekable_chars.next();
}
}
previous_char = ch;
}
Ok(())
}
Expand All @@ -203,6 +222,10 @@ pub fn escape_single_quote_string(s: &str) -> EscapeQuotedString<'_> {
escape_quoted_string(s, '\'')
}

pub fn escape_double_quote_string(s: &str) -> EscapeQuotedString<'_> {
escape_quoted_string(s, '\"')
}

pub struct EscapeEscapedStringLiteral<'a>(&'a str);

impl<'a> fmt::Display for EscapeEscapedStringLiteral<'a> {
Expand Down
4 changes: 3 additions & 1 deletion src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ mod tests {
use crate::dialect::GenericDialect;
use crate::parser::Parser;
use crate::tokenizer::Tokenizer;
use crate::tokenizer::TokenizerOptions;

#[derive(Default)]
struct TestVisitor {
Expand Down Expand Up @@ -632,7 +633,8 @@ mod tests {

fn do_visit(sql: &str) -> Vec<String> {
let dialect = GenericDialect {};
let mut tokenizer = Tokenizer::new(&dialect, sql);
let option = TokenizerOptions::new().with_no_escape(false);
let mut tokenizer = Tokenizer::new(&dialect, sql, &option);
let tokens = tokenizer.tokenize().unwrap();
let s = Parser::new(&dialect)
.with_tokens(tokens)
Expand Down
16 changes: 9 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const DEFAULT_REMAINING_DEPTH: usize = 50;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ParserOptions {
pub trailing_commas: bool,
pub no_escape: bool,
}

pub struct Parser<'a> {
Expand All @@ -207,7 +208,7 @@ pub struct Parser<'a> {
/// The current dialect to use
dialect: &'a dyn Dialect,
/// Additional options that allow you to mix & match behavior otherwise
/// constrained to certain dialects (e.g. trailing commas)
/// constrained to certain dialects (e.g. trailing commas) and/or format of parse (e.g. no escape)
options: ParserOptions,
/// ensure the stack does not overflow by limiting recursion depth
recursion_counter: RecursionCounter,
Expand Down Expand Up @@ -277,7 +278,7 @@ impl<'a> Parser<'a> {
/// # fn main() -> Result<(), ParserError> {
/// let dialect = GenericDialect{};
/// let result = Parser::new(&dialect)
/// .with_options(ParserOptions { trailing_commas: true })
/// .with_options(ParserOptions { trailing_commas: true, no_escape: false })
/// .try_with_sql("SELECT a, b, COUNT(*), FROM foo GROUP BY a, b,")?
/// .parse_statements();
/// assert!(matches!(result, Ok(_)));
Expand Down Expand Up @@ -317,7 +318,8 @@ impl<'a> Parser<'a> {
/// See example on [`Parser::new()`] for an example
pub fn try_with_sql(self, sql: &str) -> Result<Self, ParserError> {
debug!("Parsing sql '{}'...", sql);
let mut tokenizer = Tokenizer::new(self.dialect, sql);
let tokenizer_options = TokenizerOptions::new().with_no_escape(self.options.no_escape);
let mut tokenizer = Tokenizer::new(self.dialect, sql, &tokenizer_options);
let tokens = tokenizer.tokenize()?;
Ok(self.with_tokens(tokens))
}
Expand Down Expand Up @@ -3562,7 +3564,7 @@ impl<'a> Parser<'a> {
self.expect_token(&Token::RParen)?;
Ok(Some(ColumnOption::Check(expr)))
} else if self.parse_keyword(Keyword::AUTO_INCREMENT)
&& dialect_of!(self is MySqlDialect | GenericDialect)
&& dialect_of!(self is MySqlDialect | GenericDialect)
{
// Support AUTO_INCREMENT for MySQL
Ok(Some(ColumnOption::DialectSpecific(vec![
Expand Down Expand Up @@ -7115,7 +7117,7 @@ mod tests {
#[test]
fn test_prev_index() {
let sql = "SELECT version";
all_dialects().run_parser_method(sql, |parser| {
all_dialects(None).run_parser_method(sql, |parser| {
assert_eq!(parser.peek_token(), Token::make_keyword("SELECT"));
assert_eq!(parser.next_token(), Token::make_keyword("SELECT"));
parser.prev_token();
Expand Down Expand Up @@ -7470,7 +7472,7 @@ mod tests {
// The expected name should be identical as the input name, that's why I don't receive both
macro_rules! test_parse_schema_name {
($input:expr, $expected_name:expr $(,)?) => {{
all_dialects().run_parser_method(&*$input, |parser| {
all_dialects(None).run_parser_method(&*$input, |parser| {
let schema_name = parser.parse_schema_name().unwrap();
// Validate that the structure is the same as expected
assert_eq!(schema_name, $expected_name);
Expand Down Expand Up @@ -7682,7 +7684,7 @@ mod tests {
fn test_parse_multipart_identifier_negative() {
macro_rules! test_parse_multipart_identifier_error {
($input:expr, $expected_err:expr $(,)?) => {{
all_dialects().run_parser_method(&*$input, |parser| {
all_dialects(None).run_parser_method(&*$input, |parser| {
let actual_err = parser.parse_multipart_identifier().unwrap_err();
assert_eq!(actual_err.to_string(), $expected_err);
});
Expand Down
4 changes: 2 additions & 2 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl TestedDialects {
}
}

pub fn all_dialects() -> TestedDialects {
pub fn all_dialects(options: Option<ParserOptions>) -> TestedDialects {
TestedDialects {
dialects: vec![
Box::new(GenericDialect {}),
Expand All @@ -170,7 +170,7 @@ pub fn all_dialects() -> TestedDialects {
Box::new(SQLiteDialect {}),
Box::new(DuckDbDialect {}),
],
options: None,
options,
}
}

Expand Down
Loading