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 resultant 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.
SQL-tString will also remove parameters if they are set to the special value of 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.
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.1.0.tar.gz
.
File metadata
- Download URL: sql_tstring-0.1.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7aa1c861b046fea09735582c35aeea23bdb8aa2e0e0a81a930c3cfdcdfacfb28 |
|
MD5 | 82f7e7eef817d85dbdcbc1d0f914980e |
|
BLAKE2b-256 | 151eeff1a3fdfaa722c3f3a32e46a3cc641d80bed866622b5411b4a954f64f1d |
Provenance
The following attestation bundles were made for sql_tstring-0.1.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.1.0.tar.gz
- Subject digest:
7aa1c861b046fea09735582c35aeea23bdb8aa2e0e0a81a930c3cfdcdfacfb28
- Sigstore transparency entry: 149342451
- Sigstore integration time:
- Predicate type:
File details
Details for the file sql_tstring-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: sql_tstring-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.4 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 | 805c13d8bb0563290eba426fd0c1ddba3fbc2146140fd9f00fb779735cf08e5f |
|
MD5 | 9340a2537657eb8a5a517e648adab70f |
|
BLAKE2b-256 | 0c921aa9825cc8d0017494e3bbb37f4c207ae9021d65493ffbf502205f343d95 |
Provenance
The following attestation bundles were made for sql_tstring-0.1.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.1.0-py3-none-any.whl
- Subject digest:
805c13d8bb0563290eba426fd0c1ddba3fbc2146140fd9f00fb779735cf08e5f
- Sigstore transparency entry: 149342453
- Sigstore integration time:
- Predicate type: