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.
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.
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.