Initial commit
This commit is contained in:
23
folkugat_web/dal/sql/_connection.py
Normal file
23
folkugat_web/dal/sql/_connection.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import sqlite3
|
||||
from contextlib import contextmanager
|
||||
from typing import Iterator, Optional
|
||||
|
||||
from folkugat_web.config.db import DB_FILE
|
||||
|
||||
Connection = sqlite3.Connection
|
||||
|
||||
|
||||
@contextmanager
|
||||
def get_connection(con: Optional[Connection] = None) -> Iterator[Connection]:
|
||||
if con:
|
||||
yield con
|
||||
else:
|
||||
con = sqlite3.connect(DB_FILE)
|
||||
try:
|
||||
yield con
|
||||
con.commit()
|
||||
except Exception:
|
||||
con.rollback()
|
||||
raise
|
||||
finally:
|
||||
con.close()
|
||||
Reference in New Issue
Block a user