Add utility
This commit is contained in:
21
api/utility.py
Normal file
21
api/utility.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from psycopg.rows import dict_row, Row
|
||||
from .connection import g_conn_dict as conn
|
||||
from .operation import *
|
||||
|
||||
def query(name: str, sql: str) -> Row | None:
|
||||
with conn[name].cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(sql)
|
||||
return cur.fetchone()
|
||||
|
||||
def decorate(value: str | None, type: str, optional: bool) -> str:
|
||||
if optional:
|
||||
value = 'NULL' if value == None else value
|
||||
if type == 'str':
|
||||
value = f'"{value}"' if value != 'NULL' else value
|
||||
return value
|
||||
|
||||
def update(name: str, sql: str, undo_sql: str):
|
||||
with conn[name].cursor() as cur:
|
||||
cur.execute(sql)
|
||||
redo = sql.replace("'", '"')
|
||||
add_operation(name, redo, undo_sql)
|
||||
Reference in New Issue
Block a user