Python Query Builder.
Project description
Py-QueryBuilder
Query builder for Python applications, designed to work with the same data structure generated by MUI-QueryBuilder.
Installation
Py-QueryBuilder is available as a pypi package.
pip install py-querybuilder
Usage
Here is a quick example to get you started.
First you'll need a templates folder in your module, containing Jinja2 templates with a {{ where }} placeholder:
SELECT *
FROM my_table
WHERE {{ where }}
You can now rely on the QueryBuilder class to render queries:
from py_querybuilder import QueryBuilder
qb = QueryBuilder("app.articles", [
{
"label": "Article",
"options": [
{
"label": "Title",
"value": "title",
"type": "text",
},
{
"label": "URL",
"value": "url",
"type": "text",
},
],
},
])
sql_query, sql_params = qb.render("query.sql", {
"combinator": "and",
"rules": [
{
"field": "title",
"operator": "contains",
"value": "Brazil",
},
],
})
The query is generated with JinjaSQL, 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 more. We use sqlparse to format the queries.
SELECT *
FROM my_table
WHERE title ~* ? AS "Title"
The default operators used for generating queries are:
{
"after": ">",
"after_equal": ">=",
"before": "<",
"before_equal": "<=",
"contains": "~*",
"greater": ">",
"greater_equal": ">=",
"equal": "=",
"in": "in",
"less": "<",
"less_equal": "<=",
"not_contains": "!~*",
"not_equal": "!=",
"not_in": "not in",
"not_null": "is not null",
"null": "is null",
}
In case the database you're targeting uses different operators, it's possible to customize those at QueryBuilder's instantiation:
qb = QueryBuilder(my_module, my_query, operators={
# Custom operators.
})
SQL parameters are returned from render() in a list of values corresponding to the placeholders that need to be bound to the query.
print(sql_params)
# ["Brazil"]
Finally, you can now use the SQL query and its bind parameters to fetch data. For example, in Django, you would do something like this:
from django.db import connection
with connection.cursor() as cursor:
cursor.execute(sql_query, sql_params)
for row in cursor.fetchall():
# Do something with the results.
pass
API
Check out the Py-QueryBuilder's API here! To learn more about the query's data structure, and how filters and operators work, please refer to MUI-QueryBuilder's documentation.
License
This project is licensed under the terms of the MIT license.
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 Distributions
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 py_querybuilder-1.0.0-py2.py3-none-any.whl.
File metadata
- Download URL: py_querybuilder-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7de2daaa5a2cc138a316fd80c6d0eaa66a0ee88551d76022a1921b282c654cf3
|
|
| MD5 |
47fe853fb40296cc9386b7e9c64cae57
|
|
| BLAKE2b-256 |
1f8d59d4f94ce9a4c3601f7a7a9867caf9deb9993d5780940c9ab044931aec2a
|