Skip to main content

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 of 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

Usage

  1. Format SQL code provided in command line
    • readsql <SQL_STRING> -s
  2. 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
  1. In Python files it looks for query strings and formats them
readsql <FILE_OR_FOLDER_PATH1> <FILE_OR_FOLDER_PATH2>

We can look for different strings in Python files with a -py arguments

readsql <FILE_OR_FOLDER_PATH> -py <PY_VAR1> <PY_VAR2>

Usage examples

  1. readsql 'select sushi from tokyo' -s command returns

    • SELECT sushi FROM tokyo
  2. a. readsql sql_example.sql command, while sql_example.sql is 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;

2.c. readsql sql_in_python_variable_example.py command, while sql_in_python_variable_example.py is 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

2.c. readsql sql_in_python_variable_example.py -py sql command, while sql_in_python_variable_example.py is 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

2.d. readsql tests -n command outputs all of the formated SQL code in tests folder, files are not replaced by the formatted version (-n argument stand for not-replace)

Add a pre-commit hook

How to add a pre-commit hook of readsql?

repos:
-   repo: https://github.com/AzisK/readsql
    rev: 0.0.5-alpha # Replace by any tag/version: https://github.com/azisk/readsql/tags
    hooks:
    -   id: readsql

Development

Having the repo cloned dig into

  • python -m readsql "select sushi from tokyo" -s takes the "select sushi from tokyo" string as input and outputs it formatted
  • python -m readsql tests/sql_example.sql converts example SQL code to easier readable format
  • python -m readsql tests/sql_in_python_example.py converts example SQL code in Python (it looks for variables query)
  • we can change the SQL variable with -py option python -m readsql tests/sql_in_python_variable_example.py -py sql
  • python -m readsql tests formats all Python and SQL files in tests folder

Testing

Have pytest installed and run pytest -v (-v stands for verbose)

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

readsql-0.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

readsql-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file readsql-0.1.1.tar.gz.

File metadata

  • Download URL: readsql-0.1.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Darwin/19.6.0

File hashes

Hashes for readsql-0.1.1.tar.gz
Algorithm Hash digest
SHA256 114e93e2479e95a692843c95f2da1d8d46d6464fa45896792c32c9a407378a7c
MD5 9df7ed263bc3ab46229651d05f7dc403
BLAKE2b-256 1ad4c7218974e85fb42145552327c5cc1969f9d0fae1736078408bc0c234722a

See more details on using hashes here.

File details

Details for the file readsql-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: readsql-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Darwin/19.6.0

File hashes

Hashes for readsql-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1569d35d1c3b9228b75192bc57864195893ada3d1d2602426fba979d14546ed9
MD5 94304ae58c02180bb1d7ed778ebb3570
BLAKE2b-256 122d91a06d1ecc7c63fbd14ab4a8acfc1a3ed1eae7cd58bf7e33f7b432222ef2

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