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.

__init__.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. """Parse SQL statements."""
  8. # Setup namespace
  9. from sqlparse import sql
  10. from sqlparse import cli
  11. from sqlparse import engine
  12. from sqlparse import tokens
  13. from sqlparse import filters
  14. from sqlparse import formatter
  15. from sqlparse.compat import text_type
  16. __version__ = '0.2.4'
  17. __all__ = ['engine', 'filters', 'formatter', 'sql', 'tokens', 'cli']
  18. def parse(sql, encoding=None):
  19. """Parse sql and return a list of statements.
  20. :param sql: A string containing one or more SQL statements.
  21. :param encoding: The encoding of the statement (optional).
  22. :returns: A tuple of :class:`~sqlparse.sql.Statement` instances.
  23. """
  24. return tuple(parsestream(sql, encoding))
  25. def parsestream(stream, encoding=None):
  26. """Parses sql statements from file-like object.
  27. :param stream: A file-like object.
  28. :param encoding: The encoding of the stream contents (optional).
  29. :returns: A generator of :class:`~sqlparse.sql.Statement` instances.
  30. """
  31. stack = engine.FilterStack()
  32. stack.enable_grouping()
  33. return stack.run(stream, encoding)
  34. def format(sql, encoding=None, **options):
  35. """Format *sql* according to *options*.
  36. Available options are documented in :ref:`formatting`.
  37. In addition to the formatting options this function accepts the
  38. keyword "encoding" which determines the encoding of the statement.
  39. :returns: The formatted SQL statement as string.
  40. """
  41. stack = engine.FilterStack()
  42. options = formatter.validate_options(options)
  43. stack = formatter.build_filter_stack(stack, options)
  44. stack.postprocess.append(filters.SerializerUnicode())
  45. return u''.join(stack.run(sql, encoding))
  46. def split(sql, encoding=None):
  47. """Split *sql* into single statements.
  48. :param sql: A string containing one or more SQL statements.
  49. :param encoding: The encoding of the statement (optional).
  50. :returns: A list of strings.
  51. """
  52. stack = engine.FilterStack()
  53. return [text_type(stmt).strip() for stmt in stack.run(sql, encoding)]