Skip to content

Commit 3fed039

Browse files
committed
Refactor filter-stack to simplify logic
if (self.stmtprocess or self.postprocess or self.split_statements or self._grouping): always evaluates to true after removing unused features
1 parent 5ce2255 commit 3fed039

3 files changed

Lines changed: 15 additions & 39 deletions

File tree

sqlparse/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,4 @@ def split(sql, encoding=None):
6666
:returns: A list of strings.
6767
"""
6868
stack = engine.FilterStack()
69-
stack.split_statements = True
7069
return [u(stmt).strip() for stmt in stack.run(sql, encoding)]

sqlparse/engine/__init__.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313

1414

1515
class FilterStack(object):
16-
1716
def __init__(self):
1817
self.preprocess = []
1918
self.stmtprocess = []
2019
self.postprocess = []
21-
self.split_statements = False
2220
self._grouping = False
2321

2422
def enable_grouping(self):
@@ -27,42 +25,20 @@ def enable_grouping(self):
2725
def run(self, sql, encoding=None):
2826
stream = lexer.tokenize(sql, encoding)
2927
# Process token stream
30-
if self.preprocess:
31-
for filter_ in self.preprocess:
32-
stream = filter_.process(stream)
33-
34-
if (self.stmtprocess or self.postprocess or
35-
self.split_statements or self._grouping):
36-
splitter = StatementFilter()
37-
stream = splitter.process(stream)
38-
39-
if self._grouping:
40-
41-
def _group(stream):
42-
for stmt in stream:
43-
grouping.group(stmt)
44-
yield stmt
45-
stream = _group(stream)
28+
for filter_ in self.preprocess:
29+
stream = filter_.process(stream)
4630

47-
if self.stmtprocess:
31+
stream = StatementFilter().process(stream)
4832

49-
def _run1(stream):
50-
ret = []
51-
for stmt in stream:
52-
for filter_ in self.stmtprocess:
53-
filter_.process(stmt)
54-
ret.append(stmt)
55-
return ret
56-
stream = _run1(stream)
33+
# Output: Stream processed Statements
34+
for stmt in stream:
35+
if self._grouping:
36+
stmt = grouping.group(stmt)
5737

58-
if self.postprocess:
38+
for filter_ in self.stmtprocess:
39+
filter_.process(stmt)
5940

60-
def _run2(stream):
61-
for stmt in stream:
62-
stmt.tokens = list(stmt.flatten())
63-
for filter_ in self.postprocess:
64-
stmt = filter_.process(stmt)
65-
yield stmt
66-
stream = _run2(stream)
41+
for filter_ in self.postprocess:
42+
stmt = filter_.process(stmt)
6743

68-
return stream
44+
yield stmt

sqlparse/engine/grouping.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def align_comments(tlist):
266266
token = tlist.token_next_by(i=sql.Comment, idx=token)
267267

268268

269-
def group(tlist):
269+
def group(stmt):
270270
for func in [
271271
group_comments,
272272
group_brackets,
@@ -291,4 +291,5 @@ def group(tlist):
291291
group_foreach,
292292
group_begin,
293293
]:
294-
func(tlist)
294+
func(stmt)
295+
return stmt

0 commit comments

Comments
 (0)