Skip to content

Commit

Permalink
Fix(tokenizer): treat quote as escape only if its followed by itself (#…
Browse files Browse the repository at this point in the history
…2199)

* Fix(tokenizer): treat quote as escape only if its followed by itself

* Cleanup

* Cleanup

* Cleanup
  • Loading branch information
georgesittas authored Sep 12, 2023
1 parent e2bb427 commit a228656
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Tokenizer(tokens.Tokenizer):
QUOTES = ["'", '"']
COMMENTS = ["--", "#", ("/*", "*/")]
IDENTIFIERS = ["`"]
STRING_ESCAPES = ["'", "\\"]
STRING_ESCAPES = ["'", '"', "\\"]
BIT_STRINGS = [("b'", "'"), ("B'", "'"), ("0b", "")]
HEX_STRINGS = [("x'", "'"), ("X'", "'"), ("0x", "")]

Expand Down
6 changes: 5 additions & 1 deletion sqlglot/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,11 @@ def _extract_string(self, delimiter: str, escapes=None) -> str:
escapes = self._STRING_ESCAPES if escapes is None else escapes

while True:
if self._char in escapes and (self._peek == delimiter or self._peek in escapes):
if (
self._char in escapes
and (self._peek == delimiter or self._peek in escapes)
and (self._char not in self._QUOTES or self._char == self._peek)
):
if self._peek == delimiter:
text += self._peek
else:
Expand Down
9 changes: 9 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ def test_canonical_functions(self):
)

def test_escape(self):
self.validate_identity("""'"abc"'""")
self.validate_identity(
r"'\'a'",
"'''a'",
)
self.validate_identity(
'''"'abc'"''',
"'''abc'''",
)
self.validate_all(
r"'a \' b '' '",
write={
Expand Down

0 comments on commit a228656

Please sign in to comment.