Skip to main content

Uses tokenized query returned by python-sqlparse and generates query metadata

Project description

sql-metadata

PyPI Code style: black Maintenance Downloads

Uses tokenized query returned by python-sqlparse and generates query metadata. Extracts column names and tables used by the query. Provides a helper for normalization of SQL queries and tables aliases resolving.

Supported queries syntax:

Usage

pip install sql-metadata
>>> import sql_metadata

>>> sql_metadata.get_query_tokens("SELECT * FROM foo")
[<DML 'SELECT' at 0x7F14FFDEB808>, <Wildcard '*' at 0x7F14FFDEB940>, <Keyword 'FROM' at 0x7F14FFDEBBB0>, <Name 'foo' at 0x7F14FFDEB9A8>]

>>> sql_metadata.get_query_columns("SELECT test, id FROM foo, bar")
[u'test', u'id']

>>> sql_metadata.get_query_tables("SELECT a.* FROM product_a.users AS a JOIN product_b.users AS b ON a.ip_address = b.ip_address")
['product_a.users', 'product_b.users']

>>> sql_metadata.get_query_columns("INSERT /* VoteHelper::addVote xxx */  INTO `page_vote` (article_id,user_id,`time`) VALUES ('442001','27574631','20180228130846')")
['article_id', 'user_id', 'time']

>>> sql_metadata.get_query_columns("SELECT a.* FROM product_a.users AS a JOIN product_b.users AS b ON a.ip_address = b.ip_address")
['a.*', 'a.ip_address', 'b.ip_address']

>>> sql_metadata.get_query_tables("SELECT test, id FROM foo, bar")
[u'foo', u'bar']

>>> sql_metadata.get_query_limit_and_offset('SELECT foo_limit FROM bar_offset LIMIT 50 OFFSET 1000')
(50, 1000)

>>> sql_metadata.get_query_limit_and_offset('SELECT foo_limit FROM bar_offset limit 2000,50')
(50, 2000)

>>> sql_metadata.get_query_table_aliases("SELECT test FROM foo AS f")
{'f': 'foo'}

See test/test_query.py file for more examples of a bit more complex queries.

Queries normalization

>>> from sql_metadata import generalize_sql
>>> generalize_sql('SELECT /* Test */ foo FROM bar WHERE id in (1, 2, 56)')
'SELECT foo FROM bar WHERE id in (XYZ)'

See test/test_normalization.py file for more examples of a bit more complex queries.

Stargazers over time

Stargazers over time

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sql_metadata-1.11.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sql_metadata-1.11.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file sql_metadata-1.11.0.tar.gz.

File metadata

  • Download URL: sql_metadata-1.11.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1039-azure

File hashes

Hashes for sql_metadata-1.11.0.tar.gz
Algorithm Hash digest
SHA256 bb38364bc071926534d202ec888ea28945ebe4cd186f1eb2dfe2b054c973da68
MD5 1e6dc9dca37fc336466e4c4f17702c69
BLAKE2b-256 274b89eafbe764efe3d9277ac74489eeb922638c14bb18361ef33e5c8a97defa

See more details on using hashes here.

File details

Details for the file sql_metadata-1.11.0-py3-none-any.whl.

File metadata

  • Download URL: sql_metadata-1.11.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1039-azure

File hashes

Hashes for sql_metadata-1.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17b5e2ad0475cf72b64214df61993585d40cfb72e44a13d799b38ebf2a1f5835
MD5 c05bdd70fc3b5f13993bea5fece49aa8
BLAKE2b-256 00a57ada01751d1895590d19b4d3c8340dae9e338e5091f0be00ca9260432266

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page