Huge refactor to api and add batch api

This commit is contained in:
WQY\qiong
2022-10-14 23:18:01 +08:00
parent 200aaaca99
commit c5480d55ca
20 changed files with 1811 additions and 1510 deletions

View File

@@ -1,18 +1,23 @@
from psycopg.rows import dict_row
from .operation import *
from .connection import g_conn_dict as conn
from .change_set import ChangeSet
def get_title(name: str) -> str:
with conn[name].cursor(row_factory=dict_row) as cur:
cur.execute(f"select * from title")
return cur.fetchone()['value']
def set_title(name: str, value: str) -> ChangeSet:
old = get_title(name)
def get_title_schema(name: str) -> dict[str, dict[str, Any]]:
return {'value': {'type': 'float', 'optional': False, 'readonly': False}}
sql = f"update title set value = '{value}'"
undo = f"update title set value = ''{old}''"
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'set_title', API_UPDATE, 'title', '')
return get_current_change_set(name)
def get_title(name: str) -> dict[str, Any]:
title = read(name, 'select * from title')
return { 'value': title['value'] }
def set_title(name: str, cs: ChangeSet) -> ChangeSet:
new = cs.operations[0]['value']
old = get_title(name)['value']
redo_sql = f"update title set value = '{new}';"
undo_sql = f"update title set value = '{old}';"
redo_cs = g_update_prefix | { 'type': 'title', 'value': new }
undo_cs = g_update_prefix | { 'type': 'title', 'value': old }
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)