Skip to content

Commit 17d5f8c

Browse files
committed
Avoid double apostrophes
If the value is Single it's already quoted with apostrophes. Avoid double apostrophes it that case by using double-quotes instead. For example, if the value is 'value' the output is "'value'" instead of ''value''.
1 parent b7e6ce7 commit 17d5f8c

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

sqlparse/sql.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ def __str__(self):
4444
def __repr__(self):
4545
cls = self._get_repr_name()
4646
value = self._get_repr_value()
47-
return "<{cls} '{value}' at 0x{id:2X}>".format(id=id(self), **locals())
47+
if value.startswith("'") and value.endswith("'"):
48+
q = '"'
49+
else:
50+
q = "'"
51+
return "<{cls} {q}{value}{q} at 0x{id:2X}>".format(
52+
id=id(self), **locals())
4853

4954
def _get_repr_name(self):
5055
return str(self.ttype).split('.')[-1]
@@ -165,7 +170,11 @@ def _pprint_tree(self, max_depth=None, depth=0, f=None):
165170
for idx, token in enumerate(self.tokens):
166171
cls = token._get_repr_name()
167172
value = token._get_repr_value()
168-
print("{indent}{idx:2d} {cls} '{value}'"
173+
if value.startswith("'") and value.endswith("'"):
174+
q = '"'
175+
else:
176+
q = "'"
177+
print("{indent}{idx:2d} {cls} {q}{value}{q}"
169178
.format(**locals()), file=f)
170179

171180
if token.is_group() and (max_depth is None or depth < max_depth):

tests/test_tokenize.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def test_tokenlist_repr():
9595
assert repr(p.tokens[0])[:len(tst)] == tst
9696

9797

98+
def test_single_quotes():
99+
p = sqlparse.parse("'test'")[0]
100+
tst = "<Single \"'test'\" at 0x"
101+
assert repr(p.tokens[0])[:len(tst)] == tst
102+
103+
98104
def test_tokenlist_first():
99105
p = sqlparse.parse(' select foo')[0]
100106
first = p.token_first()

0 commit comments

Comments
 (0)