Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/sqlparse/__init__.py: 50%
24 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1#
2# Copyright (C) 2009-2020 the sqlparse authors and contributors
3# <see AUTHORS file>
4#
5# This module is part of python-sqlparse and is released under
6# the BSD License: https://opensource.org/licenses/BSD-3-Clause
8"""Parse SQL statements."""
10# Setup namespace
11from sqlparse import sql
12from sqlparse import cli
13from sqlparse import engine
14from sqlparse import tokens
15from sqlparse import filters
16from sqlparse import formatter
19__version__ = '0.4.3'
20__all__ = ['engine', 'filters', 'formatter', 'sql', 'tokens', 'cli']
23def parse(sql, encoding=None):
24 """Parse sql and return a list of statements.
26 :param sql: A string containing one or more SQL statements.
27 :param encoding: The encoding of the statement (optional).
28 :returns: A tuple of :class:`~sqlparse.sql.Statement` instances.
29 """
30 return tuple(parsestream(sql, encoding))
33def parsestream(stream, encoding=None):
34 """Parses sql statements from file-like object.
36 :param stream: A file-like object.
37 :param encoding: The encoding of the stream contents (optional).
38 :returns: A generator of :class:`~sqlparse.sql.Statement` instances.
39 """
40 stack = engine.FilterStack()
41 stack.enable_grouping()
42 return stack.run(stream, encoding)
45def format(sql, encoding=None, **options):
46 """Format *sql* according to *options*.
48 Available options are documented in :ref:`formatting`.
50 In addition to the formatting options this function accepts the
51 keyword "encoding" which determines the encoding of the statement.
53 :returns: The formatted SQL statement as string.
54 """
55 stack = engine.FilterStack()
56 options = formatter.validate_options(options)
57 stack = formatter.build_filter_stack(stack, options)
58 stack.postprocess.append(filters.SerializerUnicode())
59 return ''.join(stack.run(sql, encoding))
62def split(sql, encoding=None):
63 """Split *sql* into single statements.
65 :param sql: A string containing one or more SQL statements.
66 :param encoding: The encoding of the statement (optional).
67 :returns: A list of strings.
68 """
69 stack = engine.FilterStack()
70 return [str(stmt).strip() for stmt in stack.run(sql, encoding)]