jinja2sql


Namejinja2sql JSON
Version 0.6.0 PyPI version JSON
download
home_pagehttps://github.com/antonrh/jinja2sql
SummaryJinja Templates to SQL
upload_time2024-11-08 12:31:43
maintainerNone
docs_urlNone
authorAnton Ruhlov
requires_python<4.0,>=3.9
licenseMIT
keywords sql query queries template jinja database db
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": "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/b9/f9/5c1496e390c884bb1b935ebcde6e24bcf69a45a56a5e7207f12ee684a47b/jinja2sql-0.6.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.6.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": "acb849173f49edb56b244e4ab5d5c415e87698b640947fedb7ab8451f2628319",
                "md5": "48daef1a9598425259fc41f65a7a101b",
                "sha256": "1b1ae2be5d433dab35fa25271caec41cd26e15b30de69c340c17fe9ff397d66d"
            },
            "downloads": -1,
            "filename": "jinja2sql-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "48daef1a9598425259fc41f65a7a101b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 7182,
            "upload_time": "2024-11-08T12:31:42",
            "upload_time_iso_8601": "2024-11-08T12:31:42.620340Z",
            "url": "https://files.pythonhosted.org/packages/ac/b8/49173f49edb56b244e4ab5d5c415e87698b640947fedb7ab8451f2628319/jinja2sql-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b9f95c1496e390c884bb1b935ebcde6e24bcf69a45a56a5e7207f12ee684a47b",
                "md5": "ddfc99c1ab4566db1cac0e9a06e678ab",
                "sha256": "eb88c8856ca72c02613717f1d67aa31c3eb58aec6c4bc6e7b59c86b809f24846"
            },
            "downloads": -1,
            "filename": "jinja2sql-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ddfc99c1ab4566db1cac0e9a06e678ab",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 7163,
            "upload_time": "2024-11-08T12:31:43",
            "upload_time_iso_8601": "2024-11-08T12:31:43.670821Z",
            "url": "https://files.pythonhosted.org/packages/b9/f9/5c1496e390c884bb1b935ebcde6e24bcf69a45a56a5e7207f12ee684a47b/jinja2sql-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-08 12:31:43",
    "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: 0.39338s