Skip to content

Commit 40b92c6

Browse files
committed
fix raw query escaping
1 parent a188500 commit 40b92c6

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

mindsdb_sql_parser/lexer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,13 @@ def INTEGER(self, t):
346346

347347
@_(r"'(?:\\.|[^'])*(?:''(?:\\.|[^'])*)*'")
348348
def QUOTE_STRING(self, t):
349+
t.raw_value = t.value
349350
t.value = t.value.replace('\\"', '"').replace("\\'", "'").replace("''", "'")
350351
return t
351352

352353
@_(r'"(?:\\.|[^"])*"')
353354
def DQUOTE_STRING(self, t):
355+
t.raw_value = t.value
354356
t.value = t.value.replace('\\"', '"').replace("\\'", "'")
355357
return t
356358

mindsdb_sql_parser/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ def from_table_aliased(self, p):
13081308
def from_table(self, p):
13091309
query = NativeQuery(
13101310
integration=p.identifier,
1311-
query=tokens_to_string(p.raw_query)
1311+
query=tokens_to_string(p.raw_query, use_raw_values=True)
13121312
)
13131313
return query
13141314

mindsdb_sql_parser/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def to_single_line(text):
5959
return text
6060

6161

62-
def tokens_to_string(tokens):
62+
def tokens_to_string(tokens, use_raw_values: bool = False):
6363
# converts list of token (after lexer) to original string
6464

6565
line_num = tokens[0].lineno
@@ -81,10 +81,15 @@ def tokens_to_string(tokens):
8181
# filling space between tokens
8282
line += ' '*(token.index - shift - len(line))
8383

84+
if use_raw_values and hasattr(token, 'raw_value'):
85+
value = token.raw_value or token.value
86+
else:
87+
value = token.value
88+
8489
# add token
85-
line += token.value
90+
line += value
8691

87-
last_pos = token.index + len(token.value)
92+
last_pos = token.index + len(value)
8893

8994
# last line
9095
content += line

sly/lex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ class Token(object):
7474
'''
7575
Representation of a single token.
7676
'''
77-
__slots__ = ('type', 'value', 'lineno', 'index', 'end')
77+
__slots__ = ('type', 'value', 'lineno', 'index', 'end', 'raw_value')
7878
def __repr__(self):
79-
return f'Token(type={self.type!r}, value={self.value!r}, lineno={self.lineno}, index={self.index}, end={self.end})'
79+
return f'Token(type={self.type!r}, value={self.value!r}, lineno={self.lineno}, index={self.index}, end={self.end}, raw_value={self.raw_value!r})'
8080

8181
class TokenStr(str):
8282
@staticmethod

0 commit comments

Comments
 (0)