Support compress batch commands as one command

This commit is contained in:
WQY\qiong
2022-10-26 20:42:53 +08:00
parent 65e2ce2541
commit 89cc1acc66
5 changed files with 223 additions and 9 deletions

View File

@@ -96,6 +96,26 @@ def execute_command(name: str, command: SqlChangeSet) -> ChangeSet:
return ChangeSet(command.redo_cs)
def execute_batch(name: str, redo_sql: str, undo_sql: str, redo_cs_s: list[dict[str, Any]], undo_cs_s: list[dict[str, Any]]) -> ChangeSet:
write(name, redo_sql)
parent = get_current_operation(name)
redo_sql = redo_sql.replace("'", "''")
undo_sql = undo_sql.replace("'", "''")
redo_cs_str = str(redo_cs_s).replace("'", "''")
undo_cs_str = str(undo_cs_s).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}")
cs = ChangeSet()
for r_cs in redo_cs_s:
cs.append(r_cs)
return cs
def execute_undo(name: str, discard: bool = False) -> ChangeSet:
row = read(name, f'select * from operation where id = {get_current_operation(name)}')
@@ -112,7 +132,15 @@ def execute_undo(name: str, discard: bool = False) -> ChangeSet:
else:
write(name, f"update operation set redo_child = {row['id']} where id = {row['parent']}")
return ChangeSet(eval(row['undo_cs']))
e = eval(row['undo_cs'])
if isinstance(e, type({})):
return ChangeSet(e)
cs = ChangeSet()
for _cs in e:
cs.append(_cs)
return cs
def execute_redo(name: str) -> ChangeSet:
@@ -125,7 +153,15 @@ def execute_redo(name: str) -> ChangeSet:
write(name, f"update current_operation set id = {row['id']} where id = {row['parent']}")
return ChangeSet(eval(row['redo_cs']))
e = eval(row['redo_cs'])
if isinstance(e, type({})):
return ChangeSet(e)
cs = ChangeSet()
for _cs in e:
cs.append(_cs)
return cs
def have_snapshot(name: str, tag: str) -> bool:
@@ -195,9 +231,30 @@ def pick_snapshot(name: str, tag: str, discard: bool) -> ChangeSet:
return pick_operation(name, target, discard)
def _get_change_set(name: str, operation: int, undo: bool) -> dict[str, Any]:
def _get_change_set(name: str, operation: int, undo: bool) -> ChangeSet:
row = read(name, f'select * from operation where id = {operation}')
return eval(row['undo_cs']) if undo else eval(row['redo_cs'])
cs = ChangeSet()
if undo:
e = eval(row['undo_cs'])
if isinstance(e, type({})):
return ChangeSet(e)
cs = ChangeSet()
for _cs in e:
cs.append(_cs)
return cs
else:
e = eval(row['redo_cs'])
if isinstance(e, type({})):
return ChangeSet(e)
cs = ChangeSet()
for _cs in e:
cs.append(_cs)
return cs
def sync_with_server(name: str, operation: int) -> ChangeSet:
@@ -212,13 +269,13 @@ def sync_with_server(name: str, operation: int) -> ChangeSet:
if fr in to_parents:
index = to_parents.index(fr) - 1
while index >= 0:
change.append(_get_change_set(name, to_parents[index], False)) #redo
change.merge(_get_change_set(name, to_parents[index], False)) #redo
index -= 1
elif to in fr_parents:
index = 0
while index <= fr_parents.index(to) - 1:
change.append(_get_change_set(name, fr_parents[index], True))
change.merge(_get_change_set(name, fr_parents[index], True))
index += 1
else:
@@ -230,12 +287,12 @@ def sync_with_server(name: str, operation: int) -> ChangeSet:
index = 0
while index <= fr_parents.index(ancestor) - 1:
change.append(_get_change_set(name, fr_parents[index], True))
change.merge(_get_change_set(name, fr_parents[index], True))
index += 1
index = to_parents.index(ancestor) - 1
while index >= 0:
change.append(_get_change_set(name, to_parents[index], False))
change.merge(_get_change_set(name, to_parents[index], False))
index -= 1
return change.compress()