Skip to main content

Run SQL as a function.

Project description

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

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

sql2func-0.0.1.dev3.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sql2func-0.0.1.dev3-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file sql2func-0.0.1.dev3.tar.gz.

File metadata

  • Download URL: sql2func-0.0.1.dev3.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sql2func-0.0.1.dev3.tar.gz
Algorithm Hash digest
SHA256 6a019df7517654e7420a8c83055c38b525214d1f597c6fd0e4beee5cc7474baa
MD5 c603fc30f73b0a66cf3444bd02e02537
BLAKE2b-256 d3d6ae8ffb41fadef6f60222a2718aea1eed05b26baa9161346627c11cd6172f

See more details on using hashes here.

Provenance

The following attestation bundles were made for sql2func-0.0.1.dev3.tar.gz:

Publisher: pypi.yml on deadblue/sql2func

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sql2func-0.0.1.dev3-py3-none-any.whl.

File metadata

  • Download URL: sql2func-0.0.1.dev3-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sql2func-0.0.1.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 a2a5a49811514e6b25a645705684811d42931701b2b611cc85096c5e5b4100b3
MD5 62e939b3bb3a5b1df0175fe29ef60950
BLAKE2b-256 5dc5d74d8ece2630131940fc010176145d977ed9f729b2ae083b1688da0278f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for sql2func-0.0.1.dev3-py3-none-any.whl:

Publisher: pypi.yml on deadblue/sql2func

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page