Skip to main content

format-sql is a tool to format SQL in your Python strings!

Project description

https://travis-ci.org/paetzke/format-sql.svg?branch=master https://coveralls.io/repos/paetzke/format-sql/badge.png?branch=master https://pypip.in/v/format-sql/badge.png

format-sql is a tool to format SQL in your Python strings!

An example:

    sql = """ SELECT country, product, SUM(profit) FROM
sales   left join x on x.id=sales.k GROUP BY country,
product having f > 7 and fk=9 limit 5;    """

Will result in:

sql = """
    SELECT
        country,
        product,
        SUM(profit)
    FROM
        sales
        LEFT JOIN x
            ON x.id = sales.k
    GROUP BY
        country,
        product
    HAVING
        f > 7
        AND fk = 9
    LIMIT
        5
    ; """

Install format-sql via pip:

$ pip install format-sql

You can then just call format-sql with files and directories:

$ format-sql -h
usage: format-sql [-h] [--types {py,sql}] [-r] [--no-semicolon]
                  paths [paths ...]

positional arguments:
  paths

optional arguments:
  -h, --help        show this help message and exit
  --types {py,sql}  Process given file types. Default value is "py".
  -r, --recursive   Process files found in subdirectories.
  --no-semicolon    Try to detect SQL queries with no trailing semicolon.

You can try format-sql online: http://paetzke.me/format-sql.

Changes

0.2.2

  • Fix an issue with passing --types command line argument.

0.2.1

  • Fix an issue with passing command line arguments.

0.2

  • Add handling for unknown token sequences.

  • Fix detecting special words. Word boundaries have been ignored.

  • Single comparison in joins are printed on one line.

  • Support LIKE and LIKE BINARY for comparison.

0.1

  • Add parameter --no-semicolon to enable taking SQL queries without semicolon into account.

Copyright (c) 2014, Friedrich Paetzke (paetzke@fastmail.fm). All rights reserved.

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

format-sql-0.2.2.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

format_sql-0.2.2-py2.py3-none-any.whl (11.0 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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