JinjaSQL is a template language for SQL statements and scripts. Since it's based in Jinja2, you have all the power it offers - conditional statements, macros, looping constructs, blocks, inheritance, and many more.
JinjaSQL automatically binds parameters that are inserted into the template. After JinjaSQL evaluates the template, you get:
- A Query with %s placeholders for the parameters
- A List of values corresponding to the placeholdersthat need to be bound to the query
JinjaSQL doesn't actually execute the query - it only prepares the query and the bind parameters. You can execute the query using any database engine / driver you are working with.
For example, if you have a template like this -
select username, sum(spend)
from transactions
where start_date > {{request.start_date}}
and end_date < {{request.end_date}}
{% if request.organization %}
and organization = {{request.organization}}
{% endif %}
then, depending on the parameters you provide, you get a query
select username, sum(spend)
from transaction
where start_date > %s
and end_date < %s
and organization = %s
with bind parameters = ['2016-10-10', '2016-10-20', 1321]
If request.organization
was empty/falsy, the corresponding and clause
would be absent from the query, and the list of bind parameters
would not have the organization id.
JinjaSQL is not meant to replace your ORM. ORMs like those provided by SQLAlchemy or Django are great for a variety of use cases, and should be the default in most cases. But there are a few use cases where you really need the power of SQL.
Use JinjaSQL for -
- Reporting, business intelligence or dashboard like use cases
- When you need aggregation/group by
- Use cases that require data from multiple tables
- Migration scripts & bulk updates that would benefit from macros
In all other use cases, you should reach to your ORM instead of writing SQL/JinjaSQL.
While JinjaSQL can handle insert/update statements, you are better off using your ORM to handle such statements. JinjaSQL is mostly meant for dynamic select statements that an ORM cannot handle as well.
First, import the JinjaSql
class and create an object. JinjaSql
is thread-safe, so you can safely create one object at startup and use it everywhere.
from jinjasql import JinjaSql
j = JinjaSql()
Next, create your template query. You can use the full power of Jinja templates over here - macros, includes, imports, if/else conditions, loops, filters and so on. You can load the template from a file or from database or wherever else Jinja supports.
template = """
SELECT project, timesheet, hours
FROM timesheet
WHERE user_id = {{ user_id }}
{% if project_id %}
AND project_id = {{ project_id }}
{% endif %}
"""
Create a context object. This object is a regular dictionary, and can contain nested dictionaries, lists or objects. The template query is evaluated against this context object.
data = {
"project_id": 123,
"user_id": u"sripathi"
}
Finally, call the prepare_query
method with the template and the context. You get back two things:
query
is the generated SQL query. Variables are replaced by %sbind_params
is an array of parameters corresponding to the %s
query, bind_params = j.prepare_query(template, data)
This is the query that is generated:
expected_query = """
SELECT project, timesheet, hours
FROM timesheet
WHERE user_id = %s
AND project_id = %s
"""
And these are the bind parameters:
self.assertEquals(bind_params, [u'sripathi', 123])
self.assertEquals(query.strip(), expected_query.strip())
You can now use the query and bind parameters to execute the query. For example, in django, you would do something like this:
from django.db import connection
with connection.cursor() as cursor:
cursor.execute(query, bind_params)
for row in cursor.fetchall():
# do something with the results
pass
Pre-Requisites :
- python 2.6+, or 3.4+, and pip.
- jinja2 >= version 2.5
To install from PyPI (recommended) :
pip install jinjasql
To install from source :
git clone https://github.com/hashedin/jinjasql
cd jinjasql
sudo python setup.py install
At it's core, JinjaSQL provides a filter called bind
. This filter gobbles up whatever value is provided, and always emits the placeholder string %s. The actual value is then stored in a thread local list of bind parameters.
jinja.prepare_query("select * from user where id = {{userid | bind}}",
{userid: 143})
When this code is evaluated, the output query is select * from user where id = %s
.
Manually applying the bind
filter to every parameter is error-prone. Sooner than later, a developer will miss the filter, and it will lead to SQL Injection.
JinjaSQL automatically applies the bind filter to ALL variables. The query template is transformed before it is evaluated.
select * from user where id = {{userid}}
becomes
select * from user where id = {{userid | bind}}
Jinja lets extensions to rewrite the token stream. JinjaSQL looks for variable_begin
and variable_end
tokens in the stream, and rewrites the stream to include the bind
filter as the last filter.
Jinja has an autoescape feature. If turned on, it automatically HTML escapes variables. It does this by wrapping strings using the Markup
class.
JinjaSQL builds on this functionality. JinjaSQL requires autoescape to be turned on. As a result, strings that are injected are wrapped using the Markup class. JinjaSQL uses this wrapper class as well to prevent double-binding of parameters.
jinjasql is licensed under the MIT License. See LICENSE
(c) 2016 HashedIn Technologies Pvt. Ltd.