SQL builder via string templates
Project description
SQL-tString allows for f-string like construction of sql queries without allowing for SQL injection. The basic usage is as follows,
from sql_tstring import sql
a = 1
query, values = sql(
"""SELECT a, b, c
FROM tbl
WHERE a = {a}""",
locals(),
)
The query is a str and values a list[Any], both are then typically passed to a DB connection. Note the parameters can only be identifiers that identify variables (in the above example in the locals()) e.g. {a - 1} is not valid.
SQL-tString will convert parameters to SQL placeholders where appropriate. In other locations SQL-tString will allow pre defined column or table names to be used,
from sql_tstring import sql, sql_context
col = "a"
table = "tbl"
with sql_context(columns={"a"}, tables={"tbl"}):
query, values = sql(
"SELECT {col} FROM {table}",
locals(),
)
If the value of col or table does not match the valid values given to the sql_context function an error will be raised.
Rewriting values
SQL-tString will also remove parameters if they are set to the special value of Absent (or RewritingValue.Absent). This is most useful for optional updates, or conditionals,
from sql_tstring import Absent, sql
a = Absent
b = Absent
query, values = sql(
"""UPDATE tbl
SET a = {a},
b = 1
WHERE b = {b}""",
locals(),
)
As both a and b are Absent the above query will be UPDATE tbl SET b =1.
In addition for conditionals the values IsNull (or RewritingValue.IS_NULL) and IsNotNull (or RewritingValue.IS_NOT_NULL) can be used to rewrite the conditional as expected. This is useful as x = NULL is always false in SQL.
t-string (PEP 750)
If, hopefully, PEP 750 is accepted the usage of this library will change to,
from sql_tstring import sql
a = 1
query, values = sql(
t"""SELECT a, b, c
FROM tbl
WHERE a = {a}""",
)
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
Built Distribution
File details
Details for the file sql_tstring-0.2.0.tar.gz
.
File metadata
- Download URL: sql_tstring-0.2.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc9979853768001aca62bdcc3882349908e270bd9378a898eb456b2352b0adb2 |
|
MD5 | ff084c589e899bbbc901dad26191d7eb |
|
BLAKE2b-256 | 5662717f2fb65b56ade4b81001b191717dd1654e50086811e304bc6220cd1332 |
Provenance
The following attestation bundles were made for sql_tstring-0.2.0.tar.gz
:
Publisher:
publish.yml
on pgjones/sql-tstring
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
sql_tstring-0.2.0.tar.gz
- Subject digest:
fc9979853768001aca62bdcc3882349908e270bd9378a898eb456b2352b0adb2
- Sigstore transparency entry: 150526968
- Sigstore integration time:
- Predicate type:
File details
Details for the file sql_tstring-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: sql_tstring-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c622f3575d0bd8166e3e6d4cb2951b6d61beb37080bdca886fc57035090812c |
|
MD5 | dc873b2446abb92d85035d920398d6b2 |
|
BLAKE2b-256 | c1b48ab5e0d6f82a9dfd16b0ca8ecf7b4ea823c639e10d4050de70ea70e11b23 |
Provenance
The following attestation bundles were made for sql_tstring-0.2.0-py3-none-any.whl
:
Publisher:
publish.yml
on pgjones/sql-tstring
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
sql_tstring-0.2.0-py3-none-any.whl
- Subject digest:
4c622f3575d0bd8166e3e6d4cb2951b6d61beb37080bdca886fc57035090812c
- Sigstore transparency entry: 150526969
- Sigstore integration time:
- Predicate type: