A simple SQL templating library using Bottle templating engine. Inspired by MyBatis.
Sqlrender uses standard Python Bottle templating engine to convert template into the parametrized Sql template, which can be then used in any database.
pip3 install sqlrenderFirst, 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
    100Also, we can see that in the sql parameters tuple there are present values with corresponding "?" in the query:
(18, 'Student', 'Graduate')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
- 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.