Skip to main content

Alternative Queries: Typed and Reusable Handcrafted SQL

Project description

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

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

altqq-0.0.5.tar.gz (7.3 kB view hashes)

Uploaded Source

Built Distribution

altqq-0.0.5-py3-none-any.whl (9.6 kB view hashes)

Uploaded Python 3

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