diff options
| author | Rob Hudson | 2009-09-22 19:14:31 -0700 |
|---|---|---|
| committer | Rob Hudson | 2009-09-22 19:20:50 -0700 |
| commit | 6c05fad6a9835ae22481b90d6b58f00f90929663 (patch) | |
| tree | 4034b0eb6cfb628782e3106126e621c6c2ab780d /debug_toolbar/utils/sqlparse/__init__.py | |
| parent | 39174123f6d7371664b448604d421a66bed74cb0 (diff) | |
| download | django-debug-toolbar-6c05fad6a9835ae22481b90d6b58f00f90929663.tar.bz2 | |
Added sqlparse, replacing my simple string replace SQL keywords and updating
management command and SQL panel.
Diffstat (limited to 'debug_toolbar/utils/sqlparse/__init__.py')
| -rw-r--r-- | debug_toolbar/utils/sqlparse/__init__.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/debug_toolbar/utils/sqlparse/__init__.py b/debug_toolbar/utils/sqlparse/__init__.py new file mode 100644 index 0000000..69873ca --- /dev/null +++ b/debug_toolbar/utils/sqlparse/__init__.py @@ -0,0 +1,59 @@ +# Copyright (C) 2008 Andi Albrecht, albrecht.andi@gmail.com +# +# This module is part of python-sqlparse and is released under +# the BSD License: http://www.opensource.org/licenses/bsd-license.php. + +"""Parse SQL statements.""" + + +__version__ = '0.1.1' + + +import os + + +class SQLParseError(Exception): + """Base class for exceptions in this module.""" + + +# Setup namespace +from debug_toolbar.utils.sqlparse import engine +from debug_toolbar.utils.sqlparse import filters +from debug_toolbar.utils.sqlparse import formatter + + +def parse(sql): + """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. + """ + stack = engine.FilterStack() + stack.full_analyze() + return tuple(stack.run(sql)) + + +def format(sql, **options): + """Format *sql* according to *options*. + + Available options are documented in :ref:`formatting`. + + Returns the formatted SQL statement as string. + """ + 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)) + + +def split(sql): + """Split *sql* into single statements. + + Returns a list of strings. + """ + stack = engine.FilterStack() + stack.split_statements = True + return [unicode(stmt) for stmt in stack.run(sql)] + |
