Skip to content

Commit 01b2e8f

Browse files
committed
* Python and PHP output filter now add a trailing whitespace.
* More test coverage.
1 parent 974222b commit 01b2e8f

5 files changed

Lines changed: 59 additions & 9 deletions

File tree

sqlparse/filters.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def _process(self, stream, varname, count, has_nl):
364364
if cnt == 1:
365365
continue
366366
after_lb = token.value.split('\n', 1)[1]
367-
yield grouping.Token(T.Text, "'")
367+
yield grouping.Token(T.Text, " '")
368368
yield grouping.Token(T.Whitespace, '\n')
369369
for i in range(len(varname)+4):
370370
yield grouping.Token(T.Whitespace, ' ')
@@ -407,11 +407,11 @@ def _process(self, stream, varname):
407407
cnt = 0
408408
for token in stream:
409409
if token.is_whitespace() and '\n' in token.value:
410-
cnt += 1
411-
if cnt == 1:
412-
continue
410+
# cnt += 1
411+
# if cnt == 1:
412+
# continue
413413
after_lb = token.value.split('\n', 1)[1]
414-
yield grouping.Token(T.Text, '"')
414+
yield grouping.Token(T.Text, ' "')
415415
yield grouping.Token(T.Operator, ';')
416416
yield grouping.Token(T.Whitespace, '\n')
417417
yield grouping.Token(T.Name, varname)

sqlparse/formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def validate_options(options):
4141
options['strip_whitespace'] = True
4242
indent_tabs = options.get('indent_tabs', False)
4343
if indent_tabs not in [True, False]:
44-
raise SQLParserError('Invalid value for indent_tabs: %r' % indent_tabs)
44+
raise SQLParseError('Invalid value for indent_tabs: %r' % indent_tabs)
4545
elif indent_tabs:
4646
options['indent_char'] = '\t'
4747
else:

sqlparse/sql.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ def token_next_by_instance(self, idx, clss):
170170
171171
If no matching token can be found ``None`` is returned.
172172
"""
173-
if type(clss) not in (types.ListType, types.TupleType):
173+
if isinstance(clss, (list, tuple)):
174174
clss = (clss,)
175-
if type(clss) is not types.TupleType:
175+
if isinstance(clss, tuple):
176176
clss = tuple(clss)
177177
for token in self.tokens[idx:]:
178178
if isinstance(token, clss):
@@ -181,7 +181,7 @@ def token_next_by_instance(self, idx, clss):
181181

182182
def token_next_by_type(self, idx, ttypes):
183183
"""Returns next matching token by it's token type."""
184-
if not isinstance(ttypes, (types.TupleType, types.ListType)):
184+
if not isinstance(ttypes, (list, tuple)):
185185
ttypes = [ttypes]
186186
for token in self.tokens[idx:]:
187187
if token.ttype in ttypes:

tests/test_format.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def test_strip_comments_single(self):
4242
sql = 'select-- foo\nfrom -- bar\nwhere'
4343
res = sqlparse.format(sql, strip_comments=True)
4444
self.ndiffAssertEqual(res, 'select from where')
45+
self.assertRaises(sqlparse.SQLParseError, sqlparse.format, sql,
46+
strip_comments=None)
4547

4648
def test_strip_comments_multi(self):
4749
sql = '/* sql starts here */\nselect'
@@ -63,10 +65,27 @@ def test_strip_ws(self):
6365
self.ndiffAssertEqual(f(s), 'select * from foo where (1 = 2)')
6466
s = 'select -- foo\nfrom bar\n'
6567
self.ndiffAssertEqual(f(s), 'select -- foo\nfrom bar')
68+
self.assertRaises(sqlparse.SQLParseError, sqlparse.format, s,
69+
strip_whitespace=None)
70+
71+
def test_outputformat(self):
72+
sql = 'select * from foo;'
73+
self.assertRaises(sqlparse.SQLParseError, sqlparse.format, sql,
74+
output_format='foo')
6675

6776

6877
class TestFormatReindent(TestCaseBase):
6978

79+
def test_option(self):
80+
self.assertRaises(sqlparse.SQLParseError, sqlparse.format, 'foo',
81+
reindent=2)
82+
self.assertRaises(sqlparse.SQLParseError, sqlparse.format, 'foo',
83+
indent_tabs=2)
84+
self.assertRaises(sqlparse.SQLParseError, sqlparse.format, 'foo',
85+
reindent=True, indent_width='foo')
86+
self.assertRaises(sqlparse.SQLParseError, sqlparse.format, 'foo',
87+
reindent=True, indent_width=-12)
88+
7089
def test_stmts(self):
7190
f = lambda sql: sqlparse.format(sql, reindent=True)
7291
s = 'select foo; select bar'
@@ -176,3 +195,29 @@ def test_duplicate_linebreaks(self): # issue3
176195
'order by c1']))
177196

178197

198+
199+
200+
class TestOutputFormat(TestCaseBase):
201+
202+
def test_python(self):
203+
sql = 'select * from foo;'
204+
f = lambda sql: sqlparse.format(sql, output_format='python')
205+
self.ndiffAssertEqual(f(sql), "sql = 'select * from foo;'")
206+
f = lambda sql: sqlparse.format(sql, output_format='python',
207+
reindent=True)
208+
self.ndiffAssertEqual(f(sql), ("sql = ('select * '\n"
209+
" 'from foo;')"))
210+
211+
def test_php(self):
212+
sql = 'select * from foo;'
213+
f = lambda sql: sqlparse.format(sql, output_format='php')
214+
self.ndiffAssertEqual(f(sql), '$sql = "select * from foo;";')
215+
f = lambda sql: sqlparse.format(sql, output_format='php',
216+
reindent=True)
217+
self.ndiffAssertEqual(f(sql), ('$sql = "select * ";\n'
218+
'$sql .= "from foo;";'))
219+
220+
def test_sql(self): # "sql" is an allowed option but has no effect
221+
sql = 'select * from foo;'
222+
f = lambda sql: sqlparse.format(sql, output_format='sql')
223+
self.ndiffAssertEqual(f(sql), 'select * from foo;')

tests/test_tokenize.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ def test_flatten(self):
6666

6767
class TestTokenList(unittest.TestCase):
6868

69+
def test_repr(self):
70+
p = sqlparse.parse('foo, bar, baz')[0]
71+
tst = "<IdentifierList 'foo, b...' at 0x"
72+
self.assertEqual(repr(p.tokens[0])[:len(tst)], tst)
73+
6974
def test_token_first(self):
7075
p = sqlparse.parse(' select foo')[0]
7176
first = p.token_first()

0 commit comments

Comments
 (0)