Development of an internal social media platform with personalised dashboards for students
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

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