Refine api to support one batch command

This commit is contained in:
WQY\qiong
2022-10-26 19:35:04 +08:00
parent 78d7be1d9a
commit 65e2ce2541
16 changed files with 182 additions and 178 deletions

View File

@@ -42,6 +42,14 @@ class ChangeSet:
return self
class SqlChangeSet:
def __init__(self, redo_sql: str, undo_sql: str, redo_cs: dict[str, str], undo_cs: dict[str, str]) -> None:
self.redo_sql = redo_sql
self.undo_sql = undo_sql
self.redo_cs = redo_cs
self.undo_cs = undo_cs
def read(name: str, sql: str) -> Row:
with conn[name].cursor(row_factory=dict_row) as cur:
cur.execute(sql)
@@ -72,20 +80,20 @@ def get_current_operation(name: str) -> int:
return int(read(name, 'select id from current_operation')['id'])
def execute_command(name: str, redo_sql: str, undo_sql: str, redo_cs: dict[str, str], undo_cs: dict[str, str]) -> ChangeSet:
write(name, redo_sql)
def execute_command(name: str, command: SqlChangeSet) -> ChangeSet:
write(name, command.redo_sql)
parent = get_current_operation(name)
redo_sql = redo_sql.replace("'", "''")
undo_sql = undo_sql.replace("'", "''")
redo_cs_str = str(redo_cs).replace("'", "''")
undo_cs_str = str(undo_cs).replace("'", "''")
redo_sql = command.redo_sql.replace("'", "''")
undo_sql = command.undo_sql.replace("'", "''")
redo_cs_str = str(command.redo_cs).replace("'", "''")
undo_cs_str = str(command.undo_cs).replace("'", "''")
write(name, f"insert into operation (id, redo, undo, parent, redo_cs, undo_cs) values (default, '{redo_sql}', '{undo_sql}', {parent}, '{redo_cs_str}', '{undo_cs_str}')")
current = read(name, 'select max(id) as id from operation')['id']
write(name, f"update current_operation set id = {current}")
return ChangeSet(redo_cs)
return ChangeSet(command.redo_cs)
def execute_undo(name: str, discard: bool = False) -> ChangeSet: