Skip to main content

Pretty-print and format SQL queries for debugging

Project description

philiprehberger-sql-print

Tests PyPI version License

Pretty-print and format SQL queries for debugging.

Installation

pip install philiprehberger-sql-print

Usage

Format SQL

from philiprehberger_sql_print import format_sql

query = "SELECT id, name FROM users WHERE active = 1 AND role = 'admin' ORDER BY name"
print(format_sql(query))

Output:

SELECT id, name
FROM users
WHERE active = 1
  AND role = 'admin'
ORDER BY name

Print SQL with syntax highlighting

from philiprehberger_sql_print import print_sql

print_sql("SELECT * FROM orders LEFT JOIN users ON orders.user_id = users.id WHERE total > 100")

Prints formatted SQL with ANSI color-coded keywords, strings, and numbers.

Disable colors or uppercase

from philiprehberger_sql_print import print_sql

# No ANSI colors (useful for logging)
print_sql("select id from users", color=False)

# Preserve original keyword casing
print_sql("select id from users", uppercase=False)

Custom indentation

from philiprehberger_sql_print import format_sql

print(format_sql("SELECT id FROM users WHERE active = 1", indent=4))

API

Function Description
format_sql(sql, *, indent=2, uppercase=True) -> str Format a SQL query with newlines and indentation.
print_sql(sql, *, indent=2, uppercase=True, color=True, file=None) -> None Print a formatted SQL query with optional ANSI syntax highlighting.

Parameters

Parameter Type Default Description
sql str The SQL query string to format.
indent int 2 Number of spaces for indentation.
uppercase bool True Whether to uppercase SQL keywords.
color bool True Whether to apply ANSI color codes (print_sql only).
file TextIO | None None Output stream. Defaults to sys.stdout (print_sql only).

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

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

philiprehberger_sql_print-0.1.6.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_sql_print-0.1.6-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_sql_print-0.1.6.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_sql_print-0.1.6.tar.gz
Algorithm Hash digest
SHA256 b2673b30d07b9059771537ea3d342e6a5f8adb9a83dfbc74b6e0c210a0b9dc6b
MD5 fff0bfe50f545cd095e618012deb0408
BLAKE2b-256 99ab9bfbe48a288af8d6c264abcffe2943eb2e97eb0c93d984b86174ac5b3ab5

See more details on using hashes here.

File details

Details for the file philiprehberger_sql_print-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_sql_print-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f94b932ede4a78494a81acea0f950d63c98d786ffc166a5ae8843ee3b94f2584
MD5 8317b0c722443fe376377a0fa0704c15
BLAKE2b-256 6d62c43b9af8d0e896e1b08c0e2d74992a9ee7acb4e990f710893c34b7c3fb8f

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