METADATA 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. Metadata-Version: 2.5
  2. Name: sqlparse
  3. Version: 0.6.0
  4. Summary: A non-validating SQL parser.
  5. Project-URL: Home, https://github.com/andialbrecht/sqlparse
  6. Project-URL: Documentation, https://sqlparse.readthedocs.io/
  7. Project-URL: Release Notes, https://sqlparse.readthedocs.io/en/latest/changes.html
  8. Project-URL: Source, https://github.com/andialbrecht/sqlparse
  9. Project-URL: Tracker, https://github.com/andialbrecht/sqlparse/issues
  10. Author-email: Andi Albrecht <albrecht.andi@gmail.com>
  11. License-File: AUTHORS
  12. License-File: LICENSE
  13. Classifier: Development Status :: 5 - Production/Stable
  14. Classifier: Intended Audience :: Developers
  15. Classifier: License :: OSI Approved :: BSD License
  16. Classifier: Operating System :: OS Independent
  17. Classifier: Programming Language :: Python
  18. Classifier: Programming Language :: Python :: 3
  19. Classifier: Programming Language :: Python :: 3 :: Only
  20. Classifier: Programming Language :: Python :: 3.10
  21. Classifier: Programming Language :: Python :: 3.11
  22. Classifier: Programming Language :: Python :: 3.12
  23. Classifier: Programming Language :: Python :: 3.13
  24. Classifier: Programming Language :: Python :: 3.14
  25. Classifier: Programming Language :: Python :: Implementation :: CPython
  26. Classifier: Programming Language :: Python :: Implementation :: PyPy
  27. Classifier: Topic :: Database
  28. Classifier: Topic :: Software Development
  29. Requires-Python: >=3.10
  30. Provides-Extra: dev
  31. Requires-Dist: build; extra == 'dev'
  32. Provides-Extra: doc
  33. Requires-Dist: furo; extra == 'doc'
  34. Requires-Dist: sphinx; extra == 'doc'
  35. Description-Content-Type: text/markdown
  36. # sqlparse
  37. [![Build Status](https://github.com/andialbrecht/sqlparse/actions/workflows/python-app.yml/badge.svg)](https://github.com/andialbrecht/sqlparse/actions/workflows/python-app.yml)
  38. [![Coverage](https://codecov.io/gh/andialbrecht/sqlparse/branch/master/graph/badge.svg)](https://codecov.io/gh/andialbrecht/sqlparse)
  39. [![Documentation](https://readthedocs.org/projects/sqlparse/badge/?version=latest)](https://sqlparse.readthedocs.io/en/latest/)
  40. [![PyPI](https://img.shields.io/pypi/v/sqlparse?color=%2334D058&label=pypi%20package)](https://pypi.org/project/sqlparse)
  41. [![Python versions](https://img.shields.io/pypi/pyversions/sqlparse)](https://pypi.org/project/sqlparse)
  42. [![License](https://img.shields.io/badge/license-BSD--3--Clause-blue)](https://opensource.org/licenses/BSD-3-Clause)
  43. A non-validating SQL parser for Python. Split scripts into statements, format
  44. them, and walk their token tree.
  45. sqlparse tokenizes SQL text and groups the parts it recognizes into a tree of
  46. statements, clauses, identifiers and expressions. It accepts any input without
  47. validating it, and makes no assumptions about a particular SQL dialect, so
  48. vendor extensions and templated SQL parse too. It is a building block for
  49. formatters, linters, editors and query analysis tools.
  50. Licensed under the
  51. [New BSD license](https://opensource.org/licenses/BSD-3-Clause).
  52. ## Install
  53. ```bash
  54. pip install sqlparse
  55. ```
  56. ## Usage
  57. Three module-level functions cover most needs: `split()`, `format()` and
  58. `parse()`.
  59. ### Split a script into statements
  60. ```python
  61. import sqlparse
  62. sqlparse.split("select * from foo; select * from bar;")
  63. # ['select * from foo;', 'select * from bar;']
  64. ```
  65. ### Format a statement
  66. ```python
  67. print(sqlparse.format("select id,name from users where active=1",
  68. reindent=True, keyword_case="upper"))
  69. ```
  70. ```sql
  71. SELECT id,
  72. name
  73. FROM users
  74. WHERE active=1
  75. ```
  76. `format()` accepts the same options as the command line, among them
  77. `reindent`, `keyword_case`, `strip_comments` and `indent_width`.
  78. ### Parse and inspect a statement
  79. ```python
  80. statement = sqlparse.parse("select id, name from users where active = 1")[0]
  81. print(statement.get_type())
  82. for token in statement.tokens:
  83. if not token.is_whitespace:
  84. print(f"{token.ttype or type(token).__name__!s:20} {token!s}")
  85. ```
  86. ```
  87. SELECT
  88. Token.Keyword.DML select
  89. IdentifierList id, name
  90. Token.Keyword from
  91. Identifier users
  92. Where where active = 1
  93. ```
  94. Tokens with a `ttype` are leaves. The others are groups you can descend into,
  95. which is how nested constructs like subqueries and `CASE` expressions are
  96. represented. See
  97. [Analyzing SQL statements](https://sqlparse.readthedocs.io/en/latest/analyzing.html)
  98. for the traversal API.
  99. ## Command line
  100. Installing sqlparse also provides the `sqlformat` command. It reads from stdin
  101. when the filename is `-`, writes to stdout by default, and rewrites files in
  102. place with `--in-place`:
  103. ```bash
  104. sqlformat --reindent --keywords upper query.sql
  105. ```
  106. Run `sqlformat --help` for all formatting options.
  107. ## Pre-commit hook
  108. Format SQL files on commit with [pre-commit](https://pre-commit.com/):
  109. ```yaml
  110. repos:
  111. - repo: https://github.com/andialbrecht/sqlparse
  112. rev: 0.5.5 # use the latest release
  113. hooks:
  114. - id: sqlformat
  115. args: [--in-place, --reindent, --keywords, upper]
  116. ```
  117. The hook defaults to `--in-place --reindent`. When overriding `args`, keep
  118. `--in-place`, or the hook writes to stdout and leaves your files unchanged.
  119. ## Links
  120. | | |
  121. |---|---|
  122. | Documentation | <https://sqlparse.readthedocs.io/> |
  123. | Release notes | <https://sqlparse.readthedocs.io/en/latest/changes.html> |
  124. | Issues | <https://github.com/andialbrecht/sqlparse/issues> |
  125. | Discussions | <https://github.com/andialbrecht/sqlparse/discussions> |
  126. | Online demo | <https://sqlformat.org/> |
  127. ## Contributing
  128. Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) to get
  129. started.
  130. sqlparse is maintained in spare time, so it can take a while before an issue or
  131. pull request gets a reply. Thanks for your patience. A pull request that comes
  132. with tests for the behavior it changes is usually the quickest to review.
  133. For security issues, please follow [SECURITY.md](SECURITY.md) rather than
  134. opening a public issue.
  135. ## License
  136. sqlparse is licensed under the New BSD license; see [LICENSE](LICENSE) for the
  137. full text. Parts of the code are based on [Pygments](https://pygments.org/),
  138. written by Georg Brandl and others.