You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filter_stack.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2018 the sqlparse authors and contributors
  4. # <see AUTHORS file>
  5. #
  6. # This module is part of python-sqlparse and is released under
  7. # the BSD License: https://opensource.org/licenses/BSD-3-Clause
  8. """filter"""
  9. from sqlparse import lexer
  10. from sqlparse.engine import grouping
  11. from sqlparse.engine.statement_splitter import StatementSplitter
  12. class FilterStack(object):
  13. def __init__(self):
  14. self.preprocess = []
  15. self.stmtprocess = []
  16. self.postprocess = []
  17. self._grouping = False
  18. def enable_grouping(self):
  19. self._grouping = True
  20. def run(self, sql, encoding=None):
  21. stream = lexer.tokenize(sql, encoding)
  22. # Process token stream
  23. for filter_ in self.preprocess:
  24. stream = filter_.process(stream)
  25. stream = StatementSplitter().process(stream)
  26. # Output: Stream processed Statements
  27. for stmt in stream:
  28. if self._grouping:
  29. stmt = grouping.group(stmt)
  30. for filter_ in self.stmtprocess:
  31. filter_.process(stmt)
  32. for filter_ in self.postprocess:
  33. stmt = filter_.process(stmt)
  34. yield stmt