Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/sqlparse/__init__.py: 73%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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 typing import Any, Generator, IO, List, Optional, Tuple, Union
13from sqlparse import sql
14from sqlparse import cli
15from sqlparse import engine
16from sqlparse import tokens
17from sqlparse import filters
18from sqlparse import formatter
21__version__ = "0.5.5.dev0"
22__all__ = ["engine", "filters", "formatter", "sql", "tokens", "cli"]
25def parse(
26 sql: str, encoding: Optional[str] = None
27) -> Tuple[sql.Statement, ...]:
28 """Parse sql and return a list of statements.
30 :param sql: A string containing one or more SQL statements.
31 :param encoding: The encoding of the statement (optional).
32 :returns: A tuple of :class:`~sqlparse.sql.Statement` instances.
33 """
34 return tuple(parsestream(sql, encoding))
37def parsestream(
38 stream: Union[str, IO[str]], encoding: Optional[str] = None
39) -> Generator[sql.Statement, None, None]:
40 """Parses sql statements from file-like object.
42 :param stream: A file-like object.
43 :param encoding: The encoding of the stream contents (optional).
44 :returns: A generator of :class:`~sqlparse.sql.Statement` instances.
45 """
46 stack = engine.FilterStack()
47 stack.enable_grouping()
48 return stack.run(stream, encoding)
51def format(sql: str, encoding: Optional[str] = None, **options: Any) -> str:
52 """Format *sql* according to *options*.
54 Available options are documented in :ref:`formatting`.
56 In addition to the formatting options this function accepts the
57 keyword "encoding" which determines the encoding of the statement.
59 :returns: The formatted SQL statement as string.
60 """
61 stack = engine.FilterStack()
62 options = formatter.validate_options(options)
63 stack = formatter.build_filter_stack(stack, options)
64 stack.postprocess.append(filters.SerializerUnicode())
65 return "".join(stack.run(sql, encoding))
68def split(
69 sql: str, encoding: Optional[str] = None, strip_semicolon: bool = False
70) -> List[str]:
71 """Split *sql* into single statements.
73 :param sql: A string containing one or more SQL statements.
74 :param encoding: The encoding of the statement (optional).
75 :param strip_semicolon: If True, remove trailing semicolons
76 (default: False).
77 :returns: A list of strings.
78 """
79 stack = engine.FilterStack(strip_semicolon=strip_semicolon)
80 return [str(stmt).strip() for stmt in stack.run(sql, encoding)]