Convert SQL to most human readable format
Project description
readsql
Convert SQL to most human readable format. For the time being it upper cases SQL keywords, it might prettify or even suggest improvements of SQL code in the future. It converts SQL code and even SQL-strings in programming languages (only Python at the moment).
So if we write
select sushi, avg(price) from tokyo where ocean = 'pacific' group by sushi
readsql will help us read it as
SELECT sushi, AVG(price) FROM tokyo WHERE ocean = 'pacific' GROUP BY sushi
Installation
pip install readsql
Requires Python 3.8+.
Usage
- Format SQL code provided in command line
readsql <SQL_STRING> -s
- Format an SQL file or folder
- as in a folder, it will look for files ending with .sql or .py
readsql <FILE_OR_FOLDER_PATH>
It supports multiple strings and files or folders as well
readsql <SQL_STRING1> <SQL_STRING2> -s
- In Python files it looks for variable
querystrings and formats them
readsql <FILE_OR_FOLDER_PATH1> <FILE_OR_FOLDER_PATH2>
We can look for different strings in Python files with -py arguments
readsql <FILE_OR_FOLDER_PATH> -py <PY_VAR1> <PY_VAR2>
Note:
readsqluses Python's AST parser, so it supports:
- Type Hints:
query: str = "..."- F-strings:
f"SELECT * FROM {table}"- Nested Scopes: Robustly handles variables inside functions and classes.
- Comments/Docstrings: Correctly ignores SQL-like text in comments or docstrings.
Usage examples
-
readsql 'select sushi from tokyo' -scommand returnsSELECT sushi FROM tokyo
-
a.
readsql sql_example.sqlcommand, whilesql_example.sqlis a SQL file with code as below,
select max(height), avg(mass), min(age) from jungle group by forest where animal=elephant;
replaces the file with this code
SELECT MAX(height), AVG(mass), MIN(age) FROM jungle GROUP BY forest WHERE animal=elephant;
- b.
readsql sql_in_python_variable_example.pycommand, whilesql_in_python_variable_example.pyis a Python file with code as below,
def get_query():
limit = 6
query = f"SELEct speed from world where animal='dolphin' limit {limit}"
return query
replaces the file with this code
def get_query():
limit = 6
query = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
return query
- c.
readsql sql_in_python_variable_example.py -py sqlcommand, whilesql_in_python_variable_example.pyis a Python file with code as below,
def get_query():
limit = 6
sql = f"SELEct speed from world where animal='dolphin' limit {limit}"
return sql
replaces the file with this code
def get_query():
limit = 6
sql = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
return sql
- d.
readsql tests -ncommand outputs all of the formated SQL code intestsfolder, files are not replaced by the formatted version (-nargument stand for not-replace)
More examples can be found in /tests folder
Add a pre-commit hook
How to add a pre-commit hook of readsql?
repos:
- repo: https://github.com/AzisK/readsql
rev: v1.0.0 # Replace by any tag/version: https://github.com/azisk/readsql/tags
hooks:
- id: readsql
Development
Clone the repo and use uv for development:
uv sync --all-extras # Install dependencies
uv run readsql "select sushi from tokyo" -s # Format a string
uv run readsql tests/sql_example.sql # Format SQL file
uv run readsql tests/sql_in_python_example.py # Format SQL in Python
uv run readsql tests/sql_in_python_variable_example.py -py sql # Custom variable
uv run readsql tests # Format all files in folder
Testing
uv run pytest -v
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
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 readsql-1.1.0.tar.gz.
File metadata
- Download URL: readsql-1.1.0.tar.gz
- Upload date:
- Size: 37.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
622fcbd019d7691e2854021e980b3304a19cf5c97074916d226801d0a7bc1391
|
|
| MD5 |
4de33519d59ed423a900c798aba0842c
|
|
| BLAKE2b-256 |
a7d8aceb4ae6eba3c9535531d147dbb7e39c86b7cdc670abb56adcec3ec16c12
|
Provenance
The following attestation bundles were made for readsql-1.1.0.tar.gz:
Publisher:
publish-pypi.yml on AzisK/readsql
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
readsql-1.1.0.tar.gz -
Subject digest:
622fcbd019d7691e2854021e980b3304a19cf5c97074916d226801d0a7bc1391 - Sigstore transparency entry: 844055311
- Sigstore integration time:
-
Permalink:
AzisK/readsql@c7ba41826b90121f9657b3003a8fa6184b40571a -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/AzisK
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@c7ba41826b90121f9657b3003a8fa6184b40571a -
Trigger Event:
push
-
Statement type:
File details
Details for the file readsql-1.1.0-py3-none-any.whl.
File metadata
- Download URL: readsql-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbf755ceb91bc53d49e1dac98b86e0efd03a2dad676622afd25359f799569443
|
|
| MD5 |
cbbb5a87a14b5cf6a9ac71d67389fcef
|
|
| BLAKE2b-256 |
3e210e01fd72782b3cc36d939666f32fc2f1ec796104072bb74148fbeb4958ab
|
Provenance
The following attestation bundles were made for readsql-1.1.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on AzisK/readsql
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
readsql-1.1.0-py3-none-any.whl -
Subject digest:
cbf755ceb91bc53d49e1dac98b86e0efd03a2dad676622afd25359f799569443 - Sigstore transparency entry: 844055327
- Sigstore integration time:
-
Permalink:
AzisK/readsql@c7ba41826b90121f9657b3003a8fa6184b40571a -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/AzisK
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@c7ba41826b90121f9657b3003a8fa6184b40571a -
Trigger Event:
push
-
Statement type: