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.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
qlsq-0.0.1-py3-none-any.whl
(6.3 kB
view details)
File details
Details for the file qlsq-0.0.1.tar.gz.
File metadata
- Download URL: qlsq-0.0.1.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 |
d260a9297281009fec2725a091fe26634d34c7b78448345e2b89afbd1f1088fc
|
|
| MD5 |
6c95b474e552fc9e06cf25ced99f79e0
|
|
| BLAKE2b-256 |
e2cc783a7065eb25d3c5df7ec85efe78f599d9c0e4a17131b35409a90fa7976b
|
File details
Details for the file qlsq-0.0.1-py3-none-any.whl.
File metadata
- Download URL: qlsq-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a425cf7b6d424e3316bee7c5e592b411bb90f9adc1bc557e19e16fd806cbe09a
|
|
| MD5 |
4170c3bcb19be88489acfc3bb99fb24a
|
|
| BLAKE2b-256 |
3c91d06451e498e0a0f297133b5f39f644d611897c5c9da3c810341d747dcd75
|