|
18 | 18 | from sqlparse.exceptions import SQLParseError |
19 | 19 |
|
20 | 20 |
|
21 | | -def parse(sql): |
| 21 | +def parse(sql, encoding=None): |
22 | 22 | """Parse sql and return a list of statements. |
23 | 23 |
|
24 | | - *sql* is a single string containting one or more SQL statements. |
25 | | -
|
26 | | - Returns a tuple of :class:`~sqlparse.sql.Statement` instances. |
| 24 | + :param sql: A string containting one or more SQL statements. |
| 25 | + :param encoding: The encoding of the statement (optional). |
| 26 | + :returns: A tuple of :class:`~sqlparse.sql.Statement` instances. |
27 | 27 | """ |
28 | | - return tuple(parsestream(sql)) |
| 28 | + return tuple(parsestream(sql, encoding)) |
29 | 29 |
|
30 | 30 |
|
31 | | -def parsestream(stream): |
| 31 | +def parsestream(stream, encoding=None): |
32 | 32 | """Parses sql statements from file-like object. |
33 | 33 |
|
34 | | - Returns a generator of Statement instances. |
| 34 | + :param stream: A file-like object. |
| 35 | + :param encoding: The encoding of the stream contents (optional). |
| 36 | + :returns: A generator of :class:`~sqlparse.sql.Statement` instances. |
35 | 37 | """ |
36 | 38 | stack = engine.FilterStack() |
37 | 39 | stack.full_analyze() |
38 | | - return stack.run(stream) |
| 40 | + return stack.run(stream, encoding) |
39 | 41 |
|
40 | 42 |
|
41 | 43 | def format(sql, **options): |
42 | 44 | """Format *sql* according to *options*. |
43 | 45 |
|
44 | 46 | Available options are documented in :ref:`formatting`. |
45 | 47 |
|
46 | | - Returns the formatted SQL statement as string. |
| 48 | + In addition to the formatting options this function accepts the |
| 49 | + keyword "encoding" which determines the encoding of the statement. |
| 50 | +
|
| 51 | + :returns: The formatted SQL statement as string. |
47 | 52 | """ |
| 53 | + encoding = options.pop('encoding', None) |
48 | 54 | stack = engine.FilterStack() |
49 | 55 | options = formatter.validate_options(options) |
50 | 56 | stack = formatter.build_filter_stack(stack, options) |
51 | 57 | stack.postprocess.append(filters.SerializerUnicode()) |
52 | | - return ''.join(stack.run(sql)) |
| 58 | + return ''.join(stack.run(sql, encoding)) |
53 | 59 |
|
54 | 60 |
|
55 | | -def split(sql): |
| 61 | +def split(sql, encoding=None): |
56 | 62 | """Split *sql* into single statements. |
57 | 63 |
|
58 | | - Returns a list of strings. |
| 64 | + :param sql: A string containting one or more SQL statements. |
| 65 | + :param encoding: The encoding of the statement (optional). |
| 66 | + :returns: A list of strings. |
59 | 67 | """ |
60 | 68 | stack = engine.FilterStack() |
61 | 69 | stack.split_statements = True |
62 | | - return [unicode(stmt) for stmt in stack.run(sql)] |
| 70 | + return [unicode(stmt) for stmt in stack.run(sql, encoding)] |
63 | 71 |
|
64 | 72 |
|
65 | 73 | from sqlparse.engine.filter import StatementFilter |
|
0 commit comments