Jinja Templates to SQL
Project description
Jinja2SQL (Jinja To SQL)
Jinja2SQL is a simple and efficient library for generating SQL queries from Jinja2 templates. It is type-friendly and offers async support, drawing significant inspiration from the excellent library at jinjasql.
Documentation
http://jinja2sql.readthedocs.io/
Requirements
Python 3.8+ and Jinja2 3.1.2+.
Installation
Install using pip:
pip install jinja2sql
or using poetry:
poetry add jinja2sql
Quick Example
from jinja2sql import Jinja2SQL
j2sql = Jinja2SQL(param_style="named") # default param style is "named"
query, params = j2sql.from_string(
"SELECT * FROM {{ table | identifier }} WHERE email = {{ email }}",
context={"table": "users", "email": "user@mail.com"},
)
assert query == "SELECT * FROM users WHERE email = :email"
assert params == {"email": "user@mail.com"}
Param Styles
Jinja2SQL supports different param styles depending on the database driver you are using.
You can choose between the following supported param styles:
from jinja2sql import Jinja2SQL
j2sql = Jinja2SQL(param_style="named") # default
query, params = j2sql.from_string(
"SELECT * FROM table WHERE param = {{ param }}",
context={"param": ...},
param_style="named", # or "qmark", "numeric", "format", "pyformat", "asyncpg"
)
| param_style | Example |
|---|---|
| named | :param |
| qmark | ? |
| numeric | :1 |
| format | %s |
| pyformat | %(param)s |
| asyncpg | $1 |
or you can provide a custom function to format your database specific param style:
from jinja2sql import Jinja2SQL
j2sql = Jinja2SQL()
query, params = j2sql.from_string(
"SELECT * FROM table WHERE column = {{ param }}",
context={"param": ...},
param_style=lambda key, _: f"{{{key}}}",
)
assert query == "SELECT * FROM table WHERE column = {email}"
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jinja2sql-0.2.0.tar.gz.
File metadata
- Download URL: jinja2sql-0.2.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.10.2 Darwin/21.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f87ab478a1d678a14462b7b44c0ab5b17167b86b42c7cf3fff2886479549adf1
|
|
| MD5 |
69873fdd78bd035b2aae163195eff905
|
|
| BLAKE2b-256 |
5f83f05498ed1ca95531825100a18bd37bad3723e85f41037b0a543b50cb1c37
|
File details
Details for the file jinja2sql-0.2.0-py3-none-any.whl.
File metadata
- Download URL: jinja2sql-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.10.2 Darwin/21.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e080c5b018dd60aacfe6da7672b2645d37df1d4ad98b4005ee06618225d92c87
|
|
| MD5 |
0e8fa4727325cae30013d4d0cce1d26b
|
|
| BLAKE2b-256 |
289f51dd3b80b0b8b04f1074781e316ab2486f94592ed4bcd413576efb7967ea
|