Code refactor for change set
This commit is contained in:
@@ -17,6 +17,13 @@ class ChangeSet:
|
||||
if ps != None:
|
||||
self.append(ps)
|
||||
|
||||
@staticmethod
|
||||
def from_list(ps: list[dict[str, Any]]):
|
||||
cs = ChangeSet()
|
||||
for _cs in ps:
|
||||
cs.append(_cs)
|
||||
return cs
|
||||
|
||||
def add(self, ps: dict[str, Any]):
|
||||
self.operations.append(g_add_prefix | ps)
|
||||
return self
|
||||
@@ -46,14 +53,7 @@ class ChangeSet:
|
||||
return self
|
||||
|
||||
|
||||
class SqlChangeSet:
|
||||
def __init__(self, redo_sql: str, undo_sql: str, redo_cs: dict[str, Any], undo_cs: dict[str, Any]) -> None:
|
||||
self.redo_sql = redo_sql
|
||||
self.undo_sql = undo_sql
|
||||
self.redo_cs = redo_cs
|
||||
self.undo_cs = undo_cs
|
||||
|
||||
class BatchSqlChangeSet:
|
||||
class DbChangeSet:
|
||||
def __init__(self, redo_sql: str, undo_sql: str, redo_cs: list[dict[str, Any]], undo_cs: list[dict[str, Any]]) -> None:
|
||||
self.redo_sql = redo_sql
|
||||
self.undo_sql = undo_sql
|
||||
@@ -91,7 +91,7 @@ def get_current_operation(name: str) -> int:
|
||||
return int(read(name, 'select id from current_operation')['id'])
|
||||
|
||||
|
||||
def execute_command(name: str, command: SqlChangeSet) -> ChangeSet:
|
||||
def execute_command(name: str, command: DbChangeSet) -> ChangeSet:
|
||||
write(name, command.redo_sql)
|
||||
|
||||
parent = get_current_operation(name)
|
||||
@@ -104,27 +104,7 @@ def execute_command(name: str, command: SqlChangeSet) -> ChangeSet:
|
||||
current = read(name, 'select max(id) as id from operation')['id']
|
||||
write(name, f"update current_operation set id = {current}")
|
||||
|
||||
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
|
||||
return ChangeSet.from_list(command.redo_cs)
|
||||
|
||||
|
||||
def execute_undo(name: str, discard: bool = False) -> ChangeSet:
|
||||
@@ -144,14 +124,7 @@ def execute_undo(name: str, discard: bool = False) -> ChangeSet:
|
||||
write(name, f"update operation set redo_child = {row['id']} where id = {row['parent']}")
|
||||
|
||||
e = eval(row['undo_cs'])
|
||||
if isinstance(e, type({})):
|
||||
return ChangeSet(e)
|
||||
|
||||
cs = ChangeSet()
|
||||
for _cs in e:
|
||||
cs.append(_cs)
|
||||
|
||||
return cs
|
||||
return ChangeSet.from_list(e)
|
||||
|
||||
|
||||
def execute_redo(name: str) -> ChangeSet:
|
||||
@@ -165,14 +138,7 @@ def execute_redo(name: str) -> ChangeSet:
|
||||
write(name, f"update current_operation set id = {row['id']} where id = {row['parent']}")
|
||||
|
||||
e = eval(row['redo_cs'])
|
||||
if isinstance(e, type({})):
|
||||
return ChangeSet(e)
|
||||
|
||||
cs = ChangeSet()
|
||||
for _cs in e:
|
||||
cs.append(_cs)
|
||||
|
||||
return cs
|
||||
return ChangeSet.from_list(e)
|
||||
|
||||
|
||||
def have_snapshot(name: str, tag: str) -> bool:
|
||||
@@ -244,28 +210,8 @@ def pick_snapshot(name: str, tag: str, discard: bool) -> ChangeSet:
|
||||
|
||||
def _get_change_set(name: str, operation: int, undo: bool) -> ChangeSet:
|
||||
row = read(name, f'select * from operation where id = {operation}')
|
||||
|
||||
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
|
||||
field= 'undo_cs' if undo else 'redo_cs'
|
||||
return ChangeSet.from_list(eval(row[field]))
|
||||
|
||||
|
||||
def sync_with_server(name: str, operation: int) -> ChangeSet:
|
||||
|
||||
Reference in New Issue
Block a user