jinja2sql


Namejinja2sql JSON
Version 0.8.0 PyPI version JSON
download
home_pageNone
SummaryJinja Templates to SQL
upload_time2025-01-15 08:20:59
maintainerNone
docs_urlNone
authorNone
requires_python~=3.9
licenseNone
keywords database db jinja queries query sql template
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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": null,
    "name": "jinja2sql",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "~=3.9",
    "maintainer_email": null,
    "keywords": "database, db, jinja, queries, query, sql, template",
    "author": null,
    "author_email": "Anton Ruhlov <antonruhlov@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/d9/80/25afe147a6fe58493b21e59eadfc204664cc1b6cf17a3a6d293c0740db9a/jinja2sql-0.8.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": null,
    "summary": "Jinja Templates to SQL",
    "version": "0.8.0",
    "project_urls": {
        "Repository": "https://github.com/antonrh/jinja2sql"
    },
    "split_keywords": [
        "database",
        " db",
        " jinja",
        " queries",
        " query",
        " sql",
        " template"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "7f004debb79ff833c12db5dbeb21fc0fe1afa75f77a29990a816ea9cf1f1e9b7",
                "md5": "890a7856ba12ab2fa7b4cb4af9da3d44",
                "sha256": "94d0668dcfa33bd007c89069ec1a60f665b5f172c8e2b99b50c3568e551b2bac"
            },
            "downloads": -1,
            "filename": "jinja2sql-0.8.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "890a7856ba12ab2fa7b4cb4af9da3d44",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "~=3.9",
            "size": 7045,
            "upload_time": "2025-01-15T08:20:56",
            "upload_time_iso_8601": "2025-01-15T08:20:56.790326Z",
            "url": "https://files.pythonhosted.org/packages/7f/00/4debb79ff833c12db5dbeb21fc0fe1afa75f77a29990a816ea9cf1f1e9b7/jinja2sql-0.8.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "d98025afe147a6fe58493b21e59eadfc204664cc1b6cf17a3a6d293c0740db9a",
                "md5": "75ecea1db082734d380c80226b2cb0fd",
                "sha256": "32fdb29f509ace7818d7c400cbfd22f5193d313ea0396e1be92f7b5b9b845e2b"
            },
            "downloads": -1,
            "filename": "jinja2sql-0.8.0.tar.gz",
            "has_sig": false,
            "md5_digest": "75ecea1db082734d380c80226b2cb0fd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "~=3.9",
            "size": 77478,
            "upload_time": "2025-01-15T08:20:59",
            "upload_time_iso_8601": "2025-01-15T08:20:59.526512Z",
            "url": "https://files.pythonhosted.org/packages/d9/80/25afe147a6fe58493b21e59eadfc204664cc1b6cf17a3a6d293c0740db9a/jinja2sql-0.8.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-15 08:20:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "antonrh",
    "github_project": "jinja2sql",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "jinja2sql"
}
        
Elapsed time: 6.20722s