Skip to content

Commit e75e358

Browse files
author
casey
committed
Recognize escaped backslashes within strings
Previously if a single quoted string ended with an escaped backslash, parsing would not consider the string to be terminated.
1 parent 77e0789 commit e75e358

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

sqlparse/lexer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ class Lexer(object):
191191
(r'[-]?[0-9]*(\.[0-9]+)?[eE][-]?[0-9]+', tokens.Number.Float),
192192
(r'[-]?[0-9]*\.[0-9]+', tokens.Number.Float),
193193
(r'[-]?[0-9]+', tokens.Number.Integer),
194-
# TODO: Backslash escapes?
195-
(r"'(''|\\'|[^'])*'", tokens.String.Single),
194+
(r"'(''|\\\\|\\'|[^'])*'", tokens.String.Single),
196195
# not a real string literal in ANSI SQL:
197196
(r'(""|".*?[^\\]")', tokens.String.Symbol),
198197
(r'(?<=[\w\]])(\[[^\]]*?\])', tokens.Punctuation.ArrayIndex),

tests/test_split.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def test_split_semicolon(self):
2222
self.ndiffAssertEqual(unicode(stmts[0]), self._sql1)
2323
self.ndiffAssertEqual(unicode(stmts[1]), sql2)
2424

25+
def test_split_backslash(self):
26+
stmts = sqlparse.parse(r"select '\\'; select '\''; select '\\\'';")
27+
self.assertEqual(len(stmts), 3)
28+
2529
def test_create_function(self):
2630
sql = load_file('function.sql')
2731
stmts = sqlparse.parse(sql)

0 commit comments

Comments
 (0)