dynamic sql generetor
Project description
$(QL)^2$
https://pypi.org/project/qlsq/
qlsq (QL-squared) is library for generating predictable sql queries from lisp-like queries.
- Avoids N + 1 Query problem.
- Lets you controll query complexity
- prevent filtering by non index columns.
- only does left joins for required selected fields (it is developers resposibility to ensure 1:1 mapping).
- Allows claim based access controll for each field.
- Does not support nesting (unless implemented in sql)
- Only works with psycopg - generates parametrized queries like
SELECT %(param_0)s;
How it looks like:
# 1. Each query will be generated in a context.
# Context is a set of fields and tables definitions.
# This example assumes there exist `user_tasks` and `users` tables.
# Normal application would have multiple contexts.
from qlsq import ContextTable, ContextField, Context, QueryType
tables = [
ContextTable(
alias="ut",
source="user_tasks",
join_condition=None,
depends_on=[]
),
ContextTable(
alias="u",
source="users",
join_condition="u.id = ut.user_id",
depends_on=["ut"]
),
]
fields = [
ContextField(
alias="full_name",
source="u.full_name",
query_type=QueryType.text,
depends_on=["u"],
read_claim="r_full_name",
edit_claim="e_full_name",
filter_claim="f_full_name",
),
ContextField(
alias="user_id",
source="ut.user_id",
query_type=QueryType.numeric,
depends_on=["ut"],
read_claim="r_user_id",
edit_claim="e_user_id",
filter_claim="f_user_id",
),
]
context = Context(tables, fields)
# 2. write a query (this could be done on a frontend)
lq = [["select", "full_name"], ["where", ["eq", "user_id", 3]]]
# 3. create query object
q = context.parse_query(lq)
# 4. generate sql and params
sql, params = q.to_sql()
# expected sql:
# SELECT u.full_name FROM user_tasks ut LEFT JOIN users u ON u.id = ut.user_id WHERE (ut.user_id = %(param_0)s);
# expected params:
# {"param_0": 3}
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
qlsq-0.0.1.dev0.tar.gz
(6.5 kB
view details)
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 qlsq-0.0.1.dev0.tar.gz.
File metadata
- Download URL: qlsq-0.0.1.dev0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0102f5abb7fc2cade4c8ae77887d643a059e51a3ce7e3a2ba85ff2df488bb16
|
|
| MD5 |
20ff5fff1fa2dc5e4b7d44501063d9c9
|
|
| BLAKE2b-256 |
1923a2e87127a558ec3d0e3103f4ff0c4be6f2df94b25fef7307551c621d729b
|
File details
Details for the file qlsq-0.0.1.dev0-py3-none-any.whl.
File metadata
- Download URL: qlsq-0.0.1.dev0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
652f7bfa08e5cec414b27e55a8d4faa0565e221e2d11e1c757424b1c4b32597f
|
|
| MD5 |
7a7046b8d8aca294f46d31d2602d8b98
|
|
| BLAKE2b-256 |
853efa0af985ea613d5c243fea97d5a05fdf28745b50b5782b3a6b8b79858c1d
|