Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

sql2func

Run SQL as a function!

Example

from dataclasses import dataclass
from typing import List

import mariadb
from sql2func import SqlContext, select
from sql2func.dbapi2 import Connection


@dataclass
class Foobar:
    """
    Foobar data class
    """
    fb_id: int
    foo: str
    bar: str


@select(statement='''
SELECT fb_id, foo, bar
FROM tbl_foobar
WHERE foo = {{ foo }}
''')
def select_foobars(foo: str) -> List[Foobar]:
    pass


@update(statement='''
UPDATE tbl_foobar
SET bar = {{ bar }}
WHERE fb_id = {{ fb_id }}
''')
def update_foobar(fb_id: int, bar: str) -> int:
    pass


def connect_to_db() -> Connection:
    return mariadb.connect(
        host='localhost',
        user='db_user',
        password='db_password',
        database='db_name'
    )


def _main():
    with SqlContext(connector=connect_to_db):
        # All SQLs in this context will be run via one DB connection.
        # Run select.
        for result in select_foobars(foo='foo'):
            print(result)
        # Run update.
        update_foobar(fb_id=1, bar='blabla')
    # DB connection will be closed after SqlContext exited.


if __name__ == '__main__':
    _main()

Install

# Install release version
pip install sql2funcs

# Install develop version
pip install git+https://github.com/deadblue/sql2func.git@develop

Release files for sql2func 0.0.1.dev3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sql2func 0.0.1.dev3
File Size Uploaded
sql2func-0.0.1.dev3.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sql2func 0.0.1.dev3
File Interpreter ABI Platform
sql2func-0.0.1.dev3-py3-none-any.whl Python 3 none any Details

Total release size: 20.6 kB

Release files / sql2func-0.0.1.dev3.tar.gz

Download URL sql2func-0.0.1.dev3.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6a019df7517654e7420a8c83055c38b525214d1f597c6fd0e4beee5cc7474baa
BLAKE2b-256 checksum
How to use checksums
d3d6ae8ffb41fadef6f60222a2718aea1eed05b26baa9161346627c11cd6172f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 27, 2025.

Transparency log

Release files / sql2func-0.0.1.dev3-py3-none-any.whl

Download URL sql2func-0.0.1.dev3-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2a5a49811514e6b25a645705684811d42931701b2b611cc85096c5e5b4100b3
BLAKE2b-256 checksum
How to use checksums
5dc5d74d8ece2630131940fc010176145d977ed9f729b2ae083b1688da0278f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 27, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.0.1.dev3 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page