Simple and fast sql templating engine inspired by the classical library MyBatis.
Project description
The Sqlrender
A simple SQL templating library using Bottle templating engine. Inspired by MyBatis.
About
Sqlrender uses standard Python Bottle templating engine to convert template into the parametrized Sql template, which can be then used in any database.
Installation
pip3 install sqlrender
Simple example
First, define the Sql query as a Bottle template. As you can see, we are using standard Bottle syntax:
import sqlrender
# Define sql query as Bottle template
template = """
SELECT
name, age, role, gender
FROM
users
WHERE 1 = 1
% if ageMin:
AND age >= {{ageMin}}
% end
AND role IN {{Util.join(roles)}}
ORDER BY
{{!orderBy}}
LIMIT
{{!limit}}
"""
Next, we specify the input parameters for the Bottle template:
# Define input template parameters
parameters = {
'roles': ['Student', 'Graduate'],
'ageMin': 18,
'orderBy': 'name ASC',
'limit': 100,
}
Call the render method to obtain the output sql template and the output sql parameters:
# Call render method
sql_template, sql_params = sqlrender.render(template, parameters)
print(sql_template)
print(sql_params)
It will print the bellow sql query. As you can see, the query parameters are present as "?" symbols whereas the ORDER BY and LIMIT has value directly present (thanks to "!" symbol):
SELECT
name, age, role, gender
FROM
users
WHERE 1 = 1
AND age >= ?
AND role IN (?, ?)
ORDER BY
name ASC
LIMIT
100
Also, we can see that in the sql parameters tuple there are present values with corresponding "?" in the query:
(18, 'Student', 'Graduate')
Example Breakdown
Few points:
- As has been said, the standard Bottle template is used, so read the Bottle docs to see how to use other features
- As you can see, the LIMIT and ORDER BY has value directly rendered - this is achieved by symbol "!" used in template.
- There is used custom util function "Util.join()" which is part of the library. You can create your own functions and call them in the template. However, see my function first and do it accordingly.
- That's all
Notes
- The Python Bottle template has been used because it is very simple and allows customizations which was not possible with Jinja2.
- Please give me a note about bugs
- The MyBatis is powerful and classical Java library which, unfortunately, is not known by many. That's sad because it can be way better than ORM.
Project details
Release history Release notifications | RSS feed
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 sqlrender-1.0.4.tar.gz.
File metadata
- Download URL: sqlrender-1.0.4.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
718ac3e09a48378c56f92afd3739bdd0ddd1b6130084dc396bbfae5a12c0d10e
|
|
| MD5 |
eded067b6db30d08230a43ed2b9dffdb
|
|
| BLAKE2b-256 |
cc62583416580fccc19a9ed5da292cc25a1dc49972d943e47e53f32839dbf5bd
|
File details
Details for the file sqlrender-1.0.4-py3-none-any.whl.
File metadata
- Download URL: sqlrender-1.0.4-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12340e0679ee42175a83a48923f03db10ef369b2d3a09eaf5da2a7f329721c2a
|
|
| MD5 |
e317e49305ca14b18c7496bd00bd2ab3
|
|
| BLAKE2b-256 |
aa1a98520eca59951dc2108e10088053e9b9ca50e103973b44f8cf719dd7e8ad
|