SQL + Jinja Templates Done Right™
Project description
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 search 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.
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
File details
Details for the file sql-ninja-0.2.0.tar.gz
.
File metadata
- Download URL: sql-ninja-0.2.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1d4b28717691af84dcd196b928a6f1af5df2e5d7a3bb0ba24922a5364c77bc5 |
|
MD5 | 510febdfa1d4300e2c3470f2b5223508 |
|
BLAKE2b-256 | 490d24e953de40254db6d6725441de37111429a896954da411d156be2f7b58fb |
File details
Details for the file sql_ninja-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: sql_ninja-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60054b8722247a6570b62ad327ae6e145344243c36baa1f182bf3fe5776aac0c |
|
MD5 | 716ef61f79feaa724ead520fc3bb4f15 |
|
BLAKE2b-256 | 2831addf990014a3c1b594eeba6b663b22cdd8bec5325f77f5dea835db1faf96 |