https://github.com/wseaton/jinjasql-rs
SQL Extension API for Rust MiniJinja templates
https://github.com/wseaton/jinjasql-rs
jinja python rust sql
Last synced: 2 months ago
JSON representation
SQL Extension API for Rust MiniJinja templates
- Host: GitHub
- URL: https://github.com/wseaton/jinjasql-rs
- Owner: wseaton
- License: mit
- Created: 2022-01-03T19:44:43.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-18T18:49:33.000Z (over 4 years ago)
- Last Synced: 2025-10-30T21:55:17.215Z (8 months ago)
- Topics: jinja, python, rust, sql
- Language: Rust
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jinjasql-rs
WIP Rust port of the [JinjaSQL Python extension](https://github.com/sripathikrishnan/jinjasql) for Jinja2, powered by `minijinja`.
## Usage
The `inclause` and `bind` filters are the main attraction here, as they allow us to keep track of the variables injected into the SQL statement and stash them for passing into the client at query time via params.
```rust
let j = JinjaSqlBuilder::new().build();
let query_string = indoc! {"
select{% for col in columns %}
{{ col }}{%- if not loop.last %},{% endif %}{% endfor %}
from
(
select {% for col in columns %}
{{ col }}{%- if not loop.last %},{% endif %}{% endfor %} from {{ table_name | upper }}
where sku in {{ skus | inclause }}
)a
where
tag in {{ tags | reverse | inclause }}
and stock_date = {{ baz | bind }}
"};
let (res, params) = j
.render_query(
Some(query_string),
None,
context!(
columns => vec!["apple", "lettuce", "lemon"],
table_name => "orders.stock_data",
tags => vec!["moldy", "sweet", "fresh"],
skus => vec!["EE-001", "EA-001", "BA-001"],
baz => "2022-01-01"
),
)
.unwrap();
```
returns:
```sql
select
apple,
lettuce,
lemon
from
(
select
apple,
lettuce,
lemon from ORDERS.STOCK_DATA
where sku in ($1, $2, $3)
)a
where
tag in ($4, $5, $6)
and stock_date = $7
-- ["EE-001", "EA-001", "BA-001", "fresh", "sweet", "moldy", "2022-01-01"]
```
## TODO
- [x] alternate param styles
- [ ] automatic `bind` filtering (depends on the ability to modify the parser stream, a-la `jinja2.Extension`)
- [ ] benchmarks
- [x] better state encapsulation
- solved for now until a solution for [mutable filter state](https://github.com/mitsuhiko/minijinja/issues/42) stabilizes
- [ ] better tests
- [ ] publish to crates.io
- [ ] docs
- [ ] python bindings?