sqlparse is a non-validating SQL parser for Python. It provides support for parsing, splitting and formatting SQL statements.
The module is compatible with Python 3.8+ and released under the terms of the New BSD license.
Visit the project page at https://github.com/andialbrecht/sqlparse for further information about this project.
$ pip install sqlparse>>> import sqlparse
>>> # Split a string containing two SQL statements:
>>> raw = 'select * from foo; select * from bar;'
>>> statements = sqlparse.split(raw)
>>> statements
['select * from foo;', 'select * from bar;']
>>> # Format the first statement and print it out:
>>> first = statements[0]
>>> print(sqlparse.format(first, reindent=True, keyword_case='upper'))
SELECT *
FROM foo;
>>> # Parsing a SQL statement:
>>> parsed = sqlparse.parse('select * from foo')[0]
>>> parsed.tokens
[<DML 'select' at 0x7f22c5e15368>, <Whitespace ' ' at 0x7f22c5e153b0>, <Wildcard '*' … ]
>>>sqlparse can be used as a pre-commit hook to automatically format SQL files before committing:
repos:
- repo: https://github.com/andialbrecht/sqlparse
rev: 0.5.4 # Use the latest version
hooks:
- id: sqlformat
# Optional: Add more formatting options
# IMPORTANT: --in-place is required, already included by default
args: [--in-place, --reindent, --keywords, upper]Then install the hook:
$ pre-commit installYour SQL files will now be automatically formatted on each commit.
Note: The hook uses --in-place --reindent by default. If you override
the args, you must include --in-place for the hook to work.
- Project page
- https://github.com/andialbrecht/sqlparse
- Bug tracker
- https://github.com/andialbrecht/sqlparse/issues
- Documentation
- https://sqlparse.readthedocs.io/
- Online Demo
- https://sqlformat.org/
sqlparse is licensed under the BSD license.
Parts of the code are based on pygments written by Georg Brandl and others. pygments-Homepage: http://pygments.org/