forked from Yelp/sqlparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
84 lines (70 loc) · 2.42 KB
/
__init__.py
File metadata and controls
84 lines (70 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright (C) 2008 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php.
"""filter"""
from sqlparse import lexer
from sqlparse.engine import grouping
from sqlparse.engine.filter import StatementFilter
# XXX remove this when cleanup is complete
Filter = object
class FilterStack(object):
default_grouping_funcs = [
grouping.group_comments,
grouping.group_brackets,
grouping.group_functions,
grouping.group_where,
grouping.group_case,
grouping.group_identifier,
grouping.group_order,
grouping.group_typecasts,
grouping.group_as,
grouping.group_aliased,
grouping.group_assignment,
grouping.group_comparison,
grouping.align_comments,
grouping.group_identifier_list,
grouping.group_if,
grouping.group_for,
grouping.group_foreach,
grouping.group_begin
]
def __init__(
self,
stmtprocess=None,
postprocess=None,
grouping_funcs=None
):
self.stmtprocess = stmtprocess or []
self.postprocess = postprocess or []
self.grouping_funcs = grouping_funcs or self.default_grouping_funcs
self._grouping = False
def _flatten(self, stream):
for token in stream:
if token.is_group():
for t in self._flatten(token.tokens):
yield t
else:
yield token
def enable_grouping(self):
self._grouping = True
def run(self, statement):
statement = self._group_token(statement)
statement = self._process_statement(statement)
statement = self._post_process_statement(statement)
return statement
def _group_token(self, statement):
if self._grouping:
grouping.group(statement, self.grouping_funcs)
return statement
def _process_statement(self, statement):
if self.stmtprocess:
for filter_ in self.stmtprocess:
filter_.process(self, statement)
return statement
def _post_process_statement(self, statement):
if self.postprocess:
statement.tokens = list(self._flatten(statement.tokens))
for filter_ in self.postprocess:
statement = filter_.process(self, statement)
return statement