Uses tokenized query returned by python-sqlparse and generates query metadata
Project description
sql-metadata
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:
- MySQL
- PostgreSQL
- Apache Hive
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.pyfile 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.pyfile for more examples of a bit more complex queries.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sql_metadata-1.8.0.tar.gz.
File metadata
- Download URL: sql_metadata-1.8.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46fa5270d30c79c8c5ee6f870173a65966c702ff83d239605bd49279b8d2dac1
|
|
| MD5 |
dae00873449a371a94c28c12c7881232
|
|
| BLAKE2b-256 |
5499e6cc9ebb3336c0a441b140cae9b23fbae6122e043c6fe371739f81dc737f
|
File details
Details for the file sql_metadata-1.8.0-py3-none-any.whl.
File metadata
- Download URL: sql_metadata-1.8.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6479766e86f1ca0a4dacb636bce52a52055994e5d2fa5d67355f9d5c73582688
|
|
| MD5 |
2cad051cd1ecaaad28a554c7e6e58346
|
|
| BLAKE2b-256 |
1425aba135b2362852e649f0af389d10f06bba59dce8fb9d4495b5b13f772f0f
|