Skip to main content

SQL builder via string templates

Project description

Build Status pypi python license

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sql_tstring-0.2.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

sql_tstring-0.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

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

Hashes for sql_tstring-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fc9979853768001aca62bdcc3882349908e270bd9378a898eb456b2352b0adb2
MD5 ff084c589e899bbbc901dad26191d7eb
BLAKE2b-256 5662717f2fb65b56ade4b81001b191717dd1654e50086811e304bc6220cd1332

See more details on using hashes here.

Provenance

The following attestation bundles were made for sql_tstring-0.2.0.tar.gz:

Publisher: publish.yml on pgjones/sql-tstring

Attestations:

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

Hashes for sql_tstring-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c622f3575d0bd8166e3e6d4cb2951b6d61beb37080bdca886fc57035090812c
MD5 dc873b2446abb92d85035d920398d6b2
BLAKE2b-256 c1b48ab5e0d6f82a9dfd16b0ca8ecf7b4ea823c639e10d4050de70ea70e11b23

See more details on using hashes here.

Provenance

The following attestation bundles were made for sql_tstring-0.2.0-py3-none-any.whl:

Publisher: publish.yml on pgjones/sql-tstring

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page