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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a019df7517654e7420a8c83055c38b525214d1f597c6fd0e4beee5cc7474baa
|
|
| MD5 |
c603fc30f73b0a66cf3444bd02e02537
|
|
| BLAKE2b-256 |
d3d6ae8ffb41fadef6f60222a2718aea1eed05b26baa9161346627c11cd6172f
|
Provenance
The following attestation bundles were made for sql2func-0.0.1.dev3.tar.gz:
Publisher:
pypi.yml on deadblue/sql2func
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sql2func-0.0.1.dev3.tar.gz -
Subject digest:
6a019df7517654e7420a8c83055c38b525214d1f597c6fd0e4beee5cc7474baa - Sigstore transparency entry: 203344250
- Sigstore integration time:
-
Permalink:
deadblue/sql2func@fc75c2747c207e5b09371fc7e8e70d589945045c -
Branch / Tag:
refs/tags/v0.0.1.dev3 - Owner: https://github.com/deadblue
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@fc75c2747c207e5b09371fc7e8e70d589945045c -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2a5a49811514e6b25a645705684811d42931701b2b611cc85096c5e5b4100b3
|
|
| MD5 |
62e939b3bb3a5b1df0175fe29ef60950
|
|
| BLAKE2b-256 |
5dc5d74d8ece2630131940fc010176145d977ed9f729b2ae083b1688da0278f6
|
Provenance
The following attestation bundles were made for sql2func-0.0.1.dev3-py3-none-any.whl:
Publisher:
pypi.yml on deadblue/sql2func
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sql2func-0.0.1.dev3-py3-none-any.whl -
Subject digest:
a2a5a49811514e6b25a645705684811d42931701b2b611cc85096c5e5b4100b3 - Sigstore transparency entry: 203344251
- Sigstore integration time:
-
Permalink:
deadblue/sql2func@fc75c2747c207e5b09371fc7e8e70d589945045c -
Branch / Tag:
refs/tags/v0.0.1.dev3 - Owner: https://github.com/deadblue
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@fc75c2747c207e5b09371fc7e8e70d589945045c -
Trigger Event:
push
-
Statement type: