diff options
Diffstat (limited to 'sqlparse/__init__.py')
| -rw-r--r-- | sqlparse/__init__.py | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/sqlparse/__init__.py b/sqlparse/__init__.py index b338350..e4de928 100644 --- a/sqlparse/__init__.py +++ b/sqlparse/__init__.py @@ -18,24 +18,26 @@ from sqlparse import formatter from sqlparse.exceptions import SQLParseError -def parse(sql): +def parse(sql, encoding=None): """Parse sql and return a list of statements. - *sql* is a single string containting one or more SQL statements. - - Returns a tuple of :class:`~sqlparse.sql.Statement` instances. + :param sql: A string containting one or more SQL statements. + :param encoding: The encoding of the statement (optional). + :returns: A tuple of :class:`~sqlparse.sql.Statement` instances. """ - return tuple(parsestream(sql)) + return tuple(parsestream(sql, encoding)) -def parsestream(stream): +def parsestream(stream, encoding=None): """Parses sql statements from file-like object. - Returns a generator of Statement instances. + :param stream: A file-like object. + :param encoding: The encoding of the stream contents (optional). + :returns: A generator of :class:`~sqlparse.sql.Statement` instances. """ stack = engine.FilterStack() stack.full_analyze() - return stack.run(stream) + return stack.run(stream, encoding) def format(sql, **options): @@ -43,23 +45,29 @@ def format(sql, **options): Available options are documented in :ref:`formatting`. - Returns the formatted SQL statement as string. + In addition to the formatting options this function accepts the + keyword "encoding" which determines the encoding of the statement. + + :returns: The formatted SQL statement as string. """ + encoding = options.pop('encoding', None) stack = engine.FilterStack() options = formatter.validate_options(options) stack = formatter.build_filter_stack(stack, options) stack.postprocess.append(filters.SerializerUnicode()) - return ''.join(stack.run(sql)) + return ''.join(stack.run(sql, encoding)) -def split(sql): +def split(sql, encoding=None): """Split *sql* into single statements. - Returns a list of strings. + :param sql: A string containting one or more SQL statements. + :param encoding: The encoding of the statement (optional). + :returns: A list of strings. """ stack = engine.FilterStack() stack.split_statements = True - return [unicode(stmt) for stmt in stack.run(sql)] + return [unicode(stmt) for stmt in stack.run(sql, encoding)] from sqlparse.engine.filter import StatementFilter |
