Skip to main content

Overview

SQL + Jinja Done Right™

This project is a thin wrapper around Jinja templates to help manage the generation of SQL.

In your project create a directory name sql/templates to manage your SQL files:

.
└── sql
    └── templates
        ├── foo.sql
        └── bar.sql

Templates are also searched from the current working directory and will have priority over files in sql/templates.

An example foo.sql might be:

SELECT '{{msg}}' as message

Then to access the SQL template use the following Python snippet:

from sqlninja import engine as sqlninja

query = sqlninja.render("foo.sql", msg="Hello World")

The resulting query would be:

SELECT 'Hello World' as message

In bar.sql lets try including foo.sql:

SELECT * FROM ({% include 'foo.sql' %}) as t1

The rendered SQL would be:

SELECT * FROM (SELECT 'Hello World' as message) as t1

Command Line Interface

The Python interface makes sense at runtime, but for development, the CLI is more convenient.

The library installs a CLI script: sql

$ sql --help
Usage: sql [OPTIONS] SRC

Options:
--template_path TEXT  Base directory where SQL templates are located.
Defaults to `sql/templates`

--help                Show this message and exit.

To see the resulting SQL we can try:

sql foo.sql msg='Hello World'

# => SELECT 'Hello World' as message

Installation

pip install sql-ninja

Or add to requirements.txt

sql-ninja

Or add to setup.py

setup(
    install_requires=[
        'sql-ninja',
    ]

Docker

Docker users can pull directly from ddrscott/sql-ninja

docker run --rm -v $PWD:/app -w /app ddrscott/sql-ninja sample.sql
#            ^   ^            ^      ^                  ^
#            |   |            |      |                  |
#            |   |            |      |                  + the template
#            |   |            |      |
#            |   |            |      + the image
#            |   |            |
#            |   |            + start in /app path
#            |   |            
#            |   + volume mount current path to /app
#            |
#            + remove container when complete

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ddrscott/sql-ninja

License

The gem is available as open-source under the terms of the MIT License.

Metadata

Release files for sql-ninja 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sql-ninja 0.2.1
File Size Uploaded Provenance
sql-ninja-0.2.1.tar.gz 3.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sql-ninja 0.2.1
File Interpreter ABI Platform
sql_ninja-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.1 kB

Release files / sql-ninja-0.2.1.tar.gz

Download URL sql-ninja-0.2.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f797c6d86ccb0245c2e715bbb239b50d6c2ca96123a27f4e8d43a3f8823ab5b3
BLAKE2b-256 checksum
How to use checksums
a17b19f89aff79beb2aa26d91ac746d1cfa91453b9d60ae2763d7074b965bbf6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Release files / sql_ninja-0.2.1-py3-none-any.whl

Download URL sql_ninja-0.2.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec2c67b9c8ab99c46635b0e04759070c1652ce32b1f947f95505d578472d9ae9
BLAKE2b-256 checksum
How to use checksums
38604de301774ebbe5191e1b0e13bca393600d99782defb669ee2bad16a2dd7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Security for sql-ninja 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page