Skip to content

Latest commit

 

History

History
66 lines (51 loc) · 2.46 KB

README.md

File metadata and controls

66 lines (51 loc) · 2.46 KB

pre-commit Ruff Checked with pyright qa release

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']