Alternative Queries: Typed and Reusable Handcrafted SQL
Project description
Alternative Queries
Alternative queries is a library created to help with handcrafted SQL queries.
It works by providing a class that represent the queries where its parameter
types are checked by Pydantic
.
If you want to write reusable and nested handcrafted SQL queries, you can check more information on the Alternative Queries Documentation. If you want to know how Alternative Queries can help you, check the Why use Alternative Queries? section of the documentation.
Installation
The library is available in the Python Package Index.
pip install altqq
Quick Start
To start, define a class by inheriting the altqq.Query
class. The class should
have a query following the python formatting standards. The variable names
inside the __query__
must match the other attributes defined on the class.
import altqq
class SelectUserByFirstName(altqq.Query):
__query__ = """
SELECT * FROM "Users"
WHERE first_name = {first_name}
"""
first_name: str
The class can be used like a dataclass
. In fact, classes inheriting the
altqq.Query
class are turned into a Pydantic
dataclass
.
query = SelectUserByFirstName(first_name="arietta")
The object can be converted into a query suitable for a DBMS library of your
choice. For example, calling the altqq.to_pyodbc
function will convert the
object to PyODBCQuery
which provides the query string and the parameters.
pyodbc_query = altqq.to_pyodbc(query)
print(pyodbc_query.query)
#
# SELECT * FROM "Users"
# WHERE first_name = ?
#
print(pyodbc_query.parameters)
# ['arietta']
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 altqq-0.0.7.tar.gz
.
File metadata
- Download URL: altqq-0.0.7.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ad2ae1c71fbdfcb4229d9e05ea1745e83c5878304dd4cf468d028ba30b2401c |
|
MD5 | 17b25a11c442c90d048d18f4703effd1 |
|
BLAKE2b-256 | 4e56cbc4a61747b19bb80a29be0030ddae020083df33c349a4fb64dcd029dff6 |
File details
Details for the file altqq-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: altqq-0.0.7-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78abb1d863dcf28e877026cb8cf09fc757cce41c66a98eda5ef1feee2c848509 |
|
MD5 | d638133909e4daeb92e6b5fd6bbe55f6 |
|
BLAKE2b-256 | 5cbc02bdaf5e46a881fbad80f65f19496efa81528b88b2ef6520d62f9269b7bb |