# Jinja2SQL (Jinja To SQL)
`Jinja2SQL` is a simple and efficient library for generating SQL queries from [Jinja2](https://jinja.palletsprojects.com/en/3.1.x/) templates. It is type-friendly and offers `async` support, drawing significant inspiration from the excellent library at [jinjasql](https://github.com/sripathikrishnan/jinjasql).
[![CI](https://github.com/antonrh/jinja2sql/actions/workflows/ci.yml/badge.svg)](https://github.com/antonrh/jinja2sql/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/antonrh/jinja2sql/branch/main/graph/badge.svg?token=67CLD19I0C)](https://codecov.io/gh/antonrh/jinja2sql)
[![Documentation Status](https://readthedocs.org/projects/jinja2sql/badge/?version=latest)](https://jinja2sql.readthedocs.io/en/latest/?badge=latest)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
---
Documentation
http://jinja2sql.readthedocs.io/
---
## Requirements
`Python 3.9+` and `Jinja2 3.1.2+`.
## Installation
Install using `pip`:
```shell
pip install jinja2sql
```
or using `poetry`:
```shell
poetry add jinja2sql
```
## Quick Example
```python
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"},
)
# using with your favorite database driver connection
conn.execute(query, params)
```
### Param Styles
`Jinja2SQL` supports different param styles depending on the database driver you are using.
You can choose between the following supported param styles:
```python
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:
```python
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}"
```
Raw data
{
"_id": null,
"home_page": "https://github.com/antonrh/jinja2sql",
"name": "jinja2sql",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "sql, query, queries, template, jinja, database, db",
"author": "Anton Ruhlov",
"author_email": "antonruhlov@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/5f/8f/56c971aa96978d1ee2e56520aba2563a881576c7a2723a69e9a322b24eb7/jinja2sql-0.7.0.tar.gz",
"platform": null,
"description": "# Jinja2SQL (Jinja To SQL)\n\n`Jinja2SQL` is a simple and efficient library for generating SQL queries from [Jinja2](https://jinja.palletsprojects.com/en/3.1.x/) templates. It is type-friendly and offers `async` support, drawing significant inspiration from the excellent library at [jinjasql](https://github.com/sripathikrishnan/jinjasql).\n\n[![CI](https://github.com/antonrh/jinja2sql/actions/workflows/ci.yml/badge.svg)](https://github.com/antonrh/jinja2sql/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/antonrh/jinja2sql/branch/main/graph/badge.svg?token=67CLD19I0C)](https://codecov.io/gh/antonrh/jinja2sql)\n[![Documentation Status](https://readthedocs.org/projects/jinja2sql/badge/?version=latest)](https://jinja2sql.readthedocs.io/en/latest/?badge=latest)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n---\nDocumentation\n\nhttp://jinja2sql.readthedocs.io/\n\n---\n\n## Requirements\n\n`Python 3.9+` and `Jinja2 3.1.2+`.\n\n## Installation\n\nInstall using `pip`:\n\n```shell\npip install jinja2sql\n```\n\nor using `poetry`:\n\n```shell\npoetry add jinja2sql\n```\n\n## Quick Example\n\n```python\nfrom jinja2sql import Jinja2SQL\n\n\nj2sql = Jinja2SQL(param_style=\"named\") # default param style is \"named\"\n\nquery, params = j2sql.from_string(\n \"SELECT * FROM {{ table | identifier }} WHERE email = {{ email }}\",\n context={\"table\": \"users\", \"email\": \"user@mail.com\"},\n)\n\n# using with your favorite database driver connection\n\nconn.execute(query, params)\n```\n\n### Param Styles\n\n`Jinja2SQL` supports different param styles depending on the database driver you are using.\n\nYou can choose between the following supported param styles:\n\n```python\nfrom jinja2sql import Jinja2SQL\n\n\nj2sql = Jinja2SQL(param_style=\"named\") # default\n\nquery, params = j2sql.from_string(\n \"SELECT * FROM table WHERE param = {{ param }}\",\n context={\"param\": ...},\n param_style=\"named\", # or \"qmark\", \"numeric\", \"format\", \"pyformat\", \"asyncpg\"\n)\n```\n\n| param_style | Example |\n| ------------- | ----------- |\n| `named` | `:param` |\n| `qmark` | `?` |\n| `numeric` | `:1` |\n| `format` | `%s` |\n| `pyformat` | `%(param)s` |\n| `asyncpg` | `$1` |\n\n\nor you can provide a custom function to format your database specific param style:\n\n\n```python\nfrom jinja2sql import Jinja2SQL\n\n\nj2sql = Jinja2SQL()\n\nquery, params = j2sql.from_string(\n \"SELECT * FROM table WHERE column = {{ param }}\",\n context={\"param\": ...},\n param_style=lambda key, _: f\"{{{key}}}\",\n)\n\nassert query == \"SELECT * FROM table WHERE column = {email}\"\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Jinja Templates to SQL",
"version": "0.7.0",
"project_urls": {
"Homepage": "https://github.com/antonrh/jinja2sql",
"Repository": "https://github.com/antonrh/jinja2sql"
},
"split_keywords": [
"sql",
" query",
" queries",
" template",
" jinja",
" database",
" db"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ce1959b09fc6025c0d985b7e685a527211ce753c028ae1c7602e2e86b692b991",
"md5": "9073097618573f8b7c37bd38eaa33d50",
"sha256": "af6f6275867ee3dc975118eccaf4e567dd6b43737858d746c5ebe757feaa0dfe"
},
"downloads": -1,
"filename": "jinja2sql-0.7.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9073097618573f8b7c37bd38eaa33d50",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 7080,
"upload_time": "2024-12-10T12:20:43",
"upload_time_iso_8601": "2024-12-10T12:20:43.414938Z",
"url": "https://files.pythonhosted.org/packages/ce/19/59b09fc6025c0d985b7e685a527211ce753c028ae1c7602e2e86b692b991/jinja2sql-0.7.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5f8f56c971aa96978d1ee2e56520aba2563a881576c7a2723a69e9a322b24eb7",
"md5": "93c0df8f8f130ea828d16596a047d95d",
"sha256": "672073b50f8f3793bfeb6d07d0a018ef99b0d1f19026eecd26a9e447178959e9"
},
"downloads": -1,
"filename": "jinja2sql-0.7.0.tar.gz",
"has_sig": false,
"md5_digest": "93c0df8f8f130ea828d16596a047d95d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 7062,
"upload_time": "2024-12-10T12:20:45",
"upload_time_iso_8601": "2024-12-10T12:20:45.667319Z",
"url": "https://files.pythonhosted.org/packages/5f/8f/56c971aa96978d1ee2e56520aba2563a881576c7a2723a69e9a322b24eb7/jinja2sql-0.7.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-10 12:20:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "antonrh",
"github_project": "jinja2sql",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "jinja2sql"
}