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
sql2func-0.0.1.dev2.tar.gz
(8.0 kB
view details)
Built Distribution
File details
Details for the file sql2func-0.0.1.dev2.tar.gz
.
File metadata
- Download URL: sql2func-0.0.1.dev2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4660867d2953c50690b134c47c14c5240fad4982184ae5360b452d6ee8008611 |
|
MD5 | 2fcc817709047aceef95d7413bdfd3e4 |
|
BLAKE2b-256 | d3119111842ba20b24a8833502d8c93899dda405a5e570f216e6fdddf6726c8c |
File details
Details for the file sql2func-0.0.1.dev2-py3-none-any.whl
.
File metadata
- Download URL: sql2func-0.0.1.dev2-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d13634669637c0e3ffcdb498d0763d833db27fe61ebc8816a51a7c1f716950fb |
|
MD5 | f9ca87f2b6aaecd2bf4091d13cee167c |
|
BLAKE2b-256 | d64b01167aadb1c070b4b4b4fcfa3884640330aa3d9195588f89c2af4f72581c |